001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.common.xmap;
023
024import java.io.ByteArrayOutputStream;
025import java.io.IOException;
026import java.io.OutputStream;
027
028import javax.xml.parsers.DocumentBuilderFactory;
029
030import org.apache.xml.serialize.OutputFormat;
031import org.apache.xml.serialize.XMLSerializer;
032import org.w3c.dom.Document;
033import org.w3c.dom.DocumentFragment;
034import org.w3c.dom.Element;
035import org.w3c.dom.Node;
036import org.w3c.dom.ranges.DocumentRange;
037import org.w3c.dom.ranges.Range;
038
039/**
040 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
041 */
042public final class DOMSerializer {
043
044    private static final DocumentBuilderFactory BUILDER_FACTORY = DocumentBuilderFactory.newInstance();
045
046    // Default output format which is : no xml declaration, no document type,
047    // indent.
048    private static final OutputFormat DEFAULT_FORMAT = new OutputFormat();
049
050    static {
051        DEFAULT_FORMAT.setOmitXMLDeclaration(false);
052        DEFAULT_FORMAT.setIndenting(true);
053        DEFAULT_FORMAT.setMethod("xml");
054        DEFAULT_FORMAT.setEncoding("UTF-8");
055    }
056
057    // Utility class.
058    private DOMSerializer() {
059    }
060
061    /**
062     * @return the builderFactory
063     */
064    public static DocumentBuilderFactory getBuilderFactory() {
065        return BUILDER_FACTORY;
066    }
067
068    public static String toString(Element element) throws IOException {
069        return toString(element, DEFAULT_FORMAT);
070    }
071
072    public static String toStringOmitXml(Element element) throws IOException {
073        OutputFormat of = new OutputFormat();
074        of.setOmitXMLDeclaration(true);
075        return toString(element, of);
076    }
077
078    public static String toString(Element element, OutputFormat format) throws IOException {
079        ByteArrayOutputStream baos = new ByteArrayOutputStream();
080        write(element, format, baos);
081        return baos.toString();
082    }
083
084    public static String toString(DocumentFragment fragment) throws IOException {
085        return toString(fragment, DEFAULT_FORMAT);
086    }
087
088    public static String toString(DocumentFragment fragment, OutputFormat format) throws IOException {
089        ByteArrayOutputStream baos = new ByteArrayOutputStream();
090        write(fragment, format, baos);
091        return baos.toString();
092    }
093
094    public static String toString(Document doc) throws IOException {
095        return toString(doc, DEFAULT_FORMAT);
096    }
097
098    public static String toString(Document doc, OutputFormat format) throws IOException {
099        ByteArrayOutputStream baos = new ByteArrayOutputStream();
100        write(doc, format, baos);
101        return baos.toString();
102    }
103
104    public static void write(Element element, OutputStream out) throws IOException {
105        write(element, DEFAULT_FORMAT, out);
106    }
107
108    public static void write(Element element, OutputFormat format, OutputStream out) throws IOException {
109        XMLSerializer serializer = new XMLSerializer(out, format);
110        serializer.asDOMSerializer().serialize(element);
111    }
112
113    public static void write(DocumentFragment fragment, OutputStream out) throws IOException {
114        write(fragment, DEFAULT_FORMAT, out);
115    }
116
117    public static void write(DocumentFragment fragment, OutputFormat format, OutputStream out) throws IOException {
118        XMLSerializer serializer = new XMLSerializer(out, format);
119        serializer.asDOMSerializer().serialize(fragment);
120    }
121
122    public static void write(Document doc, OutputStream out) throws IOException {
123        write(doc, DEFAULT_FORMAT, out);
124    }
125
126    public static void write(Document doc, OutputFormat format, OutputStream out) throws IOException {
127        XMLSerializer serializer = new XMLSerializer(out, format);
128        serializer.asDOMSerializer().serialize(doc);
129    }
130
131    public static DocumentFragment getContentAsFragment(Element element) {
132        Node node = element.getFirstChild();
133        if (node == null) {
134            return null; // no content
135        }
136        Range range = ((DocumentRange) element.getOwnerDocument()).createRange();
137        range.setStartBefore(node);
138        range.setEndAfter(element.getLastChild());
139        return range.cloneContents();
140    }
141
142}