KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > sam > xmlform > dtd > OptionImpl


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

261     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
262         entityResolver = resolver;
263     }
264
265     /**
266      * <p>
267      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
268      * </p>
269      *
270      * @param handler the entity resolver to use.
271      */

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