Example usage for org.apache.commons.digester.substitution VariableSubstitutor substitute

List of usage examples for org.apache.commons.digester.substitution VariableSubstitutor substitute

Introduction

In this page you can find the example usage for org.apache.commons.digester.substitution VariableSubstitutor substitute.

Prototype

@Override
public String substitute(String bodyText) 

Source Link

Document

Substitutes for the body text.

Usage

From source file:org.dawb.passerelle.common.parameter.ParameterUtils.java

/**
 * substutes variables in the string as determined from the actor.
 * /*from  w ww .j  av a 2s. c  o m*/
 * @param filePath
 * @param dataExportTransformer
 * @throws Exception 
 */
public static String substitute(String stringValue, final NamedObj actor) throws Exception {

    final Map<String, Object> variables = new HashMap<String, Object>(3);
    if (actor != null) {
        if (ModelUtils.getProject(actor) != null) {
            variables.put("project_name", ModelUtils.getProject(actor).getName());
        }
        variables.put("actor_name", actor.getName());
    }

    MultiVariableExpander expander = new MultiVariableExpander();
    expander.addSource("$", variables);
    // Create a substitutor with the expander
    VariableSubstitutor substitutor = new VariableSubstitutor(expander);

    // Commented out while waiting for a fix
    //      try {
    //         VariablesPlugin.getDefault().getStringVariableManager().validateStringVariables(stringValue);
    //          stringValue = VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(stringValue, false);
    //      } catch (Throwable ignored) {
    //         // We just try any eclipse vars
    //      }
    return substitutor.substitute(stringValue);
}

From source file:org.dawb.passerelle.common.utils.SubstituteUtils.java

public static final String substitute(final String expand, final Map<String, String> variables) {

    if (variables == null)
        return expand;

    // Create an expander with the Map that matches ${var}
    MultiVariableExpander expander = new MultiVariableExpander();
    expander.addSource("$", variables);
    // Create a substitutor with the expander
    VariableSubstitutor substitutor = new VariableSubstitutor(expander);

    String value = substitutor.substitute(expand);
    return value;

}

From source file:org.dawb.passerelle.common.utils.XMLUtils.java

public static Map<String, String> getVariables(final Map<?, ?> variables, final String xmlSource,
        final Map<String, String> scalarSource) throws Exception {

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(false); // never forget this!
    docFactory.setValidating(false);//from  w  w  w  . j a v a 2  s.c  o m
    DocumentBuilder builder = docFactory.newDocumentBuilder();
    Document doc = null;

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    final Map<String, String> values = new HashMap<String, String>(variables.size());
    for (Object varName : variables.keySet()) {
        final String varValue = (String) variables.get(varName.toString());
        if (varValue == null || "".equals(varValue)) {
            values.put(varName.toString(), scalarSource.get(varName));
            continue;
        }

        if ("/".equals(varValue)) {
            values.put(varName.toString(), xmlSource);
            continue;
        }

        final XPathExpression exp = xpath.compile(varValue);
        if (doc == null)
            doc = builder.parse(new InputSource(new StringReader(xmlSource)));

        final NodeList nodeList = (NodeList) exp.evaluate(doc, XPathConstants.NODESET);
        values.put(varName.toString(), getNodeValue(nodeList));
    }

    // We allow names of variables to expand values of other variables.
    final Map<String, String> all = new HashMap<String, String>(values.size());
    all.putAll(scalarSource);
    all.putAll(values);

    final Map<String, String> ret = new HashMap<String, String>(variables.size());
    final MultiVariableExpander expander = new MultiVariableExpander();
    expander.addSource("$", all);

    // Create a substitutor with the expander
    final VariableSubstitutor substitutor = new VariableSubstitutor(expander);

    for (final String varName : values.keySet()) {

        if (!varName.contains("$")) {
            ret.put(varName, values.get(varName));
        } else {
            ret.put(substitutor.substitute(varName), values.get(varName));
        }
    }

    return ret;
}