skitching 2005/01/17 01:56:34 Modified: digester/src/examples/api/addressbook Main.java Person.java example.xml Added: digester/src/examples/api/addressbook Address.java Log: Add example usage of SetNestedPropertiesRule. Patch inspired by code contributed to xmlrules addressbook example by Wendy Smoak. Revision Changes Path 1.2 +14 -0 jakarta-commons/digester/src/examples/api/addressbook/Main.java Index: Main.java =================================================================== RCS file: /home/cvs/jakarta-commons/digester/src/examples/api/addressbook/Main.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- Main.java 17 Apr 2004 10:57:46 -0000 1.1 +++ Main.java 17 Jan 2005 09:56:34 -0000 1.2 @@ -129,6 +129,20 @@ 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"); } private static void usage() { 1.6 +10 -0 jakarta-commons/digester/src/examples/api/addressbook/Person.java Index: Person.java =================================================================== RCS file: /home/cvs/jakarta-commons/digester/src/examples/api/addressbook/Person.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- Person.java 17 Apr 2004 10:59:37 -0000 1.5 +++ Person.java 17 Jan 2005 09:56:34 -0000 1.6 @@ -14,8 +14,10 @@ * limitations under the License. */ +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import java.util.List; /** * See Main.java. @@ -25,6 +27,7 @@ private String category; private String name; private HashMap emails = new HashMap(); + private List addresses = new ArrayList(); /** * A unique id for this person. Note that the Digester automatically @@ -46,6 +49,10 @@ public void addEmail(String type, String address) { emails.put(type, address); } + + public void addAddress( Address addr ) { + addresses.add( addr ); + } public void print() { System.out.println("Person #" + id); @@ -58,5 +65,8 @@ System.out.println(" email (type " + type + ") : " + address); } + + System.out.println( addresses ); + } } 1.3 +25 -1 jakarta-commons/digester/src/examples/api/addressbook/example.xml Index: example.xml =================================================================== RCS file: /home/cvs/jakarta-commons/digester/src/examples/api/addressbook/example.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- example.xml 9 Sep 2004 20:38:20 -0000 1.2 +++ example.xml 17 Jan 2005 09:56:34 -0000 1.3 @@ -1,3 +1,4 @@ +<?xml version='1.0'?> <!-- Copyright 2004 The Apache Software Foundation. @@ -13,17 +14,40 @@ See the License for the specific language governing permissions and limitations under the License. --> - <address-book> <person id="1" category="acquaintance"> <name>Gonzo</name> <email type="business">[EMAIL PROTECTED]</email> + <address> + <type>home</type> + <street>123 Maine Ave.</street> + <city>Las Vegas</city> + <state>NV</state> + <zip>01234</zip> + <country>USA</country> + </address> + <address> + <type>business</type> + <street>234 Maple Dr.</street> + <city>Los Angeles</city> + <state>CA</state> + <zip>98765</zip> + <country>USA</country> + </address> </person> <person id="2" category="rolemodel"> <name>Kermit</name> <email type="business">[EMAIL PROTECTED]</email> <email type="home">[EMAIL PROTECTED]</email> + <address> + <type>business</type> + <street>987 Brown Rd</street> + <city>Las Cruces</city> + <state>NM</state> + <zip>75321</zip> + <country>USA</country> + </address> </person> </address-book> 1.1 jakarta-commons/digester/src/examples/api/addressbook/Address.java Index: Address.java =================================================================== /*
* Copyright 2001-2004 The Apache Software Foundation. * * Licensed 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. */ import java.util.HashMap; import java.util.Iterator; /** * See Main.java. */ public class Address { private String type; private String street; private String city; private String state; private String zip; private String country; public String toString() { StringBuffer sb = new StringBuffer(); sb.append( " address (type "+ type + ")\n"); sb.append( " " + street + "\n"); sb.append( " " + city + " " + state + " " + zip + "\n"); sb.append( " " + country + "\n"); return sb.toString(); } /** * Returns the value of street. */ public String getStreet() { return street; } /** * Sets the value of street. * @param street The value to assign to street. */ public void setStreet(String street) { this.street = street; } /** * Returns the value of city. */ public String getCity() { return city; } /** * Sets the value of city. * @param city The value to assign to city. */ public void setCity(String city) { this.city = city; } /** * Returns the value of state. */ public String getState() { return state; } /** * Sets the value of state. * @param state The value to assign to state. */ public void setState(String state) { this.state = state; } /** * Returns the value of zip. */ public String getZip() { return zip; } /** * Sets the value of zip. * @param zip The value to assign to zip. */ public void setZip(String zip) { this.zip = zip; } /** * Returns the value of country. */ public String getCountry() { return country; } /** * Sets the value of country. * @param country The value to assign to country. */ public void setCountry(String country) { this.country = country; } /** * Returns the value of type. */ public String getType() { return type; } /** * Sets the value of type. * @param type The value to assign to type. */ public void setType(String type) { this.type = type; } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]