KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Main


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import org.apache.commons.digester.Digester;
18
19 /**
20  * A simple program to demonstrate the basic functionality of the
21  * Commons Digester module.
22  * <p>
23  * This code will parse the provided "example.xml" file to build a tree
24  * of java objects, then cause those objects to print out their values
25  * to demonstrate that the input file has been processed correctly.
26  * <p>
27  * As with all code, there are many ways of achieving the same goal;
28  * the solution here is only one possible solution to the problem.
29 * <p>
30  * Very verbose comments are included here, as this class is intended
31  * as a tutorial; if you look closely at method "addRules", you will
32  * see that the amount of code required to use the Digester is actually
33  * quite low.
34  * <p>
35  * Usage: java Main example.xml
36  */

37 public class Main {
38     
39     /**
40      * Main method : entry point for running this example program.
41      * <p>
42      * Usage: java Example example.xml
43      */

44     public static void main(String JavaDoc[] args) {
45         if (args.length != 1) {
46             usage();
47             System.exit(-1);
48         }
49         
50         String JavaDoc filename = args[0];
51         
52         // Create a Digester instance
53
Digester d = new Digester();
54         
55         // Prime the digester stack with an object for rules to
56
// operate on. Note that it is quite common for "this"
57
// to be the object pushed.
58
AddressBook book = new AddressBook();
59         d.push(book);
60         
61         // Add rules to the digester that will be triggered while
62
// parsing occurs.
63
addRules(d);
64         
65         // Process the input file.
66
try {
67             java.io.File JavaDoc srcfile = new java.io.File JavaDoc(filename);
68             d.parse(srcfile);
69         }
70         catch(java.io.IOException JavaDoc ioe) {
71             System.out.println("Error reading input file:" + ioe.getMessage());
72             System.exit(-1);
73         }
74         catch(org.xml.sax.SAXException JavaDoc se) {
75             System.out.println("Error parsing input file:" + se.getMessage());
76             System.exit(-1);
77         }
78         
79         
80         // Print out all the contents of the address book, as loaded from
81
// the input file.
82
book.print();
83     }
84     
85     private static void addRules(Digester d) {
86
87         //--------------------------------------------------
88
// when we encounter a "person" tag, do the following:
89

90         // create a new instance of class Person, and push that
91
// object onto the digester stack of objects
92
d.addObjectCreate("address-book/person", Person.class);
93         
94         // map *any* attributes on the tag to appropriate
95
// setter-methods on the top object on the stack (the Person
96
// instance created by the preceeding rule).
97
//
98
// For example:
99
// if attribute "id" exists on the xml tag, and method setId
100
// with one parameter exists on the object that is on top of
101
// the digester object stack, then a call will be made to that
102
// method. The value will be type-converted from string to
103
// whatever type the target method declares (where possible),
104
// using the commons ConvertUtils functionality.
105
//
106
// Attributes on the xml tag for which no setter methods exist
107
// on the top object on the stack are just ignored.
108
d.addSetProperties("address-book/person");
109
110         // call the addPerson method on the second-to-top object on
111
// the stack (the AddressBook object), passing the top object
112
// on the stack (the recently created Person object).
113
d.addSetNext("address-book/person", "addPerson");
114         
115         //--------------------------------------------------
116
// when we encounter a "name" tag, call setName on the top
117
// object on the stack, passing the text contained within the
118
// body of that name element [specifying a zero parameter count
119
// implies one actual parameter, being the body text].
120
// The top object on the stack will be a person object, because
121
// the pattern address-book/person always triggers the
122
// ObjectCreateRule we added previously.
123
d.addCallMethod("address-book/person/name", "setName", 0);
124         
125         //--------------------------------------------------
126
// when we encounter an "email" tag, call addEmail on the top
127
// object on the stack, passing two parameters: the "type"
128
// attribute, and the text within the tag body.
129
d.addCallMethod("address-book/person/email", "addEmail", 2);
130         d.addCallParam("address-book/person/email", 0, "type");
131         d.addCallParam("address-book/person/email", 1);
132         
133         //--------------------------------------------------
134
// When we encounter an "address" tag, create an instance of class
135
// Address and push it on the digester stack of objects. After
136
// doing that, call addAddress on the second-to-top object on the
137
// digester stack (a "Person" object), passing the top object on
138
// the digester stack (the "Address" object). And also set things
139
// up so that for each child xml element encountered between the start
140
// of the address tag and the end of the address tag, the text
141
// contained in that element is passed to a setXXX method on the
142
// Address object where XXX is the name of the xml element found.
143
d.addObjectCreate("address-book/person/address", Address.class);
144         d.addSetNext("address-book/person/address", "addAddress");
145         d.addSetNestedProperties("address-book/person/address");
146     }
147
148     private static void usage() {
149         System.out.println("Usage: java Main example.xml");
150     }
151 }
Popular Tags