KickJava   Java API By Example, From Geeks To Geeks.

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


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 SelectImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, Select {
51
52     private List JavaDoc optionList;
53     private String JavaDoc multivalue;
54     private static boolean zeus_MultivalueInitialized;
55     private static java.util.Vector JavaDoc zeus_validMultivalue;
56     private boolean zeus_MultivalueSet;
57     private String JavaDoc xmlclass;
58     private boolean zeus_XmlclassSet;
59     private String JavaDoc method;
60     private boolean zeus_MethodSet;
61
62     /** Any DOCTYPE reference/statements. */
63     private String JavaDoc docTypeString;
64
65     /** The encoding for the output document */
66     private String JavaDoc outputEncoding;
67
68     /** The current node in unmarshalling */
69     private Unmarshallable zeus_currentUNode;
70
71     /** The parent node in unmarshalling */
72     private Unmarshallable zeus_parentUNode;
73
74     /** Whether this node has been handled */
75     private boolean zeus_thisNodeHandled = false;
76
77     /** Whether a DTD exists for an unmarshal call */
78     private boolean hasDTD;
79
80     /** Whether validation is occurring */
81     private boolean validate;
82
83     /** The namespace mappings on this element */
84     private Map JavaDoc namespaceMappings;
85
86     /** The EntityResolver for SAX parsing to use */
87     private static EntityResolver JavaDoc entityResolver;
88
89     /** The ErrorHandler for SAX parsing to use */
90     private static ErrorHandler JavaDoc errorHandler;
91
92     private static SelectImpl prototype = null;
93
94     public static void setPrototype(SelectImpl prototype) {
95         SelectImpl.prototype = prototype;
96     }
97     public static SelectImpl newInstance() {
98         try {
99             return (prototype!=null)?(SelectImpl)prototype.clone(): new SelectImpl();
100         } catch (CloneNotSupportedException JavaDoc e) {
101             return null; // never
102
}
103     }
104     public SelectImpl() {
105         optionList = new LinkedList JavaDoc();
106         zeus_MultivalueInitialized = false;
107         zeus_validMultivalue = null;
108         zeus_MultivalueSet = false;
109         zeus_XmlclassSet = false;
110         zeus_MethodSet = false;
111         docTypeString = "";
112         hasDTD = false;
113         validate = false;
114         namespaceMappings = new HashMap JavaDoc();
115     }
116
117     public List JavaDoc getOptionList() {
118         return optionList;
119     }
120
121     public void setOptionList(List JavaDoc optionList) {
122         this.optionList = optionList;
123     }
124
125     public void addOption(Option option) {
126         optionList.add(option);
127     }
128
129     public void removeOption(Option option) {
130         optionList.remove(option);
131     }
132
133     public String JavaDoc getMultivalue() {
134         return multivalue;
135     }
136
137     public void setMultivalue(String JavaDoc multivalue)
138         throws IllegalArgumentException JavaDoc {
139         if (!zeus_MultivalueInitialized) {
140             zeus_validMultivalue = new java.util.Vector JavaDoc();
141             zeus_validMultivalue.addElement("true");
142             zeus_validMultivalue.addElement("false");
143             zeus_MultivalueInitialized = true;
144         }
145         if (!zeus_validMultivalue.contains(multivalue)) {
146               throw new IllegalArgumentException JavaDoc("Illegal value for attribute 'multivalue'");
147         } else {
148               this.multivalue = multivalue;
149             this.zeus_MultivalueSet = true;
150         }
151     }
152
153     public String JavaDoc getXmlclass() {
154         return xmlclass;
155     }
156
157     public void setXmlclass(String JavaDoc xmlclass) {
158         this.xmlclass = xmlclass;
159         zeus_XmlclassSet = true;
160     }
161
162     public String JavaDoc getMethod() {
163         return method;
164     }
165
166     public void setMethod(String JavaDoc method) {
167         this.method = method;
168         zeus_MethodSet = true;
169     }
170
171     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
172         try {
173             startDTD(name, publicID, systemID);
174         } catch (SAXException JavaDoc neverHappens) { }
175     }
176
177     public void setOutputEncoding(String JavaDoc outputEncoding) {
178         this.outputEncoding = outputEncoding;
179     }
180
181     public void marshal(File JavaDoc file) throws IOException JavaDoc {
182         // Delegate to the marshal(Writer) method
183
marshal(new FileWriter JavaDoc(file));
184     }
185
186     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
187         // Delegate to the marshal(Writer) method
188
marshal(new OutputStreamWriter JavaDoc(outputStream));
189     }
190
191     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
192         // Write out the XML declaration
193
writer.write("<?xml version=\"1.0\" ");
194         if (outputEncoding != null) {
195             writer.write("encoding=\"");
196             writer.write(outputEncoding);
197             writer.write("\"?>\n\n");
198
199         } else {
200             writer.write("encoding=\"UTF-8\"?>\n\n");
201
202         }
203         // Handle DOCTYPE declaration
204
writer.write(docTypeString);
205         writer.write("\n");
206         // Now start the recursive writing
207
writeXMLRepresentation(writer, "");
208
209         // Close up
210
writer.flush();
211         writer.close();
212     }
213
214     protected void writeXMLRepresentation(Writer JavaDoc writer,
215                                           String JavaDoc indent)
216         throws IOException JavaDoc {
217
218         writer.write(indent);
219         writer.write("<select");
220
221         // Handle namespace mappings (if needed)
222
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
223             String JavaDoc prefix = (String JavaDoc)i.next();
224             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
225             writer.write(" xmlns");
226             if (!prefix.trim().equals("")) {
227                 writer.write(":");
228                 writer.write(prefix);
229             }
230             writer.write("=\"");
231             writer.write(uri);
232             writer.write("\"\n ");
233         }
234
235         // Handle attributes (if needed)
236
if (zeus_MultivalueSet) {
237             writer.write(" multivalue=\"");
238             writer.write(escapeAttributeValue(multivalue));
239             writer.write("\"");
240         }
241         if (zeus_XmlclassSet) {
242             writer.write(" class=\"");
243             writer.write(escapeAttributeValue(xmlclass));
244             writer.write("\"");
245         }
246         if (zeus_MethodSet) {
247             writer.write(" method=\"");
248             writer.write(escapeAttributeValue(method));
249             writer.write("\"");
250         }
251         writer.write(">");
252         writer.write("\n");
253
254         // Handle child elements
255
for (Iterator JavaDoc i=optionList.iterator(); i.hasNext(); ) {
256             OptionImpl option = (OptionImpl)i.next();
257             option.writeXMLRepresentation(writer,
258                 new StringBuffer JavaDoc(indent).append(" ").toString());
259         }
260         writer.write(indent);
261         writer.write("</select>\n");
262     }
263
264     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
265         String JavaDoc returnValue = attributeValue;
266         for (int i = 0; i < returnValue.length(); i++) {
267             char ch = returnValue.charAt(i);
268             if (ch == '"') {
269                 returnValue = new StringBuffer JavaDoc()
270                     .append(returnValue.substring(0, i))
271                     .append("&quot;")
272                     .append(returnValue.substring(i+1))
273                     .toString();
274             }
275         }
276         return returnValue;
277     }
278
279     private String JavaDoc escapeTextValue(String JavaDoc textValue) {
280         String JavaDoc returnValue = textValue;
281         for (int i = 0; i < returnValue.length(); i++) {
282             char ch = returnValue.charAt(i);
283             if (ch == '<') {
284                 returnValue = new StringBuffer JavaDoc()
285                     .append(returnValue.substring(0, i))
286                     .append("&lt;")
287                     .append(returnValue.substring(i+1))
288                     .toString();
289             } else if (ch == '>') {
290                 returnValue = new StringBuffer JavaDoc()
291                     .append(returnValue.substring(0, i))
292                     .append("&gt;")
293                     .append(returnValue.substring(i+1))
294                     .toString();
295             }
296         }
297         return returnValue;
298     }
299
300     /**
301      * <p>
302      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
303      * </p>
304      *
305      * @param resolver the entity resolver to use.
306      */

