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
/*
* 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
* @author Commons
* Configuration team
* @version $Id: PatternSubtreeConfigurationWrapper.java 1842194 2018-09-27 22:24:23Z ggregory $
*/
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 = config;
this.path = path;
this.trailing = path.endsWith("/");
this.init = true;
}
@Override
protected void addPropertyInternal(final String key, final Object value)
{
config.addProperty(makePath(key), value);
}
@Override
protected void clearInternal()
{
getConfig().clear();
}
@Override
protected void clearPropertyDirect(final String key)
{
config.clearProperty(makePath(key));
}
@Override
protected boolean containsKeyInternal(final String key)
{
return config.containsKey(makePath(key));
}
@Override
public BigDecimal getBigDecimal(final String key, final BigDecimal defaultValue)
{
return config.getBigDecimal(makePath(key), defaultValue);
}
@Override
public BigDecimal getBigDecimal(final String key)
{
return config.getBigDecimal(makePath(key));
}
@Override
public BigInteger getBigInteger(final String key, final BigInteger defaultValue)
{
return config.getBigInteger(makePath(key), defaultValue);
}
@Override
public BigInteger getBigInteger(final String key)
{
return config.getBigInteger(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 boolean getBoolean(final String key)
{
return config.getBoolean(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);
}
@Override
public byte getByte(final String key)
{
return config.getByte(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 double getDouble(final String key)
{
return config.getDouble(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 float getFloat(final String key)
{
return config.getFloat(makePath(key));
}
@Override
public int getInt(final String key, final int defaultValue)
{
return config.getInt(makePath(key), defaultValue);
}
@Override
public int getInt(final String key)
{
return config.getInt(makePath(key));
}
@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