src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/sax/SAXDocumentSerializerWithPrefixMapping.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.sax;
aoqi@0 29
aoqi@0 30 import com.sun.xml.internal.fastinfoset.EncodingConstants;
aoqi@0 31 import com.sun.xml.internal.fastinfoset.QualifiedName;
aoqi@0 32 import com.sun.xml.internal.fastinfoset.util.KeyIntMap;
aoqi@0 33 import com.sun.xml.internal.fastinfoset.util.LocalNameQualifiedNamesMap;
aoqi@0 34 import com.sun.xml.internal.fastinfoset.util.StringIntMap;
aoqi@0 35 import java.io.IOException;
aoqi@0 36 import java.util.HashMap;
aoqi@0 37 import org.xml.sax.SAXException;
aoqi@0 38 import java.util.Map;
aoqi@0 39 import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetException;
aoqi@0 40 import com.sun.xml.internal.org.jvnet.fastinfoset.RestrictedAlphabet;
aoqi@0 41 import com.sun.xml.internal.org.jvnet.fastinfoset.sax.EncodingAlgorithmAttributes;
aoqi@0 42 import org.xml.sax.Attributes;
aoqi@0 43
aoqi@0 44 /**
aoqi@0 45 * The Fast Infoset SAX serializer that maps prefixes to user specified prefixes
aoqi@0 46 * that are specified in a namespace URI to prefix map.
aoqi@0 47 * <p>
aoqi@0 48 * This serializer will not preserve the original prefixes and this serializer
aoqi@0 49 * should not be used when prefixes need to be preserved, such as the case
aoqi@0 50 * when there are qualified names in content.
aoqi@0 51 * <p>
aoqi@0 52 * A namespace URI to prefix map is utilized such that the prefixes
aoqi@0 53 * in the map are utilized rather than the prefixes specified in
aoqi@0 54 * the qualified name for elements and attributes.
aoqi@0 55 * <p>
aoqi@0 56 * Any namespace declarations with a namespace URI that is not present in
aoqi@0 57 * the map are added.
aoqi@0 58 * <p>
aoqi@0 59 */
aoqi@0 60 public class SAXDocumentSerializerWithPrefixMapping extends SAXDocumentSerializer {
aoqi@0 61 protected Map _namespaceToPrefixMapping;
aoqi@0 62 protected Map _prefixToPrefixMapping;
aoqi@0 63 protected String _lastCheckedNamespace;
aoqi@0 64 protected String _lastCheckedPrefix;
aoqi@0 65
aoqi@0 66 protected StringIntMap _declaredNamespaces;
aoqi@0 67
aoqi@0 68 public SAXDocumentSerializerWithPrefixMapping(Map namespaceToPrefixMapping) {
aoqi@0 69 // Use the local name to look up elements/attributes
aoqi@0 70 super(true);
aoqi@0 71 _namespaceToPrefixMapping = new HashMap(namespaceToPrefixMapping);
aoqi@0 72 _prefixToPrefixMapping = new HashMap();
aoqi@0 73
aoqi@0 74 // Empty prefix
aoqi@0 75 _namespaceToPrefixMapping.put("", "");
aoqi@0 76 // 'xml' prefix
aoqi@0 77 _namespaceToPrefixMapping.put(EncodingConstants.XML_NAMESPACE_NAME, EncodingConstants.XML_NAMESPACE_PREFIX);
aoqi@0 78
aoqi@0 79 _declaredNamespaces = new StringIntMap(4);
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 public final void startPrefixMapping(String prefix, String uri) throws SAXException {
aoqi@0 83 try {
aoqi@0 84 if (_elementHasNamespaces == false) {
aoqi@0 85 encodeTermination();
aoqi@0 86
aoqi@0 87 // Mark the current buffer position to flag attributes if necessary
aoqi@0 88 mark();
aoqi@0 89 _elementHasNamespaces = true;
aoqi@0 90
aoqi@0 91 // Write out Element byte with namespaces
aoqi@0 92 write(EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_NAMESPACES_FLAG);
aoqi@0 93
aoqi@0 94 _declaredNamespaces.clear();
aoqi@0 95 _declaredNamespaces.obtainIndex(uri);
aoqi@0 96 } else {
aoqi@0 97 if (_declaredNamespaces.obtainIndex(uri) != KeyIntMap.NOT_PRESENT) {
aoqi@0 98 final String p = getPrefix(uri);
aoqi@0 99 if (p != null) {
aoqi@0 100 _prefixToPrefixMapping.put(prefix, p);
aoqi@0 101 }
aoqi@0 102 return;
aoqi@0 103 }
aoqi@0 104 }
aoqi@0 105
aoqi@0 106 final String p = getPrefix(uri);
aoqi@0 107 if (p != null) {
aoqi@0 108 encodeNamespaceAttribute(p, uri);
aoqi@0 109 _prefixToPrefixMapping.put(prefix, p);
aoqi@0 110 } else {
aoqi@0 111 putPrefix(uri, prefix);
aoqi@0 112 encodeNamespaceAttribute(prefix, uri);
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 } catch (IOException e) {
aoqi@0 116 throw new SAXException("startElement", e);
aoqi@0 117 }
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 protected final void encodeElement(String namespaceURI, String qName, String localName) throws IOException {
aoqi@0 121 LocalNameQualifiedNamesMap.Entry entry = _v.elementName.obtainEntry(localName);
aoqi@0 122 if (entry._valueIndex > 0) {
aoqi@0 123 if (encodeElementMapEntry(entry, namespaceURI)) return;
aoqi@0 124 // Check the entry is a member of the read only map
aoqi@0 125 if (_v.elementName.isQNameFromReadOnlyMap(entry._value[0])) {
aoqi@0 126 entry = _v.elementName.obtainDynamicEntry(localName);
aoqi@0 127 if (entry._valueIndex > 0) {
aoqi@0 128 if (encodeElementMapEntry(entry, namespaceURI)) return;
aoqi@0 129 }
aoqi@0 130 }
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 encodeLiteralElementQualifiedNameOnThirdBit(namespaceURI, getPrefix(namespaceURI),
aoqi@0 134 localName, entry);
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 protected boolean encodeElementMapEntry(LocalNameQualifiedNamesMap.Entry entry, String namespaceURI) throws IOException {
aoqi@0 138 QualifiedName[] names = entry._value;
aoqi@0 139 for (int i = 0; i < entry._valueIndex; i++) {
aoqi@0 140 if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) {
aoqi@0 141 encodeNonZeroIntegerOnThirdBit(names[i].index);
aoqi@0 142 return true;
aoqi@0 143 }
aoqi@0 144 }
aoqi@0 145 return false;
aoqi@0 146 }
aoqi@0 147
aoqi@0 148
aoqi@0 149 protected final void encodeAttributes(Attributes atts) throws IOException, FastInfosetException {
aoqi@0 150 boolean addToTable;
aoqi@0 151 boolean mustToBeAddedToTable;
aoqi@0 152 String value;
aoqi@0 153 if (atts instanceof EncodingAlgorithmAttributes) {
aoqi@0 154 final EncodingAlgorithmAttributes eAtts = (EncodingAlgorithmAttributes)atts;
aoqi@0 155 Object data;
aoqi@0 156 String alphabet;
aoqi@0 157 for (int i = 0; i < eAtts.getLength(); i++) {
aoqi@0 158 final String uri = atts.getURI(i);
aoqi@0 159 if (encodeAttribute(uri, atts.getQName(i), atts.getLocalName(i))) {
aoqi@0 160 data = eAtts.getAlgorithmData(i);
aoqi@0 161 // If data is null then there is no algorithm data
aoqi@0 162 if (data == null) {
aoqi@0 163 value = eAtts.getValue(i);
aoqi@0 164 addToTable = isAttributeValueLengthMatchesLimit(value.length());
aoqi@0 165 mustToBeAddedToTable = eAtts.getToIndex(i);
aoqi@0 166 alphabet = eAtts.getAlpababet(i);
aoqi@0 167 if (alphabet == null) {
aoqi@0 168 if (uri == "http://www.w3.org/2001/XMLSchema-instance" ||
aoqi@0 169 uri.equals("http://www.w3.org/2001/XMLSchema-instance")) {
aoqi@0 170 value = convertQName(value);
aoqi@0 171 }
aoqi@0 172 encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable, mustToBeAddedToTable);
aoqi@0 173 } else if (alphabet == RestrictedAlphabet.DATE_TIME_CHARACTERS) {
aoqi@0 174 encodeDateTimeNonIdentifyingStringOnFirstBit(
aoqi@0 175 value, addToTable, mustToBeAddedToTable);
aoqi@0 176 } else if (alphabet == RestrictedAlphabet.NUMERIC_CHARACTERS) {
aoqi@0 177 encodeNumericNonIdentifyingStringOnFirstBit(
aoqi@0 178 value, addToTable, mustToBeAddedToTable);
aoqi@0 179 } else {
aoqi@0 180 encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable, mustToBeAddedToTable);
aoqi@0 181 }
aoqi@0 182 } else {
aoqi@0 183 encodeNonIdentifyingStringOnFirstBit(eAtts.getAlgorithmURI(i),
aoqi@0 184 eAtts.getAlgorithmIndex(i), data);
aoqi@0 185 }
aoqi@0 186 }
aoqi@0 187 }
aoqi@0 188 } else {
aoqi@0 189 for (int i = 0; i < atts.getLength(); i++) {
aoqi@0 190 final String uri = atts.getURI(i);
aoqi@0 191 if (encodeAttribute(atts.getURI(i), atts.getQName(i), atts.getLocalName(i))) {
aoqi@0 192 value = atts.getValue(i);
aoqi@0 193 addToTable = isAttributeValueLengthMatchesLimit(value.length());
aoqi@0 194
aoqi@0 195 if (uri == "http://www.w3.org/2001/XMLSchema-instance" ||
aoqi@0 196 uri.equals("http://www.w3.org/2001/XMLSchema-instance")) {
aoqi@0 197 value = convertQName(value);
aoqi@0 198 }
aoqi@0 199 encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable, false);
aoqi@0 200 }
aoqi@0 201 }
aoqi@0 202 }
aoqi@0 203 _b = EncodingConstants.TERMINATOR;
aoqi@0 204 _terminate = true;
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 private String convertQName(String qName) {
aoqi@0 208 int i = qName.indexOf(':');
aoqi@0 209 String prefix = "";
aoqi@0 210 String localName = qName;
aoqi@0 211 if (i != -1) {
aoqi@0 212 prefix = qName.substring(0, i);
aoqi@0 213 localName = qName.substring(i + 1);
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 String p = (String)_prefixToPrefixMapping.get(prefix);
aoqi@0 217 if (p != null) {
aoqi@0 218 if (p.length() == 0)
aoqi@0 219 return localName;
aoqi@0 220 else
aoqi@0 221 return p + ":" + localName;
aoqi@0 222 } else {
aoqi@0 223 return qName;
aoqi@0 224 }
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 protected final boolean encodeAttribute(String namespaceURI, String qName, String localName) throws IOException {
aoqi@0 228 LocalNameQualifiedNamesMap.Entry entry = _v.attributeName.obtainEntry(localName);
aoqi@0 229 if (entry._valueIndex > 0) {
aoqi@0 230 if (encodeAttributeMapEntry(entry, namespaceURI)) return true;
aoqi@0 231 // Check the entry is a member of the read only map
aoqi@0 232 if (_v.attributeName.isQNameFromReadOnlyMap(entry._value[0])) {
aoqi@0 233 entry = _v.attributeName.obtainDynamicEntry(localName);
aoqi@0 234 if (entry._valueIndex > 0) {
aoqi@0 235 if (encodeAttributeMapEntry(entry, namespaceURI)) return true;
aoqi@0 236 }
aoqi@0 237 }
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 return encodeLiteralAttributeQualifiedNameOnSecondBit(namespaceURI, getPrefix(namespaceURI),
aoqi@0 241 localName, entry);
aoqi@0 242 }
aoqi@0 243
aoqi@0 244 protected boolean encodeAttributeMapEntry(LocalNameQualifiedNamesMap.Entry entry, String namespaceURI) throws IOException {
aoqi@0 245 QualifiedName[] names = entry._value;
aoqi@0 246 for (int i = 0; i < entry._valueIndex; i++) {
aoqi@0 247 if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) {
aoqi@0 248 encodeNonZeroIntegerOnSecondBitFirstBitZero(names[i].index);
aoqi@0 249 return true;
aoqi@0 250 }
aoqi@0 251 }
aoqi@0 252 return false;
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 protected final String getPrefix(String namespaceURI) {
aoqi@0 256 if (_lastCheckedNamespace == namespaceURI) return _lastCheckedPrefix;
aoqi@0 257
aoqi@0 258 _lastCheckedNamespace = namespaceURI;
aoqi@0 259 return _lastCheckedPrefix = (String)_namespaceToPrefixMapping.get(namespaceURI);
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 protected final void putPrefix(String namespaceURI, String prefix) {
aoqi@0 263 _namespaceToPrefixMapping.put(namespaceURI, prefix);
aoqi@0 264
aoqi@0 265 _lastCheckedNamespace = namespaceURI;
aoqi@0 266 _lastCheckedPrefix = prefix;
aoqi@0 267 }
aoqi@0 268 }

mercurial