Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package es.iti.wakamiti.api.imconfig.internal;
import es.iti.wakamiti.api.imconfig.Configuration;
import es.iti.wakamiti.api.imconfig.*;
import org.apache.commons.configuration2.AbstractConfiguration;
import org.apache.commons.configuration2.*;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.convert.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
public class ApacheConfiguration2Factory implements ConfigurationFactory {
private final ConversionHandler conversionHandler = new ApacheConfiguration2ConversionHandler();
private final PropertyDefinitionParser parser = new PropertyDefinitionParser();
private char separator = 0;
@Override
public ConfigurationFactory multiValueSeparator(char separator) {
if (separator == 0) {
throw new IllegalArgumentException("Invalid separator symbol: "+separator);
}
this.separator = separator;
return this;
}
@Override
public boolean hasMultiValueSeparator() {
return this.separator != 0;
}
@Override
public char multiValueSeparator() {
return this.separator;
}
@Override
public Configuration merge(Configuration base, Configuration delta) {
AbstractConfiguration result = new BaseConfiguration();
for (String property : delta.keys()) {
var existing = base.getList(property,String.class);
var added = delta.getList(property,String.class);
if (existing.isEmpty() && added.isEmpty()) {
result.setProperty(property,"");
} else if (!added.isEmpty()) {
added.forEach(value -> result.addProperty(property,value));
}
}
for (String property : base.keys()) {
if (result.containsKey(property)) {
continue;
}
base.getList(property,String.class).forEach(value -> result.addProperty(property,value));
}
Map definitions = new HashMap<>(base.getDefinitions());
definitions.putAll(delta.getDefinitions());
return new ApacheConfiguration2(this, definitions, result);
}
@Override
public Configuration empty() {
return new ApacheConfiguration2(this, new BaseConfiguration());
}
@Override
public Configuration fromAnnotation(Class> configuredClass) {
return Optional.ofNullable(configuredClass.getAnnotation(AnnotatedConfiguration.class))
.map(this::fromAnnotation)
.orElseThrow(
() -> new ConfigurationException(
configuredClass + " is not annotated with @Configurator"
)
);
}
@Override
public Configuration fromAnnotation(AnnotatedConfiguration annotation) {
BaseConfiguration configuration = configure(new BaseConfiguration());
for (Property property : annotation.value()) {
String[] value = property.value();
if (value.length == 1) {
configuration.addProperty(property.key(), value[0]);
} else {
configuration.addProperty(property.key(), value);
}
}
return new ApacheConfiguration2(this, configuration);
}
@Override
public Configuration fromEnvironment() {
return new ApacheConfiguration2(this, new EnvironmentConfiguration());
}
@Override
public Configuration fromSystem() {
return new ApacheConfiguration2(this, new SystemConfiguration());
}
@Override
public Configuration fromPath(Path path) {
return fromURI(path.toUri());
}
@SuppressWarnings("CollectionDeclaredAsConcreteClass")
@Override
public Configuration fromProperties(Properties properties) {
final BaseConfiguration configuration = configure(new BaseConfiguration());
for (final Entry