KickJava   Java API By Example, From Geeks To Geeks.

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


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

209     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
210         entityResolver = resolver;
211     }
212
213     /**
214      * <p>
215      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
216      * </p>
217      *
218      * @param handler the entity resolver to use.
219      */

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