KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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