KickJava   Java API By Example, From Geeks To Geeks.

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


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 ElementImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, Element {
46
47     private Text text;
48     private Textarea textarea;
49     private Select select;
50     private Radio radio;
51     private Hidden hidden;
52     private Validator validator;
53     private String JavaDoc label;
54     private boolean zeus_LabelSet;
55     private String JavaDoc name;
56     private boolean zeus_NameSet;
57     private String JavaDoc ref;
58     private boolean zeus_RefSet;
59
60     /** Any DOCTYPE reference/statements. */
61     private String JavaDoc docTypeString;
62
63     /** The encoding for the output document */
64     private String JavaDoc outputEncoding;
65
66     /** The current node in unmarshalling */
67     private Unmarshallable zeus_currentUNode;
68
69     /** The parent node in unmarshalling */
70     private Unmarshallable zeus_parentUNode;
71
72     /** Whether this node has been handled */
73     private boolean zeus_thisNodeHandled = false;
74
75     /** Whether a DTD exists for an unmarshal call */
76     private boolean hasDTD;
77
78     /** Whether validation is occurring */
79     private boolean validate;
80
81     /** The namespace mappings on this element */
82     private Map JavaDoc namespaceMappings;
83
84     /** The EntityResolver for SAX parsing to use */
85     private static EntityResolver JavaDoc entityResolver;
86
87     /** The ErrorHandler for SAX parsing to use */
88     private static ErrorHandler JavaDoc errorHandler;
89
90     private static ElementImpl prototype = null;
91
92     public static void setPrototype(ElementImpl prototype) {
93         ElementImpl.prototype = prototype;
94     }
95     public static ElementImpl newInstance() {
96         try {
97             return (prototype!=null)?(ElementImpl)prototype.clone(): new ElementImpl();
98         } catch (CloneNotSupportedException JavaDoc e) {
99             return null; // never
100
}
101     }
102     public ElementImpl() {
103         zeus_LabelSet = false;
104         zeus_NameSet = false;
105         zeus_RefSet = false;
106         docTypeString = "";
107         hasDTD = false;
108         validate = false;
109         namespaceMappings = new HashMap JavaDoc();
110     }
111
112     public Text getText() {
113         return text;
114     }
115
116     public void setText(Text text) {
117         this.text = text;
118     }
119
120     public Textarea getTextarea() {
121         return textarea;
122     }
123
124     public void setTextarea(Textarea textarea) {
125         this.textarea = textarea;
126     }
127
128     public Select getSelect() {
129         return select;
130     }
131
132     public void setSelect(Select select) {
133         this.select = select;
134     }
135
136     public Radio getRadio() {
137         return radio;
138     }
139
140     public void setRadio(Radio radio) {
141         this.radio = radio;
142     }
143
144     public Hidden getHidden() {
145         return hidden;
146     }
147
148     public void setHidden(Hidden hidden) {
149         this.hidden = hidden;
150     }
151
152     public Validator getValidator() {
153         return validator;
154     }
155
156     public void setValidator(Validator validator) {
157         this.validator = validator;
158     }
159
160     public String JavaDoc getLabel() {
161         return label;
162     }
163
164     public void setLabel(String JavaDoc label) {
165         this.label = label;
166         zeus_LabelSet = true;
167     }
168
169     public String JavaDoc getName() {
170         return name;
171     }
172
173     public void setName(String JavaDoc name) {
174         this.name = name;
175         zeus_NameSet = true;
176     }
177
178     public String JavaDoc getRef() {
179         return ref;
180     }
181
182     public void setRef(String JavaDoc ref) {
183         this.ref = ref;
184         zeus_RefSet = true;
185     }
186
187     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
188         try {
189             startDTD(name, publicID, systemID);
190         } catch (SAXException JavaDoc neverHappens) { }
191     }
192
193     public void setOutputEncoding(String JavaDoc outputEncoding) {
194         this.outputEncoding = outputEncoding;
195     }
196
197     public void marshal(File JavaDoc file) throws IOException JavaDoc {
198         // Delegate to the marshal(Writer) method
199
marshal(new FileWriter JavaDoc(file));
200     }
201
202     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
203         // Delegate to the marshal(Writer) method
204
marshal(new OutputStreamWriter JavaDoc(outputStream));
205     }
206
207     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
208         // Write out the XML declaration
209
writer.write("<?xml version=\"1.0\" ");
210         if (outputEncoding != null) {
211             writer.write("encoding=\"");
212             writer.write(outputEncoding);
213             writer.write("\"?>\n\n");
214
215         } else {
216             writer.write("encoding=\"UTF-8\"?>\n\n");
217
218         }
219         // Handle DOCTYPE declaration
220
writer.write(docTypeString);
221         writer.write("\n");
222         // Now start the recursive writing
223
writeXMLRepresentation(writer, "");
224
225         // Close up
226
writer.flush();
227         writer.close();
228     }
229
230     protected void writeXMLRepresentation(Writer JavaDoc writer,
231                                           String JavaDoc indent)
232         throws IOException JavaDoc {
233
234         writer.write(indent);
235         writer.write("<element");
236
237         // Handle namespace mappings (if needed)
238
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
239             String JavaDoc prefix = (String JavaDoc)i.next();
240             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
241             writer.write(" xmlns");
242             if (!prefix.trim().equals("")) {
243                 writer.write(":");
244                 writer.write(prefix);
245             }
246             writer.write("=\"");
247             writer.write(uri);
248             writer.write("\"\n ");
249         }
250
251         // Handle attributes (if needed)
252
if (zeus_LabelSet) {
253             writer.write(" label=\"");
254             writer.write(escapeAttributeValue(label));
255             writer.write("\"");
256         }
257         if (zeus_NameSet) {
258             writer.write(" name=\"");
259             writer.write(escapeAttributeValue(name));
260             writer.write("\"");
261         }
262         if (zeus_RefSet) {
263             writer.write(" ref=\"");
264             writer.write(escapeAttributeValue(ref));
265             writer.write("\"");
266         }
267         writer.write(">");
268         writer.write("\n");
269
270         // Handle child elements
271
if (text != null) {
272             ((TextImpl)text).writeXMLRepresentation(writer,
273                 new StringBuffer JavaDoc(indent).append(" ").toString());
274         }
275
276         if (textarea != null) {
277             ((TextareaImpl)textarea).writeXMLRepresentation(writer,
278                 new StringBuffer JavaDoc(indent).append(" ").toString());
279         }
280
281         if (select != null) {
282             ((SelectImpl)select).writeXMLRepresentation(writer,
283                 new StringBuffer JavaDoc(indent).append(" ").toString());
284         }
285
286         if (radio != null) {
287             ((RadioImpl)radio).writeXMLRepresentation(writer,
288                 new StringBuffer JavaDoc(indent).append(" ").toString());
289         }
290
291         if (hidden != null) {
292             ((HiddenImpl)hidden).writeXMLRepresentation(writer,
293                 new StringBuffer JavaDoc(indent).append(" ").toString());
294         }
295
296         if (validator != null) {
297             ((ValidatorImpl)validator).writeXMLRepresentation(writer,
298                 new StringBuffer JavaDoc(indent).append(" ").toString());
299         }
300
301         writer.write(indent);
302         writer.write("</element>\n");
303     }
304
305     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
306         String JavaDoc returnValue = attributeValue;
307         for (int i = 0; i < returnValue.length(); i++) {
308             char ch = returnValue.charAt(i);
309             if (ch == '"') {
310                 returnValue = new StringBuffer JavaDoc()
311                     .append(returnValue.substring(0, i))
312                     .append("&quot;")
313                     .append(returnValue.substring(i+1))
314                     .toString();
315             }
316         }
317         return returnValue;
318     }
319
320     private String JavaDoc escapeTextValue(String JavaDoc textValue) {
321         String JavaDoc returnValue = textValue;
322         for (int i = 0; i < returnValue.length(); i++) {
323             char ch = returnValue.charAt(i);
324             if (ch == '<') {
325                 returnValue = new StringBuffer JavaDoc()
326                     .append(returnValue.substring(0, i))
327                     .append("&lt;")
328                     .append(returnValue.substring(i+1))
329                     .toString();
330             } else if (ch == '>') {
331                 returnValue = new StringBuffer JavaDoc()
332                     .append(returnValue.substring(0, i))
333                     .append("&gt;")
334                     .append(returnValue.substring(i+1))
335                     .toString();
336             }
337         }
338         return returnValue;
339     }
340
341     /**
342      * <p>
343      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
344      * </p>
345      *
346      * @param resolver the entity resolver to use.
347      */

