KickJava   Java API By Example, From Geeks To Geeks.

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


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 // Local Implementation Import Statements
46
import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.LinkedList JavaDoc;
49
50 public class MapsetImpl extends DefaultHandler JavaDoc implements Unmarshallable, LexicalHandler JavaDoc, Mapset {
51
52     private List JavaDoc mapentryList;
53     private String JavaDoc xmlpackage;
54     private boolean zeus_XmlpackageSet;
55
56     /** Any DOCTYPE reference/statements. */
57     private String JavaDoc docTypeString;
58
59     /** The encoding for the output document */
60     private String JavaDoc outputEncoding;
61
62     /** The current node in unmarshalling */
63     private Unmarshallable zeus_currentUNode;
64
65     /** The parent node in unmarshalling */
66     private Unmarshallable zeus_parentUNode;
67
68     /** Whether this node has been handled */
69     private boolean zeus_thisNodeHandled = false;
70
71     /** Whether a DTD exists for an unmarshal call */
72     private boolean hasDTD;
73
74     /** Whether validation is occurring */
75     private boolean validate;
76
77     /** The namespace mappings on this element */
78     private Map JavaDoc namespaceMappings;
79
80     /** The EntityResolver for SAX parsing to use */
81     private static EntityResolver JavaDoc entityResolver;
82
83     /** The ErrorHandler for SAX parsing to use */
84     private static ErrorHandler JavaDoc errorHandler;
85
86     public MapsetImpl() {
87         mapentryList = new LinkedList JavaDoc();
88         zeus_XmlpackageSet = false;
89         docTypeString = "";
90         hasDTD = false;
91         validate = false;
92         namespaceMappings = new HashMap JavaDoc();
93     }
94
95     public List JavaDoc getMapentryList() {
96         return mapentryList;
97     }
98
99     public void setMapentryList(List JavaDoc mapentryList) {
100         this.mapentryList = mapentryList;
101     }
102
103     public void addMapentry(Mapentry mapentry) {
104         mapentryList.add(mapentry);
105     }
106
107     public void removeMapentry(Mapentry mapentry) {
108         mapentryList.remove(mapentry);
109     }
110
111     public String JavaDoc getXmlpackage() {
112         return xmlpackage;
113     }
114
115     public void setXmlpackage(String JavaDoc xmlpackage) {
116         this.xmlpackage = xmlpackage;
117         zeus_XmlpackageSet = true;
118     }
119
120     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
121         try {
122             startDTD(name, publicID, systemID);
123         } catch (SAXException JavaDoc neverHappens) { }
124     }
125
126     public void setOutputEncoding(String JavaDoc outputEncoding) {
127         this.outputEncoding = outputEncoding;
128     }
129
130     public void marshal(File JavaDoc file) throws IOException JavaDoc {
131         // Delegate to the marshal(Writer) method
132
marshal(new FileWriter JavaDoc(file));
133     }
134
135     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
136         // Delegate to the marshal(Writer) method
137
marshal(new OutputStreamWriter JavaDoc(outputStream));
138     }
139
140     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
141         // Write out the XML declaration
142
writer.write("<?xml version=\"1.0\" ");
143         if (outputEncoding != null) {
144             writer.write("encoding=\"");
145             writer.write(outputEncoding);
146             writer.write("\"?>\n\n");
147
148         } else {
149             writer.write("encoding=\"UTF-8\"?>\n\n");
150
151         }
152         // Handle DOCTYPE declaration
153
writer.write(docTypeString);
154         writer.write("\n");
155         // Now start the recursive writing
156
writeXMLRepresentation(writer, "");
157
158         // Close up
159
writer.flush();
160         writer.close();
161     }
162
163     protected void writeXMLRepresentation(Writer JavaDoc writer,
164                                           String JavaDoc indent)
165         throws IOException JavaDoc {
166
167         writer.write(indent);
168         writer.write("<mapset");
169
170         // Handle namespace mappings (if needed)
171
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
172             String JavaDoc prefix = (String JavaDoc)i.next();
173             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
174             writer.write(" xmlns");
175             if (!prefix.trim().equals("")) {
176                 writer.write(":");
177                 writer.write(prefix);
178             }
179             writer.write("=\"");
180             writer.write(uri);
181             writer.write("\"\n ");
182         }
183
184         // Handle attributes (if needed)
185
if (zeus_XmlpackageSet) {
186             writer.write(" package=\"");
187             writer.write(escapeAttributeValue(xmlpackage));
188             writer.write("\"");
189         }
190         writer.write(">");
191         writer.write("\n");
192
193         // Handle child elements
194
for (Iterator JavaDoc i=mapentryList.iterator(); i.hasNext(); ) {
195             MapentryImpl mapentry = (MapentryImpl)i.next();
196             mapentry.writeXMLRepresentation(writer,
197                 new StringBuffer JavaDoc(indent).append(" ").toString());
198         }
199         writer.write(indent);
200         writer.write("</mapset>\n");
201     }
202
203     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
204         String JavaDoc returnValue = attributeValue;
205         for (int i = 0; i < returnValue.length(); i++) {
206             char ch = returnValue.charAt(i);
207             if (ch == '"') {
208                 returnValue = new StringBuffer JavaDoc()
209                     .append(returnValue.substring(0, i))
210                     .append("&quot;")
211                     .append(returnValue.substring(i+1))
212                     .toString();
213             }
214         }
215         return returnValue;
216     }
217
218     /**
219      * <p>
220      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
221      * </p>
222      *
223      * @param resolver the entity resolver to use.
224      */

