KickJava   Java API By Example, From Geeks To Geeks.

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


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 EntryImpl extends DefaultHandler JavaDoc implements Unmarshallable, LexicalHandler JavaDoc, Entry {
46
47     private String JavaDoc value;
48     private boolean zeus_ValueSet;
49     private String JavaDoc key;
50     private boolean zeus_KeySet;
51     private String JavaDoc type;
52     private static boolean zeus_TypeInitialized;
53     private static java.util.Vector JavaDoc zeus_validType;
54     private boolean zeus_TypeSet;
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 EntryImpl() {
87         zeus_ValueSet = false;
88         zeus_KeySet = false;
89         type = "Integer";
90         zeus_TypeInitialized = false;
91         zeus_validType = null;
92         zeus_TypeSet = false;
93         docTypeString = "";
94         hasDTD = false;
95         validate = false;
96         namespaceMappings = new HashMap JavaDoc();
97     }
98
99     public String JavaDoc getValue() {
100         return value;
101     }
102
103     public void setValue(String JavaDoc value) {
104         this.value = value;
105         zeus_ValueSet = true;
106     }
107
108     public String JavaDoc getKey() {
109         return key;
110     }
111
112     public void setKey(String JavaDoc key) {
113         this.key = key;
114         zeus_KeySet = true;
115     }
116
117     public String JavaDoc getType() {
118         return type;
119     }
120
121     public void setType(String JavaDoc type)
122         throws IllegalArgumentException JavaDoc {
123         if (!zeus_TypeInitialized) {
124             zeus_validType = new java.util.Vector JavaDoc();
125             zeus_validType.addElement("Integer");
126             zeus_validType.addElement("String");
127             zeus_validType.addElement("Boolean");
128             zeus_validType.addElement("Class");
129             zeus_TypeInitialized = true;
130         }
131         if (!zeus_validType.contains(type)) {
132               throw new IllegalArgumentException JavaDoc("Illegal value for attribute 'type'");
133         } else {
134               this.type = type;
135             this.zeus_TypeSet = true;
136         }
137     }
138
139     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
140         try {
141             startDTD(name, publicID, systemID);
142         } catch (SAXException JavaDoc neverHappens) { }
143     }
144
145     public void setOutputEncoding(String JavaDoc outputEncoding) {
146         this.outputEncoding = outputEncoding;
147     }
148
149     public void marshal(File JavaDoc file) throws IOException JavaDoc {
150         // Delegate to the marshal(Writer) method
151
marshal(new FileWriter JavaDoc(file));
152     }
153
154     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
155         // Delegate to the marshal(Writer) method
156
marshal(new OutputStreamWriter JavaDoc(outputStream));
157     }
158
159     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
160         // Write out the XML declaration
161
writer.write("<?xml version=\"1.0\" ");
162         if (outputEncoding != null) {
163             writer.write("encoding=\"");
164             writer.write(outputEncoding);
165             writer.write("\"?>\n\n");
166
167         } else {
168             writer.write("encoding=\"UTF-8\"?>\n\n");
169
170         }
171         // Handle DOCTYPE declaration
172
writer.write(docTypeString);
173         writer.write("\n");
174         // Now start the recursive writing
175
writeXMLRepresentation(writer, "");
176
177         // Close up
178
writer.flush();
179         writer.close();
180     }
181
182     protected void writeXMLRepresentation(Writer JavaDoc writer,
183                                           String JavaDoc indent)
184         throws IOException JavaDoc {
185
186         writer.write(indent);
187         writer.write("<entry");
188
189         // Handle namespace mappings (if needed)
190
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
191             String JavaDoc prefix = (String JavaDoc)i.next();
192             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
193             writer.write(" xmlns");
194             if (!prefix.trim().equals("")) {
195                 writer.write(":");
196                 writer.write(prefix);
197             }
198             writer.write("=\"");
199             writer.write(uri);
200             writer.write("\"\n ");
201         }
202
203         // Handle attributes (if needed)
204
if (zeus_KeySet) {
205             writer.write(" key=\"");
206             writer.write(escapeAttributeValue(key));
207             writer.write("\"");
208         }
209         if (zeus_TypeSet) {
210             writer.write(" type=\"");
211             writer.write(escapeAttributeValue(type));
212             writer.write("\"");
213         }
214         if (getValue() != null) {
215             // Handle PCDATA value
216
writer.write(">");
217             writer.write(getValue());
218             writer.write("</entry>\n");
219         } else {
220             writer.write("/>\n");
221         }
222     }
223
224     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
225         String JavaDoc returnValue = attributeValue;
226         for (int i = 0; i < returnValue.length(); i++) {
227             char ch = returnValue.charAt(i);
228             if (ch == '"') {
229                 returnValue = new StringBuffer JavaDoc()
230                     .append(returnValue.substring(0, i))
231                     .append("&quot;")
232                     .append(returnValue.substring(i+1))
233                     .toString();
234             }
235         }
236         return returnValue;
237     }
238
239     /**
240      * <p>
241      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
242      * </p>
243      *
244      * @param resolver the entity resolver to use.
245      */

246     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
247         entityResolver = resolver;
248     }
249
250     /**
251      * <p>
252      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
253      * </p>
254      *
255      * @param handler the entity resolver to use.
256      */

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