KickJava   Java API By Example, From Geeks To Geeks.

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


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 // 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 RadioImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, Radio {
51
52     private List JavaDoc optionList;
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 RadioImpl prototype = null;
85
86     public static void setPrototype(RadioImpl prototype) {
87         RadioImpl.prototype = prototype;
88     }
89     public static RadioImpl newInstance() {
90         try {
91             return (prototype!=null)?(RadioImpl)prototype.clone(): new RadioImpl();
92         } catch (CloneNotSupportedException JavaDoc e) {
93             return null; // never
94
}
95     }
96     public RadioImpl() {
97         optionList = new LinkedList JavaDoc();
98         docTypeString = "";
99         hasDTD = false;
100         validate = false;
101         namespaceMappings = new HashMap JavaDoc();
102     }
103
104     public List JavaDoc getOptionList() {
105         return optionList;
106     }
107
108     public void setOptionList(List JavaDoc optionList) {
109         this.optionList = optionList;
110     }
111
112     public void addOption(Option option) {
113         optionList.add(option);
114     }
115
116     public void removeOption(Option option) {
117         optionList.remove(option);
118     }
119
120     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
121         try {
122             startDTD(name, publicID, systemID);
123         } catch (SAXException JavaDoc neverHappens) { }
124     }
125
126     public void setOutputEncoding(String JavaDoc outputEncoding) {
127         this.outputEncoding = outputEncoding;
128     }
129
130     public void marshal(File JavaDoc file) throws IOException JavaDoc {
131         // Delegate to the marshal(Writer) method
132
marshal(new FileWriter JavaDoc(file));
133     }
134
135     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
136         // Delegate to the marshal(Writer) method
137
marshal(new OutputStreamWriter JavaDoc(outputStream));
138     }
139
140     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
141         // Write out the XML declaration
142
writer.write("<?xml version=\"1.0\" ");
143         if (outputEncoding != null) {
144             writer.write("encoding=\"");
145             writer.write(outputEncoding);
146             writer.write("\"?>\n\n");
147
148         } else {
149             writer.write("encoding=\"UTF-8\"?>\n\n");
150
151         }
152         // Handle DOCTYPE declaration
153
writer.write(docTypeString);
154         writer.write("\n");
155         // Now start the recursive writing
156
writeXMLRepresentation(writer, "");
157
158         // Close up
159
writer.flush();
160         writer.close();
161     }
162
163     protected void writeXMLRepresentation(Writer JavaDoc writer,
164                                           String JavaDoc indent)
165         throws IOException JavaDoc {
166
167         writer.write(indent);
168         writer.write("<radio");
169
170         // Handle namespace mappings (if needed)
171
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
172             String JavaDoc prefix = (String JavaDoc)i.next();
173             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
174             writer.write(" xmlns");
175             if (!prefix.trim().equals("")) {
176                 writer.write(":");
177                 writer.write(prefix);
178             }
179             writer.write("=\"");
180             writer.write(uri);
181             writer.write("\"\n ");
182         }
183
184         // Handle attributes (if needed)
185
writer.write(">");
186         writer.write("\n");
187
188         // Handle child elements
189
for (Iterator JavaDoc i=optionList.iterator(); i.hasNext(); ) {
190             OptionImpl option = (OptionImpl)i.next();
191             option.writeXMLRepresentation(writer,
192                 new StringBuffer JavaDoc(indent).append(" ").toString());
193         }
194         writer.write(indent);
195         writer.write("</radio>\n");
196     }
197
198     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
199         String JavaDoc returnValue = attributeValue;
200         for (int i = 0; i < returnValue.length(); i++) {
201             char ch = returnValue.charAt(i);
202             if (ch == '"') {
203                 returnValue = new StringBuffer JavaDoc()
204                     .append(returnValue.substring(0, i))
205                     .append("&quot;")
206                     .append(returnValue.substring(i+1))
207                     .toString();
208             }
209         }
210         return returnValue;
211     }
212
213     private String JavaDoc escapeTextValue(String JavaDoc textValue) {
214         String JavaDoc returnValue = textValue;
215         for (int i = 0; i < returnValue.length(); i++) {
216             char ch = returnValue.charAt(i);
217             if (ch == '<') {
218                 returnValue = new StringBuffer JavaDoc()
219                     .append(returnValue.substring(0, i))
220                     .append("&lt;")
221                     .append(returnValue.substring(i+1))
222                     .toString();
223             } else if (ch == '>') {
224                 returnValue = new StringBuffer JavaDoc()
225                     .append(returnValue.substring(0, i))
226                     .append("&gt;")
227                     .append(returnValue.substring(i+1))
228                     .toString();
229             }
230         }
231         return returnValue;
232     }
233
234     /**
235      * <p>
236      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
237      * </p>
238      *
239      * @param resolver the entity resolver to use.
240      */

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

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