KickJava   Java API By Example, From Geeks To Geeks.

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


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 EjbRefTypeImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, EjbRefType {
46
47     private String JavaDoc value;
48     private boolean zeus_ValueSet;
49     private String JavaDoc id;
50     private boolean zeus_IdSet;
51
52     /** Any DOCTYPE reference/statements. */
53     private String JavaDoc docTypeString;
54
55     /** The encoding for the output document */
56     private String JavaDoc outputEncoding;
57
58     /** The current node in unmarshalling */
59     private Unmarshallable zeus_currentUNode;
60
61     /** The parent node in unmarshalling */
62     private Unmarshallable zeus_parentUNode;
63
64     /** Whether this node has been handled */
65     private boolean zeus_thisNodeHandled = false;
66
67     /** Whether a DTD exists for an unmarshal call */
68     private boolean hasDTD;
69
70     /** Whether validation is occurring */
71     private boolean validate;
72
73     /** The namespace mappings on this element */
74     private Map JavaDoc namespaceMappings;
75
76     /** The EntityResolver for SAX parsing to use */
77     private static EntityResolver JavaDoc entityResolver;
78
79     /** The ErrorHandler for SAX parsing to use */
80     private static ErrorHandler JavaDoc errorHandler;
81
82     private static EjbRefTypeImpl prototype = null;
83
84     public static void setPrototype(EjbRefTypeImpl prototype) {
85         EjbRefTypeImpl.prototype = prototype;
86     }
87     public static EjbRefTypeImpl newInstance() {
88         try {
89             return (prototype!=null)?(EjbRefTypeImpl)prototype.clone(): new EjbRefTypeImpl();
90         } catch (CloneNotSupportedException JavaDoc e) {
91             return null; // never
92
}
93     }
94     public EjbRefTypeImpl() {
95         zeus_ValueSet = false;
96         zeus_IdSet = false;
97         docTypeString = "";
98         hasDTD = false;
99         validate = false;
100         namespaceMappings = new HashMap JavaDoc();
101     }
102
103     public String JavaDoc getValue() {
104         return value;
105     }
106
107     public void setValue(String JavaDoc value) {
108         this.value = value;
109         zeus_ValueSet = true;
110     }
111
112     public String JavaDoc getId() {
113         return id;
114     }
115
116     public void setId(String JavaDoc id) {
117         this.id = id;
118         zeus_IdSet = true;
119     }
120
121     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
122         try {
123             startDTD(name, publicID, systemID);
124         } catch (SAXException JavaDoc neverHappens) { }
125     }
126
127     public void setOutputEncoding(String JavaDoc outputEncoding) {
128         this.outputEncoding = outputEncoding;
129     }
130
131     public void marshal(File JavaDoc file) throws IOException JavaDoc {
132         // Delegate to the marshal(Writer) method
133
marshal(new FileWriter JavaDoc(file));
134     }
135
136     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
137         // Delegate to the marshal(Writer) method
138
marshal(new OutputStreamWriter JavaDoc(outputStream));
139     }
140
141     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
142         // Write out the XML declaration
143
writer.write("<?xml version=\"1.0\" ");
144         if (outputEncoding != null) {
145             writer.write("encoding=\"");
146             writer.write(outputEncoding);
147             writer.write("\"?>\n\n");
148
149         } else {
150             writer.write("encoding=\"UTF-8\"?>\n\n");
151
152         }
153         // Handle DOCTYPE declaration
154
writer.write(docTypeString);
155         writer.write("\n");
156         // Now start the recursive writing
157
writeXMLRepresentation(writer, "");
158
159         // Close up
160
writer.flush();
161         writer.close();
162     }
163
164     protected void writeXMLRepresentation(Writer JavaDoc writer,
165                                           String JavaDoc indent)
166         throws IOException JavaDoc {
167
168         writer.write(indent);
169         writer.write("<ejb-ref-type");
170
171         // Handle namespace mappings (if needed)
172
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
173             String JavaDoc prefix = (String JavaDoc)i.next();
174             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
175             writer.write(" xmlns");
176             if (!prefix.trim().equals("")) {
177                 writer.write(":");
178                 writer.write(prefix);
179             }
180             writer.write("=\"");
181             writer.write(uri);
182             writer.write("\"\n ");
183         }
184
185         // Handle attributes (if needed)
186
if (zeus_IdSet) {
187             writer.write(" id=\"");
188             writer.write(escapeAttributeValue(id));
189             writer.write("\"");
190         }
191         if (getValue() != null) {
192             // Handle PCDATA value
193
writer.write(">");
194             writer.write(escapeTextValue(getValue()));
195             writer.write("</ejb-ref-type>\n");
196         } else {
197             writer.write("/>\n");
198         }
199     }
200
201     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
202         String JavaDoc returnValue = attributeValue;
203         for (int i = 0; i < returnValue.length(); i++) {
204             char ch = returnValue.charAt(i);
205             if (ch == '"') {
206                 returnValue = new StringBuffer JavaDoc()
207                     .append(returnValue.substring(0, i))
208                     .append("&quot;")
209                     .append(returnValue.substring(i+1))
210                     .toString();
211             }
212         }
213         return returnValue;
214     }
215
216     private String JavaDoc escapeTextValue(String JavaDoc textValue) {
217         String JavaDoc returnValue = textValue;
218         for (int i = 0; i < returnValue.length(); i++) {
219             char ch = returnValue.charAt(i);
220             if (ch == '<') {
221                 returnValue = new StringBuffer JavaDoc()
222                     .append(returnValue.substring(0, i))
223                     .append("&lt;")
224                     .append(returnValue.substring(i+1))
225                     .toString();
226             } else if (ch == '>') {
227                 returnValue = new StringBuffer JavaDoc()
228                     .append(returnValue.substring(0, i))
229                     .append("&gt;")
230                     .append(returnValue.substring(i+1))
231                     .toString();
232             }
233         }
234         return returnValue;
235     }
236
237     /**
238      * <p>
239      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
240      * </p>
241      *
242      * @param resolver the entity resolver to use.
243      */

