1 38 39 package com.sun.xml.fastinfoset.dom; 40 41 import com.sun.xml.fastinfoset.Encoder; 42 import com.sun.xml.fastinfoset.EncodingConstants; 43 import com.sun.xml.fastinfoset.QualifiedName; 44 import com.sun.xml.fastinfoset.util.LocalNameQualifiedNamesMap; 45 import java.io.IOException ; 46 import org.jvnet.fastinfoset.FastInfosetException; 47 import org.w3c.dom.Document ; 48 import org.w3c.dom.NamedNodeMap ; 49 import org.w3c.dom.Node ; 50 import org.w3c.dom.NodeList ; 51 52 public class DOMDocumentSerializer extends Encoder { 53 54 public DOMDocumentSerializer() { 55 } 56 57 public final void serialize(Node n) throws IOException { 58 switch (n.getNodeType()) { 59 case Node.DOCUMENT_NODE: 60 serialize((Document )n); 61 case Node.ELEMENT_NODE: 62 serializeElementAsDocument(n); 63 break; 64 case Node.COMMENT_NODE: 65 serializeComment(n); 66 break; 67 case Node.PROCESSING_INSTRUCTION_NODE: 68 serializeProcessingInstruction(n); 69 break; 70 } 71 } 72 73 public final void serialize(Document d) throws IOException { 74 reset(); 75 encodeHeader(false); 76 encodeInitialVocabulary(); 77 78 final NodeList nl = d.getChildNodes(); 79 for (int i = 0; i < nl.getLength(); i++) { 80 final Node n = nl.item(i); 81 switch (n.getNodeType()) { 82 case Node.ELEMENT_NODE: 83 serializeElement(n); 84 break; 85 case Node.COMMENT_NODE: 86 serializeComment(n); 87 break; 88 case Node.PROCESSING_INSTRUCTION_NODE: 89 serializeProcessingInstruction(n); 90 break; 91 } 92 } 93 encodeDocumentTermination(); 94 } 95 96 protected final void serializeElementAsDocument(Node e) throws IOException { 97 reset(); 98 encodeHeader(false); 99 encodeInitialVocabulary(); 100 101 serializeElement(e); 102 103 encodeDocumentTermination(); 104 } 105 106 107 protected Node [] _namespaceAttributes = new Node [4]; 108 protected Node [] _attributes = new Node [32]; 109 110 protected final void serializeElement(Node e) throws IOException { 111 encodeTermination(); 112 113 int namespaceAttributesSize = 0; 114 int attributesSize = 0; 115 if (e.hasAttributes()) { 116 120 final NamedNodeMap nnm = e.getAttributes(); 121 for (int i = 0; i < nnm.getLength(); i++) { 122 final Node a = nnm.item(i); 123 final String namespaceURI = a.getNamespaceURI(); 124 if (namespaceURI != null && namespaceURI.equals("http://www.w3.org/2000/xmlns/")) { 125 if (namespaceAttributesSize == _namespaceAttributes.length) { 126 final Node [] attributes = new Node [namespaceAttributesSize * 3 / 2 + 1]; 127 System.arraycopy(_namespaceAttributes, 0, attributes, 0, namespaceAttributesSize); 128 _namespaceAttributes = attributes; 129 } 130 _namespaceAttributes[namespaceAttributesSize++] = a; 131 } else { 132 if (attributesSize == _attributes.length) { 133 final Node [] attributes = new Node [attributesSize * 3 / 2 + 1]; 134 System.arraycopy(_attributes, 0, attributes, 0, attributesSize); 135 _attributes = attributes; 136 } 137 _attributes[attributesSize++] = a; 138 } 139 } 140 } 141 142 if (namespaceAttributesSize > 0) { 143 if (attributesSize > 0) { 144 write(EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_NAMESPACES_FLAG | 145 EncodingConstants.ELEMENT_ATTRIBUTE_FLAG); 146 } else { 147 write(EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_NAMESPACES_FLAG); 148 } 149 150 for (int i = 0; i < namespaceAttributesSize; i++) { 152 final Node a = _namespaceAttributes[i]; 153 _namespaceAttributes[i] = null; 154 String prefix = a.getLocalName(); 155 if (prefix == "xmlns" || prefix.equals("xmlns")) { 156 prefix = ""; 157 } 158 final String uri = a.getNodeValue(); 159 encodeNamespaceAttribute(prefix, uri); 160 } 161 162 write(EncodingConstants.TERMINATOR); 163 _b = 0; 164 } else { 165 _b = (attributesSize > 0) ? EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_ATTRIBUTE_FLAG : 166 EncodingConstants.ELEMENT; 167 } 168 169 String namespaceURI = e.getNamespaceURI(); 170 namespaceURI = (namespaceURI == null) ? "" : namespaceURI; 171 encodeElement(namespaceURI, e.getNodeName(), e.getLocalName()); 172 173 if (attributesSize > 0) { 174 for (int i = 0; i < attributesSize; i++) { 176 final Node a = _attributes[i]; 177 _attributes[i] = null; 178 namespaceURI = a.getNamespaceURI(); 179 namespaceURI = (namespaceURI == null) ? "" : namespaceURI; 180 encodeAttribute(namespaceURI, a.getNodeName(), a.getLocalName()); 181 182 final String value = a.getNodeValue(); 183 final boolean addToTable = (value.length() < attributeValueSizeConstraint) ? true : false; 184 encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable); 185 } 186 187 _b = EncodingConstants.TERMINATOR; 188 _terminate = true; 189 } 190 191 if (e.hasChildNodes()) { 192 final NodeList nl = e.getChildNodes(); 194 for (int i = 0; i < nl.getLength(); i++) { 195 final Node n = nl.item(i); 196 switch (n.getNodeType()) { 197 case Node.ELEMENT_NODE: 198 serializeElement(n); 199 break; 200 case Node.TEXT_NODE: 201 serializeText(n); 202 break; 203 case Node.CDATA_SECTION_NODE: 204 serializeCDATA(n); 205 break; 206 case Node.COMMENT_NODE: 207 serializeComment(n); 208 break; 209 case Node.PROCESSING_INSTRUCTION_NODE: 210 serializeProcessingInstruction(n); 211 break; 212 } 213 } 214 } 215 encodeElementTermination(); 216 } 217 218 protected final void serializeText(Node t) throws IOException { 219 final String text = t.getNodeValue(); 220 221 final int length = (text != null) ? text.length() : 0; 222 if (length == 0) { 223 return; 224 } else if (length < _charBuffer.length) { 225 encodeTermination(); 226 text.getChars(0, length, _charBuffer, 0); 227 encodeCharacters(_charBuffer, 0, length); 228 } else { 229 encodeTermination(); 230 final char ch[] = text.toCharArray(); 231 encodeCharactersNoClone(ch, 0, length); 232 } 233 } 234 235 protected final void serializeCDATA(Node t) throws IOException { 236 final String text = t.getNodeValue(); 237 238 final int length = (text != null) ? text.length() : 0; 239 if (length == 0) { 240 return; 241 } else { 242 encodeTermination(); 243 final char ch[] = text.toCharArray(); 244 try { 245 encodeCIIBuiltInAlgorithmDataAsCDATA(ch, 0, length); 246 } catch (FastInfosetException e) { 247 throw new IOException (""); 248 } 249 } 250 } 251 252 protected final void serializeComment(Node c) throws IOException { 253 encodeTermination(); 254 255 final String comment = c.getNodeValue(); 256 257 final int length = (comment != null) ? comment.length() : 0; 258 if (length == 0) { 259 encodeComment(_charBuffer, 0, 0); 260 } else if (length < _charBuffer.length) { 261 comment.getChars(0, length, _charBuffer, 0); 262 encodeComment(_charBuffer, 0, length); 263 } else { 264 final char ch[] = comment.toCharArray(); 265 encodeCommentNoClone(ch, 0, length); 266 } 267 } 268 269 protected final void serializeProcessingInstruction(Node pi) throws IOException { 270 encodeTermination(); 271 272 final String target = pi.getNodeName(); 273 final String data = pi.getNodeValue(); 274 encodeProcessingInstruction(target, data); 275 } 276 277 protected final void encodeElement(String namespaceURI, String qName, String localName) throws IOException { 278 LocalNameQualifiedNamesMap.Entry entry = _v.elementName.obtainEntry(qName); 279 if (entry._valueIndex > 0) { 280 final QualifiedName[] names = entry._value; 281 for (int i = 0; i < entry._valueIndex; i++) { 282 if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) { 283 encodeNonZeroIntegerOnThirdBit(names[i].index); 284 return; 285 } 286 } 287 } 288 289 if (localName != null) { 291 encodeLiteralElementQualifiedNameOnThirdBit(namespaceURI, getPrefixFromQualifiedName(qName), 292 localName, entry); 293 } 294 else { 295 encodeLiteralElementQualifiedNameOnThirdBit(namespaceURI, "", qName, entry); 296 } 297 } 298 299 protected final void encodeAttribute(String namespaceURI, String qName, String localName) throws IOException { 300 LocalNameQualifiedNamesMap.Entry entry = _v.attributeName.obtainEntry(qName); 301 if (entry._valueIndex > 0) { 302 final QualifiedName[] names = entry._value; 303 for (int i = 0; i < entry._valueIndex; i++) { 304 if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) { 305 encodeNonZeroIntegerOnSecondBitFirstBitZero(names[i].index); 306 return; 307 } 308 } 309 } 310 311 if (localName != null) { 313 encodeLiteralAttributeQualifiedNameOnSecondBit(namespaceURI, getPrefixFromQualifiedName(qName), 314 localName, entry); 315 } 316 else { 317 encodeLiteralAttributeQualifiedNameOnSecondBit(namespaceURI, "", qName, entry); 318 } 319 } 320 } 321 | Popular Tags |