KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > display > filters > dtd > MarkupImpl


1 /**
2  * This class was generated from a set of XML constraints
3  * by the Enhydra Zeus XML Data Binding Framework. All
4  * source code in this file is constructed specifically
5  * to work with other Zeus-generated classes. If you
6  * modify this file by hand, you run the risk of breaking
7  * this interoperation, as well as introducing errors in
8  * source code compilation.
9  *
10  * * * * * MODIFY THIS FILE AT YOUR OWN RISK * * * * *
11  *
12  * To find out more about the Enhydra Zeus framework, you
13  * can point your browser at <http://zeus.enhydra.org>
14  * where you can download releases, join and discuss Zeus
15  * on user and developer mailing lists, and access source
16  * code. Please report any bugs through that website.
17  */

18 package org.enhydra.barracuda.contrib.dbroggisch.display.filters.dtd;
19
20 // Global Implementation Import Statements
21
import java.io.File JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.io.Writer JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34 import org.xml.sax.EntityResolver JavaDoc;
35 import org.xml.sax.ErrorHandler JavaDoc;
36 import org.xml.sax.InputSource JavaDoc;
37 import org.xml.sax.Locator JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39 import org.xml.sax.SAXParseException JavaDoc;
40 import org.xml.sax.XMLReader JavaDoc;
41 import org.xml.sax.ext.LexicalHandler JavaDoc;
42 import org.xml.sax.helpers.DefaultHandler JavaDoc;
43 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
44
45 public class MarkupImpl extends DefaultHandler JavaDoc implements Unmarshallable, LexicalHandler JavaDoc, Markup {
46
47
48     /** Any DOCTYPE reference/statements. */
49     private String JavaDoc docTypeString;
50
51     /** The encoding for the output document */
52     private String JavaDoc outputEncoding;
53
54     /** The current node in unmarshalling */
55     private Unmarshallable zeus_currentUNode;
56
57     /** The parent node in unmarshalling */
58     private Unmarshallable zeus_parentUNode;
59
60     /** Whether this node has been handled */
61     private boolean zeus_thisNodeHandled = false;
62
63     /** Whether a DTD exists for an unmarshal call */
64     private boolean hasDTD;
65
66     /** Whether validation is occurring */
67     private boolean validate;
68
69     /** The namespace mappings on this element */
70     private Map JavaDoc namespaceMappings;
71
72     /** The EntityResolver for SAX parsing to use */
73     private static EntityResolver JavaDoc entityResolver;
74
75     /** The ErrorHandler for SAX parsing to use */
76     private static ErrorHandler JavaDoc errorHandler;
77
78     public MarkupImpl() {
79         docTypeString = "";
80         hasDTD = false;
81         validate = false;
82         namespaceMappings = new HashMap JavaDoc();
83     }
84
85     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
86         try {
87             startDTD(name, publicID, systemID);
88         } catch (SAXException JavaDoc neverHappens) { }
89     }
90
91     public void setOutputEncoding(String JavaDoc outputEncoding) {
92         this.outputEncoding = outputEncoding;
93     }
94
95     public void marshal(File JavaDoc file) throws IOException JavaDoc {
96         // Delegate to the marshal(Writer) method
97
marshal(new FileWriter JavaDoc(file));
98     }
99
100     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
101         // Delegate to the marshal(Writer) method
102
marshal(new OutputStreamWriter JavaDoc(outputStream));
103     }
104
105     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
106         // Write out the XML declaration
107
writer.write("<?xml version=\"1.0\" ");
108         if (outputEncoding != null) {
109             writer.write("encoding=\"");
110             writer.write(outputEncoding);
111             writer.write("\"?>\n\n");
112
113         } else {
114             writer.write("encoding=\"UTF-8\"?>\n\n");
115
116         }
117         // Handle DOCTYPE declaration
118
writer.write(docTypeString);
119         writer.write("\n");
120         // Now start the recursive writing
121
writeXMLRepresentation(writer, "");
122
123         // Close up
124
writer.flush();
125         writer.close();
126     }
127
128     protected void writeXMLRepresentation(Writer JavaDoc writer,
129                                           String JavaDoc indent)
130         throws IOException JavaDoc {
131
132         writer.write(indent);
133         writer.write("<markup");
134
135         // Handle namespace mappings (if needed)
136
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
137             String JavaDoc prefix = (String JavaDoc)i.next();
138             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
139             writer.write(" xmlns");
140             if (!prefix.trim().equals("")) {
141                 writer.write(":");
142                 writer.write(prefix);
143             }
144             writer.write("=\"");
145             writer.write(uri);
146             writer.write("\"\n ");
147         }
148
149         // Handle attributes (if needed)
150
writer.write("/>\n");
151     }
152
153     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
154         String JavaDoc returnValue = attributeValue;
155         for (int i = 0; i < returnValue.length(); i++) {
156             char ch = returnValue.charAt(i);
157             if (ch == '"') {
158                 returnValue = new StringBuffer JavaDoc()
159                     .append(returnValue.substring(0, i))
160                     .append("&quot;")
161                     .append(returnValue.substring(i+1))
162                     .toString();
163             }
164         }
165         return returnValue;
166     }
167
168     /**
169      * <p>
170      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
171      * </p>
172      *
173      * @param resolver the entity resolver to use.
174      */

