You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2011/02/20 02:07:08 UTC
svn commit: r1072472 - in
/commons/sandbox/digester3/trunk/src/examples/catalog: ./
src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/commons/ src/main/java/org/apache/commons/digester3/
src/main/java/org/apache/commons/digester3/ex...
Author: simonetripodi
Date: Sun Feb 20 01:07:07 2011
New Revision: 1072472
URL: http://svn.apache.org/viewvc?rev=1072472&view=rev
Log:
imported and adapted the catalog example
Added:
commons/sandbox/digester3/trunk/src/examples/catalog/example.xml (with props)
commons/sandbox/digester3/trunk/src/examples/catalog/readme.txt (with props)
commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/
commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/
commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/
commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/
commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/
commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/
commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/AudioVisual.java (with props)
commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Book.java (with props)
commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/BookFactory.java (with props)
commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Catalog.java (with props)
commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/CatalogModel.java (with props)
commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Item.java (with props)
commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Main.java (with props)
Modified:
commons/sandbox/digester3/trunk/src/examples/catalog/pom.xml
Added: commons/sandbox/digester3/trunk/src/examples/catalog/example.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/examples/catalog/example.xml?rev=1072472&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/examples/catalog/example.xml (added)
+++ commons/sandbox/digester3/trunk/src/examples/catalog/example.xml Sun Feb 20 01:07:07 2011
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<!-- catalogue of items in a library. -->
+<catalog>
+ <book isbn="0-596-00184-3">
+ <title>Ant, The Definitive Guide</title>
+ <author>Jesse Tilly & Eric M. Burke</author>
+ <desc>Complete build management for Java.</desc>
+ </book>
+
+ <book isbn="0201310058">
+ <title>Effective Java</title>
+ <author>Joshua Bloch</author>
+ <desc>Tips for experienced Java software developers.</desc>
+ </book>
+
+ <dvd category="martial arts" year-made="1978">
+ <attr id="name" value="Drunken Master" />
+ <attr id="desc" value="Hilarious slapstick starring Jackie Chan." />
+ <attr id="runtime" value="106" />
+ </dvd>
+
+ <video category="drama" year-made="1993">
+ <attr id="name" value="The Piano" />
+ <attr id="desc" value="Character drama set in New Zealand during the Victorian era." />
+ <attr id="runtime" value="121" />
+ </video>
+</catalog>
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/example.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/example.xml
------------------------------------------------------------------------------
svn:keywords = Date Revision Author HeadURL Id
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/example.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml
Modified: commons/sandbox/digester3/trunk/src/examples/catalog/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/examples/catalog/pom.xml?rev=1072472&r1=1072471&r2=1072472&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/examples/catalog/pom.xml (original)
+++ commons/sandbox/digester3/trunk/src/examples/catalog/pom.xml Sun Feb 20 01:07:07 2011
@@ -32,4 +32,19 @@
<packaging>jar</packaging>
<name>Commons Digester :: Examples :: Catalog</name>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>exec-maven-plugin</artifactId>
+ <configuration>
+ <mainClass>org.apache.commons.digester3.examples.catalog.Main</mainClass>
+ <arguments>
+ <argument>example.xml</argument>
+ </arguments>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
</project>
Added: commons/sandbox/digester3/trunk/src/examples/catalog/readme.txt
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/examples/catalog/readme.txt?rev=1072472&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/examples/catalog/readme.txt (added)
+++ commons/sandbox/digester3/trunk/src/examples/catalog/readme.txt Sun Feb 20 01:07:07 2011
@@ -0,0 +1,55 @@
+#########################################################################
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#########################################################################
+== overview
+
+The files in this directory are intended as an example of how to use
+the Apache Digester's basic functionality via its java interface.
+
+Topics covered:
+* how to read xml from a string (instead of a file)
+* how to use Digester.getRoot() to retrieve the "root" object
+ created when parsing an input file.
+* how to use the "factory create" rule to create java objects which
+ do not have default (no-argument) constructors.
+* how to use the "set properties" rule (advanced usage) to map xml attributes
+ to java bean properties with names different from the xml attribute name.
+* how to use the SetPropertyRule.
+* how to use the ObjectParamRule to pass a constant string to a method.
+
+
+If you haven't read the "addressbook" example, it is recommended that
+you start there first. This example demonstrates more advanced features
+of the digester.
+
+== compiling and running
+
+
+First rename the build.properties.sample file in the parent directory
+to build.properties and edit it to suit your environment. Then in this
+directory:
+
+* to compile:
+ mvn compile
+
+* to build the jar artifact
+ mvn compile
+
+* to run:
+ mvn verify
+
+Alternatively, you can set up your CLASSPATH appropriately, and
+run the example directly.
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/readme.txt
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/readme.txt
------------------------------------------------------------------------------
svn:keywords = Date Revision Author HeadURL Id
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/readme.txt
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/AudioVisual.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/AudioVisual.java?rev=1072472&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/AudioVisual.java (added)
+++ commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/AudioVisual.java Sun Feb 20 01:07:07 2011
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.digester3.examples.catalog;
+
+/**
+ * See Main.java.
+ */
+
+public class AudioVisual implements Item {
+
+ private int yearMade;
+
+ private String category;
+
+ private String name;
+
+ private String desc;
+
+ private Integer runtime;
+
+ private String type;
+
+ // note: digester can convert a string in the xml file to an int.
+ public void setYearMade(int yearMade) {
+ this.yearMade = yearMade;
+ }
+
+ public void setCategory(String category) {
+ this.category = category;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public void setDesc(String desc) {
+ this.desc = desc;
+ }
+
+ // note: digester can convert a string in the xml file to an Integer
+ public void setRuntime(Integer runtime) {
+ this.runtime = runtime;
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+
+ public void print() {
+ System.out.println("AudioVisual:");
+ System.out.println(" type=" + type);
+ System.out.println(" yearMade=" + yearMade);
+ System.out.println(" category=" + category);
+ System.out.println(" name=" + name);
+ System.out.println(" desc=" + desc);
+ System.out.println(" runtime=" + runtime);
+ }
+
+}
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/AudioVisual.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/AudioVisual.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/AudioVisual.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Book.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Book.java?rev=1072472&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Book.java (added)
+++ commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Book.java Sun Feb 20 01:07:07 2011
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.digester3.examples.catalog;
+
+/**
+ * See Main.java.
+ */
+public class Book implements Item {
+
+ private String isbn;
+
+ private String title;
+
+ private String author;
+
+ private String desc;
+
+ public Book(String isbn) {
+ this.isbn = isbn;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public void setAuthor(String author) {
+ this.author = author;
+ }
+
+ public void setDesc(String desc) {
+ this.desc = desc;
+ }
+
+ public void print() {
+ System.out.println("Book:");
+ System.out.println(" isbn=" + isbn);
+ System.out.println(" title=" + title);
+ System.out.println(" author=" + author);
+ System.out.println(" desc=" + desc);
+ }
+
+}
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Book.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Book.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Book.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/BookFactory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/BookFactory.java?rev=1072472&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/BookFactory.java (added)
+++ commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/BookFactory.java Sun Feb 20 01:07:07 2011
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.digester3.examples.catalog;
+
+import org.apache.commons.digester3.rule.AbstractObjectCreationFactory;
+import org.xml.sax.Attributes;
+
+/**
+ * The Book class doesn't have a no-argument constructor, so the
+ * standard digester ObjectCreateRule can't be used to create instances
+ * of it.
+ * <p>
+ * To resolve this issue, the FactoryCreateRule can be used in
+ * conjunction with an appropriate factory class, like this one.
+ * The "createObject" method of the factory is invoked to generate
+ * object instances when required.
+ * <p>
+ * The factory object can access any xml attributes, plus of course
+ * any values set up within it before digester parsing starts (like
+ * JNDI references, database connections, etc) that it may in the
+ * process of generating an appropriate object.
+ * <p>
+ * Note that it is <i>not</i> possible for any data to be extracted
+ * from the body or subelements of the xml element that caused the
+ * createObject method on this factory to be invoked. For example:
+ * <pre>
+ * [book isdn="12345"]
+ * </pre>
+ * is fine; the isdn value can be accessed during the createObject method.
+ * However, given the xml:
+ * <pre>
+ * [book]
+ * [isdn]12345[/isdn]
+ * ...
+ * </pre>
+ * it is not possible to access the isdn number until after the
+ * Book instance has been created.
+ * <p>
+ * Note that even if the class to be created does have a default constructor,
+ * you may wish to use a factory class, in order to initialise the created
+ * object in specific ways, or insert created objects into a central
+ * register, etc.
+ * <p>
+ * And don't forget, either, that factories may be implemented as
+ * inner classes or anonymous classes if appropriate, reducing the
+ * overhead of using this functionality in many cases.
+ */
+public class BookFactory extends AbstractObjectCreationFactory<Book> {
+
+ @Override
+ public Book createObject(Attributes attributes) throws Exception {
+ String isbn = attributes.getValue("isbn");
+
+ if (isbn == null) {
+ throw new Exception(
+ "Mandatory isbn attribute not present on book tag.");
+ }
+
+ return new Book(isbn);
+ }
+
+}
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/BookFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/BookFactory.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/BookFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Catalog.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Catalog.java?rev=1072472&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Catalog.java (added)
+++ commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Catalog.java Sun Feb 20 01:07:07 2011
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.digester3.examples.catalog;
+
+import java.util.LinkedList;
+
+/**
+ * See Main.java.
+ */
+public class Catalog {
+
+ private final LinkedList<Item> items = new LinkedList<Item>();
+
+ public void addItem(Item item) {
+ items.addLast(item);
+ }
+
+ public void print() {
+ System.out.println("This catalog has " + items.size() + " items");
+
+ for (Item item : this.items) {
+ item.print();
+ }
+ }
+
+}
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Catalog.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Catalog.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Catalog.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/CatalogModel.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/CatalogModel.java?rev=1072472&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/CatalogModel.java (added)
+++ commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/CatalogModel.java Sun Feb 20 01:07:07 2011
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.digester3.examples.catalog;
+
+import org.apache.commons.digester3.AbstractRulesModule;
+
+public final class CatalogModel extends AbstractRulesModule {
+
+ @Override
+ protected void configure() {
+ // when we encounter the root "catalog" tag, create an
+ // instance of the Catalog class.
+ //
+ // 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.
+ forPattern("catalog").createObject().ofType(Catalog.class);
+
+ forPattern("catalog/book")
+ // 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.
+ .factoryCreate().usingFactory(new BookFactory())
+ .then()
+ // 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")
+ .setNestedProperties()
+ .then()
+ // and add the book to the parent catalog object (which is
+ // the next-to-top object on the digester object stack).
+ .setNext("addItem");
+
+ forPattern("catalog/dvd")
+ // We are using the "AudioVisual" class to represent both
+ // dvds and videos, so when the "dvd" tag is encountered,
+ // create an AudioVisual object.
+ .createObject().ofType(AudioVisual.class)
+ .then()
+ // 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.
+ .setProperties().addAlias("year-made", "yearMade")
+ .then()
+ // 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.
+ .callMethod("setType").withParamCount(1)
+ .then()
+ // pass literal "dvd" string
+ .callParam()
+ .then()
+ // add this dvd to the parent catalog object
+ .setNext("addItem");
+
+ // 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.
+ forPattern("catalog/dvd/attr").setProperty("id").extractingValueFromAttribute("value");
+
+ // and here we repeat the dvd rules, but for the video tag.
+ forPattern("catalog/video")
+ .createObject().ofType(AudioVisual.class)
+ .then()
+ .setProperties().addAlias("year-made", "yearMade")
+ .then()
+ .callMethod("setType").withParamCount(1)
+ .then()
+ .objectParam("video")
+ .then()
+ .setNext("addItem");
+ forPattern("catalog/video/attr").setProperty("id").extractingValueFromAttribute("value");
+ }
+
+}
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/CatalogModel.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/CatalogModel.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/CatalogModel.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Item.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Item.java?rev=1072472&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Item.java (added)
+++ commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Item.java Sun Feb 20 01:07:07 2011
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.digester3.examples.catalog;
+
+/**
+ * See Main.java.
+ */
+public interface Item {
+
+ void print();
+
+}
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Item.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Item.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Item.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Main.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Main.java?rev=1072472&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Main.java (added)
+++ commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Main.java Sun Feb 20 01:07:07 2011
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.digester3.examples.catalog;
+
+import static org.apache.commons.digester3.DigesterLoader.newLoader;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.apache.commons.digester3.Digester;
+import org.xml.sax.SAXException;
+
+/**
+ * A simple program to demonstrate some of the functionality of the
+ * Commons Digester module.
+ * <p>
+ * This code will parse the provided "example.xml" file to build a tree
+ * of java objects, then cause those objects to print out their values
+ * to demonstrate that the input file has been processed correctly. The
+ * input file represents a catalog of items in a library.
+ * <p>
+ * As with all code, there are many ways of achieving the same goal;
+ * the solution here is only one possible implementation.
+* <p>
+ * Very verbose comments are included here, as this class is intended
+ * as a tutorial; if you look closely at method "addRules", you will
+ * see that the amount of code required to use the Digester is actually
+ * quite low.
+ * <p>
+ * Usage: java Main example.xml
+ */
+public class Main {
+
+ /**
+ * Main method : entry point for running this example program.
+ * <p>
+ * Usage: java CatalogDigester example.xml
+ */
+ public static void main(String[] args) {
+ if (args.length != 1) {
+ usage();
+ System.exit(-1);
+ }
+
+ String filename = args[0];
+
+ // Create a Digester instance
+ Digester d = newLoader(new CatalogModel()).newDigester();
+
+ // Process the input file.
+ try {
+ Reader reader = getInputData(filename);
+ d.parse(reader);
+ } catch (IOException ioe) {
+ System.out.println("Error reading input file:" + ioe.getMessage());
+ System.exit(-1);
+ } catch (SAXException se) {
+ System.out.println("Error parsing input file:" + se.getMessage());
+ System.exit(-1);
+ }
+
+ // Get the first object created by the digester's rules
+ // (the "root" object). Note that this is exactly the same object
+ // returned by the Digester.parse method; either approach works.
+ Catalog catalog = (Catalog) d.getRoot();
+
+ // Print out all the contents of the catalog, as loaded from
+ // the input file.
+ catalog.print();
+ }
+
+ /*
+ * Reads the specified file into memory, and returns a StringReader
+ * object which reads from that in-memory buffer.
+ * <p>
+ * This method exists just to demonstrate that the input to the
+ * digester doesn't need to be from a file; for example, xml could
+ * be read from a database or generated dynamically; any old buffer
+ * in memory can be processed by the digester.
+ * <p>
+ * Clearly, if the data is always coming from a file, then calling
+ * the Digester.parse method that takes a File object would be
+ * more sensible (see AddressBook example).
+ */
+ private static Reader getInputData(String filename) throws IOException {
+ File srcfile = new File(filename);
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
+ byte[] buf = new byte[100];
+ FileInputStream fis = new FileInputStream(srcfile);
+ for(;;) {
+ int nread = fis.read(buf);
+ if (nread == -1) {
+ break;
+ }
+ baos.write(buf, 0, nread);
+ }
+ fis.close();
+
+ return new StringReader( baos.toString() );
+ }
+
+ private static void usage() {
+ System.out.println("Usage: java Main example.xml");
+ }
+
+}
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Main.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Main.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/digester3/trunk/src/examples/catalog/src/main/java/org/apache/commons/digester3/examples/catalog/Main.java
------------------------------------------------------------------------------
svn:mime-type = text/plain