KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > convert > xml > EnvEntryImpl


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.convert.xml;
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 EnvEntryImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, EnvEntry {
46
47     private Description description;
48     private EnvEntryName envEntryName;
49     private EnvEntryValue envEntryValue;
50     private EnvEntryType envEntryType;
51     private String JavaDoc id;
52     private boolean zeus_IdSet;
53
54     /** Any DOCTYPE reference/statements. */
55     private String JavaDoc docTypeString;
56
57     /** The encoding for the output document */
58     private String JavaDoc outputEncoding;
59
60     /** The current node in unmarshalling */
61     private Unmarshallable zeus_currentUNode;
62
63     /** The parent node in unmarshalling */
64     private Unmarshallable zeus_parentUNode;
65
66     /** Whether this node has been handled */
67     private boolean zeus_thisNodeHandled = false;
68
69     /** Whether a DTD exists for an unmarshal call */
70     private boolean hasDTD;
71
72     /** Whether validation is occurring */
73     private boolean validate;
74
75     /** The namespace mappings on this element */
76     private Map JavaDoc namespaceMappings;
77
78     /** The EntityResolver for SAX parsing to use */
79     private static EntityResolver JavaDoc entityResolver;
80
81     /** The ErrorHandler for SAX parsing to use */
82     private static ErrorHandler JavaDoc errorHandler;
83
84     private static EnvEntryImpl prototype = null;
85
86     public static void setPrototype(EnvEntryImpl prototype) {
87         EnvEntryImpl.prototype = prototype;
88     }
89     public static EnvEntryImpl newInstance() {
90         try {
91             return (prototype!=null)?(EnvEntryImpl)prototype.clone(): new EnvEntryImpl();
92         } catch (CloneNotSupportedException JavaDoc e) {
93             return null; // never
94
}
95     }
96     public EnvEntryImpl() {
97         zeus_IdSet = false;
98         docTypeString = "";
99         hasDTD = false;
100         validate = false;
101         namespaceMappings = new HashMap JavaDoc();
102     }
103
104     public Description getDescription() {
105         return description;
106     }
107
108     public void setDescription(Description description) {
109         this.description = description;
110     }
111
112     public EnvEntryName getEnvEntryName() {
113         return envEntryName;
114     }
115
116     public void setEnvEntryName(EnvEntryName envEntryName) {
117         this.envEntryName = envEntryName;
118     }
119
120     public EnvEntryValue getEnvEntryValue() {
121         return envEntryValue;
122     }
123
124     public void setEnvEntryValue(EnvEntryValue envEntryValue) {
125         this.envEntryValue = envEntryValue;
126     }
127
128     public EnvEntryType getEnvEntryType() {
129         return envEntryType;
130     }
131
132     public void setEnvEntryType(EnvEntryType envEntryType) {
133         this.envEntryType = envEntryType;
134     }
135
136     public String JavaDoc getId() {
137         return id;
138     }
139
140     public void setId(String JavaDoc id) {
141         this.id = id;
142         zeus_IdSet = true;
143     }
144
145     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
146         try {
147             startDTD(name, publicID, systemID);
148         } catch (SAXException JavaDoc neverHappens) { }
149     }
150
151     public void setOutputEncoding(String JavaDoc outputEncoding) {
152         this.outputEncoding = outputEncoding;
153     }
154
155     public void marshal(File JavaDoc file) throws IOException JavaDoc {
156         // Delegate to the marshal(Writer) method
157
marshal(new FileWriter JavaDoc(file));
158     }
159
160     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
161         // Delegate to the marshal(Writer) method
162
marshal(new OutputStreamWriter JavaDoc(outputStream));
163     }
164
165     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
166         // Write out the XML declaration
167
writer.write("<?xml version=\"1.0\" ");
168         if (outputEncoding != null) {
169             writer.write("encoding=\"");
170             writer.write(outputEncoding);
171             writer.write("\"?>\n\n");
172
173         } else {
174             writer.write("encoding=\"UTF-8\"?>\n\n");
175
176         }
177         // Handle DOCTYPE declaration
178
writer.write(docTypeString);
179         writer.write("\n");
180         // Now start the recursive writing
181
writeXMLRepresentation(writer, "");
182
183         // Close up
184
writer.flush();
185         writer.close();
186     }
187
188     protected void writeXMLRepresentation(Writer JavaDoc writer,
189                                           String JavaDoc indent)
190         throws IOException JavaDoc {
191
192         writer.write(indent);
193         
194         /* Slobodan Vujasinovic
195          * Addition to Zeus generated source
196          */

197         if (description != null) {
198             if (!description.getValue().equals("")){
199               writer.write("\n<!--\n" + description.getValue() + "\n-->\n ");
200               description.setValue("");
201             }
202         }
203         
204         writer.write("<env-entry");
205
206         // Handle namespace mappings (if needed)
207
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
208             String JavaDoc prefix = (String JavaDoc)i.next();
209             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
210             writer.write(" xmlns");
211             if (!prefix.trim().equals("")) {
212                 writer.write(":");
213                 writer.write(prefix);
214             }
215             writer.write("=\"");
216             writer.write(uri);
217             writer.write("\"\n ");
218         }
219
220         // Handle attributes (if needed)
221
if (zeus_IdSet) {
222             writer.write(" id=\"");
223             writer.write(escapeAttributeValue(id));
224             writer.write("\"");
225         }
226         writer.write(">");
227         writer.write("\n");
228
229         // Handle child elements
230
if (description != null) {
231             ((DescriptionImpl)description).writeXMLRepresentation(writer,
232                 new StringBuffer JavaDoc(indent).append(" ").toString());
233         }
234
235         if (envEntryName != null) {
236             ((EnvEntryNameImpl)envEntryName).writeXMLRepresentation(writer,
237                 new StringBuffer JavaDoc(indent).append(" ").toString());
238         }
239
240         if (envEntryValue != null) {
241             ((EnvEntryValueImpl)envEntryValue).writeXMLRepresentation(writer,
242                 new StringBuffer JavaDoc(indent).append(" ").toString());
243         }
244
245         if (envEntryType != null) {
246             ((EnvEntryTypeImpl)envEntryType).writeXMLRepresentation(writer,
247                 new StringBuffer JavaDoc(indent).append(" ").toString());
248         }
249
250         writer.write(indent);
251         writer.write("</env-entry>\n");
252     }
253
254     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
255         String JavaDoc returnValue = attributeValue;
256         for (int i = 0; i < returnValue.length(); i++) {
257             char ch = returnValue.charAt(i);
258             if (ch == '"') {
259                 returnValue = new StringBuffer JavaDoc()
260                     .append(returnValue.substring(0, i))
261                     .append("&quot;")
262                     .append(returnValue.substring(i+1))
263                     .toString();
264             }
265         }
266         return returnValue;
267     }
268
269     private String JavaDoc escapeTextValue(String JavaDoc textValue) {
270         String JavaDoc returnValue = textValue;
271         for (int i = 0; i < returnValue.length(); i++) {
272             char ch = returnValue.charAt(i);
273             if (ch == '<') {
274                 returnValue = new StringBuffer JavaDoc()
275                     .append(returnValue.substring(0, i))
276                     .append("&lt;")
277                     .append(returnValue.substring(i+1))
278                     .toString();
279             } else if (ch == '>') {
280                 returnValue = new StringBuffer JavaDoc()
281                     .append(returnValue.substring(0, i))
282                     .append("&gt;")
283                     .append(returnValue.substring(i+1))
284                     .toString();
285             }
286         }
287         return returnValue;
288     }
289
290     /**
291      * <p>
292      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
293      * </p>
294      *
295      * @param resolver the entity resolver to use.
296      */

297     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
298         entityResolver = resolver;
299     }
300
301     /**
302      * <p>
303      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
304      * </p>
305      *
306      * @param handler the entity resolver to use.
307      */

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