org.apache.commons.configuration2.PatternSubtreeConfigurationWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-configuration2 Show documentation
Show all versions of commons-configuration2 Show documentation
Tools to assist in the reading of configuration/preferences files in
various formats
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.configuration2;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import org.apache.commons.configuration2.event.Event;
import org.apache.commons.configuration2.event.EventListener;
import org.apache.commons.configuration2.event.EventType;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.io.FileBased;
import org.apache.commons.configuration2.tree.ExpressionEngine;
import org.apache.commons.configuration2.tree.ImmutableNode;
/**
* Wraps a BaseHierarchicalConfiguration and allows subtrees to be accessed via a configured path with replaceable
* tokens derived from the ConfigurationInterpolator. When used with injection frameworks such as Spring it allows
* components to be injected with subtrees of the configuration.
*
* @since 1.6
*/
public class PatternSubtreeConfigurationWrapper extends BaseHierarchicalConfiguration implements FileBasedConfiguration {
/** The wrapped configuration */
private final HierarchicalConfiguration config;
/** The path to the subtree */
private final String path;
/** True if the path ends with '/', false otherwise */
private final boolean trailing;
/** True if the constructor has finished */
private final boolean init;
/**
* Constructor
*
* @param config The Configuration to be wrapped.
* @param path The base path pattern.
*/
public PatternSubtreeConfigurationWrapper(final HierarchicalConfiguration config, final String path) {
this.config = Objects.requireNonNull(config, "config");
this.path = path;
this.trailing = path.endsWith("/");
this.init = true;
}
@Override
public void addEventListener(final EventType eventType, final EventListener super T> listener) {
getConfig().addEventListener(eventType, listener);
}
@Override
protected void addNodesInternal(final String key, final Collection extends ImmutableNode> nodes) {
getConfig().addNodes(key, nodes);
}
@Override
protected void addPropertyInternal(final String key, final Object value) {
config.addProperty(makePath(key), value);
}
@Override
public void clearErrorListeners() {
getConfig().clearErrorListeners();
}
@Override
public void clearEventListeners() {
getConfig().clearEventListeners();
}
@Override
protected void clearInternal() {
getConfig().clear();
}
@Override
protected void clearPropertyDirect(final String key) {
config.clearProperty(makePath(key));
}
@Override
protected Object clearTreeInternal(final String key) {
config.clearTree(makePath(key));
return Collections.emptyList();
}
@Override
public HierarchicalConfiguration configurationAt(final String key) {
return config.configurationAt(makePath(key));
}
@Override
public HierarchicalConfiguration configurationAt(final String key, final boolean supportUpdates) {
return config.configurationAt(makePath(key), supportUpdates);
}
@Override
public List> configurationsAt(final String key) {
return config.configurationsAt(makePath(key));
}
@Override
protected boolean containsKeyInternal(final String key) {
return config.containsKey(makePath(key));
}
/**
* Tests whether this configuration contains one or more matches to this value. This operation stops at first
* match but may be more expensive than the containsKey method.
* @since 2.11.0
*/
@Override
protected boolean containsValueInternal(final Object value) {
return config.containsValue(value);
}
/**
* Returns the wrapped configuration as a {@code FileBased} object. If this cast is not possible, an exception is
* thrown.
*
* @return the wrapped configuration as {@code FileBased}
* @throws ConfigurationException if the wrapped configuration does not implement {@code FileBased}
*/
private FileBased fetchFileBased() throws ConfigurationException {
if (!(config instanceof FileBased)) {
throw new ConfigurationException("Wrapped configuration does not implement FileBased!" + " No I/O operations are supported.");
}
return (FileBased) config;
}
@Override
public BigDecimal getBigDecimal(final String key) {
return config.getBigDecimal(makePath(key));
}
@Override
public BigDecimal getBigDecimal(final String key, final BigDecimal defaultValue) {
return config.getBigDecimal(makePath(key), defaultValue);
}
@Override
public BigInteger getBigInteger(final String key) {
return config.getBigInteger(makePath(key));
}
@Override
public BigInteger getBigInteger(final String key, final BigInteger defaultValue) {
return config.getBigInteger(makePath(key), defaultValue);
}
@Override
public boolean getBoolean(final String key) {
return config.getBoolean(makePath(key));
}
@Override
public boolean getBoolean(final String key, final boolean defaultValue) {
return config.getBoolean(makePath(key), defaultValue);
}
@Override
public Boolean getBoolean(final String key, final Boolean defaultValue) {
return config.getBoolean(makePath(key), defaultValue);
}
@Override
public byte getByte(final String key) {
return config.getByte(makePath(key));
}
@Override
public byte getByte(final String key, final byte defaultValue) {
return config.getByte(makePath(key), defaultValue);
}
@Override
public Byte getByte(final String key, final Byte defaultValue) {
return config.getByte(makePath(key), defaultValue);
}
private BaseHierarchicalConfiguration getConfig() {
return (BaseHierarchicalConfiguration) config.configurationAt(makePath());
}
@Override
public double getDouble(final String key) {
return config.getDouble(makePath(key));
}
@Override
public double getDouble(final String key, final double defaultValue) {
return config.getDouble(makePath(key), defaultValue);
}
@Override
public Double getDouble(final String key, final Double defaultValue) {
return config.getDouble(makePath(key), defaultValue);
}
@Override
public Collection> getEventListeners(final EventType eventType) {
return getConfig().getEventListeners(eventType);
}
@Override
public ExpressionEngine getExpressionEngine() {
return config.getExpressionEngine();
}
@Override
public float getFloat(final String key) {
return config.getFloat(makePath(key));
}
@Override
public float getFloat(final String key, final float defaultValue) {
return config.getFloat(makePath(key), defaultValue);
}
@Override
public Float getFloat(final String key, final Float defaultValue) {
return config.getFloat(makePath(key), defaultValue);
}
@Override
public int getInt(final String key) {
return config.getInt(makePath(key));
}
@Override
public int getInt(final String key, final int defaultValue) {
return config.getInt(makePath(key), defaultValue);
}
@Override
public Integer getInteger(final String key, final Integer defaultValue) {
return config.getInteger(makePath(key), defaultValue);
}
@Override
protected Iterator getKeysInternal() {
return config.getKeys(makePath());
}
@Override
protected Iterator getKeysInternal(final String prefix) {
return config.getKeys(makePath(prefix));
}
@Override
public List