Rev 1571 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
244 | jmachado | 1 | package pt.estgp.estgweb.utils; |
2 | |||
3 | import org.dom4j.Document; |
||
4 | import org.dom4j.DocumentException; |
||
5 | import org.dom4j.Element; |
||
6 | import org.dom4j.io.*; |
||
726 | jmachado | 7 | import org.xml.sax.ErrorHandler; |
8 | import org.xml.sax.InputSource; |
||
9 | import org.xml.sax.SAXException; |
||
10 | import org.xml.sax.SAXParseException; |
||
1814 | jmachado | 11 | import pt.estgp.estgweb.utils.Globals; |
244 | jmachado | 12 | |
13 | import javax.xml.transform.*; |
||
726 | jmachado | 14 | import javax.xml.transform.dom.DOMResult; |
1553 | jmachado | 15 | import javax.xml.transform.dom.DOMSource; |
726 | jmachado | 16 | import javax.xml.transform.sax.SAXSource; |
1553 | jmachado | 17 | import javax.xml.transform.stream.StreamResult; |
244 | jmachado | 18 | import javax.xml.transform.stream.StreamSource; |
19 | import java.io.*; |
||
20 | import java.net.MalformedURLException; |
||
21 | import java.net.URL; |
||
726 | jmachado | 22 | import java.util.HashMap; |
244 | jmachado | 23 | import java.util.Hashtable; |
24 | import java.util.Map; |
||
25 | |||
26 | |||
27 | |||
28 | /** |
||
29 | * Class Description |
||
30 | * |
||
31 | * @author Jorge Machado <machadofisher@gmail.com> |
||
32 | * @see ; |
||
33 | */ |
||
34 | |||
35 | public class Dom4jUtil |
||
36 | { |
||
37 | public static 5+0%2Fdocs%2Fapi+Document">Document parse(1.5.0/docs/api/java/net/URL.html">URL url) throws DocumentException |
||
38 | { |
||
39 | SAXReader reader = new SAXReader(); |
||
40 | createIgnoreErrorHandler(reader); |
||
41 | return reader.read(url); |
||
42 | } |
||
43 | |||
44 | public static 5+0%2Fdocs%2Fapi+Document">Document parse(1.5.0/docs/api/java/lang/String.html">String xml) throws DocumentException |
||
45 | { |
||
46 | SAXReader reader = new SAXReader(); |
||
47 | createIgnoreErrorHandler(reader); |
||
48 | return reader.read(new 1.5.0/docs/api/java/io/StringReader.html">StringReader(xml)); |
||
49 | } |
||
368 | jmachado | 50 | |
244 | jmachado | 51 | public static void createIgnoreErrorHandler(SAXReader reader) |
52 | { |
||
53 | reader.setValidation(false); |
||
54 | reader.setErrorHandler(new 1.5.0/docs/api/org/xml/sax/ErrorHandler.html">ErrorHandler() |
||
55 | { |
||
56 | |||
57 | public void warning(1.5.0/docs/api/org/xml/sax/SAXParseException.html">SAXParseException exception) throws 1.5.0/docs/api/org/xml/sax/SAXException.html">SAXException |
||
58 | { |
||
59 | 1.5.0/docs/api/java/lang/System.html">System.out.println(exception); |
||
60 | } |
||
61 | |||
62 | public void error(1.5.0/docs/api/org/xml/sax/SAXParseException.html">SAXParseException exception) throws 1.5.0/docs/api/org/xml/sax/SAXException.html">SAXException |
||
63 | { |
||
64 | 1.5.0/docs/api/java/lang/System.html">System.out.println(exception); |
||
65 | } |
||
66 | |||
67 | public void fatalError(1.5.0/docs/api/org/xml/sax/SAXParseException.html">SAXParseException exception) throws 1.5.0/docs/api/org/xml/sax/SAXException.html">SAXException |
||
68 | { |
||
69 | 1.5.0/docs/api/java/lang/System.html">System.out.println(exception); |
||
70 | } |
||
71 | }); |
||
72 | } |
||
368 | jmachado | 73 | |
379 | jmachado | 74 | private static Map<String,byte[]> sourceXmls = new HashMap<String,byte[]>(); |
75 | |||
76 | /** |
||
77 | * Search for Source Document in cache |
||
78 | * |
||
79 | * @param file in src tree |
||
80 | * @return bytes |
||
81 | * @throws java.io.IOException on read error |
||
82 | */ |
||
83 | public static byte[] getSourceFile(1.5.0/docs/api/java/lang/String.html">String file) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
84 | { |
||
85 | byte[] bytes; |
||
86 | if((bytes = sourceXmls.get(file)) != null) |
||
87 | { |
||
88 | return bytes; |
||
89 | } |
||
90 | |||
91 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream = 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getContextClassLoader().getResourceAsStream(file); |
||
92 | 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream output = new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream(); |
||
93 | int read; |
||
94 | byte[] buffer = new byte[1024]; |
||
95 | while((read = stream.read(buffer)) > 0) |
||
96 | { |
||
97 | output.write(buffer,0,read); |
||
98 | } |
||
99 | output.close(); |
||
100 | stream.close(); |
||
101 | bytes = output.toByteArray(); |
||
102 | sourceXmls.put(file, bytes); |
||
103 | return bytes; |
||
104 | } |
||
105 | |||
106 | public static 5+0%2Fdocs%2Fapi+Document">Document parseSourceFileFromCache(1.5.0/docs/api/java/lang/String.html">String file) throws DocumentException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
107 | { |
||
108 | byte[] bytes = getSourceFile(file); |
||
109 | SAXReader reader = new SAXReader(); |
||
110 | reader.setValidation(false); |
||
111 | createIgnoreErrorHandler(reader); |
||
112 | return reader.read(new 1.5.0/docs/api/java/io/ByteArrayInputStream.html">ByteArrayInputStream(bytes)); |
||
113 | } |
||
114 | |||
368 | jmachado | 115 | public static 5+0%2Fdocs%2Fapi+Document">Document parseSourceFile(1.5.0/docs/api/java/lang/String.html">String file) throws DocumentException, 1.5.0/docs/api/java/io/IOException.html">IOException |
116 | { |
||
117 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream = 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getContextClassLoader().getResourceAsStream(file); |
||
379 | jmachado | 118 | |
368 | jmachado | 119 | SAXReader reader = new SAXReader(); |
120 | reader.setValidation(false); |
||
121 | createIgnoreErrorHandler(reader); |
||
122 | 5+0%2Fdocs%2Fapi+Document">Document dom = reader.read(stream); |
||
123 | stream.close(); |
||
124 | return dom; |
||
125 | } |
||
126 | |||
244 | jmachado | 127 | public static 5+0%2Fdocs%2Fapi+Document">Document parse(1.5.0/docs/api/java/io/File.html">File file) throws DocumentException, 1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException |
128 | { |
||
129 | SAXReader reader = new SAXReader(); |
||
130 | reader.setValidation(false); |
||
131 | createIgnoreErrorHandler(reader); |
||
132 | return reader.read(file); |
||
133 | } |
||
134 | |||
135 | public static 5+0%2Fdocs%2Fapi+Document">Document parse(1.5.0/docs/api/org/xml/sax/InputSource.html">InputSource source) throws DocumentException, 1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException |
||
136 | { |
||
137 | SAXReader reader = new SAXReader(); |
||
138 | createIgnoreErrorHandler(reader); |
||
139 | return reader.read(source); |
||
140 | } |
||
141 | |||
248 | jmachado | 142 | public static 5+0%2Fdocs%2Fapi+Document">Document parse(5+0%2Fdocs%2Fapi+InputStream">InputStream stream) throws DocumentException, 1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException |
143 | { |
||
144 | SAXReader reader = new SAXReader(); |
||
145 | createIgnoreErrorHandler(reader); |
||
146 | return reader.read(stream); |
||
147 | } |
||
244 | jmachado | 148 | |
149 | |||
150 | |||
248 | jmachado | 151 | |
244 | jmachado | 152 | public static void write(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/lang/String.html">String fileDest) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
153 | { |
||
154 | // lets write to a file |
||
155 | XMLWriter writer = new XMLWriter( |
||
156 | new 1.5.0/docs/api/java/io/FileWriter.html">FileWriter( fileDest ) |
||
157 | ); |
||
158 | writer.write( document ); |
||
159 | writer.close(); |
||
160 | } |
||
161 | |||
1553 | jmachado | 162 | public static void writeSout(org.w3c.dom.5+0%2Fdocs%2Fapi+Document">Document doc) throws 1.5.0/docs/api/java/io/IOException.html">IOException, 1.5.0/docs/api/javax/xml/transform/TransformerException.html">TransformerException { |
163 | 1.5.0/docs/api/javax/xml/transform/TransformerFactory.html">TransformerFactory tf = 1.5.0/docs/api/javax/xml/transform/TransformerFactory.html">TransformerFactory.newInstance(); |
||
164 | 1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer = tf.newTransformer(); |
||
165 | transformer.setOutputProperty(1.5.0/docs/api/javax/xml/transform/OutputKeys.html">OutputKeys.OMIT_XML_DECLARATION, "no"); |
||
166 | transformer.setOutputProperty(1.5.0/docs/api/javax/xml/transform/OutputKeys.html">OutputKeys.METHOD, "xml"); |
||
167 | transformer.setOutputProperty(1.5.0/docs/api/javax/xml/transform/OutputKeys.html">OutputKeys.INDENT, "yes"); |
||
168 | transformer.setOutputProperty(1.5.0/docs/api/javax/xml/transform/OutputKeys.html">OutputKeys.ENCODING, "UTF-8"); |
||
169 | transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); |
||
170 | |||
171 | transformer.transform(new 1.5.0/docs/api/javax/xml/transform/dom/DOMSource.html">DOMSource(doc), |
||
172 | new 1.5.0/docs/api/javax/xml/transform/stream/StreamResult.html">StreamResult(new 1.5.0/docs/api/java/io/OutputStreamWriter.html">OutputStreamWriter(1.5.0/docs/api/java/lang/System.html">System.out, "UTF-8"))); |
||
173 | } |
||
174 | |||
1571 | jmachado | 175 | public static void write(org.w3c.dom.5+0%2Fdocs%2Fapi+Document">Document doc,1.5.0/docs/api/java/io/File.html">File f) throws 1.5.0/docs/api/java/io/IOException.html">IOException, 1.5.0/docs/api/javax/xml/transform/TransformerException.html">TransformerException { |
176 | 1.5.0/docs/api/javax/xml/transform/TransformerFactory.html">TransformerFactory tf = 1.5.0/docs/api/javax/xml/transform/TransformerFactory.html">TransformerFactory.newInstance(); |
||
177 | 1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer = tf.newTransformer(); |
||
178 | transformer.setOutputProperty(1.5.0/docs/api/javax/xml/transform/OutputKeys.html">OutputKeys.OMIT_XML_DECLARATION, "no"); |
||
179 | transformer.setOutputProperty(1.5.0/docs/api/javax/xml/transform/OutputKeys.html">OutputKeys.METHOD, "xml"); |
||
180 | transformer.setOutputProperty(1.5.0/docs/api/javax/xml/transform/OutputKeys.html">OutputKeys.INDENT, "yes"); |
||
181 | transformer.setOutputProperty(1.5.0/docs/api/javax/xml/transform/OutputKeys.html">OutputKeys.ENCODING, "UTF-8"); |
||
182 | transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); |
||
1553 | jmachado | 183 | |
1571 | jmachado | 184 | transformer.transform(new 1.5.0/docs/api/javax/xml/transform/dom/DOMSource.html">DOMSource(doc), |
185 | new 1.5.0/docs/api/javax/xml/transform/stream/StreamResult.html">StreamResult(new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(f))); |
||
186 | } |
||
187 | |||
188 | |||
244 | jmachado | 189 | public static void writeSout(5+0%2Fdocs%2Fapi+Document">Document document) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
190 | { |
||
191 | // Compact format to System.out |
||
192 | OutputFormat format = OutputFormat.createCompactFormat(); |
||
193 | XMLWriter writer = new XMLWriter( 1.5.0/docs/api/java/lang/System.html">System.out, format ); |
||
194 | writer.write( document ); |
||
195 | } |
||
196 | |||
197 | public static void write(5+0%2Fdocs%2Fapi+Document">Document document,5+0%2Fdocs%2Fapi+OutputStream">OutputStream stream) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
198 | { |
||
199 | // lets write to a file |
||
200 | XMLWriter writer = new XMLWriter(stream); |
||
201 | writer.write( document ); |
||
202 | writer.close(); |
||
203 | } |
||
204 | |||
205 | public static void write(5+0%2Fdocs%2Fapi+Element">Element document,5+0%2Fdocs%2Fapi+OutputStream">OutputStream stream) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
206 | { |
||
207 | // lets write to a file |
||
208 | XMLWriter writer = new XMLWriter(stream); |
||
209 | writer.write( document ); |
||
210 | writer.close(); |
||
211 | } |
||
212 | |||
213 | public static void write(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/io/Writer.html">Writer writerStream) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
214 | { |
||
1312 | jmachado | 215 | write(document,writerStream,false); |
216 | } |
||
217 | public static void write(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/io/Writer.html">Writer writerStream,boolean omitXmlDecl) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
218 | { |
||
219 | |||
244 | jmachado | 220 | // lets write to a file |
1312 | jmachado | 221 | XMLWriter writer; |
222 | if(omitXmlDecl) |
||
223 | { |
||
224 | OutputFormat format = new OutputFormat(); |
||
225 | format.setSuppressDeclaration(true); |
||
226 | writer = new XMLWriter(writerStream,format); |
||
227 | } |
||
228 | else |
||
229 | writer = new XMLWriter(writerStream); |
||
230 | |||
244 | jmachado | 231 | writer.write( document ); |
232 | writer.close(); |
||
233 | } |
||
368 | jmachado | 234 | public static void write(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/io/Writer.html">Writer writerStream, OutputFormat outputFormat) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
235 | { |
||
236 | // lets write to a file |
||
237 | XMLWriter writer = new XMLWriter(writerStream,outputFormat); |
||
238 | writer.write( document ); |
||
239 | writer.close(); |
||
240 | } |
||
244 | jmachado | 241 | |
242 | |||
368 | jmachado | 243 | private static 1.5.0/docs/api/javax/xml/transform/TransformerFactory.html">TransformerFactory transFact = 1.5.0/docs/api/javax/xml/transform/TransformerFactory.html">TransformerFactory.newInstance(); |
244 | jmachado | 244 | private static Map<String,Templates> templates =new Hashtable<String,Templates>(); |
245 | |||
246 | |||
247 | /** |
||
248 | * |
||
249 | * @param document to transform |
||
250 | * @param stylesheet relative to classPath |
||
251 | * @return transformed document |
||
252 | * @throws Exception parsing stylesheet |
||
253 | */ |
||
254 | public static 1.5.0/docs/api/java/lang/String.html">String styleDocument(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/lang/String.html">String stylesheet) throws 1.5.0/docs/api/java/lang/Exception.html">Exception { |
||
368 | jmachado | 255 | return styleDocument(document,stylesheet, (Map<String,Object>) null); |
256 | } |
||
1312 | jmachado | 257 | public static 1.5.0/docs/api/java/lang/String.html">String styleDocument(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/lang/String.html">String stylesheet, Map<String,Object> parameters) throws 1.5.0/docs/api/java/lang/Exception.html">Exception |
258 | { |
||
259 | return styleDocument( document, stylesheet, parameters,false); |
||
260 | } |
||
244 | jmachado | 261 | |
1312 | jmachado | 262 | public static 1.5.0/docs/api/java/lang/String.html">String styleDocument(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/lang/String.html">String stylesheet, Map<String,Object> parameters,boolean omitXmlDecl) throws 1.5.0/docs/api/java/lang/Exception.html">Exception { |
263 | |||
244 | jmachado | 264 | 1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer = getTransformerFromSystem(stylesheet); |
368 | jmachado | 265 | if(parameters != null) |
266 | { |
||
267 | for(1.5.0/docs/api/java/util/Map.html">Map.Entry<String,Object> entry: parameters.entrySet()) |
||
268 | { |
||
269 | transformer.setParameter(entry.getKey(),entry.getValue()); |
||
270 | } |
||
271 | } |
||
244 | jmachado | 272 | // now lets style the given document |
273 | DocumentSource source = new DocumentSource( document ); |
||
274 | DocumentResult result = new DocumentResult(); |
||
275 | transformer.transform( source, result ); |
||
276 | // return the transformed document |
||
277 | 5+0%2Fdocs%2Fapi+Document">Document transformedDoc = result.getDocument(); |
||
278 | 1.5.0/docs/api/java/io/StringWriter.html">StringWriter writer = new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter(); |
||
1312 | jmachado | 279 | write(transformedDoc,writer,omitXmlDecl); |
244 | jmachado | 280 | return writer.toString(); |
281 | } |
||
282 | |||
283 | /** |
||
284 | * |
||
285 | * @param document to transform |
||
286 | * @param stylesheet relative to classPath |
||
287 | * @param fileDest to write the destination file |
||
288 | * @return transformed document |
||
289 | * @throws Exception parsing stylesheet |
||
290 | */ |
||
291 | public static 5+0%2Fdocs%2Fapi+Document">Document styleDocument(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/lang/String.html">String stylesheet, 1.5.0/docs/api/java/lang/String.html">String fileDest) throws 1.5.0/docs/api/java/lang/Exception.html">Exception { |
||
292 | |||
293 | 1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer = getTransformerFromSystem(stylesheet); |
||
294 | // now lets style the given document |
||
295 | DocumentSource source = new DocumentSource( document ); |
||
296 | DocumentResult result = new DocumentResult(); |
||
297 | transformer.transform( source, result ); |
||
298 | // return the transformed document |
||
299 | 5+0%2Fdocs%2Fapi+Document">Document transformedDoc = result.getDocument(); |
||
300 | write(transformedDoc,fileDest); |
||
301 | return transformedDoc; |
||
302 | } |
||
303 | |||
304 | |||
305 | private static 1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer getTransformerFromSystem(1.5.0/docs/api/java/lang/String.html">String xsltFile) throws 1.5.0/docs/api/javax/xml/transform/TransformerConfigurationException.html">TransformerConfigurationException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
306 | { |
||
1312 | jmachado | 307 | 1.5.0/docs/api/javax/xml/transform/Templates.html">Templates tpl = null; |
308 | if(Globals.USE_XSL_CACHE) |
||
309 | tpl = templates.get(xsltFile); |
||
244 | jmachado | 310 | if (tpl==null) |
311 | { |
||
312 | 1.5.0/docs/api/java/lang/String.html">String path = Globals.CLASSES_ABSOLUTE_PATH + xsltFile; |
||
313 | 1.5.0/docs/api/javax/xml/transform/Source.html">Source xsltSource = new 1.5.0/docs/api/javax/xml/transform/stream/StreamSource.html">StreamSource(path); |
||
314 | tpl = transFact.newTemplates(xsltSource); |
||
315 | templates.put(xsltFile,tpl); |
||
316 | } |
||
317 | return tpl.newTransformer(); |
||
318 | } |
||
319 | |||
726 | jmachado | 320 | |
321 | public static org.w3c.dom.5+0%2Fdocs%2Fapi+Document">Document toW3c(org.dom4j.5+0%2Fdocs%2Fapi+Document">Document dom4jDoc) throws 1.5.0/docs/api/javax/xml/transform/TransformerException.html">TransformerException { |
||
322 | 1.5.0/docs/api/javax/xml/transform/TransformerFactory.html">TransformerFactory transformerFactory = 1.5.0/docs/api/javax/xml/transform/TransformerFactory.html">TransformerFactory.newInstance(); |
||
323 | 1.5.0/docs/api/javax/xml/transform/sax/SAXSource.html">SAXSource source = new DocumentSource(dom4jDoc); |
||
324 | 1.5.0/docs/api/javax/xml/transform/dom/DOMResult.html">DOMResult result = new 1.5.0/docs/api/javax/xml/transform/dom/DOMResult.html">DOMResult(); |
||
325 | |||
326 | 1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer = transformerFactory.newTransformer(); |
||
327 | |||
328 | transformer.transform(source, result); |
||
329 | return (org.w3c.dom.5+0%2Fdocs%2Fapi+Document">Document) result.getNode(); |
||
330 | } |
||
331 | |||
244 | jmachado | 332 | public static void main(1.5.0/docs/api/java/lang/String.html">String [] args) throws 1.5.0/docs/api/java/lang/Exception.html">Exception |
333 | { |
||
334 | |||
335 | 1.5.0/docs/api/java/lang/System.html">System.out.println(Dom4jUtil.styleDocument(Dom4jUtil.parse(new 1.5.0/docs/api/java/io/File.html">File("C:\\Servidores\\workspace\\baco\\impl\\src\\xsd\\curso_EI.xml")),"/template/xsd/curso.xsl")); |
||
336 | } |
||
337 | } |