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