307     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
308         entityResolver = resolver;
309     }
310
311     /**
312      * <p>
313      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
314      * </p>
315      *
316      * @param handler the entity resolver to use.
317      */

318     public static void setErrorHandler(ErrorHandler JavaDoc handler) {
319         errorHandler = handler;
320     }
321
322     public static Select unmarshal(File JavaDoc file) throws IOException JavaDoc {
323         // Delegate to the unmarshal(Reader) method
324
return unmarshal(new FileReader JavaDoc(file));
325     }
326
327     public static Select unmarshal(File JavaDoc file, boolean validate) throws IOException JavaDoc {
328         // Delegate to the unmarshal(Reader) method
329
return unmarshal(new FileReader JavaDoc(file), validate);
330     }
331
332     public static Select unmarshal(InputStream JavaDoc inputStream) throws IOException JavaDoc {
333         // Delegate to the unmarshal(Reader) method
334
return unmarshal(new InputStreamReader JavaDoc(inputStream));
335     }
336
337     public static Select unmarshal(InputStream JavaDoc inputStream, boolean validate) throws IOException JavaDoc {
338         // Delegate to the unmarshal(Reader) method
339
return unmarshal(new InputStreamReader JavaDoc(inputStream), validate);
340     }
341
342     public static Select unmarshal(Reader JavaDoc reader) throws IOException JavaDoc {
343         // Delegate with default validation value
344
return unmarshal(reader, false);
345     }
346
347     public static Select unmarshal(Reader JavaDoc reader, boolean validate) throws IOException JavaDoc {
348         SelectImpl select = SelectImpl.newInstance();
349         select.setValidating(validate);
350         select.setCurrentUNode(select);
351         select.setParentUNode(null);
352         // Load the XML parser
353
XMLReader JavaDoc parser = null;
354         String JavaDoc parserClass = System.getProperty("org.xml.sax.driver",
355             "org.apache.xerces.parsers.SAXParser");
356         try {
357             parser = XMLReaderFactory.createXMLReader(parserClass);
358
359             // Set entity resolver, if needed
360
if (entityResolver != null) {
361                 parser.setEntityResolver(entityResolver);
362             }
363
364             // Set error handler
365
parser.setErrorHandler(select);
366
367             // Register lexical handler
368
parser.setProperty("http://xml.org/sax/properties/lexical-handler", select);
369
370             // Register content handler
371
parser.setContentHandler(select);
372         } catch (SAXException JavaDoc e) {
373             throw new IOException JavaDoc("Could not load XML parser: " +
374                 e.getMessage());
375         }
376
377         InputSource JavaDoc inputSource = new InputSource JavaDoc(reader);
378         try {
379             parser.setFeature("http://xml.org/sax/features/validation", new Boolean JavaDoc(validate).booleanValue());
380             parser.setFeature("http://xml.org/sax/features/namespaces", true);
381             parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
382             parser.parse(inputSource);
383         } catch (SAXException JavaDoc e) {
384             throw new IOException JavaDoc("Error parsing XML document: " +
385                 e.getMessage());
386         }
387
388         // Return the resultant object
389
return select;
390     }
391
392     public Unmarshallable getParentUNode() {
393         return zeus_parentUNode;
394     }
395
396     public void setParentUNode(Unmarshallable parentUNode) {
397         this.zeus_parentUNode = parentUNode;
398     }
399
400     public Unmarshallable getCurrentUNode() {
401         return zeus_currentUNode;
402     }
403
404     public void setCurrentUNode(Unmarshallable currentUNode) {
405         this.zeus_currentUNode = currentUNode;
406     }
407
408     public void setValidating(boolean validate) {
409         this.validate = validate;
410     }
411
412     public void startDocument() throws SAXException JavaDoc {
413         // no-op
414
}
415
416     public void setDocumentLocator(Locator JavaDoc locator) {
417         // no-op
418
}
419
420     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
421         throws SAXException JavaDoc {
422         namespaceMappings.put(prefix, uri);
423     }
424
425     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
426                              String JavaDoc qName, org.xml.sax.Attributes JavaDoc atts)
427         throws SAXException JavaDoc {
428
429         // Feed this to the correct ContentHandler
430
Unmarshallable current = getCurrentUNode();
431         if (current != this) {
432             current.startElement(namespaceURI, localName, qName, atts);
433             return;
434         }
435
436         // See if we handle, or we delegate
437
if ((localName.equals("select")) && (!zeus_thisNodeHandled)) {
438             // Handle ourselves
439
for (int i=0, len=atts.getLength(); i<len; i++) {
440                 String JavaDoc attName= atts.getLocalName(i);
441                 String JavaDoc attValue = atts.getValue(i);
442                 if (attName.equals("multivalue")) {
443                     setMultivalue(attValue);
444                 }
445                 if (attName.equals("class")) {
446                     setXmlclass(attValue);
447                 }
448                 if (attName.equals("method")) {
449                     setMethod(attValue);
450                 }
451             }
452             zeus_thisNodeHandled = true;
453             return;
454         } else {
455             // Delegate handling
456
if (localName.equals("option")) {
457                 OptionImpl option = OptionImpl.newInstance();
458                 current = getCurrentUNode();
459                 option.setParentUNode(current);
460                 option.setCurrentUNode(option);
461                 this.setCurrentUNode(option);
462                 option.startElement(namespaceURI, localName, qName, atts);
463                 // Add this value in
464
optionList.add(option);
465                 return;
466             }
467         }
468     }
469
470     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
471                            String JavaDoc qName)
472         throws SAXException JavaDoc {
473
474         Unmarshallable current = getCurrentUNode();
475         if (current != this) {
476             current.endElement(namespaceURI, localName, qName);
477             return;
478         }
479
480         Unmarshallable parent = getCurrentUNode().getParentUNode();
481         if (parent != null) {
482             parent.setCurrentUNode(parent);
483         }
484     }
485
486     public void characters(char[] ch, int start, int len)
487         throws SAXException JavaDoc {
488
489         // Feed this to the correct ContentHandler
490
Unmarshallable current = getCurrentUNode();
491         if (current != this) {
492             current.characters(ch, start, len);
493             return;
494         }
495
496         String JavaDoc text = new String JavaDoc(ch, start, len);
497         text = text.trim();
498     }
499
500     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
501         // Currently no-op
502
}
503
504     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
505         if (errorHandler != null) {
506             errorHandler.warning(e);
507         }
508     }
509
510     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
511         if ((validate) && (!hasDTD)) {
512             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.");
513         }
514         if (errorHandler != null) {
515             errorHandler.error(e);
516         }
517     }
518
519     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
520         if ((validate) && (!hasDTD)) {
521             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.");
522         }
523         if (errorHandler != null) {
524             errorHandler.fatalError(e);
525         }
526     }
527
528     public void startDTD(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID)
529         throws SAXException JavaDoc {
530
531         if ((name == null) || (name.equals(""))) {
532             docTypeString = "";
533             return;
534         }
535
536         hasDTD = true;
537         StringBuffer JavaDoc docTypeSB = new StringBuffer JavaDoc();
538         boolean hasPublic = false;
539
540         docTypeSB.append("<!DOCTYPE ")
541                  .append(name);
542
543         if ((publicID != null) && (!publicID.equals(""))) {
544             docTypeSB.append(" PUBLIC \"")
545                      .append(publicID)
546                      .append("\"");
547             hasPublic = true;
548         }
549
550         if ((systemID != null) && (!systemID.equals(""))) {
551             if (!hasPublic) {
552                 docTypeSB.append(" SYSTEM");
553             }
554             docTypeSB.append(" \"")
555                      .append(systemID)
556                      .append("\"");
557
558         }
559
560         docTypeSB.append(">");
561
562         docTypeString = docTypeSB.toString();
563     }
564
565     public void endDTD() throws SAXException JavaDoc {
566         // Currently no-op
567
}
568
569     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
570         // Currently no-op
571
}
572
573     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
574         // Currently no-op
575
}
576
577     public void startCDATA() throws SAXException JavaDoc {
578         // Currently no-op
579
}
580
581     public void endCDATA() throws SAXException JavaDoc {
582         // Currently no-op
583
}
584
585 }
586
Popular Tags