KickJava   Java API By Example, From Geeks To Geeks.

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


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 FormImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, Form {
51
52     private List JavaDoc ref_idList;
53     private InsertBefore insertBefore;
54     private AppendChild appendChild;
55     private List JavaDoc elementList;
56     private List JavaDoc validatorList;
57     private String JavaDoc repeat_prefix;
58     private boolean zeus_Repeat_prefixSet;
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 FormImpl prototype = null;
91
92     public static void setPrototype(FormImpl prototype) {
93         FormImpl.prototype = prototype;
94     }
95     public static FormImpl newInstance() {
96         try {
97             return (prototype!=null)?(FormImpl)prototype.clone(): new FormImpl();
98         } catch (CloneNotSupportedException JavaDoc e) {
99             return null; // never
100
}
101     }
102     public FormImpl() {
103         ref_idList = new LinkedList JavaDoc();
104         elementList = new LinkedList JavaDoc();
105         validatorList = new LinkedList JavaDoc();
106         zeus_Repeat_prefixSet = false;
107         docTypeString = "";
108         hasDTD = false;
109         validate = false;
110         namespaceMappings = new HashMap JavaDoc();
111     }
112
113     public List JavaDoc getRef_idList() {
114         return ref_idList;
115     }
116
117     public void setRef_idList(List JavaDoc ref_idList) {
118         this.ref_idList = ref_idList;
119     }
120
121     public void addRef_id(Ref_id ref_id) {
122         ref_idList.add(ref_id);
123     }
124
125     public void removeRef_id(Ref_id ref_id) {
126         ref_idList.remove(ref_id);
127     }
128
129     public InsertBefore getInsertBefore() {
130         return insertBefore;
131     }
132
133     public void setInsertBefore(InsertBefore insertBefore) {
134         this.insertBefore = insertBefore;
135     }
136
137     public AppendChild getAppendChild() {
138         return appendChild;
139     }
140
141     public void setAppendChild(AppendChild appendChild) {
142         this.appendChild = appendChild;
143     }
144
145     public List JavaDoc getElementList() {
146         return elementList;
147     }
148
149     public void setElementList(List JavaDoc elementList) {
150         this.elementList = elementList;
151     }
152
153     public void addElement(Element element) {
154         elementList.add(element);
155     }
156
157     public void removeElement(Element element) {
158         elementList.remove(element);
159     }
160
161     public List JavaDoc getValidatorList() {
162         return validatorList;
163     }
164
165     public void setValidatorList(List JavaDoc validatorList) {
166         this.validatorList = validatorList;
167     }
168
169     public void addValidator(Validator validator) {
170         validatorList.add(validator);
171     }
172
173     public void removeValidator(Validator validator) {
174         validatorList.remove(validator);
175     }
176
177     public String JavaDoc getRepeat_prefix() {
178         return repeat_prefix;
179     }
180
181     public void setRepeat_prefix(String JavaDoc repeat_prefix) {
182         this.repeat_prefix = repeat_prefix;
183         zeus_Repeat_prefixSet = true;
184     }
185
186     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
187         try {
188             startDTD(name, publicID, systemID);
189         } catch (SAXException JavaDoc neverHappens) { }
190     }
191
192     public void setOutputEncoding(String JavaDoc outputEncoding) {
193         this.outputEncoding = outputEncoding;
194     }
195
196     public void marshal(File JavaDoc file) throws IOException JavaDoc {
197         // Delegate to the marshal(Writer) method
198
marshal(new FileWriter JavaDoc(file));
199     }
200
201     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
202         // Delegate to the marshal(Writer) method
203
marshal(new OutputStreamWriter JavaDoc(outputStream));
204     }
205
206     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
207         // Write out the XML declaration
208
writer.write("<?xml version=\"1.0\" ");
209         if (outputEncoding != null) {
210             writer.write("encoding=\"");
211             writer.write(outputEncoding);
212             writer.write("\"?>\n\n");
213
214         } else {
215             writer.write("encoding=\"UTF-8\"?>\n\n");
216
217         }
218         // Handle DOCTYPE declaration
219
writer.write(docTypeString);
220         writer.write("\n");
221         // Now start the recursive writing
222
writeXMLRepresentation(writer, "");
223
224         // Close up
225
writer.flush();
226         writer.close();
227     }
228
229     protected void writeXMLRepresentation(Writer JavaDoc writer,
230                                           String JavaDoc indent)
231         throws IOException JavaDoc {
232
233         writer.write(indent);
234         writer.write("<form");
235
236         // Handle namespace mappings (if needed)
237
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
238             String JavaDoc prefix = (String JavaDoc)i.next();
239             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
240             writer.write(" xmlns");
241             if (!prefix.trim().equals("")) {
242                 writer.write(":");
243                 writer.write(prefix);
244             }
245             writer.write("=\"");
246             writer.write(uri);
247             writer.write("\"\n ");
248         }
249
250         // Handle attributes (if needed)
251
if (zeus_Repeat_prefixSet) {
252             writer.write(" repeat_prefix=\"");
253             writer.write(escapeAttributeValue(repeat_prefix));
254             writer.write("\"");
255         }
256         writer.write(">");
257         writer.write("\n");
258
259         // Handle child elements
260
for (Iterator JavaDoc i=ref_idList.iterator(); i.hasNext(); ) {
261             Ref_idImpl ref_id = (Ref_idImpl)i.next();
262             ref_id.writeXMLRepresentation(writer,
263                 new StringBuffer JavaDoc(indent).append(" ").toString());
264         }
265         if (insertBefore != null) {
266             ((InsertBeforeImpl)insertBefore).writeXMLRepresentation(writer,
267                 new StringBuffer JavaDoc(indent).append(" ").toString());
268         }
269
270         if (appendChild != null) {
271             ((AppendChildImpl)appendChild).writeXMLRepresentation(writer,
272                 new StringBuffer JavaDoc(indent).append(" ").toString());
273         }
274
275         for (Iterator JavaDoc i=elementList.iterator(); i.hasNext(); ) {
276             ElementImpl element = (ElementImpl)i.next();
277             element.writeXMLRepresentation(writer,
278                 new StringBuffer JavaDoc(indent).append(" ").toString());
279         }
280         for (Iterator JavaDoc i=validatorList.iterator(); i.hasNext(); ) {
281             ValidatorImpl validator = (ValidatorImpl)i.next();
282             validator.writeXMLRepresentation(writer,
283                 new StringBuffer JavaDoc(indent).append(" ").toString());
284         }
285         writer.write(indent);
286         writer.write("</form>\n");
287     }
288
289     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
290         String JavaDoc returnValue = attributeValue;
291         for (int i = 0; i < returnValue.length(); i++) {
292             char ch = returnValue.charAt(i);
293             if (ch == '"') {
294                 returnValue = new StringBuffer JavaDoc()
295                     .append(returnValue.substring(0, i))
296                     .append("&quot;")
297                     .append(returnValue.substring(i+1))
298                     .toString();
299             }
300         }
301         return returnValue;
302     }
303
304     private String JavaDoc escapeTextValue(String JavaDoc textValue) {
305         String JavaDoc returnValue = textValue;
306         for (int i = 0; i < returnValue.length(); i++) {
307             char ch = returnValue.charAt(i);
308             if (ch == '<') {
309                 returnValue = new StringBuffer JavaDoc()
310                     .append(returnValue.substring(0, i))
311                     .append("&lt;")
312                     .append(returnValue.substring(i+1))
313                     .toString();
314             } else if (ch == '>') {
315                 returnValue = new StringBuffer JavaDoc()
316                     .append(returnValue.substring(0, i))
317                     .append("&gt;")
318                     .append(returnValue.substring(i+1))
319                     .toString();
320             }
321         }
322         return returnValue;
323     }
324
325     /**
326      * <p>
327      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
328      * </p>
329      *
330      * @param resolver the entity resolver to use.
331      */

332     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
333         entityResolver = resolver;
334     }
335
336     /**
337      * <p>
338      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
339      * </p>
340      *
341      * @param handler the entity resolver to use.
342      */

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