KickJava   Java API By Example, From Geeks To Geeks.

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


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 SwitchImpl extends DefaultHandler JavaDoc implements Unmarshallable, LexicalHandler JavaDoc, Switch {
51
52     private List JavaDoc callList;
53     private List JavaDoc lookupList;
54     private List JavaDoc markupList;
55     private List JavaDoc xmlswitchList;
56     private List JavaDoc constantList;
57     private List JavaDoc dateList;
58     private List JavaDoc countList;
59
60     /** Any DOCTYPE reference/statements. */
61     private String JavaDoc docTypeString;
62
63     /** The encoding for the output document */
64     private String JavaDoc outputEncoding;
65
66     /** The current node in unmarshalling */
67     private Unmarshallable zeus_currentUNode;
68
69     /** The parent node in unmarshalling */
70     private Unmarshallable zeus_parentUNode;
71
72     /** Whether this node has been handled */
73     private boolean zeus_thisNodeHandled = false;
74
75     /** Whether a DTD exists for an unmarshal call */
76     private boolean hasDTD;
77
78     /** Whether validation is occurring */
79     private boolean validate;
80
81     /** The namespace mappings on this element */
82     private Map JavaDoc namespaceMappings;
83
84     /** The EntityResolver for SAX parsing to use */
85     private static EntityResolver JavaDoc entityResolver;
86
87     /** The ErrorHandler for SAX parsing to use */
88     private static ErrorHandler JavaDoc errorHandler;
89
90     public SwitchImpl() {
91         callList = new LinkedList JavaDoc();
92         lookupList = new LinkedList JavaDoc();
93         markupList = new LinkedList JavaDoc();
94         xmlswitchList = new LinkedList JavaDoc();
95         constantList = new LinkedList JavaDoc();
96         dateList = new LinkedList JavaDoc();
97         countList = new LinkedList JavaDoc();
98         docTypeString = "";
99         hasDTD = false;
100         validate = false;
101         namespaceMappings = new HashMap JavaDoc();
102     }
103
104     public List JavaDoc getCallList() {
105         return callList;
106     }
107
108     public void setCallList(List JavaDoc callList) {
109         this.callList = callList;
110     }
111
112     public void addCall(Call call) {
113         callList.add(call);
114     }
115
116     public void removeCall(Call call) {
117         callList.remove(call);
118     }
119
120     public List JavaDoc getLookupList() {
121         return lookupList;
122     }
123
124     public void setLookupList(List JavaDoc lookupList) {
125         this.lookupList = lookupList;
126     }
127
128     public void addLookup(Lookup lookup) {
129         lookupList.add(lookup);
130     }
131
132     public void removeLookup(Lookup lookup) {
133         lookupList.remove(lookup);
134     }
135
136     public List JavaDoc getMarkupList() {
137         return markupList;
138     }
139
140     public void setMarkupList(List JavaDoc markupList) {
141         this.markupList = markupList;
142     }
143
144     public void addMarkup(Markup markup) {
145         markupList.add(markup);
146     }
147
148     public void removeMarkup(Markup markup) {
149         markupList.remove(markup);
150     }
151
152     public List JavaDoc getXmlswitchList() {
153         return xmlswitchList;
154     }
155
156     public void setXmlswitchList(List JavaDoc xmlswitchList) {
157         this.xmlswitchList = xmlswitchList;
158     }
159
160     public void addXmlswitch(Switch xmlswitch) {
161         xmlswitchList.add(xmlswitch);
162     }
163
164     public void removeXmlswitch(Switch xmlswitch) {
165         xmlswitchList.remove(xmlswitch);
166     }
167
168     public List JavaDoc getConstantList() {
169         return constantList;
170     }
171
172     public void setConstantList(List JavaDoc constantList) {
173         this.constantList = constantList;
174     }
175
176     public void addConstant(Constant constant) {
177         constantList.add(constant);
178     }
179
180     public void removeConstant(Constant constant) {
181         constantList.remove(constant);
182     }
183
184     public List JavaDoc getDateList() {
185         return dateList;
186     }
187
188     public void setDateList(List JavaDoc dateList) {
189         this.dateList = dateList;
190     }
191
192     public void addDate(Date date) {
193         dateList.add(date);
194     }
195
196     public void removeDate(Date date) {
197         dateList.remove(date);
198     }
199
200     public List JavaDoc getCountList() {
201         return countList;
202     }
203
204     public void setCountList(List JavaDoc countList) {
205         this.countList = countList;
206     }
207
208     public void addCount(Count count) {
209         countList.add(count);
210     }
211
212     public void removeCount(Count count) {
213         countList.remove(count);
214     }
215
216     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
217         try {
218             startDTD(name, publicID, systemID);
219         } catch (SAXException JavaDoc neverHappens) { }
220     }
221
222     public void setOutputEncoding(String JavaDoc outputEncoding) {
223         this.outputEncoding = outputEncoding;
224     }
225
226     public void marshal(File JavaDoc file) throws IOException JavaDoc {
227         // Delegate to the marshal(Writer) method
228
marshal(new FileWriter JavaDoc(file));
229     }
230
231     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
232         // Delegate to the marshal(Writer) method
233
marshal(new OutputStreamWriter JavaDoc(outputStream));
234     }
235
236     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
237         // Write out the XML declaration
238
writer.write("<?xml version=\"1.0\" ");
239         if (outputEncoding != null) {
240             writer.write("encoding=\"");
241             writer.write(outputEncoding);
242             writer.write("\"?>\n\n");
243
244         } else {
245             writer.write("encoding=\"UTF-8\"?>\n\n");
246
247         }
248         // Handle DOCTYPE declaration
249
writer.write(docTypeString);
250         writer.write("\n");
251         // Now start the recursive writing
252
writeXMLRepresentation(writer, "");
253
254         // Close up
255
writer.flush();
256         writer.close();
257     }
258
259     protected void writeXMLRepresentation(Writer JavaDoc writer,
260                                           String JavaDoc indent)
261         throws IOException JavaDoc {
262
263         writer.write(indent);
264         writer.write("<switch");
265
266         // Handle namespace mappings (if needed)
267
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
268             String JavaDoc prefix = (String JavaDoc)i.next();
269             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
270             writer.write(" xmlns");
271             if (!prefix.trim().equals("")) {
272                 writer.write(":");
273                 writer.write(prefix);
274             }
275             writer.write("=\"");
276             writer.write(uri);
277             writer.write("\"\n ");
278         }
279
280         // Handle attributes (if needed)
281
writer.write(">");
282         writer.write("\n");
283
284         // Handle child elements
285
for (Iterator JavaDoc i=callList.iterator(); i.hasNext(); ) {
286             CallImpl call = (CallImpl)i.next();
287             call.writeXMLRepresentation(writer,
288                 new StringBuffer JavaDoc(indent).append(" ").toString());
289         }
290         for (Iterator JavaDoc i=lookupList.iterator(); i.hasNext(); ) {
291             LookupImpl lookup = (LookupImpl)i.next();
292             lookup.writeXMLRepresentation(writer,
293                 new StringBuffer JavaDoc(indent).append(" ").toString());
294         }
295         for (Iterator JavaDoc i=markupList.iterator(); i.hasNext(); ) {
296             MarkupImpl markup = (MarkupImpl)i.next();
297             markup.writeXMLRepresentation(writer,
298                 new StringBuffer JavaDoc(indent).append(" ").toString());
299         }
300         for (Iterator JavaDoc i=xmlswitchList.iterator(); i.hasNext(); ) {
301             SwitchImpl xmlswitch = (SwitchImpl)i.next();
302             xmlswitch.writeXMLRepresentation(writer,
303                 new StringBuffer JavaDoc(indent).append(" ").toString());
304         }
305         for (Iterator JavaDoc i=constantList.iterator(); i.hasNext(); ) {
306             ConstantImpl constant = (ConstantImpl)i.next();
307             constant.writeXMLRepresentation(writer,
308                 new StringBuffer JavaDoc(indent).append(" ").toString());
309         }
310         for (Iterator JavaDoc i=dateList.iterator(); i.hasNext(); ) {
311             DateImpl date = (DateImpl)i.next();
312             date.writeXMLRepresentation(writer,
313                 new StringBuffer JavaDoc(indent).append(" ").toString());
314         }
315         for (Iterator JavaDoc i=countList.iterator(); i.hasNext(); ) {
316             CountImpl count = (CountImpl)i.next();
317             count.writeXMLRepresentation(writer,
318                 new StringBuffer JavaDoc(indent).append(" ").toString());
319         }
320         writer.write(indent);
321         writer.write("</switch>\n");
322     }
323
324     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
325         String JavaDoc returnValue = attributeValue;
326         for (int i = 0; i < returnValue.length(); i++) {
327             char ch = returnValue.charAt(i);
328             if (ch == '"') {
329                 returnValue = new StringBuffer JavaDoc()
330                     .append(returnValue.substring(0, i))
331                     .append("&quot;")
332                     .append(returnValue.substring(i+1))
333                     .toString();
334             }
335         }
336         return returnValue;
337     }
338
339     /**
340      * <p>
341      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
342      * </p>
343      *
344      * @param resolver the entity resolver to use.
345      */

