KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > convert > xml > 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.convert.xml;
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 FilterImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, Filter {
51
52     private Icon icon;
53     private FilterName filterName;
54     private DisplayName displayName;
55     private Description description;
56     private FilterClass filterClass;
57     private List JavaDoc initParamList;
58     private String JavaDoc id;
59     private boolean zeus_IdSet;
60
61     /** Any DOCTYPE reference/statements. */
62     private String JavaDoc docTypeString;
63
64     /** The encoding for the output document */
65     private String JavaDoc outputEncoding;
66
67     /** The current node in unmarshalling */
68     private Unmarshallable zeus_currentUNode;
69
70     /** The parent node in unmarshalling */
71     private Unmarshallable zeus_parentUNode;
72
73     /** Whether this node has been handled */
74     private boolean zeus_thisNodeHandled = false;
75
76     /** Whether a DTD exists for an unmarshal call */
77     private boolean hasDTD;
78
79     /** Whether validation is occurring */
80     private boolean validate;
81
82     /** The namespace mappings on this element */
83     private Map JavaDoc namespaceMappings;
84
85     /** The EntityResolver for SAX parsing to use */
86     private static EntityResolver JavaDoc entityResolver;
87
88     /** The ErrorHandler for SAX parsing to use */
89     private static ErrorHandler JavaDoc errorHandler;
90
91     private static FilterImpl prototype = null;
92
93     public static void setPrototype(FilterImpl prototype) {
94         FilterImpl.prototype = prototype;
95     }
96     public static FilterImpl newInstance() {
97         try {
98             return (prototype!=null)?(FilterImpl)prototype.clone(): new FilterImpl();
99         } catch (CloneNotSupportedException JavaDoc e) {
100             return null; // never
101
}
102     }
103     public FilterImpl() {
104         initParamList = new LinkedList JavaDoc();
105         zeus_IdSet = false;
106         docTypeString = "";
107         hasDTD = false;
108         validate = false;
109         namespaceMappings = new HashMap JavaDoc();
110     }
111
112     public Icon getIcon() {
113         return icon;
114     }
115
116     public void setIcon(Icon icon) {
117         this.icon = icon;
118     }
119
120     public FilterName getFilterName() {
121         return filterName;
122     }
123
124     public void setFilterName(FilterName filterName) {
125         this.filterName = filterName;
126     }
127
128     public DisplayName getDisplayName() {
129         return displayName;
130     }
131
132     public void setDisplayName(DisplayName displayName) {
133         this.displayName = displayName;
134     }
135
136     public Description getDescription() {
137         return description;
138     }
139
140     public void setDescription(Description description) {
141         this.description = description;
142     }
143
144     public FilterClass getFilterClass() {
145         return filterClass;
146     }
147
148     public void setFilterClass(FilterClass filterClass) {
149         this.filterClass = filterClass;
150     }
151
152     public List JavaDoc getInitParamList() {
153         return initParamList;
154     }
155
156     public void setInitParamList(List JavaDoc initParamList) {
157         this.initParamList = initParamList;
158     }
159
160     public void addInitParam(InitParam initParam) {
161         initParamList.add(initParam);
162     }
163
164     public void removeInitParam(InitParam initParam) {
165         initParamList.remove(initParam);
166     }
167
168     public String JavaDoc getId() {
169         return id;
170     }
171
172     public void setId(String JavaDoc id) {
173         this.id = id;
174         zeus_IdSet = true;
175     }
176
177     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
178         try {
179             startDTD(name, publicID, systemID);
180         } catch (SAXException JavaDoc neverHappens) { }
181     }
182
183     public void setOutputEncoding(String JavaDoc outputEncoding) {
184         this.outputEncoding = outputEncoding;
185     }
186
187     public void marshal(File JavaDoc file) throws IOException JavaDoc {
188         // Delegate to the marshal(Writer) method
189
marshal(new FileWriter JavaDoc(file));
190     }
191
192     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
193         // Delegate to the marshal(Writer) method
194
marshal(new OutputStreamWriter JavaDoc(outputStream));
195     }
196
197     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
198         // Write out the XML declaration
199
writer.write("<?xml version=\"1.0\" ");
200         if (outputEncoding != null) {
201             writer.write("encoding=\"");
202             writer.write(outputEncoding);
203             writer.write("\"?>\n\n");
204
205         } else {
206             writer.write("encoding=\"UTF-8\"?>\n\n");
207
208         }
209         // Handle DOCTYPE declaration
210
writer.write(docTypeString);
211         writer.write("\n");
212         // Now start the recursive writing
213
writeXMLRepresentation(writer, "");
214
215         // Close up
216
writer.flush();
217         writer.close();
218     }
219
220     protected void writeXMLRepresentation(Writer JavaDoc writer,
221                                           String JavaDoc indent)
222         throws IOException JavaDoc {
223
224         writer.write(indent);
225         writer.write("<filter");
226
227         // Handle namespace mappings (if needed)
228
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
229             String JavaDoc prefix = (String JavaDoc)i.next();
230             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
231             writer.write(" xmlns");
232             if (!prefix.trim().equals("")) {
233                 writer.write(":");
234                 writer.write(prefix);
235             }
236             writer.write("=\"");
237             writer.write(uri);
238             writer.write("\"\n ");
239         }
240
241         // Handle attributes (if needed)
242
if (zeus_IdSet) {
243             writer.write(" id=\"");
244             writer.write(escapeAttributeValue(id));
245             writer.write("\"");
246         }
247         writer.write(">");
248         writer.write("\n");
249
250         // Handle child elements
251
if (icon != null) {
252             ((IconImpl)icon).writeXMLRepresentation(writer,
253                 new StringBuffer JavaDoc(indent).append(" ").toString());
254         }
255
256         if (filterName != null) {
257             ((FilterNameImpl)filterName).writeXMLRepresentation(writer,
258                 new StringBuffer JavaDoc(indent).append(" ").toString());
259         }
260
261         if (displayName != null) {
262             ((DisplayNameImpl)displayName).writeXMLRepresentation(writer,
263                 new StringBuffer JavaDoc(indent).append(" ").toString());
264         }
265
266         if (description != null) {
267             ((DescriptionImpl)description).writeXMLRepresentation(writer,
268                 new StringBuffer JavaDoc(indent).append(" ").toString());
269         }
270
271         if (filterClass != null) {
272             ((FilterClassImpl)filterClass).writeXMLRepresentation(writer,
273                 new StringBuffer JavaDoc(indent).append(" ").toString());
274         }
275
276         for (Iterator JavaDoc i=initParamList.iterator(); i.hasNext(); ) {
277             InitParamImpl initParam = (InitParamImpl)i.next();
278             initParam.writeXMLRepresentation(writer,
279                 new StringBuffer JavaDoc(indent).append(" ").toString());
280         }
281         writer.write(indent);
282         writer.write("</filter>\n");
283     }
284
285     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
286         String JavaDoc returnValue = attributeValue;
287         for (int i = 0; i < returnValue.length(); i++) {
288             char ch = returnValue.charAt(i);
289             if (ch == '"') {
290                 returnValue = new StringBuffer JavaDoc()
291                     .append(returnValue.substring(0, i))
292                     .append("&quot;")
293                     .append(returnValue.substring(i+1))
294                     .toString();
295             }
296         }
297         return returnValue;
298     }
299
300     private String JavaDoc escapeTextValue(String JavaDoc textValue) {
301         String JavaDoc returnValue = textValue;
302         for (int i = 0; i < returnValue.length(); i++) {
303             char ch = returnValue.charAt(i);
304             if (ch == '<') {
305                 returnValue = new StringBuffer JavaDoc()
306                     .append(returnValue.substring(0, i))
307                     .append("&lt;")
308                     .append(returnValue.substring(i+1))
309                     .toString();
310             } else if (ch == '>') {
311                 returnValue = new StringBuffer JavaDoc()
312                     .append(returnValue.substring(0, i))
313                     .append("&gt;")
314                     .append(returnValue.substring(i+1))
315                     .toString();
316             }
317         }
318         return returnValue;
319     }
320
321     /**
322      * <p>
323      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
324      * </p>
325      *
326      * @param resolver the entity resolver to use.
327      */

328     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
329         entityResolver = resolver;
330     }
331
332     /**
333      * <p>
334      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
335      * </p>
336      *
337      * @param handler the entity resolver to use.
338      */

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