1 package zeus.concepts.xmlobject;
2
3 import org.xml.sax.SAXException;
4 import org.xml.sax.InputSource;
5
6 import java.io.InputStream;
7 import java.io.File;
8 import java.io.FileInputStream;
9 import java.io.FileNotFoundException;
10 import java.io.ByteArrayInputStream;
11 import java.io.FileWriter;
12 import java.io.IOException;
13
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Element;
16
17 import org.apache.xerces.parsers.DOMParser;
18
19 import java.util.List;
20 import java.util.Vector;
21
22 /***
23 * Basic implementation for XML parsers. Performs DOM based validating
24 * parsing, requiring only that the {@link #translateRoot(Document)}
25 * method be implemented to take control of the parsing. Requires an
26 * underlying Xerces parser to be available.
27 */
28 public abstract class Parser {
29
30 /*** Input data stream. */
31 protected InputStream inputStream;
32
33 /*** Root object of the XML data. */
34 protected Object root;
35
36 /***
37 * Default constructor.
38 */
39 public Parser() {
40 }
41
42 /***
43 * Construct instance, and use specified file as input source.
44 */
45 public Parser(File file) throws FileNotFoundException {
46 this();
47 setInputStream(new FileInputStream(file));
48 }
49
50 /***
51 * Construct instance, and use specified string as input source.
52 */
53 public Parser(String string) {
54 this();
55 setInputStream(new ByteArrayInputStream(string.getBytes()));
56 }
57
58 /***
59 * Translate the root element of the document, then return it as a
60 * Java object.
61 */
62 protected abstract Object translateRoot(Document doc);
63
64 /***
65 * Read the input source and build the Java object structure. Places
66 * the result into the root object variable.
67 */
68 public void load() {
69 try {
70 DOMParser parser = new DOMParser();
71 setupParser(parser);
72 InputSource input = new InputSource(getInputStream());
73 parser.parse(input);
74 Document doc = parser.getDocument();
75 setRootObject(translateRoot(doc));
76 }
77 catch(SAXException s) {
78 ;
79 }
80 catch(java.io.IOException i) {
81 ;
82 }
83 }
84
85 /***
86 * Set the validation features of the underlying parser. Currently
87 * requires a Xerces parser.
88 */
89 protected void setupParser(DOMParser parser) {
90 String[] features = new String[] {
91 "http://xml.org/sax/features/validation",
92 "http://xml.org/sax/features/namespaces",
93 "http://apache.org/xml/features/validation/dynamic",
94 "http://apache.org/xml/features/validation/schema",
95 "http://apache.org/xml/features/validation/schema-full-checking" };
96
97 for(int index = 0 ; index < features.length ; index++ ) {
98 setFeature(features[index], parser);
99 }
100 }
101
102 /***
103 * Set a feature of the parser.
104 */
105 protected boolean setFeature(String feature, DOMParser parser) {
106 try {
107 parser.setFeature(feature, true);
108 return true;
109 }
110 catch(SAXException s) {
111 return false;
112 }
113 }
114
115 /***
116 * Set the input source for the parser.
117 */
118 public void setInputStream(InputStream inputStream) {
119 this.inputStream = inputStream;
120 }
121
122 /***
123 * Retrieve the input stream for the parser.
124 */
125 public InputStream getInputStream() {
126 return inputStream;
127 }
128
129 /***
130 * Set the root of the object hierarchy.
131 */
132 public void setRootObject(Object root) {
133 this.root = root;
134 }
135
136 /***
137 * Retrieve the root object. If it is currently null, it will
138 * attempt to load an instance from the input stream.
139 */
140 public Object getRootObject() {
141 if(root == null) {
142 load();
143 }
144 return root;
145 }
146
147 /***
148 * Update the object hierarchy from the current input stream.
149 */
150 public void refresh() {
151 load();
152 }
153
154 /***
155 * Write the current root object to the specified file.
156 */
157 public boolean write(File file) {
158 try {
159 FileWriter writer = new FileWriter(file);
160 writer.write(getRootObject().toString());
161 writer.flush();
162 writer.close();
163 return true;
164 }
165 catch(IOException i) {
166 return false;
167 }
168 }
169
170 }