Example usage for org.apache.commons.digester3 Digester addSetNestedProperties

List of usage examples for org.apache.commons.digester3 Digester addSetNestedProperties

Introduction

In this page you can find the example usage for org.apache.commons.digester3 Digester addSetNestedProperties.

Prototype

public void addSetNestedProperties(String pattern) 

Source Link

Document

Adds an SetNestedPropertiesRule .

Usage

From source file:br.univali.celine.lms.config.LMSConfig.java

private static void doBuildConfig(String path) throws Exception {

    Digester d = new Digester();
    d.setUseContextClassLoader(true);//from  ww w. j  a va  2 s  . c  o  m
    d.push(config);

    d.addCallMethod("*/courses-folder", "setCoursesFolder", 0);
    //        d.addCallMethod("*/login-page", "setLoginPage", 0);
    d.addCallMethod("*/registerCourseOnImport", "setRegisterCourseOnImport", 0);
    d.addCallMethod("*/registerUserOnInsertCourse", "setRegisterUserOnInsert", 0);
    d.addCallMethod("*/error-page", "setErrorPage", 0);
    d.addCallMethod("*/lmsIntegration", "setLMSIntegration", 0);

    d.addObjectCreate("*/database-source/rdb", RDBDAO.class);
    d.addSetNestedProperties("*/database-source/rdb");
    d.addSetNext("*/database-source/rdb", "setDAO");

    d.addObjectCreate("*/database-source/xml", XMLDAO.class);
    d.addCallMethod("*/database-source/xml", "setFileName", 0);
    d.addSetNext("*/database-source/xml", "setDAO");

    d.addObjectCreate("*/database-source/bean", "", "class"); // devido a um bug da versao 3.3 tive que fazer esse workaround !!!
    //d.addObjectCreate("*/database-source/bean", "class", DAO.class); 
    d.addSetNext("*/database-source/bean", "setDAO");

    d.addRule("*/database-source/bean/bean-attribute", new BeanSetterAttribute());

    String fileName = path + "/celine-config.xml";
    java.io.File srcfile = new java.io.File(fileName);
    d.parse(srcfile);

    if (config.dao == null)
        throw new Exception("DAO is not defined at celine-config.xml");

    config.dao.initialize();

}

From source file:org.apache.commons.digester3.examples.api.addressbook.Main.java

private static void addRules(Digester d) {

    // --------------------------------------------------
    // when we encounter a "person" tag, do the following:

    // create a new instance of class Person, and push that
    // object onto the digester stack of objects
    d.addObjectCreate("address-book/person", Person.class);

    // map *any* attributes on the tag to appropriate
    // setter-methods on the top object on the stack (the Person
    // instance created by the preceeding rule).
    ///*from www. j  av a  2 s. co m*/
    // For example:
    // if attribute "id" exists on the xml tag, and method setId
    // with one parameter exists on the object that is on top of
    // the digester object stack, then a call will be made to that
    // method. The value will be type-converted from string to
    // whatever type the target method declares (where possible),
    // using the commons ConvertUtils functionality.
    //
    // Attributes on the xml tag for which no setter methods exist
    // on the top object on the stack are just ignored.
    d.addSetProperties("address-book/person");

    // call the addPerson method on the second-to-top object on
    // the stack (the AddressBook object), passing the top object
    // on the stack (the recently created Person object).
    d.addSetNext("address-book/person", "addPerson");

    // --------------------------------------------------
    // when we encounter a "name" tag, call setName on the top
    // object on the stack, passing the text contained within the
    // body of that name element [specifying a zero parameter count
    // implies one actual parameter, being the body text].
    // The top object on the stack will be a person object, because
    // the pattern address-book/person always triggers the
    // ObjectCreateRule we added previously.
    d.addCallMethod("address-book/person/name", "setName", 0);

    // --------------------------------------------------
    // when we encounter an "email" tag, call addEmail on the top
    // object on the stack, passing two parameters: the "type"
    // attribute, and the text within the tag body.
    d.addCallMethod("address-book/person/email", "addEmail", 2);
    d.addCallParam("address-book/person/email", 0, "type");
    d.addCallParam("address-book/person/email", 1);

    // --------------------------------------------------
    // When we encounter an "address" tag, create an instance of class
    // Address and push it on the digester stack of objects. After
    // doing that, call addAddress on the second-to-top object on the
    // digester stack (a "Person" object), passing the top object on
    // the digester stack (the "Address" object). And also set things
    // up so that for each child xml element encountered between the start
    // of the address tag and the end of the address tag, the text
    // contained in that element is passed to a setXXX method on the
    // Address object where XXX is the name of the xml element found.
    d.addObjectCreate("address-book/person/address", Address.class);
    d.addSetNext("address-book/person/address", "addAddress");
    d.addSetNestedProperties("address-book/person/address");
}

