KickJava   Java API By Example, From Geeks To Geeks.

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


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

194     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
195         entityResolver = resolver;
196     }
197
198     /**
199      * <p>
200      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
201      * </p>
202      *
203      * @param handler the entity resolver to use.
204      */

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