KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > io > xml > xppdom > Xpp3DomBuilder


1 package com.thoughtworks.xstream.io.xml.xppdom;
2
3 import org.xmlpull.mxp1.MXParser;
4 import org.xmlpull.v1.XmlPullParser;
5
6 import java.io.Reader JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9
10 public class Xpp3DomBuilder {
11     public static Xpp3Dom build(Reader JavaDoc reader)
12             throws Exception JavaDoc {
13         List JavaDoc elements = new ArrayList JavaDoc();
14
15         List JavaDoc values = new ArrayList JavaDoc();
16
17         Xpp3Dom configuration = null;
18
19         XmlPullParser parser = new MXParser();
20
21         parser.setInput(reader);
22
23         int eventType = parser.getEventType();
24
25         while (eventType != XmlPullParser.END_DOCUMENT) {
26             if (eventType == XmlPullParser.START_TAG) {
27                 String JavaDoc rawName = parser.getName();
28
29                 Xpp3Dom childConfiguration = createConfiguration(rawName);
30
31                 int depth = elements.size();
32
33                 if (depth > 0) {
34                     Xpp3Dom parent = (Xpp3Dom) elements.get(depth - 1);
35
36                     parent.addChild(childConfiguration);
37                 }
38
39                 elements.add(childConfiguration);
40
41                 values.add(new StringBuffer JavaDoc());
42
43                 int attributesSize = parser.getAttributeCount();
44
45                 for (int i = 0; i < attributesSize; i++) {
46                     String JavaDoc name = parser.getAttributeName(i);
47
48                     String JavaDoc value = parser.getAttributeValue(i);
49
50                     childConfiguration.setAttribute(name, value);
51                 }
52             } else if (eventType == XmlPullParser.TEXT) {
53                 int depth = values.size() - 1;
54
55                 StringBuffer JavaDoc valueBuffer = (StringBuffer JavaDoc) values.get(depth);
56
57                 valueBuffer.append(parser.getText());
58             } else if (eventType == XmlPullParser.END_TAG) {
59                 int depth = elements.size() - 1;
60
61                 Xpp3Dom finishedConfiguration = (Xpp3Dom) elements.remove(depth);
62
63                 String JavaDoc accumulatedValue = (values.remove(depth)).toString();
64
65                 if (finishedConfiguration.getChildCount() == 0) {
66                     String JavaDoc finishedValue;
67
68                     if (0 == accumulatedValue.length()) {
69                         finishedValue = null;
70                     } else {
71                         finishedValue = accumulatedValue;
72                     }
73
74                     finishedConfiguration.setValue(finishedValue);
75                 }
76
77                 if (0 == depth) {
78                     configuration = finishedConfiguration;
79                 }
80             }
81
82             eventType = parser.next();
83         }
84
85         reader.close();
86
87         return configuration;
88     }
89
90     private static Xpp3Dom createConfiguration(String JavaDoc localName) {
91         return new Xpp3Dom(localName);
92     }
93 }
Popular Tags