KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > XMLDocumentFactory


1 package org.enhydra.xml;
2
3 import java.io.File JavaDoc;
4 import java.io.FileOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.util.Properties JavaDoc;
7
8 import javax.xml.parsers.DocumentBuilder JavaDoc;
9 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
10 import javax.xml.parsers.ParserConfigurationException JavaDoc;
11
12 import org.w3c.dom.Document JavaDoc;
13 import org.w3c.dom.Node JavaDoc;
14 import org.xml.sax.ErrorHandler JavaDoc;
15 import org.xml.sax.SAXException JavaDoc;
16 import org.xml.sax.SAXParseException JavaDoc;
17
18
19
20 /**
21  * @author Tweety
22  *
23  * A class for manipulating the entire xml file (reading, writing...).
24  *
25  * @version 1.0
26  */

27 public class XMLDocumentFactory {
28
29    private static String JavaDoc[] properties = {
30             "method",
31             "version",
32             "encoding",
33             "omit-xml-declaration",
34             "standalone",
35             "doctype-public",
36             "doctype-system",
37             "indent",
38             "media-type"
39    };
40
41
42    private static int METHOD = 0;
43    private static int VERSION = 1;
44    private static int ENCODING = 2;
45    private static int OMIT_XML_DECLARATION = 3;
46    private static int STANDALONE = 4;
47    private static int DOCTYPE_PUBLIC = 5;
48    private static int DOCTYPE_SYSTEM = 6;
49    private static int CDATA_SECTION_ELEMENTS = 7;
50    private static int INDENT = 8;
51    private static int MEDIA_TYPE = 9;
52
53     /**
54      * xml file name.
55      */

56     private String JavaDoc fileName;
57
58
59     /**
60      * Constructs an empty <code>XMLDocumentFactory</code>
61      */

62     public XMLDocumentFactory() {
63     }
64
65
66     /**
67      * Constructs a <code>XMLDocumentFactory</code> with the given
68      * xml file name as <code>String</code>
69      */

70     public XMLDocumentFactory(String JavaDoc fileName) {
71         this.fileName = fileName;
72     }
73
74
75     /**
76      * Returns xml file name.
77      *
78      * @return xml file name.
79      */

80     public String JavaDoc getFileName() {
81         return this.fileName;
82     }
83
84
85     /**
86      * Parses xml file with the given name and creates <code>Document</code>.
87      *
88      * @param fileName xml file name.
89      *
90      * @return document.
91      */

92     public static Document JavaDoc parse(String JavaDoc fileName) {
93           DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
94          try {
95            DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
96            builder.setErrorHandler(new UtilErrorHandler());
97            Document JavaDoc doc = builder.parse(fileName);
98            return doc;
99          } catch (SAXParseException JavaDoc e) {
100            e.printStackTrace();
101          } catch (ParserConfigurationException JavaDoc e) {
102            e.printStackTrace();
103          } catch (IOException JavaDoc e) {
104            e.printStackTrace();
105          } catch (SAXException JavaDoc e) {
106            e.printStackTrace();
107          }
108           return null;
109
110           //OLD with apache xerces
111
// DOMParser parser = new DOMParser();
112
// try {
113
// parser.parse(fileName);
114
// return parser.getDocument();
115
// } catch (SAXException e) {
116
// e.printStackTrace();
117
// } catch (IOException e) {
118
// e.printStackTrace();
119
// }
120
// return null;
121
}
122
123
124     /**
125      * Parses xml file and creates creates <code>Document</code>.
126      */

127     public Document JavaDoc parse() {
128           DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
129           try {
130             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
131             builder.setErrorHandler(new UtilErrorHandler());
132             Document JavaDoc doc = builder.parse(fileName);
133             return doc;
134           } catch (SAXParseException JavaDoc e) {
135             e.printStackTrace();
136           } catch (ParserConfigurationException JavaDoc e) {
137             e.printStackTrace();
138           } catch (IOException JavaDoc e) {
139             e.printStackTrace();
140           } catch (SAXException JavaDoc e) {
141             e.printStackTrace();
142           }
143           return null;
144
145           //OLD with apache xerces
146
// DOMParser parser = new DOMParser();
147
// try {
148
// parser.parse(this.fileName);
149
// return parser.getDocument();
150
// } catch (SAXException e) {
151
// System.err.println("SAXException - bad xml format");
152
// } catch (IOException e) {
153
// }
154
// return null;
155
}
156
157
158     /**
159      * Serializes node with all subnodes to the xml file with the given name,
160      * and with the <code>Properties</code> of the xml declaration.
161      *
162      * @param node root node of the document.
163      * @param fileName xml file name
164      * @param prop <code>Properties</code> of the xml declaration.
165      */

166     public static void serialize(Node JavaDoc node, String JavaDoc fileName, Properties JavaDoc prop) {
167         String JavaDoc out = "<?xml version=\"1.0\"?>";
168       File JavaDoc file = new File JavaDoc(fileName);
169       
170       //serialize xml declaration
171
if (prop != null) {
172          out = "<?xml";
173          String JavaDoc str = "";
174          for (int i=0; i<properties.length; i++) {
175             str = (String JavaDoc)prop.get(properties[i]);
176             if (str != null)
177                out += " "+properties[i]+"=\""+str+"\"";
178          }
179          out += "?>";
180       }
181       
182       //serialize document
183
try {
184          FileOutputStream JavaDoc outStream = new FileOutputStream JavaDoc(file);
185          out += node.toString();
186          outStream.write(out.getBytes());
187          outStream.close();
188       } catch(Exception JavaDoc e) {
189          System.err.println("Error serializing file");
190       }
191     }
192
193
194     /**
195      * Serializes node with all subnodes to the xml file
196      * with the default <code>Properties</code> of the xml declaration.
197      *
198      * @param node root node of the document.
199      */

200     public void serialize(Node JavaDoc node) {
201         
202         //TODO: NAPRAVITI I SERIALIZE ZA XML DECLARATION !!!!
203

204         File JavaDoc file = new File JavaDoc(fileName);
205         try {
206             FileOutputStream JavaDoc outStream = new FileOutputStream JavaDoc(file);
207             outStream.write(node.toString().getBytes());
208             outStream.close();
209         } catch(Exception JavaDoc e) {
210             System.err.println("Error serializing file");
211         }
212     }
213
214      static class UtilErrorHandler implements ErrorHandler JavaDoc
215   {
216
217     // throw SAXException for fatal errors
218
public void fatalError( SAXParseException JavaDoc exception ) throws SAXException JavaDoc
219     {
220       throw new SAXException JavaDoc(exception);
221     }
222
223     public void error( SAXParseException JavaDoc errorException ) throws SAXException JavaDoc
224     {
225       throw new SAXException JavaDoc(errorException);
226     }
227
228     // print any warnings
229
public void warning( SAXParseException JavaDoc warningError ) throws SAXException JavaDoc
230     {
231       System.err.println("[Validation : Warning] URI = " + warningError.getMessage());
232     }
233   }
234
235
236
237 }
238
Popular Tags