175     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
176         entityResolver = resolver;
177     }
178
179     /**
180      * <p>
181      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
182      * </p>
183      *
184      * @param handler the entity resolver to use.
185      */

186     public static void setErrorHandler(ErrorHandler JavaDoc handler) {
187         errorHandler = handler;
188     }
189
190     public static Markup unmarshal(File JavaDoc file) throws IOException JavaDoc {
191         // Delegate to the unmarshal(Reader) method
192
return unmarshal(new FileReader JavaDoc(file));
193     }
194
195     public static Markup unmarshal(File JavaDoc file, boolean validate) throws IOException JavaDoc {
196         // Delegate to the unmarshal(Reader) method
197
return unmarshal(new FileReader JavaDoc(file), validate);
198     }
199
200     public static Markup unmarshal(InputStream JavaDoc inputStream) throws IOException JavaDoc {
201         // Delegate to the unmarshal(Reader) method
202
return unmarshal(new InputStreamReader JavaDoc(inputStream));
203     }
204
205     public static Markup unmarshal(InputStream JavaDoc inputStream, boolean validate) throws IOException JavaDoc {
206         // Delegate to the unmarshal(Reader) method
207
return unmarshal(new InputStreamReader JavaDoc(inputStream), validate);
208     }
209
210     public static Markup unmarshal(Reader JavaDoc reader) throws IOException JavaDoc {
211         // Delegate with default validation value
212
return unmarshal(reader, false);
213     }
214
215     public static Markup unmarshal(Reader JavaDoc reader, boolean validate) throws IOException JavaDoc {
216         MarkupImpl markup = new MarkupImpl();
217         markup.setValidating(validate);
218         markup.setCurrentUNode(markup);
219         markup.setParentUNode(null);
220         // Load the XML parser
221
XMLReader JavaDoc parser = null;
222         String JavaDoc parserClass = System.getProperty("org.xml.sax.driver",
223             "org.apache.xerces.parsers.SAXParser");
224         try {
225             parser = XMLReaderFactory.createXMLReader(parserClass);
226
227             // Set entity resolver, if needed
228
if (entityResolver != null) {
229                 parser.setEntityResolver(entityResolver);
230             }
231
232             // Set error handler
233
parser.setErrorHandler(markup);
234
235             // Register lexical handler
236
parser.setProperty("http://xml.org/sax/properties/lexical-handler", markup);
237
238             // Register content handler
239
parser.setContentHandler(markup);
240         } catch (SAXException JavaDoc e) {
241             throw new IOException JavaDoc("Could not load XML parser: " +
242                 e.getMessage());
243         }
244
245         InputSource JavaDoc inputSource = new InputSource JavaDoc(reader);
246         try {
247             parser.setFeature("http://xml.org/sax/features/validation", new Boolean JavaDoc(validate).booleanValue());
248             parser.setFeature("http://xml.org/sax/features/namespaces", true);
249             parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
250             parser.parse(inputSource);
251         } catch (SAXException JavaDoc e) {
252             throw new IOException JavaDoc("Error parsing XML document: " +
253                 e.getMessage());
254         }
255
256         // Return the resultant object
257
return markup;
258     }
259
260     public Unmarshallable getParentUNode() {
261         return zeus_parentUNode;
262     }
263
264     public void setParentUNode(Unmarshallable parentUNode) {
265         this.zeus_parentUNode = parentUNode;
266     }
267
268     public Unmarshallable getCurrentUNode() {
269         return zeus_currentUNode;
270     }
271
272     public void setCurrentUNode(Unmarshallable currentUNode) {
273         this.zeus_currentUNode = currentUNode;
274     }
275
276     public void setValidating(boolean validate) {
277         this.validate = validate;
278     }
279
280     public void startDocument() throws SAXException JavaDoc {
281         // no-op
282
}
283
284     public void setDocumentLocator(Locator JavaDoc locator) {
285         // no-op
286
}
287
288     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
289         throws SAXException JavaDoc {
290         namespaceMappings.put(prefix, uri);
291     }
292
293     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
294                              String JavaDoc qName, org.xml.sax.Attributes JavaDoc atts)
295         throws SAXException JavaDoc {
296
297         // Feed this to the correct ContentHandler
298
Unmarshallable current = getCurrentUNode();
299         if (current != this) {
300             current.startElement(namespaceURI, localName, qName, atts);
301             return;
302         }
303
304         // See if we handle, or we delegate
305
if ((localName.equals("markup")) && (!zeus_thisNodeHandled)) {
306             // Handle ourselves
307
for (int i=0, len=atts.getLength(); i<len; i++) {
308                 String JavaDoc attName= atts.getLocalName(i);
309                 String JavaDoc attValue = atts.getValue(i);
310             }
311             zeus_thisNodeHandled = true;
312             return;
313         } else {
314             // Delegate handling
315
}
316     }
317
318     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
319                            String JavaDoc qName)
320         throws SAXException JavaDoc {
321
322         Unmarshallable current = getCurrentUNode();
323         if (current != this) {
324             current.endElement(namespaceURI, localName, qName);
325             return;
326         }
327
328         Unmarshallable parent = getCurrentUNode().getParentUNode();
329         if (parent != null) {
330             parent.setCurrentUNode(parent);
331         }
332     }
333
334     public void characters(char[] ch, int start, int len)
335         throws SAXException JavaDoc {
336
337         // Feed this to the correct ContentHandler
338
Unmarshallable current = getCurrentUNode();
339         if (current != this) {
340             current.characters(ch, start, len);
341             return;
342         }
343
344         String JavaDoc text = new String JavaDoc(ch, start, len);
345         text = text.trim();
346     }
347
348     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
349         // Currently no-op
350
}
351
352     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
353         if (errorHandler != null) {
354             errorHandler.warning(e);
355         }
356     }
357
358     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
359         if ((validate) && (!hasDTD)) {
360             throw new SAXException JavaDoc("Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference.");
361         }
362         if (errorHandler != null) {
363             errorHandler.error(e);
364         }
365     }
366
367     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
368         if ((validate) && (!hasDTD)) {
369             throw new SAXException JavaDoc("Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference.");
370         }
371         if (errorHandler != null) {
372             errorHandler.fatalError(e);
373         }
374     }
375
376     public void startDTD(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID)
377         throws SAXException JavaDoc {
378
379         if ((name == null) || (name.equals(""))) {
380             docTypeString = "";
381             return;
382         }
383
384         hasDTD = true;
385         StringBuffer JavaDoc docTypeSB = new StringBuffer JavaDoc();
386         boolean hasPublic = false;
387
388         docTypeSB.append("<!DOCTYPE ")
389                  .append(name);
390
391         if ((publicID != null) && (!publicID.equals(""))) {
392             docTypeSB.append(" PUBLIC \"")
393                      .append(publicID)
394                      .append("\"");
395             hasPublic = true;
396         }
397
398         if ((systemID != null) && (!systemID.equals(""))) {
399             if (!hasPublic) {
400                 docTypeSB.append(" SYSTEM");
401             }
402             docTypeSB.append(" \"")
403                      .append(systemID)
404                      .append("\"");
405
406         }
407
408         docTypeSB.append(">");
409
410         docTypeString = docTypeSB.toString();
411     }
412
413     public void endDTD() throws SAXException JavaDoc {
414         // Currently no-op
415
}
416
417     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
418         // Currently no-op
419
}
420
421     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
422         // Currently no-op
423
}
424
425     public void startCDATA() throws SAXException JavaDoc {
426         // Currently no-op
427
}
428
429     public void endCDATA() throws SAXException JavaDoc {
430         // Currently no-op
431
}
432
433 }
434
Popular Tags