1 package org.enhydra.xml;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.util.Properties;
7
8 import org.apache.xerces.parsers.DOMParser;
9 import org.w3c.dom.Document;
10 import org.w3c.dom.Node;
11 import org.xml.sax.SAXException;
12
13
14
15 /***
16 * @author Tweety
17 *
18 * A class for manipulating the entire xml file (reading, writing...).
19 *
20 * @version 1.0
21 */
22 public class XMLDocumentFactory {
23
24 private static String[] properties = {
25 "method",
26 "version",
27 "encoding",
28 "omit-xml-declaration",
29 "standalone",
30 "doctype-public",
31 "doctype-system",
32 "indent",
33 "media-type"
34 };
35
36
37 private static int METHOD = 0;
38 private static int VERSION = 1;
39 private static int ENCODING = 2;
40 private static int OMIT_XML_DECLARATION = 3;
41 private static int STANDALONE = 4;
42 private static int DOCTYPE_PUBLIC = 5;
43 private static int DOCTYPE_SYSTEM = 6;
44 private static int CDATA_SECTION_ELEMENTS = 7;
45 private static int INDENT = 8;
46 private static int MEDIA_TYPE = 9;
47
48 /***
49 * xml file name.
50 */
51 private String fileName;
52
53
54 /***
55 * Constructs an empty <code>XMLDocumentFactory</code>
56 */
57 public XMLDocumentFactory() {
58 }
59
60
61 /***
62 * Constructs a <code>XMLDocumentFactory</code> with the given
63 * xml file name as <code>String</code>
64 */
65 public XMLDocumentFactory(String fileName) {
66 this.fileName = fileName;
67 }
68
69
70 /***
71 * Returns xml file name.
72 *
73 * @return xml file name.
74 */
75 public String getFileName() {
76 return this.fileName;
77 }
78
79
80 /***
81 * Parses xml file with the given name and creates <code>Document</code>.
82 *
83 * @param fileName xml file name.
84 *
85 * @return document.
86 */
87 public static Document parse(String fileName) {
88 DOMParser parser = new DOMParser();
89 try {
90 parser.parse(fileName);
91 return parser.getDocument();
92 } catch (SAXException e) {
93 e.printStackTrace();
94 } catch (IOException e) {
95 e.printStackTrace();
96 }
97 return null;
98 }
99
100
101 /***
102 * Parses xml file and creates creates <code>Document</code>.
103 */
104 public Document parse() {
105 DOMParser parser = new DOMParser();
106 try {
107 parser.parse(this.fileName);
108 return parser.getDocument();
109 } catch (SAXException e) {
110 System.err.println("SAXException - bad xml format");
111 } catch (IOException e) {
112 }
113 return null;
114 }
115
116
117 /***
118 * Serializes node with all subnodes to the xml file with the given name,
119 * and with the <code>Properties</code> of the xml declaration.
120 *
121 * @param node root node of the document.
122 * @param fileName xml file name
123 * @param prop <code>Properties</code> of the xml declaration.
124 */
125 public static void serialize(Node node, String fileName, Properties prop) {
126 String out = "<?xml version=\"1.0\"?>";
127 File file = new File(fileName);
128
129 //serialize xml declaration
130 if (prop != null) {
131 out = "<?xml";
132 String str = "";
133 for (int i=0; i<properties.length; i++) {
134 str = (String)prop.get(properties[i]);
135 if (str != null)
136 out += " "+properties[i]+"=\""+str+"\"";
137 }
138 out += "?>";
139 }
140
141 //serialize document
142 try {
143 FileOutputStream outStream = new FileOutputStream(file);
144 out += node.toString();
145 outStream.write(out.getBytes());
146 outStream.close();
147 } catch(Exception e) {
148 System.err.println("Error serializing file");
149 }
150 }
151
152
153 /***
154 * Serializes node with all subnodes to the xml file
155 * with the default <code>Properties</code> of the xml declaration.
156 *
157 * @param node root node of the document.
158 */
159 public void serialize(Node node) {
160
161 //TODO: NAPRAVITI I SERIALIZE ZA XML DECLARATION !!!!
162
163 File file = new File(fileName);
164 try {
165 FileOutputStream outStream = new FileOutputStream(file);
166 outStream.write(node.toString().getBytes());
167 outStream.close();
168 } catch(Exception e) {
169 System.err.println("Error serializing file");
170 }
171 }
172
173
174
175 }
This page automatically generated by Maven