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 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.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
*
* This class parses and resolve strings which contains variables.
* To add a variable call addVariable.
*
* The algorithm is optimized that each character is touched only once.
*
* To resolve a string a valid syntax is required:
*
* {}: Use curly brackets for variables or properties. For example "{var1}"
* or "{MCR.basedir}"
*
* []: Use squared brackets to define a condition. All data within
* squared brackets is only used if the internal variables are
* not null and not empty. For example "[hello {lastName}]" is only resolved
* if the value of "lastName" is not null and not empty. Otherwise the whole
* content in the squared brackets are ignored.
*
* \: Use the escape character to use all predefined characters.
*
*
* @author Matthias Eichner
*/
public class MCRTextResolver {
private static final Logger LOGGER = LogManager.getLogger(MCRTextResolver.class);
protected TermContainer termContainer;
/**
* This map contains all variables that can be resolved.
*/
protected Map variablesMap;
/**
* Retains the text if a variable couldn't be resolved.
* Example if {Variable} could not be resolved:
* true: "Hello {Variable}" -> "Hello {Variable}"
* false: "Hello "
*
By default retainText is true
*/
protected boolean retainText;
/**
* Defines how deep the text is resolved.
*
*
Deep
everything is resolved
*
NoVariables
the value of variables is not being resolved
*
*/
protected ResolveDepth resolveDepth;
protected CircularDependencyTracker tracker;
/**
* Creates the term list for the text resolver and adds
* the default terms.
*/
protected void registerDefaultTerms() throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException, InstantiationException {
registerTerm(Variable.class);
registerTerm(Condition.class);
registerTerm(EscapeCharacter.class);
}
/**
* Register a new term. The resolver invokes the term via reflection.
*
* @param termClass the term class to register.
*/
public void registerTerm(Class termClass) throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException, InstantiationException {
this.termContainer.add(termClass);
}
/**
* Unregister a term.
*
* @param termClass this class is unregistered
*/
public void unregisterTerm(Class termClass) throws NoSuchMethodException,
InvocationTargetException, InstantiationException, IllegalAccessException {
this.termContainer.remove(termClass);
}
/**
* Defines how deep the text is resolved.
*
*
Deep
everything is resolved
*
NoVariables
the value of variables is not being resolved
*
*/
public enum ResolveDepth {
Deep, NoVariables
}
/**
* Creates a new text resolver with a map of variables.
*/
public MCRTextResolver() {
this.variablesMap = new HashMap<>();
this.setResolveDepth(ResolveDepth.Deep);
this.setRetainText(true);
this.tracker = new CircularDependencyTracker(this);
try {
this.termContainer = new TermContainer(this);
this.registerDefaultTerms();
} catch (Exception exc) {
throw new MCRException("Unable to register default terms", exc);
}
}
/**
* Creates a new text resolver. To add variables call
* addVariable, otherwise only MyCoRe property
* resolving is possible.
*/
public MCRTextResolver(Map variablesMap) {
this();
mixin(variablesMap);
}
public MCRTextResolver(Properties properties) {
this();
mixin(properties);
}
protected TermContainer getTermContainer() {
return this.termContainer;
}
protected CircularDependencyTracker getTracker() {
return this.tracker;
}
public void mixin(Map variables) {
for (Entry entrySet : variables.entrySet()) {
String key = entrySet.getKey();
String value = entrySet.getValue();
this.addVariable(key, value);
}
}
public void mixin(Properties properties) {
for (Entry