1 16 17 import org.apache.commons.digester.Digester; 18 19 37 public class Main { 38 39 44 public static void main(String [] args) { 45 if (args.length != 1) { 46 usage(); 47 System.exit(-1); 48 } 49 50 String filename = args[0]; 51 52 Digester d = new Digester(); 54 55 AddressBook book = new AddressBook(); 59 d.push(book); 60 61 addRules(d); 64 65 try { 67 java.io.File srcfile = new java.io.File (filename); 68 d.parse(srcfile); 69 } 70 catch(java.io.IOException ioe) { 71 System.out.println("Error reading input file:" + ioe.getMessage()); 72 System.exit(-1); 73 } 74 catch(org.xml.sax.SAXException se) { 75 System.out.println("Error parsing input file:" + se.getMessage()); 76 System.exit(-1); 77 } 78 79 80 book.print(); 83 } 84 85 private static void addRules(Digester d) { 86 87 90 d.addObjectCreate("address-book/person", Person.class); 93 94 d.addSetProperties("address-book/person"); 109 110 d.addSetNext("address-book/person", "addPerson"); 114 115 d.addCallMethod("address-book/person/name", "setName", 0); 124 125 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 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 |