346     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
347         entityResolver = resolver;
348     }
349
350     /**
351      * <p>
352      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
353      * </p>
354      *
355      * @param handler the entity resolver to use.
356      */

357     public static void setErrorHandler(ErrorHandler JavaDoc handler) {
358         errorHandler = handler;
359     }
360
361     public static Switch unmarshal(File JavaDoc file) throws IOException JavaDoc {
362         // Delegate to the unmarshal(Reader) method
363
return unmarshal(new FileReader JavaDoc(file));
364     }
365
366     public static Switch unmarshal(File JavaDoc file, boolean validate) throws IOException JavaDoc {
367         // Delegate to the unmarshal(Reader) method
368
return unmarshal(new FileReader JavaDoc(file), validate);
369     }
370
371     public static Switch unmarshal(InputStream JavaDoc inputStream) throws IOException JavaDoc {
372         // Delegate to the unmarshal(Reader) method
373
return unmarshal(new InputStreamReader JavaDoc(inputStream));
374     }
375
376     public static Switch unmarshal(InputStream JavaDoc inputStream, boolean validate) throws IOException JavaDoc {
377         // Delegate to the unmarshal(Reader) method
378
return unmarshal(new InputStreamReader JavaDoc(inputStream), validate);
379     }
380
381     public static Switch unmarshal(Reader JavaDoc reader) throws IOException JavaDoc {
382         // Delegate with default validation value
383
return unmarshal(reader, false);
384     }
385
386     public static Switch unmarshal(Reader JavaDoc reader, boolean validate) throws IOException JavaDoc {
387         SwitchImpl xmlswitch = new SwitchImpl();
388         xmlswitch.setValidating(validate);
389         xmlswitch.setCurrentUNode(xmlswitch);
390         xmlswitch.setParentUNode(null);
391         // Load the XML parser
392
XMLReader JavaDoc parser = null;
393         String JavaDoc parserClass = System.getProperty("org.xml.sax.driver",
394             "org.apache.xerces.parsers.SAXParser");
395         try {
396             parser = XMLReaderFactory.createXMLReader(parserClass);
397
398             // Set entity resolver, if needed
399
if (entityResolver != null) {
400                 parser.setEntityResolver(entityResolver);
401             }
402
403             // Set error handler
404
parser.setErrorHandler(xmlswitch);
405
406             // Register lexical handler
407
parser.setProperty("http://xml.org/sax/properties/lexical-handler", xmlswitch);
408
409             // Register content handler
410
parser.setContentHandler(xmlswitch);
411         } catch (SAXException JavaDoc e) {
412             throw new IOException JavaDoc("Could not load XML parser: " +
413                 e.getMessage());
414         }
415
416         InputSource JavaDoc inputSource = new InputSource JavaDoc(reader);
417         try {
418             parser.setFeature("http://xml.org/sax/features/validation", new Boolean JavaDoc(validate).booleanValue());
419             parser.setFeature("http://xml.org/sax/features/namespaces", true);
420             parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
421             parser.parse(inputSource);
422         } catch (SAXException JavaDoc e) {
423             throw new IOException JavaDoc("Error parsing XML document: " +
424                 e.getMessage());
425         }
426
427         // Return the resultant object
428
return xmlswitch;
429     }
430
431     public Unmarshallable getParentUNode() {
432         return zeus_parentUNode;
433     }
434
435     public void setParentUNode(Unmarshallable parentUNode) {
436         this.zeus_parentUNode = parentUNode;
437     }
438
439     public Unmarshallable getCurrentUNode() {
440         return zeus_currentUNode;
441     }
442
443     public void setCurrentUNode(Unmarshallable currentUNode) {
444         this.zeus_currentUNode = currentUNode;
445     }
446
447     public void setValidating(boolean validate) {
448         this.validate = validate;
449     }
450
451     public void startDocument() throws SAXException JavaDoc {
452         // no-op
453
}
454
455     public void setDocumentLocator(Locator JavaDoc locator) {
456         // no-op
457
}
458
459     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
460         throws SAXException JavaDoc {
461         namespaceMappings.put(prefix, uri);
462     }
463
464     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
465                              String JavaDoc qName, org.xml.sax.Attributes JavaDoc atts)
466         throws SAXException JavaDoc {
467
468         // Feed this to the correct ContentHandler
469
Unmarshallable current = getCurrentUNode();
470         if (current != this) {
471             current.startElement(namespaceURI, localName, qName, atts);
472             return;
473         }
474
475         // See if we handle, or we delegate
476
if ((localName.equals("switch")) && (!zeus_thisNodeHandled)) {
477             // Handle ourselves
478
for (int i=0, len=atts.getLength(); i<len; i++) {
479                 String JavaDoc attName= atts.getLocalName(i);
480                 String JavaDoc attValue = atts.getValue(i);
481             }
482             zeus_thisNodeHandled = true;
483             return;
484         } else {
485             // Delegate handling
486
if (localName.equals("call")) {
487                 CallImpl call = new CallImpl();
488                 current = getCurrentUNode();
489                 call.setParentUNode(current);
490                 call.setCurrentUNode(call);
491                 this.setCurrentUNode(call);
492                 call.startElement(namespaceURI, localName, qName, atts);
493                 // Add this value in
494
callList.add(call);
495                 return;
496             }
497             if (localName.equals("lookup")) {
498                 LookupImpl lookup = new LookupImpl();
499                 current = getCurrentUNode();
500                 lookup.setParentUNode(current);
501                 lookup.setCurrentUNode(lookup);
502                 this.setCurrentUNode(lookup);
503                 lookup.startElement(namespaceURI, localName, qName, atts);
504                 // Add this value in
505
lookupList.add(lookup);
506                 return;
507             }
508             if (localName.equals("markup")) {
509                 MarkupImpl markup = new MarkupImpl();
510                 current = getCurrentUNode();
511                 markup.setParentUNode(current);
512                 markup.setCurrentUNode(markup);
513                 this.setCurrentUNode(markup);
514                 markup.startElement(namespaceURI, localName, qName, atts);
515                 // Add this value in
516
markupList.add(markup);
517                 return;
518             }
519             if (localName.equals("switch")) {
520                 SwitchImpl xmlswitch = new SwitchImpl();
521                 current = getCurrentUNode();
522                 xmlswitch.setParentUNode(current);
523                 xmlswitch.setCurrentUNode(xmlswitch);
524                 this.setCurrentUNode(xmlswitch);
525                 xmlswitch.startElement(namespaceURI, localName, qName, atts);
526                 // Add this value in
527
xmlswitchList.add(xmlswitch);
528                 return;
529             }
530             if (localName.equals("constant")) {
531                 ConstantImpl constant = new ConstantImpl();
532                 current = getCurrentUNode();
533                 constant.setParentUNode(current);
534                 constant.setCurrentUNode(constant);
535                 this.setCurrentUNode(constant);
536                 constant.startElement(namespaceURI, localName, qName, atts);
537                 // Add this value in
538
constantList.add(constant);
539                 return;
540             }
541             if (localName.equals("date")) {
542                 DateImpl date = new DateImpl();
543                 current = getCurrentUNode();
544                 date.setParentUNode(current);
545                 date.setCurrentUNode(date);
546                 this.setCurrentUNode(date);
547                 date.startElement(namespaceURI, localName, qName, atts);
548                 // Add this value in
549
dateList.add(date);
550                 return;
551             }
552             if (localName.equals("count")) {
553                 CountImpl count = new CountImpl();
554                 current = getCurrentUNode();
555                 count.setParentUNode(current);
556                 count.setCurrentUNode(count);
557                 this.setCurrentUNode(count);
558                 count.startElement(namespaceURI, localName, qName, atts);
559                 // Add this value in
560
countList.add(count);
561                 return;
562             }
563         }
564     }
565
566     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
567                            String JavaDoc qName)
568         throws SAXException JavaDoc {
569
570         Unmarshallable current = getCurrentUNode();
571         if (current != this) {
572             current.endElement(namespaceURI, localName, qName);
573             return;
574         }
575
576         Unmarshallable parent = getCurrentUNode().getParentUNode();
577         if (parent != null) {
578             parent.setCurrentUNode(parent);
579         }
580     }
581
582     public void characters(char[] ch, int start, int len)
583         throws SAXException JavaDoc {
584
585         // Feed this to the correct ContentHandler
586
Unmarshallable current = getCurrentUNode();
587         if (current != this) {
588             current.characters(ch, start, len);
589             return;
590         }
591
592         String JavaDoc text = new String JavaDoc(ch, start, len);
593         text = text.trim();
594     }
595
596     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
597         // Currently no-op
598
}
599
600     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
601         if (errorHandler != null) {
602             errorHandler.warning(e);
603         }
604     }
605
606     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
607         if ((validate) && (!hasDTD)) {
608             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.");
609         }
610         if (errorHandler != null) {
611             errorHandler.error(e);
612         }
613     }
614
615     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
616         if ((validate) && (!hasDTD)) {
617             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.");
618         }
619         if (errorHandler != null) {
620             errorHandler.fatalError(e);
621         }
622     }
623
624     public void startDTD(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID)
625         throws SAXException JavaDoc {
626
627         if ((name == null) || (name.equals(""))) {
628             docTypeString = "";
629             return;
630         }
631
632         hasDTD = true;
633         StringBuffer JavaDoc docTypeSB = new StringBuffer JavaDoc();
634         boolean hasPublic = false;
635
636         docTypeSB.append("<!DOCTYPE ")
637                  .append(name);
638
639         if ((publicID != null) && (!publicID.equals(""))) {
640             docTypeSB.append(" PUBLIC \"")
641                      .append(publicID)
642                      .append("\"");
643             hasPublic = true;
644         }
645
646         if ((systemID != null) && (!systemID.equals(""))) {
647             if (!hasPublic) {
648                 docTypeSB.append(" SYSTEM");
649             }
650             docTypeSB.append(" \"")
651                      .append(systemID)
652                      .append("\"");
653
654         }
655
656         docTypeSB.append(">");
657
658         docTypeString = docTypeSB.toString();
659     }
660
661     public void endDTD() throws SAXException JavaDoc {
662         // Currently no-op
663
}
664
665     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
666         // Currently no-op
667
}
668
669     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
670         // Currently no-op
671
}
672
673     public void startCDATA() throws SAXException JavaDoc {
674         // Currently no-op
675
}
676
677     public void endCDATA() throws SAXException JavaDoc {
678         // Currently no-op
679
}
680
681 }
682
Popular Tags