KickJava   Java API By Example, From Geeks To Geeks.

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


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 FilterImpl extends DefaultHandler JavaDoc implements Unmarshallable, LexicalHandler JavaDoc, Filter {
46
47     private Call call;
48     private Lookup lookup;
49     private Markup markup;
50     private Switch xmlswitch;
51     private Constant constant;
52     private Date date;
53     private Count count;
54     private String JavaDoc name;
55     private boolean zeus_NameSet;
56
57     /** Any DOCTYPE reference/statements. */
58     private String JavaDoc docTypeString;
59
60     /** The encoding for the output document */
61     private String JavaDoc outputEncoding;
62
63     /** The current node in unmarshalling */
64     private Unmarshallable zeus_currentUNode;
65
66     /** The parent node in unmarshalling */
67     private Unmarshallable zeus_parentUNode;
68
69     /** Whether this node has been handled */
70     private boolean zeus_thisNodeHandled = false;
71
72     /** Whether a DTD exists for an unmarshal call */
73     private boolean hasDTD;
74
75     /** Whether validation is occurring */
76     private boolean validate;
77
78     /** The namespace mappings on this element */
79     private Map JavaDoc namespaceMappings;
80
81     /** The EntityResolver for SAX parsing to use */
82     private static EntityResolver JavaDoc entityResolver;
83
84     /** The ErrorHandler for SAX parsing to use */
85     private static ErrorHandler JavaDoc errorHandler;
86
87     public FilterImpl() {
88         zeus_NameSet = false;
89         docTypeString = "";
90         hasDTD = false;
91         validate = false;
92         namespaceMappings = new HashMap JavaDoc();
93     }
94
95     public Call getCall() {
96         return call;
97     }
98
99     public void setCall(Call call) {
100         this.call = call;
101     }
102
103     public Lookup getLookup() {
104         return lookup;
105     }
106
107     public void setLookup(Lookup lookup) {
108         this.lookup = lookup;
109     }
110
111     public Markup getMarkup() {
112         return markup;
113     }
114
115     public void setMarkup(Markup markup) {
116         this.markup = markup;
117     }
118
119     public Switch getXmlswitch() {
120         return xmlswitch;
121     }
122
123     public void setXmlswitch(Switch xmlswitch) {
124         this.xmlswitch = xmlswitch;
125     }
126
127     public Constant getConstant() {
128         return constant;
129     }
130
131     public void setConstant(Constant constant) {
132         this.constant = constant;
133     }
134
135     public Date getDate() {
136         return date;
137     }
138
139     public void setDate(Date date) {
140         this.date = date;
141     }
142
143     public Count getCount() {
144         return count;
145     }
146
147     public void setCount(Count count) {
148         this.count = count;
149     }
150
151     public String JavaDoc getName() {
152         return name;
153     }
154
155     public void setName(String JavaDoc name) {
156         this.name = name;
157         zeus_NameSet = true;
158     }
159
160     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
161         try {
162             startDTD(name, publicID, systemID);
163         } catch (SAXException JavaDoc neverHappens) { }
164     }
165
166     public void setOutputEncoding(String JavaDoc outputEncoding) {
167         this.outputEncoding = outputEncoding;
168     }
169
170     public void marshal(File JavaDoc file) throws IOException JavaDoc {
171         // Delegate to the marshal(Writer) method
172
marshal(new FileWriter JavaDoc(file));
173     }
174
175     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
176         // Delegate to the marshal(Writer) method
177
marshal(new OutputStreamWriter JavaDoc(outputStream));
178     }
179
180     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
181         // Write out the XML declaration
182
writer.write("<?xml version=\"1.0\" ");
183         if (outputEncoding != null) {
184             writer.write("encoding=\"");
185             writer.write(outputEncoding);
186             writer.write("\"?>\n\n");
187
188         } else {
189             writer.write("encoding=\"UTF-8\"?>\n\n");
190
191         }
192         // Handle DOCTYPE declaration
193
writer.write(docTypeString);
194         writer.write("\n");
195         // Now start the recursive writing
196
writeXMLRepresentation(writer, "");
197
198         // Close up
199
writer.flush();
200         writer.close();
201     }
202
203     protected void writeXMLRepresentation(Writer JavaDoc writer,
204                                           String JavaDoc indent)
205         throws IOException JavaDoc {
206
207         writer.write(indent);
208         writer.write("<filter");
209
210         // Handle namespace mappings (if needed)
211
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
212             String JavaDoc prefix = (String JavaDoc)i.next();
213             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
214             writer.write(" xmlns");
215             if (!prefix.trim().equals("")) {
216                 writer.write(":");
217                 writer.write(prefix);
218             }
219             writer.write("=\"");
220             writer.write(uri);
221             writer.write("\"\n ");
222         }
223
224         // Handle attributes (if needed)
225
if (zeus_NameSet) {
226             writer.write(" name=\"");
227             writer.write(escapeAttributeValue(name));
228             writer.write("\"");
229         }
230         writer.write(">");
231         writer.write("\n");
232
233         // Handle child elements
234
if (call != null) {
235             ((CallImpl)call).writeXMLRepresentation(writer,
236                 new StringBuffer JavaDoc(indent).append(" ").toString());
237         }
238
239         if (lookup != null) {
240             ((LookupImpl)lookup).writeXMLRepresentation(writer,
241                 new StringBuffer JavaDoc(indent).append(" ").toString());
242         }
243
244         if (markup != null) {
245             ((MarkupImpl)markup).writeXMLRepresentation(writer,
246                 new StringBuffer JavaDoc(indent).append(" ").toString());
247         }
248
249         if (xmlswitch != null) {
250             ((SwitchImpl)xmlswitch).writeXMLRepresentation(writer,
251                 new StringBuffer JavaDoc(indent).append(" ").toString());
252         }
253
254         if (constant != null) {
255             ((ConstantImpl)constant).writeXMLRepresentation(writer,
256                 new StringBuffer JavaDoc(indent).append(" ").toString());
257         }
258
259         if (date != null) {
260             ((DateImpl)date).writeXMLRepresentation(writer,
261                 new StringBuffer JavaDoc(indent).append(" ").toString());
262         }
263
264         if (count != null) {
265             ((CountImpl)count).writeXMLRepresentation(writer,
266                 new StringBuffer JavaDoc(indent).append(" ").toString());
267         }
268
269         writer.write(indent);
270         writer.write("</filter>\n");
271     }
272
273     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
274         String JavaDoc returnValue = attributeValue;
275         for (int i = 0; i < returnValue.length(); i++) {
276             char ch = returnValue.charAt(i);
277             if (ch == '"') {
278                 returnValue = new StringBuffer JavaDoc()
279                     .append(returnValue.substring(0, i))
280                     .append("&quot;")
281                     .append(returnValue.substring(i+1))
282                     .toString();
283             }
284         }
285         return returnValue;
286     }
287
288     /**
289      * <p>
290      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
291      * </p>
292      *
293      * @param resolver the entity resolver to use.
294      */

295     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
296         entityResolver = resolver;
297     }
298
299     /**
300      * <p>
301      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
302      * </p>
303      *
304      * @param handler the entity resolver to use.
305      */

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