KickJava   Java API By Example, From Geeks To Geeks.

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


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

272     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
273         entityResolver = resolver;
274     }
275
276     /**
277      * <p>
278      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
279      * </p>
280      *
281      * @param handler the entity resolver to use.
282      */

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