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 LocalImpl extends DefaultHandler implements Cloneable,
045: Unmarshallable, LexicalHandler, Local {
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 LocalImpl prototype = null;
083:
084: public static void setPrototype(LocalImpl prototype) {
085: LocalImpl.prototype = prototype;
086: }
087:
088: public static LocalImpl newInstance() {
089: try {
090: return (prototype != null) ? (LocalImpl) prototype.clone()
091: : new LocalImpl();
092: } catch (CloneNotSupportedException e) {
093: return null; // never
094: }
095: }
096:
097: public LocalImpl() {
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("<local");
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("</local>\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 Local unmarshal(File file) throws IOException {
261: // Delegate to the unmarshal(Reader) method
262: return unmarshal(new FileReader(file));
263: }
264:
265: public static Local 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 Local unmarshal(InputStream inputStream)
272: throws IOException {
273: // Delegate to the unmarshal(Reader) method
274: return unmarshal(new InputStreamReader(inputStream));
275: }
276:
277: public static Local 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 Local unmarshal(Reader reader) throws IOException {
284: // Delegate with default validation value
285: return unmarshal(reader, false);
286: }
287:
288: public static Local unmarshal(Reader reader, boolean validate)
289: throws IOException {
290: LocalImpl local = LocalImpl.newInstance();
291: local.setValidating(validate);
292: local.setCurrentUNode(local);
293: local.setParentUNode(null);
294: // Load the XML parser
295: XMLReader parser = null;
296: String parserClass = System.getProperty("org.xml.sax.driver",
297: "org.apache.xerces.parsers.SAXParser");
298: try {
299: parser = XMLReaderFactory.createXMLReader(parserClass);
300:
301: // Set entity resolver, if needed
302: if (entityResolver != null) {
303: parser.setEntityResolver(entityResolver);
304: }
305:
306: // Set error handler
307: parser.setErrorHandler(local);
308:
309: // Register lexical handler
310: parser.setProperty(
311: "http://xml.org/sax/properties/lexical-handler",
312: local);
313:
314: // Register content handler
315: parser.setContentHandler(local);
316: } catch (SAXException e) {
317: throw new IOException("Could not load XML parser: "
318: + e.getMessage());
319: }
320:
321: InputSource inputSource = new InputSource(reader);
322: try {
323: parser.setFeature("http://xml.org/sax/features/validation",
324: new Boolean(validate).booleanValue());
325: parser.setFeature("http://xml.org/sax/features/namespaces",
326: true);
327: parser.setFeature(
328: "http://xml.org/sax/features/namespace-prefixes",
329: false);
330: parser.parse(inputSource);
331: } catch (SAXException e) {
332: throw new IOException("Error parsing XML document: "
333: + e.getMessage());
334: }
335:
336: // Return the resultant object
337: return local;
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 {
361: // no-op
362: }
363:
364: public void setDocumentLocator(Locator locator) {
365: // no-op
366: }
367:
368: public void startPrefixMapping(String prefix, String uri)
369: throws SAXException {
370: namespaceMappings.put(prefix, uri);
371: }
372:
373: public void startElement(String namespaceURI, String localName,
374: String qName, org.xml.sax.Attributes atts)
375: throws SAXException {
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("local")) && (!zeus_this NodeHandled)) {
386: // Handle ourselves
387: for (int i = 0, len = atts.getLength(); i < len; i++) {
388: String attName = atts.getLocalName(i);
389: String attValue = atts.getValue(i);
390: if (attName.equals("id")) {
391: setId(attValue);
392: }
393: }
394: zeus_this NodeHandled = true;
395: return;
396: } else {
397: // Delegate handling
398: }
399: }
400:
401: public void endElement(String namespaceURI, String localName,
402: String qName) throws SAXException {
403:
404: Unmarshallable current = getCurrentUNode();
405: if (current != this ) {
406: current.endElement(namespaceURI, localName, qName);
407: return;
408: }
409:
410: // Make sure we have textual value
411: if (this .value == null) {
412: this .value = "";
413: }
414: Unmarshallable parent = getCurrentUNode().getParentUNode();
415: if (parent != null) {
416: parent.setCurrentUNode(parent);
417: }
418: }
419:
420: public void characters(char[] ch, int start, int len)
421: throws SAXException {
422:
423: // Feed this to the correct ContentHandler
424: Unmarshallable current = getCurrentUNode();
425: if (current != this ) {
426: current.characters(ch, start, len);
427: return;
428: }
429:
430: String text = new String(ch, start, len);
431: if (this .value == null) {
432: this .value = text;
433: } else {
434: this .value = new StringBuffer(this .value).append(text)
435: .toString();
436: }
437: }
438:
439: public void comment(char ch[], int start, int len)
440: throws SAXException {
441: // Currently no-op
442: }
443:
444: public void warning(SAXParseException e) throws SAXException {
445: if (errorHandler != null) {
446: errorHandler.warning(e);
447: }
448: }
449:
450: public void error(SAXParseException e) throws SAXException {
451: if ((validate) && (!hasDTD)) {
452: throw new SAXException(
453: "Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference.");
454: }
455: if (errorHandler != null) {
456: errorHandler.error(e);
457: }
458: }
459:
460: public void fatalError(SAXParseException e) throws SAXException {
461: if ((validate) && (!hasDTD)) {
462: throw new SAXException(
463: "Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference.");
464: }
465: if (errorHandler != null) {
466: errorHandler.fatalError(e);
467: }
468: }
469:
470: public void startDTD(String name, String publicID, String systemID)
471: throws SAXException {
472:
473: if ((name == null) || (name.equals(""))) {
474: docTypeString = "";
475: return;
476: }
477:
478: hasDTD = true;
479: StringBuffer docTypeSB = new StringBuffer();
480: boolean hasPublic = false;
481:
482: docTypeSB.append("<!DOCTYPE ").append(name);
483:
484: if ((publicID != null) && (!publicID.equals(""))) {
485: docTypeSB.append(" PUBLIC \"").append(publicID)
486: .append("\"");
487: hasPublic = true;
488: }
489:
490: if ((systemID != null) && (!systemID.equals(""))) {
491: if (!hasPublic) {
492: docTypeSB.append(" SYSTEM");
493: }
494: docTypeSB.append(" \"").append(systemID).append("\"");
495:
496: }
497:
498: docTypeSB.append(">");
499:
500: docTypeString = docTypeSB.toString();
501: }
502:
503: public void endDTD() throws SAXException {
504: // Currently no-op
505: }
506:
507: public void startEntity(String name) throws SAXException {
508: // Currently no-op
509: }
510:
511: public void endEntity(String name) throws SAXException {
512: // Currently no-op
513: }
514:
515: public void startCDATA() throws SAXException {
516: // Currently no-op
517: }
518:
519: public void endCDATA() throws SAXException {
520: // Currently no-op
521: }
522:
523: }
|