/Users/lyon/j4p/src/xml/adbk/ParseAml.java

1    package xml.adbk; 
2     
3    import org.xml.sax.helpers.DefaultHandler; 
4     
5    import java.util.Vector; 
6     
7    /** 
8     *  ParseAml is a <code>DocumentHandler</code> 
9     *  that can parse AddressBook Markup Language 
10    */ 
11   public class ParseAml 
12           extends DefaultHandler { 
13    
14       private AddressBook ab = new AddressBook(); 
15       private Address a = new Address(); 
16       private Vector stringVector = new Vector(); 
17    
18       public AddressBook getAddressBook() { 
19           return ab; 
20       } 
21    
22       public String getLastValue() { 
23           if (stringVector.size() == 0) return null; 
24    
25           return (String) stringVector.elementAt(stringVector.size() - 1); 
26       } 
27    
28       public void endElement(String s1, String s2, String name) { 
29           String sa[] = new String[stringVector.size()]; 
30           stringVector.copyInto(sa); 
31           /* 
32                   (userId | password | 
33                   title | firstName| lastName)*, 
34               (streetAddress|address1 | address2| address3 )*, 
35               (homePage | emailAddress)*, 
36               (homePhone | businessPhone | faxPhone)*, 
37               city, 
38               state, 
39               zip?)> 
40           */ 
41           if (name.equals("userId")) 
42               a.setUserId(getLastValue()); 
43           if (name.equals("password")) 
44               a.setPassword(getLastValue()); 
45           if (name.equals("title")) 
46               a.setTitle(getLastValue()); 
47           if (name.equals("firstName")) 
48               a.setFirstName(getLastValue()); 
49           if (name.equals("lastName")) 
50               a.setLastName(getLastValue()); 
51           if (name.equals("streetAddress")) 
52               a.setStreet(getLastValue()); 
53           if (name.equals("address1")) 
54               a.setAddress1(getLastValue()); 
55           if (name.equals("address2")) 
56               a.setAddress2(getLastValue()); 
57           if (name.equals("address3")) 
58               a.setAddress3(getLastValue()); 
59           if (name.equals("homePage")) 
60               a.setHomePage(getLastValue()); 
61           if (name.equals("emailAddress")) 
62               a.setEmailAddress(getLastValue()); 
63           if (name.equals("homePhone")) 
64               a.setHomePhone(getLastValue()); 
65           if (name.equals("businessPhone")) 
66               a.setBusinessPhone(getLastValue()); 
67           if (name.equals("faxPhone")) 
68               a.setFaxPhone(getLastValue()); 
69           if (name.equals("company")) 
70               a.setCompany(getLastValue()); 
71           if (name.equals("zip")) 
72               a.setZip(getLastValue()); 
73           if (name.equals("state")) 
74               a.setState(getLastValue()); 
75           if (name.equals("street")) 
76               a.setStreet(getLastValue()); 
77           if (name.equals("city")) 
78               a.setCity(getLastValue()); 
79    
80           // if </Address>, we are done with this record 
81           if (name.equals("Address")) 
82               addAddressRecord(); 
83       } 
84    
85       public void addAddressRecord() { 
86           System.out.println("adding record:" + a.toXml()); 
87           ab.addAddress(a); 
88           a = new Address(); 
89       } 
90    
91       /** 
92        * 
93        * when characters are found, we add them to 
94        * the string vector for latter use. 
95        */ 
96       public void characters(char buf [], int offset, int len) { 
97           String s = new String(buf, offset, len); 
98           stringVector.addElement(s); 
99       } 
100   
101      public void ignorableWhitespace( 
102              char buf [], int offset, int len) { 
103      } 
104   
105      /** 
106       * The processingInstruction is 
107       *  called back when a non XML 
108       * declaration is made. 
109       */ 
110      public void processingInstruction( 
111              String target, 
112              String data) { 
113      } 
114  } 
115