KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > business > xml > PathImpl


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

255     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
256         entityResolver = resolver;
257     }
258
259     /**
260      * <p>
261      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
262      * </p>
263      *
264      * @param handler the entity resolver to use.
265      */

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