001: /*
002: Copyright (C) 2003 Together
003:
004: This library is free software; you can redistribute it and/or
005: modify it under the terms of the GNU Lesser General Public
006: License as published by the Free Software Foundation; either
007: version 2.1 of the License, or (at your option) any later version.
008:
009: This library is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: Lesser General Public License for more details.
013:
014: You should have received a copy of the GNU Lesser General Public
015: License along with this library; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package org.enhydra.xml;
020:
021: import java.io.File;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.util.Properties;
025:
026: //import org.apache.xerces.parsers.DOMParser;
027: import javax.xml.parsers.DocumentBuilderFactory;
028: import javax.xml.parsers.DocumentBuilder;
029: import org.xml.sax.ErrorHandler;
030: import org.xml.sax.SAXException;
031: import org.xml.sax.SAXParseException;
032: import javax.xml.parsers.ParserConfigurationException;
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Node;
035: import org.xml.sax.SAXException;
036:
037: /**
038: * @author Tweety
039: *
040: * A class for manipulating the entire xml file (reading, writing...).
041: *
042: * @version 1.0
043: */
044: public class XMLDocumentFactory {
045:
046: private static String[] properties = { "method", "version",
047: "encoding", "omit-xml-declaration", "standalone",
048: "doctype-public", "doctype-system", "indent", "media-type" };
049:
050: private static int METHOD = 0;
051: private static int VERSION = 1;
052: private static int ENCODING = 2;
053: private static int OMIT_XML_DECLARATION = 3;
054: private static int STANDALONE = 4;
055: private static int DOCTYPE_PUBLIC = 5;
056: private static int DOCTYPE_SYSTEM = 6;
057: private static int CDATA_SECTION_ELEMENTS = 7;
058: private static int INDENT = 8;
059: private static int MEDIA_TYPE = 9;
060:
061: /**
062: * xml file name.
063: */
064: private String fileName;
065:
066: /**
067: * Constructs an empty <code>XMLDocumentFactory</code>
068: */
069: public XMLDocumentFactory() {
070: }
071:
072: /**
073: * Constructs a <code>XMLDocumentFactory</code> with the given
074: * @param fileName as <code>String</code>
075: */
076: public XMLDocumentFactory(String fileName) {
077: this .fileName = fileName;
078: }
079:
080: /**
081: * Returns xml file name.
082: *
083: * @return xml file name.
084: */
085: public String getFileName() {
086: return this .fileName;
087: }
088:
089: /**
090: * Parses xml file with the given name and creates <code>Document</code>.
091: *
092: * @param fileName xml file name.
093: *
094: * @return document.
095: */
096: public static Document parse(String fileName) {
097: DocumentBuilderFactory factory = DocumentBuilderFactory
098: .newInstance();
099: try {
100: DocumentBuilder builder = factory.newDocumentBuilder();
101: builder.setErrorHandler(new UtilErrorHandler());
102: Document doc = builder.parse(fileName);
103: return doc;
104: } catch (SAXParseException e) {
105: e.printStackTrace();
106: } catch (ParserConfigurationException e) {
107: e.printStackTrace();
108: } catch (IOException e) {
109: e.printStackTrace();
110: } catch (SAXException e) {
111: e.printStackTrace();
112: }
113: return null;
114:
115: //OLD with apache xerces
116: // DOMParser parser = new DOMParser();
117: // try {
118: // parser.parse(fileName);
119: // return parser.getDocument();
120: // } catch (SAXException e) {
121: // e.printStackTrace();
122: // } catch (IOException e) {
123: // e.printStackTrace();
124: // }
125: // return null;
126: }
127:
128: /**
129: * Parses xml file and creates creates <code>Document</code>.
130: * @return object of document
131: */
132: public Document parse() {
133: DocumentBuilderFactory factory = DocumentBuilderFactory
134: .newInstance();
135: try {
136: DocumentBuilder builder = factory.newDocumentBuilder();
137: builder.setErrorHandler(new UtilErrorHandler());
138: Document doc = builder.parse(fileName);
139: return doc;
140: } catch (SAXParseException e) {
141: e.printStackTrace();
142: } catch (ParserConfigurationException e) {
143: e.printStackTrace();
144: } catch (IOException e) {
145: e.printStackTrace();
146: } catch (SAXException e) {
147: e.printStackTrace();
148: }
149: return null;
150:
151: //OLD with apache xerces
152: // DOMParser parser = new DOMParser();
153: // try {
154: // parser.parse(this.fileName);
155: // return parser.getDocument();
156: // } catch (SAXException e) {
157: // System.err.println("SAXException - bad xml format");
158: // } catch (IOException e) {
159: // }
160: // return null;
161: }
162:
163: /**
164: * Serializes node with all subnodes to the xml file with the given name,
165: * and with the <code>Properties</code> of the xml declaration.
166: *
167: * @param node root node of the document.
168: * @param fileName xml file name
169: * @param prop <code>Properties</code> of the xml declaration.
170: */
171: public static void serialize(Node node, String fileName,
172: Properties prop) {
173: String out = "<?xml version=\"1.0\"?>";
174: File file = new File(fileName);
175:
176: //serialize xml declaration
177: if (prop != null) {
178: out = "<?xml";
179: String str = "";
180: for (int i = 0; i < properties.length; i++) {
181: str = (String) prop.get(properties[i]);
182: if (str != null)
183: out += " " + properties[i] + "=\"" + str + "\"";
184: }
185: out += "?>";
186: }
187:
188: //serialize document
189: try {
190: FileOutputStream outStream = new FileOutputStream(file);
191: out += node.toString();
192: outStream.write(out.getBytes());
193: outStream.close();
194: } catch (Exception e) {
195: System.err.println("Error serializing file");
196: }
197: }
198:
199: /**
200: * Serializes node with all subnodes to the xml file
201: * with the default <code>Properties</code> of the xml declaration.
202: *
203: * @param node root node of the document.
204: */
205: public void serialize(Node node) {
206:
207: File file = new File(fileName);
208: try {
209: FileOutputStream outStream = new FileOutputStream(file);
210: outStream.write(node.toString().getBytes());
211: outStream.close();
212: } catch (Exception e) {
213: System.err.println("Error serializing file");
214: }
215: }
216:
217: static class UtilErrorHandler implements ErrorHandler {
218:
219: // throw SAXException for fatal errors
220: public void fatalError(SAXParseException exception)
221: throws SAXException {
222: throw new SAXException(exception);
223: }
224:
225: public void error(SAXParseException errorException)
226: throws SAXException {
227: throw new SAXException(errorException);
228: }
229:
230: // print any warnings
231: public void warning(SAXParseException warningError)
232: throws SAXException {
233: System.err.println("[Validation : Warning] URI = "
234: + warningError.getMessage());
235: }
236: }
237:
238: }
|