325N/A/*
325N/A * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A *
325N/A * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.fastinfoset.sax;
325N/A
325N/Aimport com.sun.xml.internal.fastinfoset.EncodingConstants;
325N/Aimport com.sun.xml.internal.fastinfoset.QualifiedName;
325N/Aimport com.sun.xml.internal.fastinfoset.util.KeyIntMap;
325N/Aimport com.sun.xml.internal.fastinfoset.util.LocalNameQualifiedNamesMap;
325N/Aimport com.sun.xml.internal.fastinfoset.util.StringIntMap;
325N/Aimport java.io.IOException;
325N/Aimport java.util.HashMap;
325N/Aimport org.xml.sax.SAXException;
325N/Aimport java.util.Map;
325N/Aimport com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetException;
325N/Aimport com.sun.xml.internal.org.jvnet.fastinfoset.RestrictedAlphabet;
325N/Aimport com.sun.xml.internal.org.jvnet.fastinfoset.sax.EncodingAlgorithmAttributes;
325N/Aimport org.xml.sax.Attributes;
325N/A
325N/A/**
325N/A * The Fast Infoset SAX serializer that maps prefixes to user specified prefixes
325N/A * that are specified in a namespace URI to prefix map.
325N/A * <p>
325N/A * This serializer will not preserve the original prefixes and this serializer
325N/A * should not be used when prefixes need to be preserved, such as the case
325N/A * when there are qualified names in content.
325N/A * <p>
325N/A * A namespace URI to prefix map is utilized such that the prefixes
325N/A * in the map are utilized rather than the prefixes specified in
325N/A * the qualified name for elements and attributes.
325N/A * <p>
325N/A * Any namespace declarations with a namespace URI that is not present in
325N/A * the map are added.
325N/A * <p>
325N/A */
325N/Apublic class SAXDocumentSerializerWithPrefixMapping extends SAXDocumentSerializer {
325N/A protected Map _namespaceToPrefixMapping;
325N/A protected Map _prefixToPrefixMapping;
325N/A protected String _lastCheckedNamespace;
325N/A protected String _lastCheckedPrefix;
325N/A
325N/A protected StringIntMap _declaredNamespaces;
325N/A
325N/A public SAXDocumentSerializerWithPrefixMapping(Map namespaceToPrefixMapping) {
325N/A // Use the local name to look up elements/attributes
325N/A super(true);
325N/A _namespaceToPrefixMapping = new HashMap(namespaceToPrefixMapping);
325N/A _prefixToPrefixMapping = new HashMap();
325N/A
325N/A // Empty prefix
325N/A _namespaceToPrefixMapping.put("", "");
325N/A // 'xml' prefix
325N/A _namespaceToPrefixMapping.put(EncodingConstants.XML_NAMESPACE_NAME, EncodingConstants.XML_NAMESPACE_PREFIX);
325N/A
325N/A _declaredNamespaces = new StringIntMap(4);
325N/A }
325N/A
325N/A public final void startPrefixMapping(String prefix, String uri) throws SAXException {
325N/A try {
325N/A if (_elementHasNamespaces == false) {
325N/A encodeTermination();
325N/A
325N/A // Mark the current buffer position to flag attributes if necessary
325N/A mark();
325N/A _elementHasNamespaces = true;
325N/A
325N/A // Write out Element byte with namespaces
325N/A write(EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_NAMESPACES_FLAG);
325N/A
325N/A _declaredNamespaces.clear();
325N/A _declaredNamespaces.obtainIndex(uri);
325N/A } else {
325N/A if (_declaredNamespaces.obtainIndex(uri) != KeyIntMap.NOT_PRESENT) {
325N/A final String p = getPrefix(uri);
325N/A if (p != null) {
325N/A _prefixToPrefixMapping.put(prefix, p);
325N/A }
325N/A return;
325N/A }
325N/A }
325N/A
325N/A final String p = getPrefix(uri);
325N/A if (p != null) {
325N/A encodeNamespaceAttribute(p, uri);
325N/A _prefixToPrefixMapping.put(prefix, p);
325N/A } else {
325N/A putPrefix(uri, prefix);
325N/A encodeNamespaceAttribute(prefix, uri);
325N/A }
325N/A
325N/A } catch (IOException e) {
325N/A throw new SAXException("startElement", e);
325N/A }
325N/A }
325N/A
325N/A protected final void encodeElement(String namespaceURI, String qName, String localName) throws IOException {
325N/A LocalNameQualifiedNamesMap.Entry entry = _v.elementName.obtainEntry(localName);
325N/A if (entry._valueIndex > 0) {
325N/A if (encodeElementMapEntry(entry, namespaceURI)) return;
325N/A // Check the entry is a member of the read only map
325N/A if (_v.elementName.isQNameFromReadOnlyMap(entry._value[0])) {
325N/A entry = _v.elementName.obtainDynamicEntry(localName);
325N/A if (entry._valueIndex > 0) {
325N/A if (encodeElementMapEntry(entry, namespaceURI)) return;
325N/A }
325N/A }
325N/A }
325N/A
325N/A encodeLiteralElementQualifiedNameOnThirdBit(namespaceURI, getPrefix(namespaceURI),
325N/A localName, entry);
325N/A }
325N/A
325N/A protected boolean encodeElementMapEntry(LocalNameQualifiedNamesMap.Entry entry, String namespaceURI) throws IOException {
325N/A QualifiedName[] names = entry._value;
325N/A for (int i = 0; i < entry._valueIndex; i++) {
325N/A if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) {
325N/A encodeNonZeroIntegerOnThirdBit(names[i].index);
325N/A return true;
325N/A }
325N/A }
325N/A return false;
325N/A }
325N/A
325N/A
325N/A protected final void encodeAttributes(Attributes atts) throws IOException, FastInfosetException {
325N/A boolean addToTable;
325N/A boolean mustToBeAddedToTable;
325N/A String value;
325N/A if (atts instanceof EncodingAlgorithmAttributes) {
325N/A final EncodingAlgorithmAttributes eAtts = (EncodingAlgorithmAttributes)atts;
325N/A Object data;
325N/A String alphabet;
325N/A for (int i = 0; i < eAtts.getLength(); i++) {
325N/A final String uri = atts.getURI(i);
325N/A if (encodeAttribute(uri, atts.getQName(i), atts.getLocalName(i))) {
325N/A data = eAtts.getAlgorithmData(i);
325N/A // If data is null then there is no algorithm data
325N/A if (data == null) {
325N/A value = eAtts.getValue(i);
325N/A addToTable = isAttributeValueLengthMatchesLimit(value.length());
325N/A mustToBeAddedToTable = eAtts.getToIndex(i);
325N/A alphabet = eAtts.getAlpababet(i);
325N/A if (alphabet == null) {
325N/A if (uri == "http://www.w3.org/2001/XMLSchema-instance" ||
325N/A uri.equals("http://www.w3.org/2001/XMLSchema-instance")) {
325N/A value = convertQName(value);
325N/A }
325N/A encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable, mustToBeAddedToTable);
325N/A } else if (alphabet == RestrictedAlphabet.DATE_TIME_CHARACTERS) {
325N/A encodeDateTimeNonIdentifyingStringOnFirstBit(
325N/A value, addToTable, mustToBeAddedToTable);
325N/A } else if (alphabet == RestrictedAlphabet.NUMERIC_CHARACTERS) {
325N/A encodeNumericNonIdentifyingStringOnFirstBit(
325N/A value, addToTable, mustToBeAddedToTable);
325N/A } else {
325N/A encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable, mustToBeAddedToTable);
325N/A }
325N/A } else {
325N/A encodeNonIdentifyingStringOnFirstBit(eAtts.getAlgorithmURI(i),
325N/A eAtts.getAlgorithmIndex(i), data);
325N/A }
325N/A }
325N/A }
325N/A } else {
325N/A for (int i = 0; i < atts.getLength(); i++) {
325N/A final String uri = atts.getURI(i);
325N/A if (encodeAttribute(atts.getURI(i), atts.getQName(i), atts.getLocalName(i))) {
325N/A value = atts.getValue(i);
325N/A addToTable = isAttributeValueLengthMatchesLimit(value.length());
325N/A
325N/A if (uri == "http://www.w3.org/2001/XMLSchema-instance" ||
325N/A uri.equals("http://www.w3.org/2001/XMLSchema-instance")) {
325N/A value = convertQName(value);
325N/A }
325N/A encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable, false);
325N/A }
325N/A }
325N/A }
325N/A _b = EncodingConstants.TERMINATOR;
325N/A _terminate = true;
325N/A }
325N/A
325N/A private String convertQName(String qName) {
325N/A int i = qName.indexOf(':');
325N/A String prefix = "";
325N/A String localName = qName;
325N/A if (i != -1) {
325N/A prefix = qName.substring(0, i);
325N/A localName = qName.substring(i + 1);
325N/A }
325N/A
325N/A String p = (String)_prefixToPrefixMapping.get(prefix);
325N/A if (p != null) {
325N/A if (p.length() == 0)
325N/A return localName;
325N/A else
325N/A return p + ":" + localName;
325N/A } else {
325N/A return qName;
325N/A }
325N/A }
325N/A
325N/A protected final boolean encodeAttribute(String namespaceURI, String qName, String localName) throws IOException {
325N/A LocalNameQualifiedNamesMap.Entry entry = _v.attributeName.obtainEntry(localName);
325N/A if (entry._valueIndex > 0) {
325N/A if (encodeAttributeMapEntry(entry, namespaceURI)) return true;
325N/A // Check the entry is a member of the read only map
325N/A if (_v.attributeName.isQNameFromReadOnlyMap(entry._value[0])) {
325N/A entry = _v.attributeName.obtainDynamicEntry(localName);
325N/A if (entry._valueIndex > 0) {
325N/A if (encodeAttributeMapEntry(entry, namespaceURI)) return true;
325N/A }
325N/A }
325N/A }
325N/A
325N/A return encodeLiteralAttributeQualifiedNameOnSecondBit(namespaceURI, getPrefix(namespaceURI),
325N/A localName, entry);
325N/A }
325N/A
325N/A protected boolean encodeAttributeMapEntry(LocalNameQualifiedNamesMap.Entry entry, String namespaceURI) throws IOException {
325N/A QualifiedName[] names = entry._value;
325N/A for (int i = 0; i < entry._valueIndex; i++) {
325N/A if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) {
325N/A encodeNonZeroIntegerOnSecondBitFirstBitZero(names[i].index);
325N/A return true;
325N/A }
325N/A }
325N/A return false;
325N/A }
325N/A
325N/A protected final String getPrefix(String namespaceURI) {
325N/A if (_lastCheckedNamespace == namespaceURI) return _lastCheckedPrefix;
325N/A
325N/A _lastCheckedNamespace = namespaceURI;
325N/A return _lastCheckedPrefix = (String)_namespaceToPrefixMapping.get(namespaceURI);
325N/A }
325N/A
325N/A protected final void putPrefix(String namespaceURI, String prefix) {
325N/A _namespaceToPrefixMapping.put(namespaceURI, prefix);
325N/A
325N/A _lastCheckedNamespace = namespaceURI;
325N/A _lastCheckedPrefix = prefix;
325N/A }
325N/A}