KickJava   Java API By Example, From Geeks To Geeks.

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


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 FiltermapImpl extends DefaultHandler JavaDoc implements Unmarshallable, LexicalHandler JavaDoc, Filtermap {
51
52     private List JavaDoc remapList;
53     private List JavaDoc removeList;
54     private List JavaDoc copyList;
55     private List JavaDoc filterList;
56     private String JavaDoc name;
57     private boolean zeus_NameSet;
58     private String JavaDoc xmlextends;
59     private boolean zeus_XmlextendsSet;
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     public FiltermapImpl() {
92         remapList = new LinkedList JavaDoc();
93         removeList = new LinkedList JavaDoc();
94         copyList = new LinkedList JavaDoc();
95         filterList = new LinkedList JavaDoc();
96         zeus_NameSet = false;
97         zeus_XmlextendsSet = false;
98         docTypeString = "";
99         hasDTD = false;
100         validate = false;
101         namespaceMappings = new HashMap JavaDoc();
102     }
103
104     public List JavaDoc getRemapList() {
105         return remapList;
106     }
107
108     public void setRemapList(List JavaDoc remapList) {
109         this.remapList = remapList;
110     }
111
112     public void addRemap(Remap remap) {
113         remapList.add(remap);
114     }
115
116     public void removeRemap(Remap remap) {
117         remapList.remove(remap);
118     }
119
120     public List JavaDoc getRemoveList() {
121         return removeList;
122     }
123
124     public void setRemoveList(List JavaDoc removeList) {
125         this.removeList = removeList;
126     }
127
128     public void addRemove(Remove remove) {
129         removeList.add(remove);
130     }
131
132     public void removeRemove(Remove remove) {
133         removeList.remove(remove);
134     }
135
136     public List JavaDoc getCopyList() {
137         return copyList;
138     }
139
140     public void setCopyList(List JavaDoc copyList) {
141         this.copyList = copyList;
142     }
143
144     public void addCopy(Copy copy) {
145         copyList.add(copy);
146     }
147
148     public void removeCopy(Copy copy) {
149         copyList.remove(copy);
150     }
151
152     public List JavaDoc getFilterList() {
153         return filterList;
154     }
155
156     public void setFilterList(List JavaDoc filterList) {
157         this.filterList = filterList;
158     }
159
160     public void addFilter(Filter filter) {
161         filterList.add(filter);
162     }
163
164     public void removeFilter(Filter filter) {
165         filterList.remove(filter);
166     }
167
168     public String JavaDoc getName() {
169         return name;
170     }
171
172     public void setName(String JavaDoc name) {
173         this.name = name;
174         zeus_NameSet = true;
175     }
176
177     public String JavaDoc getXmlextends() {
178         return xmlextends;
179     }
180
181     public void setXmlextends(String JavaDoc xmlextends) {
182         this.xmlextends = xmlextends;
183         zeus_XmlextendsSet = true;
184     }
185
186     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
187         try {
188             startDTD(name, publicID, systemID);
189         } catch (SAXException JavaDoc neverHappens) { }
190     }
191
192     public void setOutputEncoding(String JavaDoc outputEncoding) {
193         this.outputEncoding = outputEncoding;
194     }
195
196     public void marshal(File JavaDoc file) throws IOException JavaDoc {
197         // Delegate to the marshal(Writer) method
198
marshal(new FileWriter JavaDoc(file));
199     }
200
201     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
202         // Delegate to the marshal(Writer) method
203
marshal(new OutputStreamWriter JavaDoc(outputStream));
204     }
205
206     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
207         // Write out the XML declaration
208
writer.write("<?xml version=\"1.0\" ");
209         if (outputEncoding != null) {
210             writer.write("encoding=\"");
211             writer.write(outputEncoding);
212             writer.write("\"?>\n\n");
213
214         } else {
215             writer.write("encoding=\"UTF-8\"?>\n\n");
216
217         }
218         // Handle DOCTYPE declaration
219
writer.write(docTypeString);
220         writer.write("\n");
221         // Now start the recursive writing
222
writeXMLRepresentation(writer, "");
223
224         // Close up
225
writer.flush();
226         writer.close();
227     }
228
229     protected void writeXMLRepresentation(Writer JavaDoc writer,
230                                           String JavaDoc indent)
231         throws IOException JavaDoc {
232
233         writer.write(indent);
234         writer.write("<filtermap");
235
236         // Handle namespace mappings (if needed)
237
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
238             String JavaDoc prefix = (String JavaDoc)i.next();
239             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
240             writer.write(" xmlns");
241             if (!prefix.trim().equals("")) {
242                 writer.write(":");
243                 writer.write(prefix);
244             }
245             writer.write("=\"");
246             writer.write(uri);
247             writer.write("\"\n ");
248         }
249
250         // Handle attributes (if needed)
251
if (zeus_NameSet) {
252             writer.write(" name=\"");
253             writer.write(escapeAttributeValue(name));
254             writer.write("\"");
255         }
256         if (zeus_XmlextendsSet) {
257             writer.write(" extends=\"");
258             writer.write(escapeAttributeValue(xmlextends));
259             writer.write("\"");
260         }
261         writer.write(">");
262         writer.write("\n");
263
264         // Handle child elements
265
for (Iterator JavaDoc i=remapList.iterator(); i.hasNext(); ) {
266             RemapImpl remap = (RemapImpl)i.next();
267             remap.writeXMLRepresentation(writer,
268                 new StringBuffer JavaDoc(indent).append(" ").toString());
269         }
270         for (Iterator JavaDoc i=removeList.iterator(); i.hasNext(); ) {
271             RemoveImpl remove = (RemoveImpl)i.next();
272             remove.writeXMLRepresentation(writer,
273                 new StringBuffer JavaDoc(indent).append(" ").toString());
274         }
275         for (Iterator JavaDoc i=copyList.iterator(); i.hasNext(); ) {
276             CopyImpl copy = (CopyImpl)i.next();
277             copy.writeXMLRepresentation(writer,
278                 new StringBuffer JavaDoc(indent).append(" ").toString());
279         }
280         for (Iterator JavaDoc i=filterList.iterator(); i.hasNext(); ) {
281             FilterImpl filter = (FilterImpl)i.next();
282             filter.writeXMLRepresentation(writer,
283                 new StringBuffer JavaDoc(indent).append(" ").toString());
284         }
285         writer.write(indent);
286         writer.write("</filtermap>\n");
287     }
288
289     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
290         String JavaDoc returnValue = attributeValue;
291         for (int i = 0; i < returnValue.length(); i++) {
292             char ch = returnValue.charAt(i);
293             if (ch == '"') {
294                 returnValue = new StringBuffer JavaDoc()
295                     .append(returnValue.substring(0, i))
296                     .append("&quot;")
297                     .append(returnValue.substring(i+1))
298                     .toString();
299             }
300         }
301         return returnValue;
302     }
303
304     /**
305      * <p>
306      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
307      * </p>
308      *
309      * @param resolver the entity resolver to use.
310      */

311     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
312         entityResolver = resolver;
313     }
314
315     /**
316      * <p>
317      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
318      * </p>
319      *
320      * @param handler the entity resolver to use.
321      */

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