KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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