1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the  "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18/*
19 * $Id:  $
20 */
21
22package org.apache.xml.serializer.dom3;
23
24import java.io.IOException;
25
26import org.apache.xml.serializer.DOM3Serializer;
27import org.apache.xml.serializer.SerializationHandler;
28import org.apache.xml.serializer.utils.WrappedRuntimeException;
29import org.w3c.dom.DOMErrorHandler;
30import org.w3c.dom.Node;
31import org.w3c.dom.ls.LSSerializerFilter;
32
33/**
34 * This class implements the DOM3Serializer interface.
35 *
36 * @xsl.usage internal
37 */
38public final class DOM3SerializerImpl implements DOM3Serializer {
39
40    /**
41     * Private class members
42     */
43    // The DOMErrorHandler
44    private DOMErrorHandler fErrorHandler;
45
46    // A LSSerializerFilter
47    private LSSerializerFilter fSerializerFilter;
48
49    // A LSSerializerFilter
50    private String fNewLine;
51
52    // A SerializationHandler ex. an instance of ToXMLStream
53    private SerializationHandler fSerializationHandler;
54
55    /**
56     * Constructor
57     *
58     * @param handler An instance of the SerializationHandler interface.
59     */
60    public DOM3SerializerImpl(SerializationHandler handler) {
61        fSerializationHandler = handler;
62    }
63
64    // Public memebers
65
66    /**
67     * Returns a DOMErrorHandler set on the DOM Level 3 Serializer.
68     *
69     * This interface is a public API.
70     *
71     * @return A Level 3 DOMErrorHandler
72     */
73    public DOMErrorHandler getErrorHandler() {
74        return fErrorHandler;
75    }
76
77    /**
78     * Returns a LSSerializerFilter set on the DOM Level 3 Serializer to filter nodes
79     * during serialization.
80     *
81     * This interface is a public API.
82     *
83     * @return The Level 3 LSSerializerFilter
84     */
85    public LSSerializerFilter getNodeFilter() {
86        return fSerializerFilter;
87    }
88
89    /**
90     * Gets the end-of-line sequence of characters to be used during serialization.
91     */
92    public char[] getNewLine() {
93        return (fNewLine != null) ? fNewLine.toCharArray() : null;
94    }
95
96    /**
97     * Serializes the Level 3 DOM node by creating an instance of DOM3TreeWalker
98     * which traverses the DOM tree and invokes handler events to serialize
99     * the DOM NOde. Throws an exception only if an I/O exception occured
100     * while serializing.
101     * This interface is a public API.
102     *
103     * @param node the Level 3 DOM node to serialize
104     * @throws IOException if an I/O exception occured while serializing
105     */
106    public void serializeDOM3(Node node) throws IOException {
107        try {
108            DOM3TreeWalker walker = new DOM3TreeWalker(fSerializationHandler,
109                    fErrorHandler, fSerializerFilter, fNewLine);
110
111            walker.traverse(node);
112        } catch (org.xml.sax.SAXException se) {
113            throw new WrappedRuntimeException(se);
114        }
115    }
116
117    /**
118     * Sets a DOMErrorHandler on the DOM Level 3 Serializer.
119     *
120     * This interface is a public API.
121     *
122     * @param handler the Level 3 DOMErrorHandler
123     */
124    public void setErrorHandler(DOMErrorHandler handler) {
125        fErrorHandler = handler;
126    }
127
128    /**
129     * Sets a LSSerializerFilter on the DOM Level 3 Serializer to filter nodes
130     * during serialization.
131     *
132     * This interface is a public API.
133     *
134     * @param filter the Level 3 LSSerializerFilter
135     */
136    public void setNodeFilter(LSSerializerFilter filter) {
137        fSerializerFilter = filter;
138    }
139
140    /**
141     * Sets a SerializationHandler on the DOM Serializer.
142     *
143     * This interface is a public API.
144     *
145     * @param handler An instance of SerializationHandler
146     */
147    public void setSerializationHandler(SerializationHandler handler) {
148        fSerializationHandler = handler;
149    }
150
151    /**
152     * Sets the end-of-line sequence of characters to be used during serialization.
153     * @param newLine The end-of-line sequence of characters to be used during serialization.
154     */
155    public void setNewLine(char[] newLine) {
156        fNewLine = (newLine != null) ? new String(newLine) : null;
157    }
158}
159