KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xml > SimpleHandler


1 package jfun.yan.xml;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.Stack JavaDoc;
6
7
8
9 import org.xml.sax.SAXException JavaDoc;
10 import org.xml.sax.SAXParseException JavaDoc;
11 import org.xml.sax.helpers.DefaultHandler JavaDoc;
12
13 import org.xml.sax.Locator JavaDoc;
14
15 class SimpleHandler extends DefaultHandler JavaDoc {
16   private final class TagBuilder{
17     private final Location loc;
18     private final List JavaDoc nodes = new ArrayList JavaDoc();
19     private final Attributes attrs;
20     private final String JavaDoc uri;
21     private final String JavaDoc name;
22     private final String JavaDoc qname;
23     public String JavaDoc toString(){
24       return name;
25     }
26     TagBuilder(Location loc, String JavaDoc uri, String JavaDoc name, String JavaDoc qname,
27         Attributes attrs) {
28       this.attrs = attrs;
29       this.loc = loc;
30       this.name = name;
31       this.qname = qname;
32       this.uri = uri;
33     }
34     void addNode(Node node){
35       nodes.add(node);
36     }
37     void collectText(TextBuilder tbuilder){
38       if(!tbuilder.isWhitespace()){
39         //we do not care whitespace at beginning element.
40
nodes.add(tbuilder.toNode());
41       }
42     }
43     Tag getResult(TextBuilder tbuilder){
44       if(tbuilder != null){
45         if(!tbuilder.isWhitespace() || nodes.isEmpty()){
46           nodes.add(tbuilder.toNode());
47         }
48       }
49       return new Tag(loc, name, attrs, nodes);
50     }
51   }
52   public Location getLocation(){
53     return MyUtil.toLocation(loc);
54   }
55   private Locator JavaDoc loc;
56   private final Stack JavaDoc builders = new Stack JavaDoc();
57   private TagBuilder builder = null;
58   private Node result;
59   
60   private static final class TextBuilder{
61     private final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
62     private final Location loc;
63     private boolean whitespace = true;
64     
65     TextBuilder(Location loc) {
66       this.loc = loc;
67     }
68     public Location getLocation() {
69       return loc;
70     }
71     boolean isWhitespace() {
72       return whitespace;
73     }
74     void setWhitespace(boolean whitespace) {
75       this.whitespace = whitespace;
76     }
77     void addText(String JavaDoc s){
78       if(s.trim().length()>0){
79         whitespace = false;
80       }
81       buf.append(s);
82     }
83     public CharacterData toNode(){
84       return new CharacterData(loc, buf.toString());
85     }
86   }
87   private TextBuilder tbuilder = null;
88   private void addText(String JavaDoc s){
89     if(s.length()==0) return;
90     if(tbuilder==null){
91       tbuilder = new TextBuilder(getLocation());
92     }
93     tbuilder.addText(s);
94   }
95   private TextBuilder readText(){
96     if(tbuilder==null) return null;
97     final TextBuilder ret = tbuilder;
98     tbuilder = null;
99     return ret;
100   }
101   public void characters(char[] chars, int start, int length){
102     final String JavaDoc text = new String JavaDoc(chars, start, length);
103     addText(text);
104     /*
105     if(text.length()==0) return;
106     if(textbuf.length()!=0){
107       textbuf.append(text);
108       return;
109     }
110     if(text.trim().length()==0){
111       if(ignorable_spaces==null){
112         ignorable_spaces_location = getLocation();
113         ignorable_spaces = text;
114       }
115       else{
116         ignorable_spaces.append(text);
117       }
118       return;
119     }
120
121     final CharacterData data = new CharacterData(getLocation(), text);
122     if(this.builder == null){
123       result = data;
124     }
125     else{
126       builder.addNode(data);
127     }*/

128   }
129
130   public void endElement(String JavaDoc uri, String JavaDoc name, String JavaDoc qname) throws SAXException JavaDoc {
131     if(builder == null)
132       throw new IllegalStateException JavaDoc("not opening tag for "+name);
133     Tag tag = builder.getResult(readText());
134     
135     if(builders.isEmpty()){
136       //top level
137
result = tag;
138     }
139     else{
140       this.builder = (TagBuilder)builders.pop();
141       this.builder.addNode(tag);
142     }
143   }
144
145   public void setDocumentLocator(org.xml.sax.Locator JavaDoc loc) {
146     this.loc = loc;
147   }
148   private static Attributes convert(org.xml.sax.Attributes JavaDoc attrs){
149     final int sz = attrs.getLength();
150     final Attributes result = new Attributes(sz);
151     for(int i=0; i<sz; i++){
152       result.add(attrs.getQName(i), attrs.getValue(i));
153     }
154     return result;
155   }
156   private void collectText(TextBuilder tbuilder){
157     if(tbuilder!=null){
158       if(builder==null){
159         if(tbuilder.isWhitespace()){
160           //do not care about whitespace when not within an element.
161
return;
162         }
163         throw new IllegalStateException JavaDoc("text outside of a tag unrecoginized");
164       }
165       else{
166         builder.collectText(tbuilder);
167       }
168     }
169   }
170   public void startElement(String JavaDoc uri, String JavaDoc name, String JavaDoc qname,
171       org.xml.sax.Attributes JavaDoc attrs){
172     /*
173     ignorable_spaces = null;//since there's element inside, we ignore those spaces.
174     ignorable_spaces_location = null;
175     */

176     collectText(readText());
177     if(builder != null){
178       builders.push(builder);
179     }
180     builder = new TagBuilder(getLocation(), uri, name, qname, convert(attrs));
181   }
182   
183   public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
184     throw new ConfigurationException("xml error: "+e.getMessage(),
185         e, getLocation());
186   }
187
188   public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
189     throw new ConfigurationException("fatal xml error: "+e.getMessage(),
190         e, getLocation());
191   }
192
193   public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
194     System.err.println(e.getMessage());
195     super.warning(e);
196   }
197
198   public Node getResult(){
199     return result;
200   }
201 }
202
Popular Tags