org.mycore.common.MCRPropertiesResolver Maven / Gradle / Ivy
/*
* This file is part of *** M y C o R e ***
* See http://www.mycore.de/ for details.
*
* MyCoRe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MyCoRe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MyCoRe. If not, see .
*/
package org.mycore.common;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
/**
* The MCRPropertiesResolver
supports substitution of any %reference%
* in a String
or Property
instance.
*
* // possible use case
* Properties p = MCRConfiguration.instance().getProperties();
* MCRPropertiesResolver r = new MCRPropertiesResolver(p);
* Properties resolvedProperties = r.resolveAll(p);
*
*
* @author Matthias Eichner
*/
public class MCRPropertiesResolver extends MCRTextResolver {
public MCRPropertiesResolver() {
super();
}
public MCRPropertiesResolver(Map propertiesMap) {
super(propertiesMap);
}
public MCRPropertiesResolver(Properties properties) {
super(properties);
}
@Override
public String addVariable(String name, String value) {
return super.addVariable(name, removeSelfReference(name, value));
}
private String removeSelfReference(String name, String value) {
int pos1 = value.indexOf("%");
if (pos1 != -1) {
int pos2 = value.indexOf("%", pos1 + 1);
if (pos2 != -1) {
String ref = value.substring(pos1 + 1, pos2);
if (name.equals(ref)) {
return value.replaceAll("\\s*%" + name + "%\\s*,?", "");
}
}
}
return value;
}
@Override
protected void registerDefaultTerms() throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException, InstantiationException {
registerTerm(Property.class);
}
/**
* Substitute all %references% of the given Properties
and
* return a new Properties
object.
*
* @param toResolve properties to resolve
* @return resolved properties
*/
public Properties resolveAll(Properties toResolve) {
Properties resolvedProperties = new Properties();
for (Entry