001: /**
002: * This class was generated from a set of XML constraints
003: * by the Enhydra Zeus XML Data Binding Framework. All
004: * source code in this file is constructed specifically
005: * to work with other Zeus-generated classes. If you
006: * modify this file by hand, you run the risk of breaking
007: * this interoperation, as well as introducing errors in
008: * source code compilation.
009: *
010: * * * * * MODIFY THIS FILE AT YOUR OWN RISK * * * * *
011: *
012: * To find out more about the Enhydra Zeus framework, you
013: * can point your browser at <http://zeus.enhydra.org>
014: * where you can download releases, join and discuss Zeus
015: * on user and developer mailing lists, and access source
016: * code. Please report any bugs through that website.
017: */package org.enhydra.convert.xml;
018:
019: // Global Implementation Import Statements
020: import java.io.File;
021: import java.io.FileReader;
022: import java.io.FileWriter;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025: import java.io.IOException;
026: import java.io.OutputStream;
027: import java.io.OutputStreamWriter;
028: import java.io.Reader;
029: import java.io.Writer;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Map;
033: import org.xml.sax.EntityResolver;
034: import org.xml.sax.ErrorHandler;
035: import org.xml.sax.InputSource;
036: import org.xml.sax.Locator;
037: import org.xml.sax.SAXException;
038: import org.xml.sax.SAXParseException;
039: import org.xml.sax.XMLReader;
040: import org.xml.sax.ext.LexicalHandler;
041: import org.xml.sax.helpers.DefaultHandler;
042: import org.xml.sax.helpers.XMLReaderFactory;
043:
044: public class ErrorPageImpl extends DefaultHandler implements Cloneable,
045: Unmarshallable, LexicalHandler, ErrorPage {
046:
047: private ErrorCode errorCode;
048: private ExceptionType exceptionType;
049: private Location location;
050: private String id;
051: private boolean zeus_IdSet;
052:
053: /** Any DOCTYPE reference/statements. */
054: private String docTypeString;
055:
056: /** The encoding for the output document */
057: private String outputEncoding;
058:
059: /** The current node in unmarshalling */
060: private Unmarshallable zeus_currentUNode;
061:
062: /** The parent node in unmarshalling */
063: private Unmarshallable zeus_parentUNode;
064:
065: /** Whether this node has been handled */
066: private boolean zeus_this NodeHandled = false;
067:
068: /** Whether a DTD exists for an unmarshal call */
069: private boolean hasDTD;
070:
071: /** Whether validation is occurring */
072: private boolean validate;
073:
074: /** The namespace mappings on this element */
075: private Map namespaceMappings;
076:
077: /** The EntityResolver for SAX parsing to use */
078: private static EntityResolver entityResolver;
079:
080: /** The ErrorHandler for SAX parsing to use */
081: private static ErrorHandler errorHandler;
082:
083: private static ErrorPageImpl prototype = null;
084:
085: public static void setPrototype(ErrorPageImpl prototype) {
086: ErrorPageImpl.prototype = prototype;
087: }
088:
089: public static ErrorPageImpl newInstance() {
090: try {
091: return (prototype != null) ? (ErrorPageImpl) prototype
092: .clone() : new ErrorPageImpl();
093: } catch (CloneNotSupportedException e) {
094: return null; // never
095: }
096: }
097:
098: public ErrorPageImpl() {
099: zeus_IdSet = false;
100: docTypeString = "";
101: hasDTD = false;
102: validate = false;
103: namespaceMappings = new HashMap();
104: }
105:
106: public ErrorCode getErrorCode() {
107: return errorCode;
108: }
109:
110: public void setErrorCode(ErrorCode errorCode) {
111: this .errorCode = errorCode;
112: }
113:
114: public ExceptionType getExceptionType() {
115: return exceptionType;
116: }
117:
118: public void setExceptionType(ExceptionType exceptionType) {
119: this .exceptionType = exceptionType;
120: }
121:
122: public Location getLocation() {
123: return location;
124: }
125:
126: public void setLocation(Location location) {
127: this .location = location;
128: }
129:
130: public String getId() {
131: return id;
132: }
133:
134: public void setId(String id) {
135: this .id = id;
136: zeus_IdSet = true;
137: }
138:
139: public void setDocType(String name, String publicID, String systemID) {
140: try {
141: startDTD(name, publicID, systemID);
142: } catch (SAXException neverHappens) {
143: }
144: }
145:
146: public void setOutputEncoding(String outputEncoding) {
147: this .outputEncoding = outputEncoding;
148: }
149:
150: public void marshal(File file) throws IOException {
151: // Delegate to the marshal(Writer) method
152: marshal(new FileWriter(file));
153: }
154:
155: public void marshal(OutputStream outputStream) throws IOException {
156: // Delegate to the marshal(Writer) method
157: marshal(new OutputStreamWriter(outputStream));
158: }
159:
160: public void marshal(Writer writer) throws IOException {
161: // Write out the XML declaration
162: writer.write("<?xml version=\"1.0\" ");
163: if (outputEncoding != null) {
164: writer.write("encoding=\"");
165: writer.write(outputEncoding);
166: writer.write("\"?>\n\n");
167:
168: } else {
169: writer.write("encoding=\"UTF-8\"?>\n\n");
170:
171: }
172: // Handle DOCTYPE declaration
173: writer.write(docTypeString);
174: writer.write("\n");
175: // Now start the recursive writing
176: writeXMLRepresentation(writer, "");
177:
178: // Close up
179: writer.flush();
180: writer.close();
181: }
182:
183: protected void writeXMLRepresentation(Writer writer, String indent)
184: throws IOException {
185:
186: writer.write(indent);
187: writer.write("<error-page");
188:
189: // Handle namespace mappings (if needed)
190: for (Iterator i = namespaceMappings.keySet().iterator(); i
191: .hasNext();) {
192: String prefix = (String) i.next();
193: String uri = (String) namespaceMappings.get(prefix);
194: writer.write(" xmlns");
195: if (!prefix.trim().equals("")) {
196: writer.write(":");
197: writer.write(prefix);
198: }
199: writer.write("=\"");
200: writer.write(uri);
201: writer.write("\"\n ");
202: }
203:
204: // Handle attributes (if needed)
205: if (zeus_IdSet) {
206: writer.write(" id=\"");
207: writer.write(escapeAttributeValue(id));
208: writer.write("\"");
209: }
210: writer.write(">");
211: writer.write("\n");
212:
213: // Handle child elements
214: if (errorCode != null) {
215: ((ErrorCodeImpl) errorCode).writeXMLRepresentation(writer,
216: new StringBuffer(indent).append(" ").toString());
217: }
218:
219: if (exceptionType != null) {
220: ((ExceptionTypeImpl) exceptionType).writeXMLRepresentation(
221: writer, new StringBuffer(indent).append(" ")
222: .toString());
223: }
224:
225: if (location != null) {
226: ((LocationImpl) location).writeXMLRepresentation(writer,
227: new StringBuffer(indent).append(" ").toString());
228: }
229:
230: writer.write(indent);
231: writer.write("</error-page>\n");
232: }
233:
234: private String escapeAttributeValue(String attributeValue) {
235: String returnValue = attributeValue;
236: for (int i = 0; i < returnValue.length(); i++) {
237: char ch = returnValue.charAt(i);
238: if (ch == '"') {
239: returnValue = new StringBuffer().append(
240: returnValue.substring(0, i)).append(""")
241: .append(returnValue.substring(i + 1))
242: .toString();
243: }
244: }
245: return returnValue;
246: }
247:
248: private String escapeTextValue(String textValue) {
249: String returnValue = textValue;
250: for (int i = 0; i < returnValue.length(); i++) {
251: char ch = returnValue.charAt(i);
252: if (ch == '<') {
253: returnValue = new StringBuffer().append(
254: returnValue.substring(0, i)).append("<")
255: .append(returnValue.substring(i + 1))
256: .toString();
257: } else if (ch == '>') {
258: returnValue = new StringBuffer().append(
259: returnValue.substring(0, i)).append(">")
260: .append(returnValue.substring(i + 1))
261: .toString();
262: }
263: }
264: return returnValue;
265: }
266:
267: /**
268: * <p>
269: * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
270: * </p>
271: *
272: * @param resolver the entity resolver to use.
273: */
274: public static void setEntityResolver(EntityResolver resolver) {
275: entityResolver = resolver;
276: }
277:
278: /**
279: * <p>
280: * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
281: * </p>
282: *
283: * @param handler the entity resolver to use.
284: */
285: public static void setErrorHandler(ErrorHandler handler) {
286: errorHandler = handler;
287: }
288:
289: public static ErrorPage unmarshal(File file) throws IOException {
290: // Delegate to the unmarshal(Reader) method
291: return unmarshal(new FileReader(file));
292: }
293:
294: public static ErrorPage unmarshal(File file, boolean validate)
295: throws IOException {
296: // Delegate to the unmarshal(Reader) method
297: return unmarshal(new FileReader(file), validate);
298: }
299:
300: public static ErrorPage unmarshal(InputStream inputStream)
301: throws IOException {
302: // Delegate to the unmarshal(Reader) method
303: return unmarshal(new InputStreamReader(inputStream));
304: }
305:
306: public static ErrorPage unmarshal(InputStream inputStream,
307: boolean validate) throws IOException {
308: // Delegate to the unmarshal(Reader) method
309: return unmarshal(new InputStreamReader(inputStream), validate);
310: }
311:
312: public static ErrorPage unmarshal(Reader reader) throws IOException {
313: // Delegate with default validation value
314: return unmarshal(reader, false);
315: }
316:
317: public static ErrorPage unmarshal(Reader reader, boolean validate)
318: throws IOException {
319: ErrorPageImpl errorPage = ErrorPageImpl.newInstance();
320: errorPage.setValidating(validate);
321: errorPage.setCurrentUNode(errorPage);
322: errorPage.setParentUNode(null);
323: // Load the XML parser
324: XMLReader parser = null;
325: String parserClass = System.getProperty("org.xml.sax.driver",
326: "org.apache.xerces.parsers.SAXParser");
327: try {
328: parser = XMLReaderFactory.createXMLReader(parserClass);
329:
330: // Set entity resolver, if needed
331: if (entityResolver != null) {
332: parser.setEntityResolver(entityResolver);
333: }
334:
335: // Set error handler
336: parser.setErrorHandler(errorPage);
337:
338: // Register lexical handler
339: parser.setProperty(
340: "http://xml.org/sax/properties/lexical-handler",
341: errorPage);
342:
343: // Register content handler
344: parser.setContentHandler(errorPage);
345: } catch (SAXException e) {
346: throw new IOException("Could not load XML parser: "
347: + e.getMessage());
348: }
349:
350: InputSource inputSource = new InputSource(reader);
351: try {
352: parser.setFeature("http://xml.org/sax/features/validation",
353: new Boolean(validate).booleanValue());
354: parser.setFeature("http://xml.org/sax/features/namespaces",
355: true);
356: parser.setFeature(
357: "http://xml.org/sax/features/namespace-prefixes",
358: false);
359: parser.parse(inputSource);
360: } catch (SAXException e) {
361: throw new IOException("Error parsing XML document: "
362: + e.getMessage());
363: }
364:
365: // Return the resultant object
366: return errorPage;
367: }
368:
369: public Unmarshallable getParentUNode() {
370: return zeus_parentUNode;
371: }
372:
373: public void setParentUNode(Unmarshallable parentUNode) {
374: this .zeus_parentUNode = parentUNode;
375: }
376:
377: public Unmarshallable getCurrentUNode() {
378: return zeus_currentUNode;
379: }
380:
381: public void setCurrentUNode(Unmarshallable currentUNode) {
382: this .zeus_currentUNode = currentUNode;
383: }
384:
385: public void setValidating(boolean validate) {
386: this .validate = validate;
387: }
388:
389: public void startDocument() throws SAXException {
390: // no-op
391: }
392:
393: public void setDocumentLocator(Locator locator) {
394: // no-op
395: }
396:
397: public void startPrefixMapping(String prefix, String uri)
398: throws SAXException {
399: namespaceMappings.put(prefix, uri);
400: }
401:
402: public void startElement(String namespaceURI, String localName,
403: String qName, org.xml.sax.Attributes atts)
404: throws SAXException {
405:
406: // Feed this to the correct ContentHandler
407: Unmarshallable current = getCurrentUNode();
408: if (current != this ) {
409: current.startElement(namespaceURI, localName, qName, atts);
410: return;
411: }
412:
413: // See if we handle, or we delegate
414: if ((localName.equals("error-page")) && (!zeus_this NodeHandled)) {
415: // Handle ourselves
416: for (int i = 0, len = atts.getLength(); i < len; i++) {
417: String attName = atts.getLocalName(i);
418: String attValue = atts.getValue(i);
419: if (attName.equals("id")) {
420: setId(attValue);
421: }
422: }
423: zeus_this NodeHandled = true;
424: return;
425: } else {
426: // Delegate handling
427: if (localName.equals("error-code") && (errorCode == null)) {
428: ErrorCodeImpl errorCode = ErrorCodeImpl.newInstance();
429: current = getCurrentUNode();
430: errorCode.setParentUNode(current);
431: errorCode.setCurrentUNode(errorCode);
432: this .setCurrentUNode(errorCode);
433: errorCode.startElement(namespaceURI, localName, qName,
434: atts);
435: // Add this value in
436: this .errorCode = errorCode;
437: return;
438: }
439: if (localName.equals("exception-type")
440: && (exceptionType == null)) {
441: ExceptionTypeImpl exceptionType = ExceptionTypeImpl
442: .newInstance();
443: current = getCurrentUNode();
444: exceptionType.setParentUNode(current);
445: exceptionType.setCurrentUNode(exceptionType);
446: this .setCurrentUNode(exceptionType);
447: exceptionType.startElement(namespaceURI, localName,
448: qName, atts);
449: // Add this value in
450: this .exceptionType = exceptionType;
451: return;
452: }
453: if (localName.equals("location") && (location == null)) {
454: LocationImpl location = LocationImpl.newInstance();
455: current = getCurrentUNode();
456: location.setParentUNode(current);
457: location.setCurrentUNode(location);
458: this .setCurrentUNode(location);
459: location.startElement(namespaceURI, localName, qName,
460: atts);
461: // Add this value in
462: this .location = location;
463: return;
464: }
465: }
466: }
467:
468: public void endElement(String namespaceURI, String localName,
469: String qName) throws SAXException {
470:
471: Unmarshallable current = getCurrentUNode();
472: if (current != this ) {
473: current.endElement(namespaceURI, localName, qName);
474: return;
475: }
476:
477: Unmarshallable parent = getCurrentUNode().getParentUNode();
478: if (parent != null) {
479: parent.setCurrentUNode(parent);
480: }
481: }
482:
483: public void characters(char[] ch, int start, int len)
484: throws SAXException {
485:
486: // Feed this to the correct ContentHandler
487: Unmarshallable current = getCurrentUNode();
488: if (current != this ) {
489: current.characters(ch, start, len);
490: return;
491: }
492:
493: String text = new String(ch, start, len);
494: }
495:
496: public void comment(char ch[], int start, int len)
497: throws SAXException {
498: // Currently no-op
499: }
500:
501: public void warning(SAXParseException e) throws SAXException {
502: if (errorHandler != null) {
503: errorHandler.warning(e);
504: }
505: }
506:
507: public void error(SAXParseException e) throws SAXException {
508: if ((validate) && (!hasDTD)) {
509: throw new SAXException(
510: "Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference.");
511: }
512: if (errorHandler != null) {
513: errorHandler.error(e);
514: }
515: }
516:
517: public void fatalError(SAXParseException e) throws SAXException {
518: if ((validate) && (!hasDTD)) {
519: throw new SAXException(
520: "Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference.");
521: }
522: if (errorHandler != null) {
523: errorHandler.fatalError(e);
524: }
525: }
526:
527: public void startDTD(String name, String publicID, String systemID)
528: throws SAXException {
529:
530: if ((name == null) || (name.equals(""))) {
531: docTypeString = "";
532: return;
533: }
534:
535: hasDTD = true;
536: StringBuffer docTypeSB = new StringBuffer();
537: boolean hasPublic = false;
538:
539: docTypeSB.append("<!DOCTYPE ").append(name);
540:
541: if ((publicID != null) && (!publicID.equals(""))) {
542: docTypeSB.append(" PUBLIC \"").append(publicID)
543: .append("\"");
544: hasPublic = true;
545: }
546:
547: if ((systemID != null) && (!systemID.equals(""))) {
548: if (!hasPublic) {
549: docTypeSB.append(" SYSTEM");
550: }
551: docTypeSB.append(" \"").append(systemID).append("\"");
552:
553: }
554:
555: docTypeSB.append(">");
556:
557: docTypeString = docTypeSB.toString();
558: }
559:
560: public void endDTD() throws SAXException {
561: // Currently no-op
562: }
563:
564: public void startEntity(String name) throws SAXException {
565: // Currently no-op
566: }
567:
568: public void endEntity(String name) throws SAXException {
569: // Currently no-op
570: }
571:
572: public void startCDATA() throws SAXException {
573: // Currently no-op
574: }
575:
576: public void endCDATA() throws SAXException {
577: // Currently no-op
578: }
579:
580: }
|