KickJava   Java API By Example, From Geeks To Geeks.

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


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

227     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
228         entityResolver = resolver;
229     }
230
231     /**
232      * <p>
233      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
234      * </p>
235      *
236      * @param handler the entity resolver to use.
237      */

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