001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util.xml;
011:
012: import java.io.*;
013:
014: import org.w3c.dom.*;
015:
016: import javax.xml.transform.*;
017: import javax.xml.transform.dom.DOMSource;
018: import javax.xml.transform.stream.StreamResult;
019:
020: import org.mmbase.util.logging.*;
021:
022: /**
023: * Util class to serialize xml (wrapper around javax.xml.transform.Transformer)
024: * @author Kees Jongenburger <keesj@dds.nl>
025: * @since MMBase-1.7
026: **/
027: public class XMLWriter {
028: private static Logger log = Logging
029: .getLoggerInstance(XMLWriter.class);
030:
031: /**
032: * defaulting version of {@link #write(Node, Writer, boolean, boolean)}. (Not ommitting xml declaration).
033: */
034: public static void write(Node node, Writer writer, boolean indent)
035: throws TransformerConfigurationException,
036: TransformerException {
037: write(node, writer, indent, false);
038: }
039:
040: /**
041: * static method to serialize an DOM document
042: * @param node the node to serialize
043: * @param writer the writer to write the node to
044: * @param indent if true the document wil be indented
045: * @param omitxml
046: **/
047: public static void write(Node node, Writer writer, boolean indent,
048: boolean omitxml) throws TransformerConfigurationException,
049: TransformerException {
050: TransformerFactory transformerFactory = TransformerFactory
051: .newInstance();
052: try {
053: transformerFactory.setAttribute(
054: "http://saxon.sf.net/feature/version-warning",
055: false);
056: } catch (IllegalArgumentException iae) {
057: // never mind
058: }
059: Transformer transformer = transformerFactory.newTransformer();
060: transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes"
061: : "no");
062: transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
063: omitxml ? "yes" : "no");
064: if (!omitxml) {
065: Document d = node.getOwnerDocument();
066: if (d != null) {
067: DocumentType dt = d.getDoctype();
068: if (dt != null) {
069: transformer
070: .setOutputProperty(
071: OutputKeys.DOCTYPE_PUBLIC, dt
072: .getPublicId());
073: transformer
074: .setOutputProperty(
075: OutputKeys.DOCTYPE_SYSTEM, dt
076: .getSystemId());
077: }
078: }
079: }
080: transformer.transform(new DOMSource(node), new StreamResult(
081: writer));
082: }
083:
084: /**
085: * Defaulting version of {@link #write(Node, boolean, boolean)}. (Not ommitting xml
086: * declaration).
087: */
088: public static String write(Node node, boolean indent) {
089: return write(node, indent, false);
090: }
091:
092: /**
093: * static method to serialize a node to a string
094: * @param node the node to serialize
095: * @param indent , if true the node wil be indented
096: * @param omitxml
097: * @return the string represneation of the xml of null if an error occured
098: **/
099: public static String write(Node node, boolean indent,
100: boolean omitxml) {
101: try {
102: StringWriter sw = new StringWriter();
103: write(node, sw, indent, omitxml);
104: return sw.toString();
105: } catch (Exception e) {
106: //sorry for this message. but this is a util class that just has to do the jobs
107: //if it fails i can't help it
108: log
109: .fatal("error in XMLWriter. it must be possible to write any node to xml withoud errors:{"
110: + e.getMessage()
111: + "} "
112: + Logging.stackTrace(e));
113: }
114: return null;
115: }
116: }
|