1 package org.apache.commons.digester3.examples.xmlrules.addressbook;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import org.apache.commons.digester3.Digester;
26 import org.apache.commons.digester3.xmlrules.FromXmlRulesModule;
27 import org.xml.sax.SAXException;
28
29 /**
30 * A simple program to demonstrate the basic functionality of the
31 * Commons Digester module with the xmlrules extension.
32 * <p>
33 * This code will parse the provided "example.xml" file to build a tree
34 * of java objects, then cause those objects to print out their values
35 * to demonstrate that the input file has been processed correctly.
36 * <p>
37 * Unlike the "addressbook" example in the "api" section, this implementation
38 * has no parsing rules hard-wired into the code in this class. Instead, the
39 * parsing rules are loaded from an external file at runtime. This allows
40 * the parsing rules to be modified without compiling the code, and
41 * potentially makes it somewhat easier to review the parsing rules.
42 * <p>
43 * Note, however, that there is tyically quite a tight coupling between
44 * the parsing rules and the <i>purpose</i> of the application which means
45 * that it may not be all that common for parsing rules to be altered
46 * without the application code also being altered, so only in some cases
47 * will this prove of benefit. As with all software, it must be determined
48 * whether this feature provides a true benefit in the context of the
49 * application it is being applied to.
50 * <p>
51 * Usage: java Main xmlrules.xml example.xml
52 */
53 public class Main
54 {
55
56 /**
57 * Main method : entry point for running this example program.
58 * <p>
59 * Usage: java Example example.xml
60 */
61 public static void main( String[] args )
62 throws Exception
63 {
64 if ( args.length != 2 )
65 {
66 usage();
67 System.exit( -1 );
68 }
69
70 final String rulesfileName = args[0];
71 String datafileName = args[1];
72
73 // Create a Digester instance which has been initialised with
74 // rules loaded from the specified file.
75 Digester d = newLoader( new FromXmlRulesModule()
76 {
77
78 @Override
79 protected void loadRules()
80 {
81 loadXMLRules( rulesfileName );
82 }
83
84 } ).newDigester();
85
86 // Prime the digester stack with an object for rules to
87 // operate on. Note that it is quite common for "this"
88 // to be the object pushed.
89 AddressBook book = new AddressBook();
90 d.push( book );
91
92 // Process the input file.
93 try
94 {
95 File srcfile = new java.io.File( datafileName );
96 d.parse( srcfile );
97 }
98 catch ( IOException ioe )
99 {
100 System.out.println( "Error reading input file:" + ioe.getMessage() );
101 System.exit( -1 );
102 }
103 catch ( SAXException se )
104 {
105 System.out.println( "Error parsing input file:" + se.getMessage() );
106 System.exit( -1 );
107 }
108
109 // Print out all the contents of the address book, as loaded from
110 // the input file.
111 book.print();
112 }
113
114 private static void usage()
115 {
116 System.out.println( "Usage: java Main xmlrules.xml example.xml" );
117 }
118
119 }