225     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
226         entityResolver = resolver;
227     }
228
229     /**
230      * <p>
231      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
232      * </p>
233      *
234      * @param handler the entity resolver to use.
235      */

236     public static void setErrorHandler(ErrorHandler JavaDoc handler) {
237         errorHandler = handler;
238     }
239
240     public static Mapset unmarshal(File JavaDoc file) throws IOException JavaDoc {
241         // Delegate to the unmarshal(Reader) method
242
return unmarshal(new FileReader JavaDoc(file));
243     }
244
245     public static Mapset unmarshal(File JavaDoc file, boolean validate) throws IOException JavaDoc {
246         // Delegate to the unmarshal(Reader) method
247
return unmarshal(new FileReader JavaDoc(file), validate);
248     }
249
250     public static Mapset unmarshal(InputStream JavaDoc inputStream) throws IOException JavaDoc {
251         // Delegate to the unmarshal(Reader) method
252
return unmarshal(new InputStreamReader JavaDoc(inputStream));
253     }
254
255     public static Mapset unmarshal(InputStream JavaDoc inputStream, boolean validate) throws IOException JavaDoc {
256         // Delegate to the unmarshal(Reader) method
257
return unmarshal(new InputStreamReader JavaDoc(inputStream), validate);
258     }
259
260     public static Mapset unmarshal(Reader JavaDoc reader) throws IOException JavaDoc {
261         // Delegate with default validation value
262
return unmarshal(reader, false);
263     }
264
265     public static Mapset unmarshal(Reader JavaDoc reader, boolean validate) throws IOException JavaDoc {
266         MapsetImpl mapset = new MapsetImpl();
267         mapset.setValidating(validate);
268         mapset.setCurrentUNode(mapset);
269         mapset.setParentUNode(null);
270         // Load the XML parser
271
XMLReader JavaDoc parser = null;
272         String JavaDoc parserClass = System.getProperty("org.xml.sax.driver",
273             "org.apache.xerces.parsers.SAXParser");
274         try {
275             parser = XMLReaderFactory.createXMLReader(parserClass);
276
277             // Set entity resolver, if needed
278
if (entityResolver != null) {
279                 parser.setEntityResolver(entityResolver);
280             }
281
282             // Set error handler
283
parser.setErrorHandler(mapset);
284
285             // Register lexical handler
286
parser.setProperty("http://xml.org/sax/properties/lexical-handler", mapset);
287
288             // Register content handler
289
parser.setContentHandler(mapset);
290         } catch (SAXException JavaDoc e) {
291             throw new IOException JavaDoc("Could not load XML parser: " +
292                 e.getMessage());
293         }
294
295         InputSource JavaDoc inputSource = new InputSource JavaDoc(reader);
296         try {
297             parser.setFeature("http://xml.org/sax/features/validation", new Boolean JavaDoc(validate).booleanValue());
298             parser.setFeature("http://xml.org/sax/features/namespaces", true);
299             parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
300             parser.parse(inputSource);
301         } catch (SAXException JavaDoc e) {
302             throw new IOException JavaDoc("Error parsing XML document: " +
303                 e.getMessage());
304         }
305
306         // Return the resultant object
307
return mapset;
308     }
309
310     public Unmarshallable getParentUNode() {
311         return zeus_parentUNode;
312     }
313
314     public void setParentUNode(Unmarshallable parentUNode) {
315         this.zeus_parentUNode = parentUNode;
316     }
317
318     public Unmarshallable getCurrentUNode() {
319         return zeus_currentUNode;
320     }
321
322     public void setCurrentUNode(Unmarshallable currentUNode) {
323         this.zeus_currentUNode = currentUNode;
324     }
325
326     public void setValidating(boolean validate) {
327         this.validate = validate;
328     }
329
330     public void startDocument() throws SAXException JavaDoc {
331         // no-op
332
}
333
334     public void setDocumentLocator(Locator JavaDoc locator) {
335         // no-op
336
}
337
338     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
339         throws SAXException JavaDoc {
340         namespaceMappings.put(prefix, uri);
341     }
342
343     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
344                              String JavaDoc qName, org.xml.sax.Attributes JavaDoc atts)
345         throws SAXException JavaDoc {
346
347         // Feed this to the correct ContentHandler
348
Unmarshallable current = getCurrentUNode();
349         if (current != this) {
350             current.startElement(namespaceURI, localName, qName, atts);
351             return;
352         }
353
354         // See if we handle, or we delegate
355
if ((localName.equals("mapset")) && (!zeus_thisNodeHandled)) {
356             // Handle ourselves
357
for (int i=0, len=atts.getLength(); i<len; i++) {
358                 String JavaDoc attName= atts.getLocalName(i);
359                 String JavaDoc attValue = atts.getValue(i);
360                 if (attName.equals("package")) {
361                     setXmlpackage(attValue);
362                 }
363             }
364             zeus_thisNodeHandled = true;
365             return;
366         } else {
367             // Delegate handling
368
if (localName.equals("mapentry")) {
369                 MapentryImpl mapentry = new MapentryImpl();
370                 current = getCurrentUNode();
371                 mapentry.setParentUNode(current);
372                 mapentry.setCurrentUNode(mapentry);
373                 this.setCurrentUNode(mapentry);
374                 mapentry.startElement(namespaceURI, localName, qName, atts);
375                 // Add this value in
376
mapentryList.add(mapentry);
377                 return;
378             }
379         }
380     }
381
382     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
383                            String JavaDoc qName)
384         throws SAXException JavaDoc {
385
386         Unmarshallable current = getCurrentUNode();
387         if (current != this) {
388             current.endElement(namespaceURI, localName, qName);
389             return;
390         }
391
392         Unmarshallable parent = getCurrentUNode().getParentUNode();
393         if (parent != null) {
394             parent.setCurrentUNode(parent);
395         }
396     }
397
398     public void characters(char[] ch, int start, int len)
399         throws SAXException JavaDoc {
400
401         // Feed this to the correct ContentHandler
402
Unmarshallable current = getCurrentUNode();
403         if (current != this) {
404             current.characters(ch, start, len);
405             return;
406         }
407
408         String JavaDoc text = new String JavaDoc(ch, start, len);
409         text = text.trim();
410     }
411
412     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
413         // Currently no-op
414
}
415
416     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
417         if (errorHandler != null) {
418             errorHandler.warning(e);
419         }
420     }
421
422     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
423         if ((validate) && (!hasDTD)) {
424             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.");
425         }
426         if (errorHandler != null) {
427             errorHandler.error(e);
428         }
429     }
430
431     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
432         if ((validate) && (!hasDTD)) {
433             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.");
434         }
435         if (errorHandler != null) {
436             errorHandler.fatalError(e);
437         }
438     }
439
440     public void startDTD(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID)
441         throws SAXException JavaDoc {
442
443         if ((name == null) || (name.equals(""))) {
444             docTypeString = "";
445             return;
446         }
447
448         hasDTD = true;
449         StringBuffer JavaDoc docTypeSB = new StringBuffer JavaDoc();
450         boolean hasPublic = false;
451
452         docTypeSB.append("<!DOCTYPE ")
453                  .append(name);
454
455         if ((publicID != null) && (!publicID.equals(""))) {
456             docTypeSB.append(" PUBLIC \"")
457                      .append(publicID)
458                      .append("\"");
459             hasPublic = true;
460         }
461
462         if ((systemID != null) && (!systemID.equals(""))) {
463             if (!hasPublic) {
464                 docTypeSB.append(" SYSTEM");
465             }
466             docTypeSB.append(" \"")
467                      .append(systemID)
468                      .append("\"");
469
470         }
471
472         docTypeSB.append(">");
473
474         docTypeString = docTypeSB.toString();
475     }
476
477     public void endDTD() throws SAXException JavaDoc {
478         // Currently no-op
479
}
480
481     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
482         // Currently no-op
483
}
484
485     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
486         // Currently no-op
487
}
488
489     public void startCDATA() throws SAXException JavaDoc {
490         // Currently no-op
491
}
492
493     public void endCDATA() throws SAXException JavaDoc {
494         // Currently no-op
495
}
496
497 }
498
Popular Tags