348     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
349         entityResolver = resolver;
350     }
351
352     /**
353      * <p>
354      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
355      * </p>
356      *
357      * @param handler the entity resolver to use.
358      */

359     public static void setErrorHandler(ErrorHandler JavaDoc handler) {
360         errorHandler = handler;
361     }
362
363     public static Element unmarshal(File JavaDoc file) throws IOException JavaDoc {
364         // Delegate to the unmarshal(Reader) method
365
return unmarshal(new FileReader JavaDoc(file));
366     }
367
368     public static Element unmarshal(File JavaDoc file, boolean validate) throws IOException JavaDoc {
369         // Delegate to the unmarshal(Reader) method
370
return unmarshal(new FileReader JavaDoc(file), validate);
371     }
372
373     public static Element unmarshal(InputStream JavaDoc inputStream) throws IOException JavaDoc {
374         // Delegate to the unmarshal(Reader) method
375
return unmarshal(new InputStreamReader JavaDoc(inputStream));
376     }
377
378     public static Element unmarshal(InputStream JavaDoc inputStream, boolean validate) throws IOException JavaDoc {
379         // Delegate to the unmarshal(Reader) method
380
return unmarshal(new InputStreamReader JavaDoc(inputStream), validate);
381     }
382
383     public static Element unmarshal(Reader JavaDoc reader) throws IOException JavaDoc {
384         // Delegate with default validation value
385
return unmarshal(reader, false);
386     }
387
388     public static Element unmarshal(Reader JavaDoc reader, boolean validate) throws IOException JavaDoc {
389         ElementImpl element = ElementImpl.newInstance();
390         element.setValidating(validate);
391         element.setCurrentUNode(element);
392         element.setParentUNode(null);
393         // Load the XML parser
394
XMLReader JavaDoc parser = null;
395         String JavaDoc parserClass = System.getProperty("org.xml.sax.driver",
396             "org.apache.xerces.parsers.SAXParser");
397         try {
398             parser = XMLReaderFactory.createXMLReader(parserClass);
399
400             // Set entity resolver, if needed
401
if (entityResolver != null) {
402                 parser.setEntityResolver(entityResolver);
403             }
404
405             // Set error handler
406
parser.setErrorHandler(element);
407
408             // Register lexical handler
409
parser.setProperty("http://xml.org/sax/properties/lexical-handler", element);
410
411             // Register content handler
412
parser.setContentHandler(element);
413         } catch (SAXException JavaDoc e) {
414             throw new IOException JavaDoc("Could not load XML parser: " +
415                 e.getMessage());
416         }
417
418         InputSource JavaDoc inputSource = new InputSource JavaDoc(reader);
419         try {
420             parser.setFeature("http://xml.org/sax/features/validation", new Boolean JavaDoc(validate).booleanValue());
421             parser.setFeature("http://xml.org/sax/features/namespaces", true);
422             parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
423             parser.parse(inputSource);
424         } catch (SAXException JavaDoc e) {
425             throw new IOException JavaDoc("Error parsing XML document: " +
426                 e.getMessage());
427         }
428
429         // Return the resultant object
430
return element;
431     }
432
433     public Unmarshallable getParentUNode() {
434         return zeus_parentUNode;
435     }
436
437     public void setParentUNode(Unmarshallable parentUNode) {
438         this.zeus_parentUNode = parentUNode;
439     }
440
441     public Unmarshallable getCurrentUNode() {
442         return zeus_currentUNode;
443     }
444
445     public void setCurrentUNode(Unmarshallable currentUNode) {
446         this.zeus_currentUNode = currentUNode;
447     }
448
449     public void setValidating(boolean validate) {
450         this.validate = validate;
451     }
452
453     public void startDocument() throws SAXException JavaDoc {
454         // no-op
455
}
456
457     public void setDocumentLocator(Locator JavaDoc locator) {
458         // no-op
459
}
460
461     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
462         throws SAXException JavaDoc {
463         namespaceMappings.put(prefix, uri);
464     }
465
466     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
467                              String JavaDoc qName, org.xml.sax.Attributes JavaDoc atts)
468         throws SAXException JavaDoc {
469
470         // Feed this to the correct ContentHandler
471
Unmarshallable current = getCurrentUNode();
472         if (current != this) {
473             current.startElement(namespaceURI, localName, qName, atts);
474             return;
475         }
476
477         // See if we handle, or we delegate
478
if ((localName.equals("element")) && (!zeus_thisNodeHandled)) {
479             // Handle ourselves
480
for (int i=0, len=atts.getLength(); i<len; i++) {
481                 String JavaDoc attName= atts.getLocalName(i);
482                 String JavaDoc attValue = atts.getValue(i);
483                 if (attName.equals("label")) {
484                     setLabel(attValue);
485                 }
486                 if (attName.equals("name")) {
487                     setName(attValue);
488                 }
489                 if (attName.equals("ref")) {
490                     setRef(attValue);
491                 }
492             }
493             zeus_thisNodeHandled = true;
494             return;
495         } else {
496             // Delegate handling
497
if (localName.equals("text") && (text==null)) {
498                 TextImpl text = TextImpl.newInstance();
499                 current = getCurrentUNode();
500                 text.setParentUNode(current);
501                 text.setCurrentUNode(text);
502                 this.setCurrentUNode(text);
503                 text.startElement(namespaceURI, localName, qName, atts);
504                 // Add this value in
505
this.text = text;
506                 return;
507             }
508             if (localName.equals("textarea") && (textarea==null)) {
509                 TextareaImpl textarea = TextareaImpl.newInstance();
510                 current = getCurrentUNode();
511                 textarea.setParentUNode(current);
512                 textarea.setCurrentUNode(textarea);
513                 this.setCurrentUNode(textarea);
514                 textarea.startElement(namespaceURI, localName, qName, atts);
515                 // Add this value in
516
this.textarea = textarea;
517                 return;
518             }
519             if (localName.equals("select") && (select==null)) {
520                 SelectImpl select = SelectImpl.newInstance();
521                 current = getCurrentUNode();
522                 select.setParentUNode(current);
523                 select.setCurrentUNode(select);
524                 this.setCurrentUNode(select);
525                 select.startElement(namespaceURI, localName, qName, atts);
526                 // Add this value in
527
this.select = select;
528                 return;
529             }
530             if (localName.equals("radio") && (radio==null)) {
531                 RadioImpl radio = RadioImpl.newInstance();
532                 current = getCurrentUNode();
533                 radio.setParentUNode(current);
534                 radio.setCurrentUNode(radio);
535                 this.setCurrentUNode(radio);
536                 radio.startElement(namespaceURI, localName, qName, atts);
537                 // Add this value in
538
this.radio = radio;
539                 return;
540             }
541             if (localName.equals("hidden") && (hidden==null)) {
542                 HiddenImpl hidden = HiddenImpl.newInstance();
543                 current = getCurrentUNode();
544                 hidden.setParentUNode(current);
545                 hidden.setCurrentUNode(hidden);
546                 this.setCurrentUNode(hidden);
547                 hidden.startElement(namespaceURI, localName, qName, atts);
548                 // Add this value in
549
this.hidden = hidden;
550                 return;
551             }
552             if (localName.equals("validator") && (validator==null)) {
553                 ValidatorImpl validator = ValidatorImpl.newInstance();
554                 current = getCurrentUNode();
555                 validator.setParentUNode(current);
556                 validator.setCurrentUNode(validator);
557                 this.setCurrentUNode(validator);
558                 validator.startElement(namespaceURI, localName, qName, atts);
559                 // Add this value in
560
this.validator = validator;
561                 return;
562             }
563         }
564     }
565
566     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
567                            String JavaDoc qName)
568         throws SAXException JavaDoc {
569
570         Unmarshallable current = getCurrentUNode();
571         if (current != this) {
572             current.endElement(namespaceURI, localName, qName);
573             return;
574         }
575
576         Unmarshallable parent = getCurrentUNode().getParentUNode();
577         if (parent != null) {
578             parent.setCurrentUNode(parent);
579         }
580     }
581
582     public void characters(char[] ch, int start, int len)
583         throws SAXException JavaDoc {
584
585         // Feed this to the correct ContentHandler
586
Unmarshallable current = getCurrentUNode();
587         if (current != this) {
588             current.characters(ch, start, len);
589             return;
590         }
591
592         String JavaDoc text = new String JavaDoc(ch, start, len);
593         text = text.trim();
594     }
595
596     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
597         // Currently no-op
598
}
599
600     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
601         if (errorHandler != null) {
602             errorHandler.warning(e);
603         }
604     }
605
606     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
607         if ((validate) && (!hasDTD)) {
608             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.");
609         }
610         if (errorHandler != null) {
611             errorHandler.error(e);
612         }
613     }
614
615     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
616         if ((validate) && (!hasDTD)) {
617             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.");
618         }
619         if (errorHandler != null) {
620             errorHandler.fatalError(e);
621         }
622     }
623
624     public void startDTD(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID)
625         throws SAXException JavaDoc {
626
627         if ((name == null) || (name.equals(""))) {
628             docTypeString = "";
629             return;
630         }
631
632         hasDTD = true;
633         StringBuffer JavaDoc docTypeSB = new StringBuffer JavaDoc();
634         boolean hasPublic = false;
635
636         docTypeSB.append("<!DOCTYPE ")
637                  .append(name);
638
639         if ((publicID != null) && (!publicID.equals(""))) {
640             docTypeSB.append(" PUBLIC \"")
641                      .append(publicID)
642                      .append("\"");
643             hasPublic = true;
644         }
645
646         if ((systemID != null) && (!systemID.equals(""))) {
647             if (!hasPublic) {
648                 docTypeSB.append(" SYSTEM");
649             }
650             docTypeSB.append(" \"")
651                      .append(systemID)
652                      .append("\"");
653
654         }
655
656         docTypeSB.append(">");
657
658         docTypeString = docTypeSB.toString();
659     }
660
661     public void endDTD() throws SAXException JavaDoc {
662         // Currently no-op
663
}
664
665     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
666         // Currently no-op
667
}
668
669     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
670         // Currently no-op
671
}
672
673     public void startCDATA() throws SAXException JavaDoc {
674         // Currently no-op
675
}
676
677     public void endCDATA() throws SAXException JavaDoc {
678         // Currently no-op
679
}
680
681 }
682
Popular Tags