001: package net.xoetrope.xml;
002:
003: import java.io.FileOutputStream;
004: import java.io.Reader;
005: import java.lang.reflect.Constructor;
006:
007: import net.xoetrope.debug.DebugLogger;
008: import net.xoetrope.xui.XProjectManager;
009: import net.xoetrope.xui.build.BuildProperties;
010:
011: /**
012: * A basic factory for setting XML parsers and createing new elements
013: * <p> Copyright (c) Xoetrope Ltd., 2002-2003</p>
014: * <p> $Revision: 1.10 $</p>
015: * <p> License: see License.txt</p>
016: */
017: public class XmlParserFactory {
018: private static Class xmlClass = null;
019: private static XmlParser xmlParser = null;
020: private static XmlWriter xmlWriter = null;
021:
022: public XmlParserFactory() {
023: }
024:
025: /**
026: * Get an instance of the parser factory
027: * @return
028: */
029: public static XmlParserFactory getInstance() {
030: return XProjectManager.getXmlParserFactory();
031: }
032:
033: /**
034: * Parse the input (without validation)
035: * @param input
036: * @return
037: */
038: public XmlElement parse(Reader input) {
039: return getParser().parse(input);
040: }
041:
042: /**
043: * Parse the input stream using a schema. The default NanoXml parser will
044: * not validate and so anoth JAXP conformant parser must be setup.
045: * @param input the input stream
046: * @param schemaSource the schmea against which to validate.
047: * @return
048: */
049: public XmlElement parse(Reader input, String schemaSource) {
050: return getParser().parse(input, schemaSource);
051: }
052:
053: /**
054: * Get a parser
055: * @return
056: */
057: public XmlParser getParser() {
058: if (xmlParser == null) {
059: // Try a default element type
060: try {
061: xmlParser = (XmlParser) (Class
062: .forName("net.xoetrope.xml.nanoxml.NanoXmlParser")
063: .newInstance());
064: } catch (InstantiationException ex) {
065: if (BuildProperties.DEBUG)
066: DebugLogger
067: .logError("XML parser class not be instantiated: "
068: + ex.getMessage());
069: } catch (IllegalAccessException ex) {
070: if (BuildProperties.DEBUG)
071: DebugLogger
072: .logError("XML parser class not be accessed: "
073: + ex.getMessage());
074: } catch (ClassNotFoundException ex) {
075: if (BuildProperties.DEBUG)
076: DebugLogger.logError("XML parser class not found: "
077: + ex.getMessage());
078: }
079: }
080:
081: return xmlParser;
082: }
083:
084: /**
085: * Get an XML writer
086: * @param fos the file output stream for the new writer
087: * @return the writer
088: */
089: public XmlWriter getWriter(FileOutputStream fos) {
090: if (xmlWriter == null) {
091: // Try a default element type
092: try {
093: Class cls = Class
094: .forName("net.xoetrope.xml.nanoxml.NanoXmlWriter");
095: Class partypes[] = new Class[1];
096: partypes[0] = FileOutputStream.class;
097: Constructor ct = cls.getConstructor(partypes);
098: Object arglist[] = new Object[1];
099: arglist[0] = fos;
100: Object retobj = ct.newInstance(arglist);
101: return xmlWriter = (XmlWriter) retobj;
102: } catch (Exception ex) {
103: if (BuildProperties.DEBUG)
104: DebugLogger
105: .logError("XML writer class not be constructed: "
106: + ex.getMessage());
107: }
108: }
109:
110: return xmlWriter;
111: }
112:
113: /**
114: * Create a new XML element iwth the specified name
115: * @param name the name of the element
116: * @return the new element
117: */
118: public XmlElement createXmlElement(String name) {
119: if (xmlClass == null) {
120: // Try a default element type
121: try {
122: xmlClass = Class
123: .forName("net.xoetrope.xml.nanoxml.NanoXmlElement");
124: } catch (ClassNotFoundException ex) {
125: if (BuildProperties.DEBUG)
126: DebugLogger.logError("XML class not found: "
127: + ex.getMessage());
128: }
129: }
130: XmlElement newElement = null;
131: try {
132: newElement = (XmlElement) xmlClass.newInstance();
133: newElement.setName(name);
134: return newElement;
135: } catch (IllegalAccessException ex) {
136: if (BuildProperties.DEBUG)
137: DebugLogger.logError("XML class not be accessed: "
138: + ex.getMessage());
139: } catch (InstantiationException ex) {
140: if (BuildProperties.DEBUG)
141: DebugLogger.logError("XML class not instantiated: "
142: + ex.getMessage());
143: }
144: return null;
145: }
146:
147: /**
148: * Register an xml element class. Any new elements will be instances of this class
149: * @param clazz
150: */
151: public void registerXmlClass(Class clazz) {
152: xmlClass = clazz;
153: }
154:
155: /**
156: * Register an XML parser
157: * @param parser
158: */
159: public void registerXmlParser(XmlParser parser) {
160: xmlParser = parser;
161: }
162:
163: /**
164: * Register an XML writer
165: * @param writer
166: */
167: public void registerXmlWriter(XmlWriter writer) {
168: xmlWriter = writer;
169: }
170: }
|