KickJava   Java API By Example, From Geeks To Geeks.

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


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

242     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
243         entityResolver = resolver;
244     }
245
246     /**
247      * <p>
248      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
249      * </p>
250      *
251      * @param handler the entity resolver to use.
252      */

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