All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mycore.common.MCRPropertiesResolver Maven / Gradle / Ivy

There is a newer version: 2024.05
Show newest version
/*
 * 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 entrySet : toResolve.entrySet()) { String key = entrySet.getKey().toString(); String value = removeSelfReference(key, entrySet.getValue().toString()); String resolvedValue = this.resolve(value); resolvedProperties.put(key, resolvedValue); } return resolvedProperties; } /** * Substitute all %references% of the given Map and * return a new Map object. * * @param toResolve properties to resolve * @return resolved properties */ public Map resolveAll(Map toResolve) { Map resolvedMap = new HashMap<>(); for (Entry entrySet : toResolve.entrySet()) { String key = entrySet.getKey(); String value = removeSelfReference(key, entrySet.getValue()); resolvedMap.put(key, this.resolve(value)); } return resolvedMap; } public static class Property extends Variable { public Property(MCRTextResolver textResolver) { super(textResolver); } @Override public String getStartEnclosingString() { return "%"; } @Override public String getEndEnclosingString() { return "%"; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy