KickJava   Java API By Example, From Geeks To Geeks.

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


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

231     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
232         entityResolver = resolver;
233     }
234
235     /**
236      * <p>
237      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
238      * </p>
239      *
240      * @param handler the entity resolver to use.
241      */

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