244     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
245         entityResolver = resolver;
246     }
247
248     /**
249      * <p>
250      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
251      * </p>
252      *
253      * @param handler the entity resolver to use.
254      */

255     public static void setErrorHandler(ErrorHandler JavaDoc handler) {
256         errorHandler = handler;
257     }
258
259     public static EjbRefType unmarshal(File JavaDoc file) throws IOException JavaDoc {
260         // Delegate to the unmarshal(Reader) method
261
return unmarshal(new FileReader JavaDoc(file));
262     }
263
264     public static EjbRefType unmarshal(File JavaDoc file, boolean validate) throws IOException JavaDoc {
265         // Delegate to the unmarshal(Reader) method
266
return unmarshal(new FileReader JavaDoc(file), validate);
267     }
268
269     public static EjbRefType unmarshal(InputStream JavaDoc inputStream) throws IOException JavaDoc {
270         // Delegate to the unmarshal(Reader) method
271
return unmarshal(new InputStreamReader JavaDoc(inputStream));
272     }
273
274     public static EjbRefType unmarshal(InputStream JavaDoc inputStream, boolean validate) throws IOException JavaDoc {
275         // Delegate to the unmarshal(Reader) method
276
return unmarshal(new InputStreamReader JavaDoc(inputStream), validate);
277     }
278
279     public static EjbRefType unmarshal(Reader JavaDoc reader) throws IOException JavaDoc {
280         // Delegate with default validation value
281
return unmarshal(reader, false);
282     }
283
284     public static EjbRefType unmarshal(Reader JavaDoc reader, boolean validate) throws IOException JavaDoc {
285         EjbRefTypeImpl ejbRefType = EjbRefTypeImpl.newInstance();
286         ejbRefType.setValidating(validate);
287         ejbRefType.setCurrentUNode(ejbRefType);
288         ejbRefType.setParentUNode(null);
289         // Load the XML parser
290
XMLReader JavaDoc parser = null;
291         String JavaDoc parserClass = System.getProperty("org.xml.sax.driver",
292             "org.apache.xerces.parsers.SAXParser");
293         try {
294             parser = XMLReaderFactory.createXMLReader(parserClass);
295
296             // Set entity resolver, if needed
297
if (entityResolver != null) {
298                 parser.setEntityResolver(entityResolver);
299             }
300
301             // Set error handler
302
parser.setErrorHandler(ejbRefType);
303
304             // Register lexical handler
305
parser.setProperty("http://xml.org/sax/properties/lexical-handler", ejbRefType);
306
307             // Register content handler
308
parser.setContentHandler(ejbRefType);
309         } catch (SAXException JavaDoc e) {
310             throw new IOException JavaDoc("Could not load XML parser: " +
311                 e.getMessage());
312         }
313
314         InputSource JavaDoc inputSource = new InputSource JavaDoc(reader);
315         try {
316             parser.setFeature("http://xml.org/sax/features/validation", new Boolean JavaDoc(validate).booleanValue());
317             parser.setFeature("http://xml.org/sax/features/namespaces", true);
318             parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
319             parser.parse(inputSource);
320         } catch (SAXException JavaDoc e) {
321             throw new IOException JavaDoc("Error parsing XML document: " +
322                 e.getMessage());
323         }
324
325         // Return the resultant object
326
return ejbRefType;
327     }
328
329     public Unmarshallable getParentUNode() {
330         return zeus_parentUNode;
331     }
332
333     public void setParentUNode(Unmarshallable parentUNode) {
334         this.zeus_parentUNode = parentUNode;
335     }
336
337     public Unmarshallable getCurrentUNode() {
338         return zeus_currentUNode;
339     }
340
341     public void setCurrentUNode(Unmarshallable currentUNode) {
342         this.zeus_currentUNode = currentUNode;
343     }
344
345     public void setValidating(boolean validate) {
346         this.validate = validate;
347     }
348
349     public void startDocument() throws SAXException JavaDoc {
350         // no-op
351
}
352
353     public void setDocumentLocator(Locator JavaDoc locator) {
354         // no-op
355
}
356
357     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
358         throws SAXException JavaDoc {
359         namespaceMappings.put(prefix, uri);
360     }
361
362     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
363                              String JavaDoc qName, org.xml.sax.Attributes JavaDoc atts)
364         throws SAXException JavaDoc {
365
366         // Feed this to the correct ContentHandler
367
Unmarshallable current = getCurrentUNode();
368         if (current != this) {
369             current.startElement(namespaceURI, localName, qName, atts);
370             return;
371         }
372
373         // See if we handle, or we delegate
374
if ((localName.equals("ejb-ref-type")) && (!zeus_thisNodeHandled)) {
375             // Handle ourselves
376
for (int i=0, len=atts.getLength(); i<len; i++) {
377                 String JavaDoc attName= atts.getLocalName(i);
378                 String JavaDoc attValue = atts.getValue(i);
379                 if (attName.equals("id")) {
380                     setId(attValue);
381                 }
382             }
383             zeus_thisNodeHandled = true;
384             return;
385         } else {
386             // Delegate handling
387
}
388     }
389
390     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
391                            String JavaDoc qName)
392         throws SAXException JavaDoc {
393
394         Unmarshallable current = getCurrentUNode();
395         if (current != this) {
396             current.endElement(namespaceURI, localName, qName);
397             return;
398         }
399
400         // Make sure we have textual value
401
if (this.value == null) {
402             this.value = "";
403         }
404         Unmarshallable parent = getCurrentUNode().getParentUNode();
405         if (parent != null) {
406             parent.setCurrentUNode(parent);
407         }
408     }
409
410     public void characters(char[] ch, int start, int len)
411         throws SAXException JavaDoc {
412
413         // Feed this to the correct ContentHandler
414
Unmarshallable current = getCurrentUNode();
415         if (current != this) {
416             current.characters(ch, start, len);
417             return;
418         }
419
420         String JavaDoc text = new String JavaDoc(ch, start, len);
421         if (this.value == null) {
422             this.value = text;
423         } else {
424             this.value = new StringBuffer JavaDoc(this.value).append(text).toString();
425         }
426     }
427
428     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
429         // Currently no-op
430
}
431
432     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
433         if (errorHandler != null) {
434             errorHandler.warning(e);
435         }
436     }
437
438     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
439         if ((validate) && (!hasDTD)) {
440             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.");
441         }
442         if (errorHandler != null) {
443             errorHandler.error(e);
444         }
445     }
446
447     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
448         if ((validate) && (!hasDTD)) {
449             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.");
450         }
451         if (errorHandler != null) {
452             errorHandler.fatalError(e);
453         }
454     }
455
456     public void startDTD(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID)
457         throws SAXException JavaDoc {
458
459         if ((name == null) || (name.equals(""))) {
460             docTypeString = "";
461             return;
462         }
463
464         hasDTD = true;
465         StringBuffer JavaDoc docTypeSB = new StringBuffer JavaDoc();
466         boolean hasPublic = false;
467
468         docTypeSB.append("<!DOCTYPE ")
469                  .append(name);
470
471         if ((publicID != null) && (!publicID.equals(""))) {
472             docTypeSB.append(" PUBLIC \"")
473                      .append(publicID)
474                      .append("\"");
475             hasPublic = true;
476         }
477
478         if ((systemID != null) && (!systemID.equals(""))) {
479             if (!hasPublic) {
480                 docTypeSB.append(" SYSTEM");
481             }
482             docTypeSB.append(" \"")
483                      .append(systemID)
484                      .append("\"");
485
486         }
487
488         docTypeSB.append(">");
489
490         docTypeString = docTypeSB.toString();
491     }
492
493     public void endDTD() throws SAXException JavaDoc {
494         // Currently no-op
495
}
496
497     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
498         // Currently no-op
499
}
500
501     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
502         // Currently no-op
503
}
504
505     public void startCDATA() throws SAXException JavaDoc {
506         // Currently no-op
507
}
508
509     public void endCDATA() throws SAXException JavaDoc {
510         // Currently no-op
511
}
512
513 }
514
Popular Tags