1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/sax/SAXDocumentSerializerWithPrefixMapping.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,268 @@ 1.4 +/* 1.5 + * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + * 1.28 + * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. 1.29 + */ 1.30 + 1.31 +package com.sun.xml.internal.fastinfoset.sax; 1.32 + 1.33 +import com.sun.xml.internal.fastinfoset.EncodingConstants; 1.34 +import com.sun.xml.internal.fastinfoset.QualifiedName; 1.35 +import com.sun.xml.internal.fastinfoset.util.KeyIntMap; 1.36 +import com.sun.xml.internal.fastinfoset.util.LocalNameQualifiedNamesMap; 1.37 +import com.sun.xml.internal.fastinfoset.util.StringIntMap; 1.38 +import java.io.IOException; 1.39 +import java.util.HashMap; 1.40 +import org.xml.sax.SAXException; 1.41 +import java.util.Map; 1.42 +import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetException; 1.43 +import com.sun.xml.internal.org.jvnet.fastinfoset.RestrictedAlphabet; 1.44 +import com.sun.xml.internal.org.jvnet.fastinfoset.sax.EncodingAlgorithmAttributes; 1.45 +import org.xml.sax.Attributes; 1.46 + 1.47 +/** 1.48 + * The Fast Infoset SAX serializer that maps prefixes to user specified prefixes 1.49 + * that are specified in a namespace URI to prefix map. 1.50 + * <p> 1.51 + * This serializer will not preserve the original prefixes and this serializer 1.52 + * should not be used when prefixes need to be preserved, such as the case 1.53 + * when there are qualified names in content. 1.54 + * <p> 1.55 + * A namespace URI to prefix map is utilized such that the prefixes 1.56 + * in the map are utilized rather than the prefixes specified in 1.57 + * the qualified name for elements and attributes. 1.58 + * <p> 1.59 + * Any namespace declarations with a namespace URI that is not present in 1.60 + * the map are added. 1.61 + * <p> 1.62 + */ 1.63 +public class SAXDocumentSerializerWithPrefixMapping extends SAXDocumentSerializer { 1.64 + protected Map _namespaceToPrefixMapping; 1.65 + protected Map _prefixToPrefixMapping; 1.66 + protected String _lastCheckedNamespace; 1.67 + protected String _lastCheckedPrefix; 1.68 + 1.69 + protected StringIntMap _declaredNamespaces; 1.70 + 1.71 + public SAXDocumentSerializerWithPrefixMapping(Map namespaceToPrefixMapping) { 1.72 + // Use the local name to look up elements/attributes 1.73 + super(true); 1.74 + _namespaceToPrefixMapping = new HashMap(namespaceToPrefixMapping); 1.75 + _prefixToPrefixMapping = new HashMap(); 1.76 + 1.77 + // Empty prefix 1.78 + _namespaceToPrefixMapping.put("", ""); 1.79 + // 'xml' prefix 1.80 + _namespaceToPrefixMapping.put(EncodingConstants.XML_NAMESPACE_NAME, EncodingConstants.XML_NAMESPACE_PREFIX); 1.81 + 1.82 + _declaredNamespaces = new StringIntMap(4); 1.83 + } 1.84 + 1.85 + public final void startPrefixMapping(String prefix, String uri) throws SAXException { 1.86 + try { 1.87 + if (_elementHasNamespaces == false) { 1.88 + encodeTermination(); 1.89 + 1.90 + // Mark the current buffer position to flag attributes if necessary 1.91 + mark(); 1.92 + _elementHasNamespaces = true; 1.93 + 1.94 + // Write out Element byte with namespaces 1.95 + write(EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_NAMESPACES_FLAG); 1.96 + 1.97 + _declaredNamespaces.clear(); 1.98 + _declaredNamespaces.obtainIndex(uri); 1.99 + } else { 1.100 + if (_declaredNamespaces.obtainIndex(uri) != KeyIntMap.NOT_PRESENT) { 1.101 + final String p = getPrefix(uri); 1.102 + if (p != null) { 1.103 + _prefixToPrefixMapping.put(prefix, p); 1.104 + } 1.105 + return; 1.106 + } 1.107 + } 1.108 + 1.109 + final String p = getPrefix(uri); 1.110 + if (p != null) { 1.111 + encodeNamespaceAttribute(p, uri); 1.112 + _prefixToPrefixMapping.put(prefix, p); 1.113 + } else { 1.114 + putPrefix(uri, prefix); 1.115 + encodeNamespaceAttribute(prefix, uri); 1.116 + } 1.117 + 1.118 + } catch (IOException e) { 1.119 + throw new SAXException("startElement", e); 1.120 + } 1.121 + } 1.122 + 1.123 + protected final void encodeElement(String namespaceURI, String qName, String localName) throws IOException { 1.124 + LocalNameQualifiedNamesMap.Entry entry = _v.elementName.obtainEntry(localName); 1.125 + if (entry._valueIndex > 0) { 1.126 + if (encodeElementMapEntry(entry, namespaceURI)) return; 1.127 + // Check the entry is a member of the read only map 1.128 + if (_v.elementName.isQNameFromReadOnlyMap(entry._value[0])) { 1.129 + entry = _v.elementName.obtainDynamicEntry(localName); 1.130 + if (entry._valueIndex > 0) { 1.131 + if (encodeElementMapEntry(entry, namespaceURI)) return; 1.132 + } 1.133 + } 1.134 + } 1.135 + 1.136 + encodeLiteralElementQualifiedNameOnThirdBit(namespaceURI, getPrefix(namespaceURI), 1.137 + localName, entry); 1.138 + } 1.139 + 1.140 + protected boolean encodeElementMapEntry(LocalNameQualifiedNamesMap.Entry entry, String namespaceURI) throws IOException { 1.141 + QualifiedName[] names = entry._value; 1.142 + for (int i = 0; i < entry._valueIndex; i++) { 1.143 + if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) { 1.144 + encodeNonZeroIntegerOnThirdBit(names[i].index); 1.145 + return true; 1.146 + } 1.147 + } 1.148 + return false; 1.149 + } 1.150 + 1.151 + 1.152 + protected final void encodeAttributes(Attributes atts) throws IOException, FastInfosetException { 1.153 + boolean addToTable; 1.154 + boolean mustToBeAddedToTable; 1.155 + String value; 1.156 + if (atts instanceof EncodingAlgorithmAttributes) { 1.157 + final EncodingAlgorithmAttributes eAtts = (EncodingAlgorithmAttributes)atts; 1.158 + Object data; 1.159 + String alphabet; 1.160 + for (int i = 0; i < eAtts.getLength(); i++) { 1.161 + final String uri = atts.getURI(i); 1.162 + if (encodeAttribute(uri, atts.getQName(i), atts.getLocalName(i))) { 1.163 + data = eAtts.getAlgorithmData(i); 1.164 + // If data is null then there is no algorithm data 1.165 + if (data == null) { 1.166 + value = eAtts.getValue(i); 1.167 + addToTable = isAttributeValueLengthMatchesLimit(value.length()); 1.168 + mustToBeAddedToTable = eAtts.getToIndex(i); 1.169 + alphabet = eAtts.getAlpababet(i); 1.170 + if (alphabet == null) { 1.171 + if (uri == "http://www.w3.org/2001/XMLSchema-instance" || 1.172 + uri.equals("http://www.w3.org/2001/XMLSchema-instance")) { 1.173 + value = convertQName(value); 1.174 + } 1.175 + encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable, mustToBeAddedToTable); 1.176 + } else if (alphabet == RestrictedAlphabet.DATE_TIME_CHARACTERS) { 1.177 + encodeDateTimeNonIdentifyingStringOnFirstBit( 1.178 + value, addToTable, mustToBeAddedToTable); 1.179 + } else if (alphabet == RestrictedAlphabet.NUMERIC_CHARACTERS) { 1.180 + encodeNumericNonIdentifyingStringOnFirstBit( 1.181 + value, addToTable, mustToBeAddedToTable); 1.182 + } else { 1.183 + encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable, mustToBeAddedToTable); 1.184 + } 1.185 + } else { 1.186 + encodeNonIdentifyingStringOnFirstBit(eAtts.getAlgorithmURI(i), 1.187 + eAtts.getAlgorithmIndex(i), data); 1.188 + } 1.189 + } 1.190 + } 1.191 + } else { 1.192 + for (int i = 0; i < atts.getLength(); i++) { 1.193 + final String uri = atts.getURI(i); 1.194 + if (encodeAttribute(atts.getURI(i), atts.getQName(i), atts.getLocalName(i))) { 1.195 + value = atts.getValue(i); 1.196 + addToTable = isAttributeValueLengthMatchesLimit(value.length()); 1.197 + 1.198 + if (uri == "http://www.w3.org/2001/XMLSchema-instance" || 1.199 + uri.equals("http://www.w3.org/2001/XMLSchema-instance")) { 1.200 + value = convertQName(value); 1.201 + } 1.202 + encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable, false); 1.203 + } 1.204 + } 1.205 + } 1.206 + _b = EncodingConstants.TERMINATOR; 1.207 + _terminate = true; 1.208 + } 1.209 + 1.210 + private String convertQName(String qName) { 1.211 + int i = qName.indexOf(':'); 1.212 + String prefix = ""; 1.213 + String localName = qName; 1.214 + if (i != -1) { 1.215 + prefix = qName.substring(0, i); 1.216 + localName = qName.substring(i + 1); 1.217 + } 1.218 + 1.219 + String p = (String)_prefixToPrefixMapping.get(prefix); 1.220 + if (p != null) { 1.221 + if (p.length() == 0) 1.222 + return localName; 1.223 + else 1.224 + return p + ":" + localName; 1.225 + } else { 1.226 + return qName; 1.227 + } 1.228 + } 1.229 + 1.230 + protected final boolean encodeAttribute(String namespaceURI, String qName, String localName) throws IOException { 1.231 + LocalNameQualifiedNamesMap.Entry entry = _v.attributeName.obtainEntry(localName); 1.232 + if (entry._valueIndex > 0) { 1.233 + if (encodeAttributeMapEntry(entry, namespaceURI)) return true; 1.234 + // Check the entry is a member of the read only map 1.235 + if (_v.attributeName.isQNameFromReadOnlyMap(entry._value[0])) { 1.236 + entry = _v.attributeName.obtainDynamicEntry(localName); 1.237 + if (entry._valueIndex > 0) { 1.238 + if (encodeAttributeMapEntry(entry, namespaceURI)) return true; 1.239 + } 1.240 + } 1.241 + } 1.242 + 1.243 + return encodeLiteralAttributeQualifiedNameOnSecondBit(namespaceURI, getPrefix(namespaceURI), 1.244 + localName, entry); 1.245 + } 1.246 + 1.247 + protected boolean encodeAttributeMapEntry(LocalNameQualifiedNamesMap.Entry entry, String namespaceURI) throws IOException { 1.248 + QualifiedName[] names = entry._value; 1.249 + for (int i = 0; i < entry._valueIndex; i++) { 1.250 + if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) { 1.251 + encodeNonZeroIntegerOnSecondBitFirstBitZero(names[i].index); 1.252 + return true; 1.253 + } 1.254 + } 1.255 + return false; 1.256 + } 1.257 + 1.258 + protected final String getPrefix(String namespaceURI) { 1.259 + if (_lastCheckedNamespace == namespaceURI) return _lastCheckedPrefix; 1.260 + 1.261 + _lastCheckedNamespace = namespaceURI; 1.262 + return _lastCheckedPrefix = (String)_namespaceToPrefixMapping.get(namespaceURI); 1.263 + } 1.264 + 1.265 + protected final void putPrefix(String namespaceURI, String prefix) { 1.266 + _namespaceToPrefixMapping.put(namespaceURI, prefix); 1.267 + 1.268 + _lastCheckedNamespace = namespaceURI; 1.269 + _lastCheckedPrefix = prefix; 1.270 + } 1.271 +}