001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: DOM3SerializerImpl.java 1225426 2011-12-29 04:13:08Z mrglavas $
020     */
021    
022    package org.apache.xml.serializer.dom3;
023    
024    import java.io.IOException;
025    
026    import org.apache.xml.serializer.DOM3Serializer;
027    import org.apache.xml.serializer.SerializationHandler;
028    import org.apache.xml.serializer.utils.WrappedRuntimeException;
029    import org.w3c.dom.DOMErrorHandler;
030    import org.w3c.dom.Node;
031    import org.w3c.dom.ls.LSSerializerFilter;
032    
033    /**
034     * This class implements the DOM3Serializer interface.
035     * 
036     * @xsl.usage internal
037     */
038    public final class DOM3SerializerImpl implements DOM3Serializer {
039    
040        /**
041         * Private class members
042         */
043        // The DOMErrorHandler
044        private DOMErrorHandler fErrorHandler;
045    
046        // A LSSerializerFilter
047        private LSSerializerFilter fSerializerFilter;
048    
049        // A LSSerializerFilter
050        private String fNewLine;
051    
052        // A SerializationHandler ex. an instance of ToXMLStream
053        private SerializationHandler fSerializationHandler;
054    
055        /**
056         * Constructor
057         * 
058         * @param handler An instance of the SerializationHandler interface. 
059         */
060        public DOM3SerializerImpl(SerializationHandler handler) {
061            fSerializationHandler = handler;
062        }
063    
064        // Public memebers
065    
066        /**
067         * Returns a DOMErrorHandler set on the DOM Level 3 Serializer.
068         * 
069         * This interface is a public API.
070         *
071         * @return A Level 3 DOMErrorHandler
072         */
073        public DOMErrorHandler getErrorHandler() {
074            return fErrorHandler;
075        }
076    
077        /**
078         * Returns a LSSerializerFilter set on the DOM Level 3 Serializer to filter nodes
079         * during serialization.
080         * 
081         * This interface is a public API.
082         *
083         * @return The Level 3 LSSerializerFilter
084         */
085        public LSSerializerFilter getNodeFilter() {
086            return fSerializerFilter;
087        }
088        
089        /**
090         * Gets the end-of-line sequence of characters to be used during serialization.
091         */
092        public char[] getNewLine() {
093            return (fNewLine != null) ? fNewLine.toCharArray() : null;
094        }
095    
096        /**
097         * Serializes the Level 3 DOM node by creating an instance of DOM3TreeWalker
098         * which traverses the DOM tree and invokes handler events to serialize
099         * 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    }