Example usage for org.apache.commons.digester Digester setSubstitutor

List of usage examples for org.apache.commons.digester Digester setSubstitutor

Introduction

In this page you can find the example usage for org.apache.commons.digester Digester setSubstitutor.

Prototype

public void setSubstitutor(Substitutor substitutor) 

Source Link

Document

Sets the Substitutor to be used to convert attributes and body text.

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 .  j a  v a2s. c  om

    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
 *//* w w  w  . ja v  a 2  s  .  com*/
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);
    }
}