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 DescriptionImpl extends DefaultHandler implements
045: Cloneable, Unmarshallable, LexicalHandler, Description {
046:
047: private String value;
048: private boolean zeus_ValueSet;
049: private String id;
050: private boolean zeus_IdSet;
051:
052: /** Any DOCTYPE reference/statements. */
053: private String docTypeString;
054:
055: /** The encoding for the output document */
056: private String outputEncoding;
057:
058: /** The current node in unmarshalling */
059: private Unmarshallable zeus_currentUNode;
060:
061: /** The parent node in unmarshalling */
062: private Unmarshallable zeus_parentUNode;
063:
064: /** Whether this node has been handled */
065: private boolean zeus_this NodeHandled = false;
066:
067: /** Whether a DTD exists for an unmarshal call */
068: private boolean hasDTD;
069:
070: /** Whether validation is occurring */
071: private boolean validate;
072:
073: /** The namespace mappings on this element */
074: private Map namespaceMappings;
075:
076: /** The EntityResolver for SAX parsing to use */
077: private static EntityResolver entityResolver;
078:
079: /** The ErrorHandler for SAX parsing to use */
080: private static ErrorHandler errorHandler;
081:
082: private static DescriptionImpl prototype = null;
083:
084: public static void setPrototype(DescriptionImpl prototype) {
085: DescriptionImpl.prototype = prototype;
086: }
087:
088: public static DescriptionImpl newInstance() {
089: try {
090: return (prototype != null) ? (DescriptionImpl) prototype
091: .clone() : new DescriptionImpl();
092: } catch (CloneNotSupportedException e) {
093: return null; // never
094: }
095: }
096:
097: public DescriptionImpl() {
098: zeus_ValueSet = false;
099: zeus_IdSet = false;
100: docTypeString = "";
101: hasDTD = false;
102: validate = false;
103: namespaceMappings = new HashMap();
104: }
105:
106: public String getValue() {
107: return value;
108: }
109:
110: public void setValue(String value) {
111: this .value = value;
112: zeus_ValueSet = true;
113: }
114:
115: public String getId() {
116: return id;
117: }
118:
119: public void setId(String id) {
120: this .id = id;
121: zeus_IdSet = true;
122: }
123:
124: public void setDocType(String name, String publicID, String systemID) {
125: try {
126: startDTD(name, publicID, systemID);
127: } catch (SAXException neverHappens) {
128: }
129: }
130:
131: public void setOutputEncoding(String outputEncoding) {
132: this .outputEncoding = outputEncoding;
133: }
134:
135: public void marshal(File file) throws IOException {
136: // Delegate to the marshal(Writer) method
137: marshal(new FileWriter(file));
138: }
139:
140: public void marshal(OutputStream outputStream) throws IOException {
141: // Delegate to the marshal(Writer) method
142: marshal(new OutputStreamWriter(outputStream));
143: }
144:
145: public void marshal(Writer writer) throws IOException {
146: // Write out the XML declaration
147: writer.write("<?xml version=\"1.0\" ");
148: if (outputEncoding != null) {
149: writer.write("encoding=\"");
150: writer.write(outputEncoding);
151: writer.write("\"?>\n\n");
152:
153: } else {
154: writer.write("encoding=\"UTF-8\"?>\n\n");
155:
156: }
157: // Handle DOCTYPE declaration
158: writer.write(docTypeString);
159: writer.write("\n");
160: // Now start the recursive writing
161: writeXMLRepresentation(writer, "");
162:
163: // Close up
164: writer.flush();
165: writer.close();
166: }
167:
168: protected void writeXMLRepresentation(Writer writer, String indent)
169: throws IOException {
170:
171: writer.write(indent);
172: writer.write("<description");
173:
174: // Handle namespace mappings (if needed)
175: for (Iterator i = namespaceMappings.keySet().iterator(); i
176: .hasNext();) {
177: String prefix = (String) i.next();
178: String uri = (String) namespaceMappings.get(prefix);
179: writer.write(" xmlns");
180: if (!prefix.trim().equals("")) {
181: writer.write(":");
182: writer.write(prefix);
183: }
184: writer.write("=\"");
185: writer.write(uri);
186: writer.write("\"\n ");
187: }
188:
189: // Handle attributes (if needed)
190: if (zeus_IdSet) {
191: writer.write(" id=\"");
192: writer.write(escapeAttributeValue(id));
193: writer.write("\"");
194: }
195: if (getValue() != null) {
196: // Handle PCDATA value
197: writer.write(">");
198: writer.write(escapeTextValue(getValue()));
199: writer.write("</description>\n");
200: } else {
201: writer.write("/>\n");
202: }
203: }
204:
205: private String escapeAttributeValue(String attributeValue) {
206: String returnValue = attributeValue;
207: for (int i = 0; i < returnValue.length(); i++) {
208: char ch = returnValue.charAt(i);
209: if (ch == '"') {
210: returnValue = new StringBuffer().append(
211: returnValue.substring(0, i)).append(""")
212: .append(returnValue.substring(i + 1))
213: .toString();
214: }
215: }
216: return returnValue;
217: }
218:
219: private String escapeTextValue(String textValue) {
220: String returnValue = textValue;
221: for (int i = 0; i < returnValue.length(); i++) {
222: char ch = returnValue.charAt(i);
223: if (ch == '<') {
224: returnValue = new StringBuffer().append(
225: returnValue.substring(0, i)).append("<")
226: .append(returnValue.substring(i + 1))
227: .toString();
228: } else if (ch == '>') {
229: returnValue = new StringBuffer().append(
230: returnValue.substring(0, i)).append(">")
231: .append(returnValue.substring(i + 1))
232: .toString();
233: }
234: }
235: return returnValue;
236: }
237:
238: /**
239: * <p>
240: * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
241: * </p>
242: *
243: * @param resolver the entity resolver to use.
244: */
245: public static void setEntityResolver(EntityResolver resolver) {
246: entityResolver = resolver;
247: }
248:
249: /**
250: * <p>
251: * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
252: * </p>
253: *
254: * @param handler the entity resolver to use.
255: */
256: public static void setErrorHandler(ErrorHandler handler) {
257: errorHandler = handler;
258: }
259:
260: public static Description unmarshal(File file) throws IOException {
261: // Delegate to the unmarshal(Reader) method
262: return unmarshal(new FileReader(file));
263: }
264:
265: public static Description unmarshal(File file, boolean validate)
266: throws IOException {
267: // Delegate to the unmarshal(Reader) method
268: return unmarshal(new FileReader(file), validate);
269: }
270:
271: public static Description unmarshal(InputStream inputStream)
272: throws IOException {
273: // Delegate to the unmarshal(Reader) method
274: return unmarshal(new InputStreamReader(inputStream));
275: }
276:
277: public static Description unmarshal(InputStream inputStream,
278: boolean validate) throws IOException {
279: // Delegate to the unmarshal(Reader) method
280: return unmarshal(new InputStreamReader(inputStream), validate);
281: }
282:
283: public static Description unmarshal(Reader reader)
284: throws IOException {
285: // Delegate with default validation value
286: return unmarshal(reader, false);
287: }
288:
289: public static Description unmarshal(Reader reader, boolean validate)
290: throws IOException {
291: DescriptionImpl description = DescriptionImpl.newInstance();
292: description.setValidating(validate);
293: description.setCurrentUNode(description);
294: description.setParentUNode(null);
295: // Load the XML parser
296: XMLReader parser = null;
297: String parserClass = System.getProperty("org.xml.sax.driver",
298: "org.apache.xerces.parsers.SAXParser");
299: try {
300: parser = XMLReaderFactory.createXMLReader(parserClass);
301:
302: // Set entity resolver, if needed
303: if (entityResolver != null) {
304: parser.setEntityResolver(entityResolver);
305: }
306:
307: // Set error handler
308: parser.setErrorHandler(description);
309:
310: // Register lexical handler
311: parser.setProperty(
312: "http://xml.org/sax/properties/lexical-handler",
313: description);
314:
315: // Register content handler
316: parser.setContentHandler(description);
317: } catch (SAXException e) {
318: throw new IOException("Could not load XML parser: "
319: + e.getMessage());
320: }
321:
322: InputSource inputSource = new InputSource(reader);
323: try {
324: parser.setFeature("http://xml.org/sax/features/validation",
325: new Boolean(validate).booleanValue());
326: parser.setFeature("http://xml.org/sax/features/namespaces",
327: true);
328: parser.setFeature(
329: "http://xml.org/sax/features/namespace-prefixes",
330: false);
331: parser.parse(inputSource);
332: } catch (SAXException e) {
333: throw new IOException("Error parsing XML document: "
334: + e.getMessage());
335: }
336:
337: // Return the resultant object
338: return description;
339: }
340:
341: public Unmarshallable getParentUNode() {
342: return zeus_parentUNode;
343: }
344:
345: public void setParentUNode(Unmarshallable parentUNode) {
346: this .zeus_parentUNode = parentUNode;
347: }
348:
349: public Unmarshallable getCurrentUNode() {
350: return zeus_currentUNode;
351: }
352:
353: public void setCurrentUNode(Unmarshallable currentUNode) {
354: this .zeus_currentUNode = currentUNode;
355: }
356:
357: public void setValidating(boolean validate) {
358: this .validate = validate;
359: }
360:
361: public void startDocument() throws SAXException {
362: // no-op
363: }
364:
365: public void setDocumentLocator(Locator locator) {
366: // no-op
367: }
368:
369: public void startPrefixMapping(String prefix, String uri)
370: throws SAXException {
371: namespaceMappings.put(prefix, uri);
372: }
373:
374: public void startElement(String namespaceURI, String localName,
375: String qName, org.xml.sax.Attributes atts)
376: throws SAXException {
377:
378: // Feed this to the correct ContentHandler
379: Unmarshallable current = getCurrentUNode();
380: if (current != this ) {
381: current.startElement(namespaceURI, localName, qName, atts);
382: return;
383: }
384:
385: // See if we handle, or we delegate
386: if ((localName.equals("description"))
387: && (!zeus_this NodeHandled)) {
388: // Handle ourselves
389: for (int i = 0, len = atts.getLength(); i < len; i++) {
390: String attName = atts.getLocalName(i);
391: String attValue = atts.getValue(i);
392: if (attName.equals("id")) {
393: setId(attValue);
394: }
395: }
396: zeus_this NodeHandled = true;
397: return;
398: } else {
399: // Delegate handling
400: }
401: }
402:
403: public void endElement(String namespaceURI, String localName,
404: String qName) throws SAXException {
405:
406: Unmarshallable current = getCurrentUNode();
407: if (current != this ) {
408: current.endElement(namespaceURI, localName, qName);
409: return;
410: }
411:
412: // Make sure we have textual value
413: if (this .value == null) {
414: this .value = "";
415: }
416: Unmarshallable parent = getCurrentUNode().getParentUNode();
417: if (parent != null) {
418: parent.setCurrentUNode(parent);
419: }
420: }
421:
422: public void characters(char[] ch, int start, int len)
423: throws SAXException {
424:
425: // Feed this to the correct ContentHandler
426: Unmarshallable current = getCurrentUNode();
427: if (current != this ) {
428: current.characters(ch, start, len);
429: return;
430: }
431:
432: String text = new String(ch, start, len);
433: if (this .value == null) {
434: this .value = text;
435: } else {
436: this .value = new StringBuffer(this .value).append(text)
437: .toString();
438: }
439: }
440:
441: public void comment(char ch[], int start, int len)
442: throws SAXException {
443: // Currently no-op
444: }
445:
446: public void warning(SAXParseException e) throws SAXException {
447: if (errorHandler != null) {
448: errorHandler.warning(e);
449: }
450: }
451:
452: public void error(SAXParseException e) throws SAXException {
453: if ((validate) && (!hasDTD)) {
454: throw new SAXException(
455: "Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference.");
456: }
457: if (errorHandler != null) {
458: errorHandler.error(e);
459: }
460: }
461:
462: public void fatalError(SAXParseException e) throws SAXException {
463: if ((validate) && (!hasDTD)) {
464: throw new SAXException(
465: "Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference.");
466: }
467: if (errorHandler != null) {
468: errorHandler.fatalError(e);
469: }
470: }
471:
472: public void startDTD(String name, String publicID, String systemID)
473: throws SAXException {
474:
475: if ((name == null) || (name.equals(""))) {
476: docTypeString = "";
477: return;
478: }
479:
480: hasDTD = true;
481: StringBuffer docTypeSB = new StringBuffer();
482: boolean hasPublic = false;
483:
484: docTypeSB.append("<!DOCTYPE ").append(name);
485:
486: if ((publicID != null) && (!publicID.equals(""))) {
487: docTypeSB.append(" PUBLIC \"").append(publicID)
488: .append("\"");
489: hasPublic = true;
490: }
491:
492: if ((systemID != null) && (!systemID.equals(""))) {
493: if (!hasPublic) {
494: docTypeSB.append(" SYSTEM");
495: }
496: docTypeSB.append(" \"").append(systemID).append("\"");
497:
498: }
499:
500: docTypeSB.append(">");
501:
502: docTypeString = docTypeSB.toString();
503: }
504:
505: public void endDTD() throws SAXException {
506: // Currently no-op
507: }
508:
509: public void startEntity(String name) throws SAXException {
510: // Currently no-op
511: }
512:
513: public void endEntity(String name) throws SAXException {
514: // Currently no-op
515: }
516:
517: public void startCDATA() throws SAXException {
518: // Currently no-op
519: }
520:
521: public void endCDATA() throws SAXException {
522: // Currently no-op
523: }
524:
525: }
|