KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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