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