root / trunk / extensions / extQuickInfo / src / org / gvsig / quickInfo / utils / xml / XML_DOM_Utilities.java @ 27817
History | View | Annotate | Download (14.4 KB)
1 |
package org.gvsig.quickInfo.utils.xml; |
---|---|
2 |
|
3 |
/* gvSIG. Geographic Information System of the Valencian Government
|
4 |
*
|
5 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
6 |
* of the Valencian Government (CIT)
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
21 |
* MA 02110-1301, USA.
|
22 |
*
|
23 |
*/
|
24 |
|
25 |
import java.io.ByteArrayInputStream; |
26 |
import java.io.ByteArrayOutputStream; |
27 |
import java.io.File; |
28 |
import java.io.FileNotFoundException; |
29 |
import java.io.FileOutputStream; |
30 |
import java.io.IOException; |
31 |
import java.io.InputStream; |
32 |
|
33 |
import javax.xml.parsers.DocumentBuilder; |
34 |
import javax.xml.parsers.DocumentBuilderFactory; |
35 |
import javax.xml.parsers.ParserConfigurationException; |
36 |
import javax.xml.transform.Transformer; |
37 |
import javax.xml.transform.TransformerException; |
38 |
import javax.xml.transform.TransformerFactory; |
39 |
import javax.xml.transform.dom.DOMSource; |
40 |
import javax.xml.transform.stream.StreamResult; |
41 |
import javax.xml.transform.stream.StreamSource; |
42 |
|
43 |
import org.w3c.dom.Comment; |
44 |
import org.w3c.dom.Document; |
45 |
import org.w3c.dom.Element; |
46 |
import org.xml.sax.SAXException; |
47 |
import org.xml.sax.SAXParseException; |
48 |
|
49 |
/**
|
50 |
* This class has some methods that are utilities related to manage XML data or files using the DOM (Document Object Model) API <br>
|
51 |
* of Java, according the JAXP (Java API for XML Processing), which other API's (JDOM, DOM, ...) are also according to. <br>
|
52 |
* JAXP is a Java interface that provides a standard approach to Parsing XML documents. <br>
|
53 |
* DOM generates in memory a tree with the XML information.
|
54 |
*
|
55 |
* @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
|
56 |
*/
|
57 |
public class XML_DOM_Utilities { |
58 |
/**
|
59 |
* Parses XML data stored in a String and generates a DOM (Document Object Model) document that contains XML data
|
60 |
*
|
61 |
* @param xmlData An string with XML content
|
62 |
*
|
63 |
* @return Document The generated XML document
|
64 |
*
|
65 |
* @throws SAXException org.xml.sax.SAXException
|
66 |
* @throws IOException java.io.IOException
|
67 |
* @throws ParserConfigurationException javax.xml.parsers.ParserConfigurationException
|
68 |
*/
|
69 |
public static Document parse_XML_String_and_create_DOM(String xmlData) throws SAXException, IOException, ParserConfigurationException |
70 |
{ |
71 |
// Document builder factory instance. This builder allows create new documents
|
72 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
73 |
|
74 |
// Instance of a DOM document builder, this object will allow to parse a document
|
75 |
DocumentBuilder DOM_document_Builder = factory.newDocumentBuilder();
|
76 |
|
77 |
// Parses the input data and if it's correct returns a new document (Document) object with that data
|
78 |
return DOM_document_Builder.parse(new ByteArrayInputStream(xmlData.getBytes())); |
79 |
} |
80 |
|
81 |
/**
|
82 |
* Parses an XML file and generates a memory document with the tree data of that file
|
83 |
*
|
84 |
* @param fileXML Name of the XML file
|
85 |
*
|
86 |
* @return Document The generated XML document
|
87 |
*
|
88 |
* @throws SAXParseException org.xml.sax.SAXParseException
|
89 |
* @throws SAXException org.xml.sax.SAXException
|
90 |
* @throws ParserConfigurationException javax.xml.parsers.ParserConfigurationException
|
91 |
* @throws IOException java.io.IOException
|
92 |
*/
|
93 |
public static Document parse_XML_file_and_create_DOM(String fileXML) throws SAXParseException, SAXException, ParserConfigurationException, IOException |
94 |
{ |
95 |
// Document builder factory instance. This builder allows create new documents
|
96 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
97 |
|
98 |
// Instance of a DOM document builder, this object will allow to parse a document
|
99 |
DocumentBuilder DOM_document_Builder = factory.newDocumentBuilder();
|
100 |
|
101 |
// Parses the input data and if it's correct returns a new document (Document) object with that data
|
102 |
return DOM_document_Builder.parse( new File(fileXML) ); |
103 |
} |
104 |
|
105 |
/**
|
106 |
* Writes a DOM document in a file with XML format
|
107 |
*
|
108 |
* @param doc The document to write
|
109 |
* @param file The name of the output file
|
110 |
*
|
111 |
* @throws FileNotFoundException java.io.FileNotFoundException
|
112 |
* @throws TransformerException javax.xml.transform.TransformerException
|
113 |
*/
|
114 |
public static void write_DOM_into_an_XML_file(Document doc, String file) throws FileNotFoundException, TransformerException |
115 |
{ |
116 |
// An instance of a object transformer factory to create 'transformer' objects
|
117 |
TransformerFactory transFactory = TransformerFactory.newInstance(); |
118 |
Transformer transformer = transFactory.newTransformer();
|
119 |
|
120 |
// Holds a tree with the information of a document in the form of a DOM tree
|
121 |
DOMSource source = new DOMSource(doc); |
122 |
|
123 |
/* Creates a StreamResult object associated to a file, transforms the document tree to XML and stores it in a file */
|
124 |
// Creates the output file
|
125 |
File newXML = new File(file); |
126 |
|
127 |
// Associates the file to a file output stream
|
128 |
FileOutputStream os = new FileOutputStream(newXML); |
129 |
|
130 |
// Associates that stream as 'stream result' object
|
131 |
StreamResult result = new StreamResult(os); |
132 |
|
133 |
// Makes the stream transformation from the source to the result
|
134 |
transformer.transform(source, result); |
135 |
} |
136 |
|
137 |
/**
|
138 |
* Transforms a memory document with XML format into another with HTML format according an XSL file, and stores it in a file
|
139 |
*
|
140 |
* @param doc The document to read
|
141 |
* @param htmlFile The name of the output (HTML) file
|
142 |
* @param xslFile The name of the XSL file which allows transform XML into HTML according its style
|
143 |
*
|
144 |
* @throws FileNotFoundException java.io.FileNotFoundException
|
145 |
* @throws TransformerException javax.xml.transform.TransformerException
|
146 |
*/
|
147 |
public static void write_DOM_into_an_HTML_file(Document doc, String htmlFile, String xslFile) throws FileNotFoundException, TransformerException |
148 |
{ |
149 |
// An instance of a object transformer factory
|
150 |
TransformerFactory tFactory = TransformerFactory.newInstance(); |
151 |
|
152 |
// Creates the output file
|
153 |
File SalidaHTML = new File(htmlFile); |
154 |
|
155 |
// Associates the file to a file output stream
|
156 |
FileOutputStream os = new FileOutputStream(SalidaHTML); |
157 |
|
158 |
// Creates a transformer object associated to the XSL file
|
159 |
Transformer transformer = tFactory.newTransformer(new StreamSource(xslFile)); |
160 |
|
161 |
// Holds a tree with the information of a document in the form of a DOM tree
|
162 |
DOMSource sourceId = new DOMSource(doc); |
163 |
|
164 |
// Makes the transformation from the source in XML to the output in HML according the transformer in XSL
|
165 |
transformer.transform(sourceId, new StreamResult(os)); |
166 |
} |
167 |
|
168 |
/**
|
169 |
* Transforms a memory document with XML format into an string according an XSL file, and stores it in a file
|
170 |
*
|
171 |
* @param doc The document to read
|
172 |
* @param xslFile The name of the XSL file which allows transformate XML into String according its style
|
173 |
*
|
174 |
* @return String value of a DOM
|
175 |
*
|
176 |
* @throws FileNotFoundException java.io.FileNotFoundException
|
177 |
* @throws TransformerException javax.xml.transform.TransformerException
|
178 |
*/
|
179 |
public static String write_DOM_into_an_String(Document doc, String xslFile) throws FileNotFoundException, TransformerException |
180 |
{ |
181 |
// An instance of a object transformer factory
|
182 |
TransformerFactory tFactory = TransformerFactory.newInstance(); |
183 |
|
184 |
// Creates a transformer object associated to the XSL file
|
185 |
Transformer transformer = tFactory.newTransformer(new StreamSource(xslFile)); |
186 |
|
187 |
// Holds a tree with the information of a document in the form of a DOM tree
|
188 |
DOMSource sourceId = new DOMSource(doc); |
189 |
|
190 |
// Makes the transformation from the source in XML to the output in stream according the transformer in XSL
|
191 |
ByteArrayOutputStream bAOS = new ByteArrayOutputStream(); |
192 |
transformer.transform(sourceId, new StreamResult(bAOS)); |
193 |
|
194 |
// Returns the string value of the stream
|
195 |
return bAOS.toString();
|
196 |
} |
197 |
|
198 |
/**
|
199 |
* Transforms a memory document with XML format into an string according an XSL
|
200 |
*
|
201 |
* @param doc The document to read
|
202 |
* @param xsl An string with the XSL which allows transformate XML into String
|
203 |
*
|
204 |
* @return An String with the DOM transformated
|
205 |
*
|
206 |
* @throws FileNotFoundException java.io.FileNotFoundException
|
207 |
* @throws TransformerException javax.xml.transform.TransformerException
|
208 |
*/
|
209 |
public static String write_DOM_into_an_String_With_An_XSL_String(Document doc, String xsl) throws FileNotFoundException, TransformerException |
210 |
{ |
211 |
// An instance of a object transformer factory
|
212 |
TransformerFactory tFactory = TransformerFactory.newInstance(); |
213 |
|
214 |
// Creates a transformer object associated to the XSL file
|
215 |
Transformer transformer = tFactory.newTransformer(new StreamSource(new ByteArrayInputStream(xsl.getBytes()))); |
216 |
|
217 |
// Holds a tree with the information of a document in the form of a DOM tree
|
218 |
DOMSource sourceId = new DOMSource(doc); |
219 |
|
220 |
// Makes the transformation from the source in XML to the output in stream according the transformer in XSL
|
221 |
ByteArrayOutputStream bAOS = new ByteArrayOutputStream(); |
222 |
transformer.transform(sourceId, new StreamResult(bAOS)); |
223 |
|
224 |
// Returns the string value of the stream
|
225 |
return bAOS.toString();
|
226 |
} |
227 |
|
228 |
/**
|
229 |
* Transforms a memory document with XML format into an string according an XSL
|
230 |
*
|
231 |
* @param doc The document to read
|
232 |
* @param iS_xsl An InputStream with the XSL which allows transformate XML into String
|
233 |
*
|
234 |
* @return An String with the DOM transformated
|
235 |
*
|
236 |
* @throws FileNotFoundException java.io.FileNotFoundException
|
237 |
* @throws TransformerException javax.xml.transform.TransformerException
|
238 |
*/
|
239 |
public static String write_DOM_into_an_String_With_An_XSL_InputStream(Document doc, InputStream iS_xsl) throws FileNotFoundException, TransformerException |
240 |
{ |
241 |
// An instance of a object transformer factory
|
242 |
TransformerFactory tFactory = TransformerFactory.newInstance(); |
243 |
|
244 |
// Creates a transformer object associated to the XSL file
|
245 |
Transformer transformer = tFactory.newTransformer(new StreamSource(iS_xsl)); |
246 |
|
247 |
// Holds a tree with the information of a document in the form of a DOM tree
|
248 |
DOMSource sourceId = new DOMSource(doc); |
249 |
|
250 |
// Makes the transformation from the source in XML to the output in stream according the transformer in XSL
|
251 |
ByteArrayOutputStream bAOS = new ByteArrayOutputStream(); |
252 |
transformer.transform(sourceId, new StreamResult(bAOS)); |
253 |
|
254 |
// Returns the string value of the stream
|
255 |
return bAOS.toString();
|
256 |
} |
257 |
|
258 |
/**
|
259 |
* Creates a black (empty) DOM document
|
260 |
*
|
261 |
* @return The document
|
262 |
* @throws ParserConfigurationException javax.xml.parsers.ParserConfigurationException
|
263 |
*/
|
264 |
public static Document createBlankDocument() throws ParserConfigurationException { |
265 |
// Creates a instance of DocumentBuilderFactory
|
266 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
267 |
|
268 |
// Gets the DocumentBuilder
|
269 |
DocumentBuilder parser = factory.newDocumentBuilder();
|
270 |
|
271 |
// Creates a blank DOM Document
|
272 |
return parser.newDocument();
|
273 |
} |
274 |
|
275 |
/**
|
276 |
* Adds an element to a document in memory
|
277 |
*
|
278 |
* @param doc Document where element will be added
|
279 |
* @param elementName The name of the element
|
280 |
*
|
281 |
* @return org.w3c.dom.Element
|
282 |
*/
|
283 |
public static Element addElementToDocument(Document doc, String elementName) { |
284 |
// Creates the element and adds it to the document
|
285 |
Element element = doc.createElement(elementName);
|
286 |
doc.appendChild(element); |
287 |
|
288 |
return element;
|
289 |
} |
290 |
|
291 |
/**
|
292 |
* Adds an element to another of a document in memory <br>
|
293 |
* (It's supposed that the parent is one element of the document)
|
294 |
*
|
295 |
* @param doc Document of the parent element
|
296 |
* @param parent Parent element where a child element will be added
|
297 |
* @param elementName The name of the child element
|
298 |
*
|
299 |
* @return org.w3c.dom.Element
|
300 |
*/
|
301 |
public static Element addElementChildToElement(Document doc, Element parent, String elementName) { |
302 |
// Creates the element and adds it to the parent
|
303 |
Element element = doc.createElement(elementName);
|
304 |
parent.appendChild(element); |
305 |
|
306 |
return element;
|
307 |
} |
308 |
|
309 |
/**
|
310 |
* Adds a commentary to an element of a document in memory <br>
|
311 |
* (It's supposed that the element is one of the document)
|
312 |
*
|
313 |
* @param doc Document of the element where the commentary will be added
|
314 |
* @param element The xml elemnet where the commentary will be added
|
315 |
* @param commentary The commentary text
|
316 |
*
|
317 |
* @return org.w3c.dom.Comment
|
318 |
*/
|
319 |
public static Comment addCommentary(Document doc, Element element, String commentary) { |
320 |
// Creates the commentary and adds it to the element
|
321 |
Comment comment = doc.createComment(commentary); |
322 |
element.appendChild(comment); |
323 |
|
324 |
return comment;
|
325 |
} |
326 |
|
327 |
/**
|
328 |
* Adds an attribute to an element of a document in memory <br>
|
329 |
* (It's supposed that the element is one of the document)
|
330 |
*
|
331 |
* @param element The xml elemnet where the attibute will be added
|
332 |
* @param attributeName The name of the attribute
|
333 |
* @param attributeValue The value of the attribute
|
334 |
*/
|
335 |
public static void setAttribute(Element element, String attributeName, String attributeValue) { |
336 |
//Adds the atribute to the element
|
337 |
element.setAttribute(attributeName, attributeValue); |
338 |
} |
339 |
} |