src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/dom/DOMDocumentSerializer.java

Wed, 12 Jun 2013 14:47:09 +0100

author
mkos
date
Wed, 12 Jun 2013 14:47:09 +0100
changeset 384
8f2986ff0235
parent 0
373ffda63c9a
permissions
-rw-r--r--

8013021: Rebase 8005432 & 8003542 against the latest jdk8/jaxws
8003542: Improve processing of MTOM attachments
8005432: Update access to JAX-WS
Reviewed-by: mullan

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 *
aoqi@0 25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
aoqi@0 26 */
aoqi@0 27
aoqi@0 28 package com.sun.xml.internal.fastinfoset.dom;
aoqi@0 29
aoqi@0 30 import com.sun.xml.internal.fastinfoset.Encoder;
aoqi@0 31 import com.sun.xml.internal.fastinfoset.EncodingConstants;
aoqi@0 32 import com.sun.xml.internal.fastinfoset.QualifiedName;
aoqi@0 33 import com.sun.xml.internal.fastinfoset.util.NamespaceContextImplementation;
aoqi@0 34 import com.sun.xml.internal.fastinfoset.util.LocalNameQualifiedNamesMap;
aoqi@0 35 import java.io.IOException;
aoqi@0 36 import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetException;
aoqi@0 37 import org.w3c.dom.Document;
aoqi@0 38 import org.w3c.dom.NamedNodeMap;
aoqi@0 39 import org.w3c.dom.Node;
aoqi@0 40 import org.w3c.dom.NodeList;
aoqi@0 41
aoqi@0 42 /**
aoqi@0 43 * The Fast Infoset DOM serializer.
aoqi@0 44 * <p>
aoqi@0 45 * Instantiate this serializer to serialize a fast infoset document in accordance
aoqi@0 46 * with the DOM API.
aoqi@0 47 *
aoqi@0 48 */
aoqi@0 49 public class DOMDocumentSerializer extends Encoder {
aoqi@0 50
aoqi@0 51 /**
aoqi@0 52 * Serialize a {@link Node}.
aoqi@0 53 *
aoqi@0 54 * @param n the node to serialize.
aoqi@0 55 */
aoqi@0 56 public final void serialize(Node n) throws IOException {
aoqi@0 57 switch (n.getNodeType()) {
aoqi@0 58 case Node.DOCUMENT_NODE:
aoqi@0 59 serialize((Document)n);
aoqi@0 60 break;
aoqi@0 61 case Node.ELEMENT_NODE:
aoqi@0 62 serializeElementAsDocument(n);
aoqi@0 63 break;
aoqi@0 64 case Node.COMMENT_NODE:
aoqi@0 65 serializeComment(n);
aoqi@0 66 break;
aoqi@0 67 case Node.PROCESSING_INSTRUCTION_NODE:
aoqi@0 68 serializeProcessingInstruction(n);
aoqi@0 69 break;
aoqi@0 70 }
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 /**
aoqi@0 74 * Serialize a {@link Document}.
aoqi@0 75 *
aoqi@0 76 * @param d the document to serialize.
aoqi@0 77 */
aoqi@0 78 public final void serialize(Document d) throws IOException {
aoqi@0 79 reset();
aoqi@0 80 encodeHeader(false);
aoqi@0 81 encodeInitialVocabulary();
aoqi@0 82
aoqi@0 83 final NodeList nl = d.getChildNodes();
aoqi@0 84 for (int i = 0; i < nl.getLength(); i++) {
aoqi@0 85 final Node n = nl.item(i);
aoqi@0 86 switch (n.getNodeType()) {
aoqi@0 87 case Node.ELEMENT_NODE:
aoqi@0 88 serializeElement(n);
aoqi@0 89 break;
aoqi@0 90 case Node.COMMENT_NODE:
aoqi@0 91 serializeComment(n);
aoqi@0 92 break;
aoqi@0 93 case Node.PROCESSING_INSTRUCTION_NODE:
aoqi@0 94 serializeProcessingInstruction(n);
aoqi@0 95 break;
aoqi@0 96 }
aoqi@0 97 }
aoqi@0 98 encodeDocumentTermination();
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 protected final void serializeElementAsDocument(Node e) throws IOException {
aoqi@0 102 reset();
aoqi@0 103 encodeHeader(false);
aoqi@0 104 encodeInitialVocabulary();
aoqi@0 105
aoqi@0 106 serializeElement(e);
aoqi@0 107
aoqi@0 108 encodeDocumentTermination();
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 // protected Node[] _namespaceAttributes = new Node[4];
aoqi@0 112
aoqi@0 113 // map which will hold all current scope prefixes and associated attributes
aoqi@0 114 // Collection of populated namespace available for current scope
aoqi@0 115 protected NamespaceContextImplementation _namespaceScopeContext = new NamespaceContextImplementation();
aoqi@0 116 protected Node[] _attributes = new Node[32];
aoqi@0 117
aoqi@0 118 protected final void serializeElement(Node e) throws IOException {
aoqi@0 119 encodeTermination();
aoqi@0 120
aoqi@0 121 int attributesSize = 0;
aoqi@0 122
aoqi@0 123 _namespaceScopeContext.pushContext();
aoqi@0 124
aoqi@0 125 if (e.hasAttributes()) {
aoqi@0 126 /*
aoqi@0 127 * Split the attribute nodes into namespace attributes
aoqi@0 128 * or normal attributes.
aoqi@0 129 */
aoqi@0 130 final NamedNodeMap nnm = e.getAttributes();
aoqi@0 131 for (int i = 0; i < nnm.getLength(); i++) {
aoqi@0 132 final Node a = nnm.item(i);
aoqi@0 133 final String namespaceURI = a.getNamespaceURI();
aoqi@0 134 if (namespaceURI != null && namespaceURI.equals("http://www.w3.org/2000/xmlns/")) {
aoqi@0 135 String attrPrefix = a.getLocalName();
aoqi@0 136 String attrNamespace = a.getNodeValue();
aoqi@0 137 if (attrPrefix == "xmlns" || attrPrefix.equals("xmlns")) {
aoqi@0 138 attrPrefix = "";
aoqi@0 139 }
aoqi@0 140 _namespaceScopeContext.declarePrefix(attrPrefix, attrNamespace);
aoqi@0 141 } else {
aoqi@0 142 if (attributesSize == _attributes.length) {
aoqi@0 143 final Node[] attributes = new Node[attributesSize * 3 / 2 + 1];
aoqi@0 144 System.arraycopy(_attributes, 0, attributes, 0, attributesSize);
aoqi@0 145 _attributes = attributes;
aoqi@0 146 }
aoqi@0 147 _attributes[attributesSize++] = a;
aoqi@0 148
aoqi@0 149 String attrNamespaceURI = a.getNamespaceURI();
aoqi@0 150 String attrPrefix = a.getPrefix();
aoqi@0 151 if (attrPrefix != null && !_namespaceScopeContext.getNamespaceURI(attrPrefix).equals(attrNamespaceURI)) {
aoqi@0 152 _namespaceScopeContext.declarePrefix(attrPrefix, attrNamespaceURI);
aoqi@0 153 }
aoqi@0 154 }
aoqi@0 155 }
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 String elementNamespaceURI = e.getNamespaceURI();
aoqi@0 159 String elementPrefix = e.getPrefix();
aoqi@0 160 if (elementPrefix == null) elementPrefix = "";
aoqi@0 161 if (elementNamespaceURI != null &&
aoqi@0 162 !_namespaceScopeContext.getNamespaceURI(elementPrefix).equals(elementNamespaceURI)) {
aoqi@0 163 _namespaceScopeContext.declarePrefix(elementPrefix, elementNamespaceURI);
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 if (!_namespaceScopeContext.isCurrentContextEmpty()) {
aoqi@0 167 if (attributesSize > 0) {
aoqi@0 168 write(EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_NAMESPACES_FLAG |
aoqi@0 169 EncodingConstants.ELEMENT_ATTRIBUTE_FLAG);
aoqi@0 170 } else {
aoqi@0 171 write(EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_NAMESPACES_FLAG);
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 for (int i = _namespaceScopeContext.getCurrentContextStartIndex();
aoqi@0 175 i < _namespaceScopeContext.getCurrentContextEndIndex(); i++) {
aoqi@0 176
aoqi@0 177 String prefix = _namespaceScopeContext.getPrefix(i);
aoqi@0 178 String uri = _namespaceScopeContext.getNamespaceURI(i);
aoqi@0 179 encodeNamespaceAttribute(prefix, uri);
aoqi@0 180 }
aoqi@0 181 write(EncodingConstants.TERMINATOR);
aoqi@0 182 _b = 0;
aoqi@0 183 } else {
aoqi@0 184 _b = (attributesSize > 0) ? EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_ATTRIBUTE_FLAG :
aoqi@0 185 EncodingConstants.ELEMENT;
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 String namespaceURI = elementNamespaceURI;
aoqi@0 189 // namespaceURI = (namespaceURI == null) ? _namespaceScopeContext.getNamespaceURI("") : namespaceURI;
aoqi@0 190 namespaceURI = (namespaceURI == null) ? "" : namespaceURI;
aoqi@0 191 encodeElement(namespaceURI, e.getNodeName(), e.getLocalName());
aoqi@0 192
aoqi@0 193 if (attributesSize > 0) {
aoqi@0 194 // Serialize the attributes
aoqi@0 195 for (int i = 0; i < attributesSize; i++) {
aoqi@0 196 final Node a = _attributes[i];
aoqi@0 197 _attributes[i] = null;
aoqi@0 198 namespaceURI = a.getNamespaceURI();
aoqi@0 199 namespaceURI = (namespaceURI == null) ? "" : namespaceURI;
aoqi@0 200 encodeAttribute(namespaceURI, a.getNodeName(), a.getLocalName());
aoqi@0 201
aoqi@0 202 final String value = a.getNodeValue();
aoqi@0 203 final boolean addToTable = isAttributeValueLengthMatchesLimit(value.length());
aoqi@0 204 encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable, false);
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 _b = EncodingConstants.TERMINATOR;
aoqi@0 208 _terminate = true;
aoqi@0 209 }
aoqi@0 210
aoqi@0 211 if (e.hasChildNodes()) {
aoqi@0 212 // Serialize the children
aoqi@0 213 final NodeList nl = e.getChildNodes();
aoqi@0 214 for (int i = 0; i < nl.getLength(); i++) {
aoqi@0 215 final Node n = nl.item(i);
aoqi@0 216 switch (n.getNodeType()) {
aoqi@0 217 case Node.ELEMENT_NODE:
aoqi@0 218 serializeElement(n);
aoqi@0 219 break;
aoqi@0 220 case Node.TEXT_NODE:
aoqi@0 221 serializeText(n);
aoqi@0 222 break;
aoqi@0 223 case Node.CDATA_SECTION_NODE:
aoqi@0 224 serializeCDATA(n);
aoqi@0 225 break;
aoqi@0 226 case Node.COMMENT_NODE:
aoqi@0 227 serializeComment(n);
aoqi@0 228 break;
aoqi@0 229 case Node.PROCESSING_INSTRUCTION_NODE:
aoqi@0 230 serializeProcessingInstruction(n);
aoqi@0 231 break;
aoqi@0 232 }
aoqi@0 233 }
aoqi@0 234 }
aoqi@0 235 encodeElementTermination();
aoqi@0 236 _namespaceScopeContext.popContext();
aoqi@0 237 }
aoqi@0 238
aoqi@0 239
aoqi@0 240 protected final void serializeText(Node t) throws IOException {
aoqi@0 241 final String text = t.getNodeValue();
aoqi@0 242
aoqi@0 243 final int length = (text != null) ? text.length() : 0;
aoqi@0 244 if (length == 0) {
aoqi@0 245 return;
aoqi@0 246 } else if (length < _charBuffer.length) {
aoqi@0 247 text.getChars(0, length, _charBuffer, 0);
aoqi@0 248 if (getIgnoreWhiteSpaceTextContent() &&
aoqi@0 249 isWhiteSpace(_charBuffer, 0, length)) return;
aoqi@0 250
aoqi@0 251 encodeTermination();
aoqi@0 252 encodeCharacters(_charBuffer, 0, length);
aoqi@0 253 } else {
aoqi@0 254 final char ch[] = text.toCharArray();
aoqi@0 255 if (getIgnoreWhiteSpaceTextContent() &&
aoqi@0 256 isWhiteSpace(ch, 0, length)) return;
aoqi@0 257
aoqi@0 258 encodeTermination();
aoqi@0 259 encodeCharactersNoClone(ch, 0, length);
aoqi@0 260 }
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 protected final void serializeCDATA(Node t) throws IOException {
aoqi@0 264 final String text = t.getNodeValue();
aoqi@0 265
aoqi@0 266 final int length = (text != null) ? text.length() : 0;
aoqi@0 267 if (length == 0) {
aoqi@0 268 return;
aoqi@0 269 } else {
aoqi@0 270 final char ch[] = text.toCharArray();
aoqi@0 271 if (getIgnoreWhiteSpaceTextContent() &&
aoqi@0 272 isWhiteSpace(ch, 0, length)) return;
aoqi@0 273
aoqi@0 274 encodeTermination();
aoqi@0 275 try {
aoqi@0 276 encodeCIIBuiltInAlgorithmDataAsCDATA(ch, 0, length);
aoqi@0 277 } catch (FastInfosetException e) {
aoqi@0 278 throw new IOException("");
aoqi@0 279 }
aoqi@0 280 }
aoqi@0 281 }
aoqi@0 282
aoqi@0 283 protected final void serializeComment(Node c) throws IOException {
aoqi@0 284 if (getIgnoreComments()) return;
aoqi@0 285
aoqi@0 286 encodeTermination();
aoqi@0 287
aoqi@0 288 final String comment = c.getNodeValue();
aoqi@0 289
aoqi@0 290 final int length = (comment != null) ? comment.length() : 0;
aoqi@0 291 if (length == 0) {
aoqi@0 292 encodeComment(_charBuffer, 0, 0);
aoqi@0 293 } else if (length < _charBuffer.length) {
aoqi@0 294 comment.getChars(0, length, _charBuffer, 0);
aoqi@0 295 encodeComment(_charBuffer, 0, length);
aoqi@0 296 } else {
aoqi@0 297 final char ch[] = comment.toCharArray();
aoqi@0 298 encodeCommentNoClone(ch, 0, length);
aoqi@0 299 }
aoqi@0 300 }
aoqi@0 301
aoqi@0 302 protected final void serializeProcessingInstruction(Node pi) throws IOException {
aoqi@0 303 if (getIgnoreProcesingInstructions()) return;
aoqi@0 304
aoqi@0 305 encodeTermination();
aoqi@0 306
aoqi@0 307 final String target = pi.getNodeName();
aoqi@0 308 final String data = pi.getNodeValue();
aoqi@0 309 encodeProcessingInstruction(target, data);
aoqi@0 310 }
aoqi@0 311
aoqi@0 312 protected final void encodeElement(String namespaceURI, String qName, String localName) throws IOException {
aoqi@0 313 LocalNameQualifiedNamesMap.Entry entry = _v.elementName.obtainEntry(qName);
aoqi@0 314 if (entry._valueIndex > 0) {
aoqi@0 315 final QualifiedName[] names = entry._value;
aoqi@0 316 for (int i = 0; i < entry._valueIndex; i++) {
aoqi@0 317 if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) {
aoqi@0 318 encodeNonZeroIntegerOnThirdBit(names[i].index);
aoqi@0 319 return;
aoqi@0 320 }
aoqi@0 321 }
aoqi@0 322 }
aoqi@0 323
aoqi@0 324 // Was DOM node created using an NS-aware call?
aoqi@0 325 if (localName != null) {
aoqi@0 326 encodeLiteralElementQualifiedNameOnThirdBit(namespaceURI, getPrefixFromQualifiedName(qName),
aoqi@0 327 localName, entry);
aoqi@0 328 } else {
aoqi@0 329 encodeLiteralElementQualifiedNameOnThirdBit(namespaceURI, "", qName, entry);
aoqi@0 330 }
aoqi@0 331 }
aoqi@0 332
aoqi@0 333 protected final void encodeAttribute(String namespaceURI, String qName, String localName) throws IOException {
aoqi@0 334 LocalNameQualifiedNamesMap.Entry entry = _v.attributeName.obtainEntry(qName);
aoqi@0 335 if (entry._valueIndex > 0) {
aoqi@0 336 final QualifiedName[] names = entry._value;
aoqi@0 337 for (int i = 0; i < entry._valueIndex; i++) {
aoqi@0 338 if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) {
aoqi@0 339 encodeNonZeroIntegerOnSecondBitFirstBitZero(names[i].index);
aoqi@0 340 return;
aoqi@0 341 }
aoqi@0 342 }
aoqi@0 343 }
aoqi@0 344
aoqi@0 345 // Was DOM node created using an NS-aware call?
aoqi@0 346 if (localName != null) {
aoqi@0 347 encodeLiteralAttributeQualifiedNameOnSecondBit(namespaceURI, getPrefixFromQualifiedName(qName),
aoqi@0 348 localName, entry);
aoqi@0 349 } else {
aoqi@0 350 encodeLiteralAttributeQualifiedNameOnSecondBit(namespaceURI, "", qName, entry);
aoqi@0 351 }
aoqi@0 352 }
aoqi@0 353 }

mercurial