From source file:org.apache.commons.digester3.examples.api.catalog.Main.java

private static void addRules(Digester d) {

    // --------------------------------------------------

    // when we encounter the root "catalog" tag, create an
    // instance of the Catalog class.
    ////from   w ww.j ava2s.c o  m
    // Note that this approach is different from the approach taken in
    // the AddressBook example, where an initial "root" object was
    // explicitly created and pushed onto the digester stack before
    // parsing started instead
    //
    // Either approach is fine.

    d.addObjectCreate("catalog", Catalog.class);

    // --------------------------------------------------

    // when we encounter a book tag, we want to create a Book
    // instance. However the Book class doesn't have a default
    // constructor (one with no arguments), so we can't use
    // the ObjectCreateRule. Instead, we use the FactoryCreateRule.

    BookFactory factory = new BookFactory();
    d.addFactoryCreate("catalog/book", factory);

    // and add the book to the parent catalog object (which is
    // the next-to-top object on the digester object stack).
    d.addSetNext("catalog/book", "addItem");

    // we want each subtag of book to map the text contents of
    // the tag into a bean property with the same name as the tag.
    // eg <title>foo</title> --> setTitle("foo")
    d.addSetNestedProperties("catalog/book");

    // -----------------------------------------------

    // We are using the "AudioVisual" class to represent both
    // dvds and videos, so when the "dvd" tag is encountered,
    // create an AudioVisual object.

    d.addObjectCreate("catalog/dvd", AudioVisual.class);

    // add this dvd to the parent catalog object

    d.addSetNext("catalog/dvd", "addItem");

    // We want to map every xml attribute onto a corresponding
    // property-setter method on the Dvd class instance. However
    // this doesn't work with the xml attribute "year-made", because
    // of the internal hyphen. We could use explicit CallMethodRule
    // rules instead, or use a version of the SetPropertiesRule that
    // allows us to override any troublesome mappings...
    //
    // If there was more than one troublesome mapping, we could
    // use the method variant that takes arrays of xml-attribute-names
    // and bean-property-names to override multiple mappings.
    //
    // For any attributes not explicitly mapped here, the default
    // processing is applied, so xml attribute "category" --> setCategory.

    d.addSetProperties("catalog/dvd", "year-made", "yearMade");

    // We also need to tell this AudioVisual object that it is actually
    // a dvd; we can use the ObjectParamRule to pass a string to any
    // method. This usage is a little artificial - normally in this
    // situation there would be separate Dvd and Video classes.
    // Note also that equivalent behaviour could be implemented by
    // using factory objects to create & initialise the AudioVisual
    // objects with their type rather than using ObjectCreateRule.

    d.addCallMethod("catalog/dvd", "setType", 1);
    d.addObjectParam("catalog/dvd", 0, "dvd"); // pass literal "dvd" string

    // Each tag of form "<attr id="foo" value="bar"/> needs to map
    // to a call to setFoo("bar").
    //
    // This is an alternative to the syntax used for books above (see
    // method addSetNestedProperties), where the name of the subtag
    // indicated which property to set. Using this syntax in the xml has
    // advantages and disadvantages both for the user and the application
    // developer. It is commonly used with the FactoryCreateRule variant
    // which allows the target class to be created to be specified in an
    // xml attribute; this feature of FactoryCreateRule is not demonstrated
    // in this example, but see the Apache Tomcat configuration files for
    // an example of this usage.
    //
    // Note that despite the name similarity, there is no link
    // between SetPropertyRule and SetPropertiesRule.

    d.addSetProperty("catalog/dvd/attr", "id", "value");

    // -----------------------------------------------

    // and here we repeat the dvd rules, but for the video tag.
    d.addObjectCreate("catalog/video", AudioVisual.class);
    d.addSetNext("catalog/video", "addItem");
    d.addSetProperties("catalog/video", "year-made", "yearMade");
    d.addCallMethod("catalog/video", "setType", 1);
    d.addObjectParam("catalog/video", 0, "video");
    d.addSetProperty("catalog/video/attr", "id", "value");
}