KickJava   Java API By Example, From Geeks To Geeks.

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


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 Ref_idImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, Ref_id {
46
47     private String JavaDoc input_id;
48     private boolean zeus_Input_idSet;
49     private String JavaDoc type;
50     private static boolean zeus_TypeInitialized;
51     private static java.util.Vector JavaDoc zeus_validType;
52     private boolean zeus_TypeSet;
53     private String JavaDoc label_id;
54     private boolean zeus_Label_idSet;
55     private String JavaDoc name_id;
56     private boolean zeus_Name_idSet;
57     private String JavaDoc id;
58     private boolean zeus_IdSet;
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 Ref_idImpl prototype = null;
91
92     public static void setPrototype(Ref_idImpl prototype) {
93         Ref_idImpl.prototype = prototype;
94     }
95     public static Ref_idImpl newInstance() {
96         try {
97             return (prototype!=null)?(Ref_idImpl)prototype.clone(): new Ref_idImpl();
98         } catch (CloneNotSupportedException JavaDoc e) {
99             return null; // never
100
}
101     }
102     public Ref_idImpl() {
103         zeus_Input_idSet = false;
104         zeus_TypeInitialized = false;
105         zeus_validType = null;
106         zeus_TypeSet = false;
107         zeus_Label_idSet = false;
108         zeus_Name_idSet = false;
109         zeus_IdSet = false;
110         docTypeString = "";
111         hasDTD = false;
112         validate = false;
113         namespaceMappings = new HashMap JavaDoc();
114     }
115
116     public String JavaDoc getInput_id() {
117         return input_id;
118     }
119
120     public void setInput_id(String JavaDoc input_id) {
121         this.input_id = input_id;
122         zeus_Input_idSet = true;
123     }
124
125     public String JavaDoc getType() {
126         return type;
127     }
128
129     public void setType(String JavaDoc type)
130         throws IllegalArgumentException JavaDoc {
131         if (!zeus_TypeInitialized) {
132             zeus_validType = new java.util.Vector JavaDoc();
133             zeus_validType.addElement("text");
134             zeus_validType.addElement("textarea");
135             zeus_validType.addElement("select");
136             zeus_validType.addElement("radio");
137             zeus_validType.addElement("hidden");
138             zeus_TypeInitialized = true;
139         }
140         if (!zeus_validType.contains(type)) {
141               throw new IllegalArgumentException JavaDoc("Illegal value for attribute 'type'");
142         } else {
143               this.type = type;
144             this.zeus_TypeSet = true;
145         }
146     }
147
148     public String JavaDoc getLabel_id() {
149         return label_id;
150     }
151
152     public void setLabel_id(String JavaDoc label_id) {
153         this.label_id = label_id;
154         zeus_Label_idSet = true;
155     }
156
157     public String JavaDoc getName_id() {
158         return name_id;
159     }
160
161     public void setName_id(String JavaDoc name_id) {
162         this.name_id = name_id;
163         zeus_Name_idSet = true;
164     }
165
166     public String JavaDoc getId() {
167         return id;
168     }
169
170     public void setId(String JavaDoc id) {
171         this.id = id;
172         zeus_IdSet = true;
173     }
174
175     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
176         try {
177             startDTD(name, publicID, systemID);
178         } catch (SAXException JavaDoc neverHappens) { }
179     }
180
181     public void setOutputEncoding(String JavaDoc outputEncoding) {
182         this.outputEncoding = outputEncoding;
183     }
184
185     public void marshal(File JavaDoc file) throws IOException JavaDoc {
186         // Delegate to the marshal(Writer) method
187
marshal(new FileWriter JavaDoc(file));
188     }
189
190     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
191         // Delegate to the marshal(Writer) method
192
marshal(new OutputStreamWriter JavaDoc(outputStream));
193     }
194
195     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
196         // Write out the XML declaration
197
writer.write("<?xml version=\"1.0\" ");
198         if (outputEncoding != null) {
199             writer.write("encoding=\"");
200             writer.write(outputEncoding);
201             writer.write("\"?>\n\n");
202
203         } else {
204             writer.write("encoding=\"UTF-8\"?>\n\n");
205
206         }
207         // Handle DOCTYPE declaration
208
writer.write(docTypeString);
209         writer.write("\n");
210         // Now start the recursive writing
211
writeXMLRepresentation(writer, "");
212
213         // Close up
214
writer.flush();
215         writer.close();
216     }
217
218     protected void writeXMLRepresentation(Writer JavaDoc writer,
219                                           String JavaDoc indent)
220         throws IOException JavaDoc {
221
222         writer.write(indent);
223         writer.write("<ref_id");
224
225         // Handle namespace mappings (if needed)
226
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
227             String JavaDoc prefix = (String JavaDoc)i.next();
228             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
229             writer.write(" xmlns");
230             if (!prefix.trim().equals("")) {
231                 writer.write(":");
232                 writer.write(prefix);
233             }
234             writer.write("=\"");
235             writer.write(uri);
236             writer.write("\"\n ");
237         }
238
239         // Handle attributes (if needed)
240
if (zeus_Input_idSet) {
241             writer.write(" input_id=\"");
242             writer.write(escapeAttributeValue(input_id));
243             writer.write("\"");
244         }
245         if (zeus_TypeSet) {
246             writer.write(" type=\"");
247             writer.write(escapeAttributeValue(type));
248             writer.write("\"");
249         }
250         if (zeus_Label_idSet) {
251             writer.write(" label_id=\"");
252             writer.write(escapeAttributeValue(label_id));
253             writer.write("\"");
254         }
255         if (zeus_Name_idSet) {
256             writer.write(" name_id=\"");
257             writer.write(escapeAttributeValue(name_id));
258             writer.write("\"");
259         }
260         if (zeus_IdSet) {
261             writer.write(" id=\"");
262             writer.write(escapeAttributeValue(id));
263             writer.write("\"");
264         }
265         writer.write("/>\n");
266     }
267
268     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
269         String JavaDoc returnValue = attributeValue;
270         for (int i = 0; i < returnValue.length(); i++) {
271             char ch = returnValue.charAt(i);
272             if (ch == '"') {
273                 returnValue = new StringBuffer JavaDoc()
274                     .append(returnValue.substring(0, i))
275                     .append("&quot;")
276                     .append(returnValue.substring(i+1))
277                     .toString();
278             }
279         }
280         return returnValue;
281     }
282
283     private String JavaDoc escapeTextValue(String JavaDoc textValue) {
284         String JavaDoc returnValue = textValue;
285         for (int i = 0; i < returnValue.length(); i++) {
286             char ch = returnValue.charAt(i);
287             if (ch == '<') {
288                 returnValue = new StringBuffer JavaDoc()
289                     .append(returnValue.substring(0, i))
290                     .append("&lt;")
291                     .append(returnValue.substring(i+1))
292                     .toString();
293             } else if (ch == '>') {
294                 returnValue = new StringBuffer JavaDoc()
295                     .append(returnValue.substring(0, i))
296                     .append("&gt;")
297                     .append(returnValue.substring(i+1))
298                     .toString();
299             }
300         }
301         return returnValue;
302     }
303
304     /**
305      * <p>
306      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
307      * </p>
308      *
309      * @param resolver the entity resolver to use.
310      */

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

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