Package org.apache.commons.digester

Examples of org.apache.commons.digester.Digester


    }

    public void testDefaultPlugins3() throws Exception {
        // * tests that the default plugin must implement or extend the
        //   plugin base class.
        Digester digester = new Digester();
        PluginRules rc = new PluginRules();
        digester.setRules(rc);

        PluginCreateRule pcr = new PluginCreateRule(Widget.class, Object.class);
        digester.addRule("root/widget", pcr);
        digester.addSetNext("root/widget", "addChild");

        Container root = new Container();
        digester.push(root);
       
        Exception exception = null;
        Log oldLog = digester.getLogger();
        try {
            digester.setLogger(new NoOpLog());
            digester.parse(
                TestAll.getInputStream(this, "test2.xml"));
        }
        catch(Exception e) {
            exception = e;
        }
        finally {
            digester.setLogger(oldLog);
        }
       
        assertTrue(exception != null);
        assertEquals(SAXParseException.class, exception.getClass());
        assertEquals(
View Full Code Here


     * Creates a Digester configured to show Ant-like capability.
     *
     * @return a Digester with rules and variable substitutor
     */
    private Digester createDigesterThatCanDoAnt() {
        Digester digester = new Digester();

        MultiVariableExpander expander = new MultiVariableExpander();
        expander.addSource("$", mutableSource);
        digester.setSubstitutor(new VariableSubstitutor(expander));

        int useRootObj = -1;
        Class[] callerArgTypes = new Class[] {String.class, String.class};
        CallMethodRule caller = new CallMethodRule(useRootObj, "addProperty",
            callerArgTypes.length, callerArgTypes);
        digester.addRule("root/property", caller);
        digester.addCallParam("root/property", 0, "name");
        digester.addCallParam("root/property", 1, "value");

        digester.addObjectCreate("root/bean", SimpleTestBean.class);
        digester.addSetProperties("root/bean");
        digester.addSetNext("root/bean", "addSimpleTestBean");
        return digester;
    }
View Full Code Here

     */
    public void testNoExpansion() throws SAXException, IOException {

        String xml = "<root alpha='${attr1}' beta='var{attr2}'/>";
        StringReader input = new StringReader(xml);
        Digester digester = new Digester();
       
        // Configure the digester as required
        digester.addObjectCreate("root", SimpleTestBean.class);
        digester.addSetProperties("root");

        // Parse our test input.
        Object root = digester.parse(input);

        assertNotNull("Digester returned no object", root);
        SimpleTestBean bean = (SimpleTestBean) root;
       
        assertEquals("${attr1}", bean.getAlpha());
View Full Code Here

     */
    public void testExpansionWithNoSource() throws SAXException, IOException {

        String xml = "<root alpha='${attr1}' beta='var{attr2}'/>";
        StringReader input = new StringReader(xml);
        Digester digester = new Digester();
       
        // Configure the digester as required
        MultiVariableExpander expander = new MultiVariableExpander();
        digester.setSubstitutor(new VariableSubstitutor(expander));
        digester.addObjectCreate("root", SimpleTestBean.class);
        digester.addSetProperties("root");

        // Parse our test input.
        Object root = digester.parse(input);

        assertNotNull("Digester returned no object", root);
        SimpleTestBean bean = (SimpleTestBean) root;
       
        assertEquals("${attr1}", bean.getAlpha());
View Full Code Here

              "<bean alpha='${attr1}' beta='var{attr1}'/>" +
              "<bean alpha='${attr2}' beta='var{attr2}'/>" +
            "</root>";
           
        StringReader input = new StringReader(xml);
        Digester digester = new Digester();
       
        // Configure the digester as required
        HashMap source1 = new HashMap();
        source1.put("attr1", "source1.attr1");
        source1.put("attr2", "source1.attr2"); // should not be used
       
        HashMap source2 = new HashMap();
        source2.put("attr1", "source2.attr1"); // should not be used
        source2.put("attr2", "source2.attr2");
       
       
        MultiVariableExpander expander = new MultiVariableExpander();
        expander.addSource("$", source1);
        expander.addSource("var", source2);
       
        digester.setSubstitutor(new VariableSubstitutor(expander));
        digester.addObjectCreate("root/bean", SimpleTestBean.class);
        digester.addSetProperties("root/bean");
        digester.addSetNext("root/bean", "addSimpleTestBean");

        // Parse our test input.
        this.simpleTestBeans.clear();
        digester.push(this);
        digester.parse(input);

        assertEquals(2, this.simpleTestBeans.size());

        {
        SimpleTestBean bean = (SimpleTestBean) this.simpleTestBeans.get(0);
View Full Code Here

            "Twas noun{1} and the noun{2}" +
            " did verb{1} and verb{2} in the noun{3}" +
            "</root>";

        StringReader input = new StringReader(xml);
        Digester digester = new Digester();
       
        // Configure the digester as required
        HashMap nouns = new HashMap();
        nouns.put("1", "brillig");
        nouns.put("2", "slithy toves");
        nouns.put("3", "wabe");
       
        HashMap verbs = new HashMap();
        verbs.put("1", "gyre");
        verbs.put("2", "gimble");
       
        MultiVariableExpander expander = new MultiVariableExpander();
        expander.addSource("noun", nouns);
        expander.addSource("verb", verbs);
        digester.setSubstitutor(new VariableSubstitutor(expander));
       
        digester.addObjectCreate("root", SimpleTestBean.class);
        digester.addCallMethod("root", "setAlpha", 0);

        // Parse our test input.
        Object root = digester.parse(input);

        assertNotNull("Digester returned no object", root);
        SimpleTestBean bean = (SimpleTestBean) root;
       
        assertEquals(
View Full Code Here

     */
    public void testExpansionException() throws IOException {

        String xml = "<root alpha='${attr1}'/>";
        StringReader input = new StringReader(xml);
        Digester digester = new Digester();
       
        // Configure the digester as required
        MultiVariableExpander expander = new MultiVariableExpander();
        expander.addSource("$", new HashMap());
        digester.setSubstitutor(new VariableSubstitutor(expander));
       
        digester.addObjectCreate("root", SimpleTestBean.class);
        digester.addSetProperties("root");

        // Parse our test input.
        try {
            digester.parse(input);
            fail("Exception expected due to unknown variable.");
        } catch(SAXException e) {
            // expected, due to reference to undefined variable
        }
    }
View Full Code Here

    public void testRecursiveRules() throws Exception {
        // * tests that a rule can declare custom PluginCreateRules
        //   that allow it to plug in instances of itself below
        //   itself.

        Digester digester = new Digester();
        PluginRules rc = new PluginRules();
        digester.setRules(rc);
       
        PluginDeclarationRule pdr = new PluginDeclarationRule();
        digester.addRule("*/plugin", pdr);
       
        PluginCreateRule pcr = new PluginCreateRule(Widget.class);
        digester.addRule("root/widget", pcr);
        digester.addSetNext("root/widget", "addChild");

        Container root = new Container();
        digester.push(root);
       
        try {
            digester.parse(
                TestAll.getInputStream(this, "test6.xml"));
        }
        catch(Exception e) {
            throw e;
        }
View Full Code Here

   
    public void testDefaultBehaviour() throws Exception {
        // tests that by default the attributes used are
        // named "plugin-class" and "plugin-id"

        Digester digester = new Digester();
        digester.setNamespaceAware(true);
        PluginRules rc = new PluginRules();
        digester.setRules(rc);
       
        PluginDeclarationRule pdr = new PluginDeclarationRule();
        digester.addRule("root/plugin", pdr);
       
        PluginCreateRule widgetPluginRule = new PluginCreateRule(Widget.class);
        digester.addRule("root/widget", widgetPluginRule);
        digester.addSetNext("root/widget", "addWidget");

        PluginCreateRule gadgetPluginRule = new PluginCreateRule(Widget.class);
        digester.addRule("root/gadget", gadgetPluginRule);
        digester.addSetNext("root/gadget", "addGadget");

        MultiContainer root = new MultiContainer();
        digester.push(root);
       
        try {
            digester.parse(
                TestAll.getInputStream(this, "test7.xml"));

        } catch(Exception e) {
            throw e;
        }
View Full Code Here

        // with "null" for namespace (ie attributes not in any namespace).
        //
        // note that in order not to screw up all other tests, we need
        // to reset the global names after we finish here!

        Digester digester = new Digester();
        digester.setNamespaceAware(true);
        PluginRules rc = new PluginRules();
        digester.setRules(rc);

        rc.setPluginIdAttribute(null, "id");
        rc.setPluginClassAttribute(null, "class");
    
        PluginDeclarationRule pdr = new PluginDeclarationRule();
        digester.addRule("root/plugin", pdr);
       
        PluginCreateRule widgetPluginRule = new PluginCreateRule(Widget.class);
        digester.addRule("root/widget", widgetPluginRule);
        digester.addSetNext("root/widget", "addWidget");

        PluginCreateRule gadgetPluginRule = new PluginCreateRule(Widget.class);
        digester.addRule("root/gadget", gadgetPluginRule);
        digester.addSetNext("root/gadget", "addGadget");

        MultiContainer root = new MultiContainer();
        digester.push(root);
       
        try {
            digester.parse(
                TestAll.getInputStream(this, "test7.xml"));
               
        } catch(Exception e) {
            throw e;
        }
View Full Code Here

TOP

Related Classes of org.apache.commons.digester.Digester

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.