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

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

Introduction

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

Prototype

public VariableSubstitutor(VariableExpander expander) 

Source Link

Document

Constructs a Substitutor which uses the same VariableExpander for both body text and attibutes.

Usage

From source file:com.discursive.jccook.xml.bean.VariableSubExample.java

public void testDigest() throws Exception {

    List emails = new ArrayList();

    Map vars = new HashMap();
    vars.put("email.to", "tobrien@discursive.com");
    vars.put("user.name", "Tim");
    vars.put("order.id", "1RR2E223WVVS");
    vars.put("product.name", "Foundation");

    MultiVariableExpander expander = new MultiVariableExpander();
    expander.addSource("$", vars);

    Substitutor substitutor = new VariableSubstitutor(expander);

    InputStream input = getClass().getResourceAsStream("./email.xml");

    URL rules = getClass().getResource("./email-rules.xml");
    Digester digester = DigesterLoader.createDigester(rules);
    digester.setSubstitutor(substitutor);
    digester.push(emails);/*  w  w  w.  ja  v  a2  s  .  c  o  m*/

    digester.parse(input);

    System.out.println("Resulting Email: " + emails.get(0));

}

From source file:com.germinus.easyconf.ConfigurationLoader.java

/**
 * Read an XML file and return an Object representation of its contents
 *//*from   w w  w.ja  v a 2 s  . co m*/
Object loadXMLFile(URL confFileUrl, ComponentProperties properties) throws IOException, SAXException {
    log.debug("Loading XML file: " + confFileUrl);
    String componentName = properties.getComponentName();
    String rulesFileName = componentName + Conventions.DIGESTERRULES_EXTENSION;
    URL digesterRulesUrl = ClasspathUtil.locateResource(rulesFileName);
    if (digesterRulesUrl == null) {
        throw new DigesterRulesNotFoundException(componentName, rulesFileName);
    }
    Digester digester = DigesterLoader.createDigester(digesterRulesUrl);
    digester.setUseContextClassLoader(true);
    digester.setValidating(false);

    MultiVariableExpander expander = new MultiVariableExpander();
    expander.addSource("$", properties.toMap());
    Substitutor substitutor = new VariableSubstitutor(expander);
    digester.setSubstitutor(substitutor);

    try {
        Object confObj = digester.parse(confFileUrl.openStream());
        log.info("Read configuration from " + confFileUrl);
        return confObj;
    } catch (IllegalArgumentException e) {
        //FIXME: it does not catch the exception
        throw new InvalidPropertyException(properties.getComponentName(), e);
    }
}

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

/**
 * substutes variables in the string as determined from the actor.
 * /*from   ww w .  ja v a  2 s .  c  om*/
 * @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);/* w  w  w  .  ja 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;
}