KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > xml > XMLWriter


1 /*
2  
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5  
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8  
9  */

10 package org.mmbase.util.xml;
11
12 import java.io.*;
13
14 import org.w3c.dom.Node JavaDoc;
15
16 import javax.xml.transform.*;
17 import javax.xml.transform.dom.DOMSource JavaDoc;
18 import javax.xml.transform.stream.StreamResult JavaDoc;
19
20 import org.mmbase.util.logging.*;
21 /**
22  * Util class to serialize xml (wrapper around javax.xml.transform.Transformer)
23  * @author Kees Jongenburger <keesj@dds.nl>
24  * @since MMBase-1.7
25  **/

26 public class XMLWriter {
27     private static Logger log = Logging.getLoggerInstance(XMLWriter.class);
28     
29     /**
30      * defaulting version of {@link #write(Node, Writer, boolean, boolean)}. (Not ommitting xml declaration).
31      */

32     public static void write(Node JavaDoc node, Writer writer, boolean indent) throws TransformerConfigurationException, TransformerException{
33         write(node, writer, indent, false);
34     }
35     /**
36      * static method to serialize an DOM document
37      * @param node the node to serialize
38      * @param writer the writer to write the node to
39      * @param indent if true the document wil be indented
40      * @param omitxml
41      **/

42     public static void write(Node JavaDoc node, Writer writer, boolean indent, boolean omitxml) throws TransformerConfigurationException, TransformerException {
43         TransformerFactory transformerFactory = TransformerFactory.newInstance();
44         Transformer transformer = transformerFactory.newTransformer();
45         transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
46         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitxml ? "yes" : "no");
47         transformer.transform(new DOMSource JavaDoc(node), new StreamResult JavaDoc(writer));
48     }
49
50     /**
51      * Defaulting version of {@link #write(Node, boolean, boolean)}. (Not ommitting xml
52      * declaration).
53      */

54     public static String JavaDoc write(Node JavaDoc node, boolean indent) {
55         return write(node, indent, false);
56     }
57     /**
58      * static method to serialize a node to a string
59      * @param node the node to serialize
60      * @param indent , if true the node wil be indented
61      * @param omitxml
62      * @return the string represneation of the xml of null if an error occured
63      **/

64     public static String JavaDoc write(Node JavaDoc node, boolean indent, boolean omitxml) {
65         try {
66             StringWriter sw = new StringWriter();
67             write(node, sw, indent, omitxml);
68             return sw.toString();
69         } catch (Exception JavaDoc e){
70             //sorry for this message. but this is a util class that just has to do the jobs
71
//if it fails i can't help it
72
log.fatal("error in XMLWriter. it must be possible to write any node to xml withoud errors:{"+ e.getMessage() +"} " + Logging.stackTrace(e));
73         }
74         return null;
75     }
76 }
77
Popular Tags