KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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