aoqi@0: /*
aefimov@1443: * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.xml.internal.bind.v2.runtime.output;
aoqi@0:
aefimov@1443: import com.sun.xml.internal.bind.marshaller.NoEscapeHandler;
aoqi@0: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
aoqi@0: import com.sun.xml.internal.bind.v2.runtime.Name;
aoqi@0: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
aoqi@0: import javax.xml.stream.XMLStreamException;
aoqi@0:
aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data;
aoqi@0: import com.sun.xml.internal.fastinfoset.EncodingConstants;
aoqi@0: import com.sun.xml.internal.fastinfoset.stax.StAXDocumentSerializer;
aoqi@0: import java.io.IOException;
aoqi@0: import java.util.Collection;
aoqi@0: import java.util.Map;
aoqi@0: import java.util.WeakHashMap;
aoqi@0: import javax.xml.bind.JAXBContext;
aoqi@0: import com.sun.xml.internal.org.jvnet.fastinfoset.VocabularyApplicationData;
aoqi@0: import org.xml.sax.SAXException;
aoqi@0:
aoqi@0: /**
aoqi@0: * {@link XmlOutput} for {@link LowLevelStAXDocumentSerializer}.
aoqi@0: *
aoqi@0: * This class is responsible for managing the indexing of elements, attributes
aoqi@0: * and local names that are known to JAXB by way of the JAXBContext (generated
aoqi@0: * from JAXB beans or schema). The pre-encoded UTF-8 representations of known
aoqi@0: * local names are also utilized.
aoqi@0: *
aoqi@0: * The lookup of elements, attributes and local names with respect to a context
aoqi@0: * is very efficient. It relies on an incrementing base line so that look up is
aoqi@0: * performed in O(1) time and only uses static memory. When the base line reaches
aoqi@0: * a point where integer overflow will occur the arrays and base line are reset
aoqi@0: * (such an event is rare and will have little impact on performance).
aoqi@0: *
aoqi@0: * A weak map of JAXB contexts to optimized tables for attributes, elements and
aoqi@0: * local names is utilized and stored on the LowLevel StAX serializer. Thus,
aoqi@0: * optimized serializing can work other multiple serializing of JAXB beans using
aoqi@0: * the same LowLevel StAX serializer instance. This approach works best when JAXB
aoqi@0: * contexts are only created once per schema or JAXB beans (which is the recommended
aoqi@0: * practice as the creation JAXB contexts are expensive, they are thread safe and
aoqi@0: * can be reused).
aoqi@0: *
aoqi@0: * @author Paul.Sandoz@Sun.Com
aoqi@0: */
aoqi@0: public final class FastInfosetStreamWriterOutput extends XMLStreamWriterOutput {
aoqi@0: private final StAXDocumentSerializer fiout;
aoqi@0: private final Encoded[] localNames;
aoqi@0: private final TablesPerJAXBContext tables;
aoqi@0:
aoqi@0: /**
aoqi@0: * Holder for the optimzed element, attribute and
aoqi@0: * local name tables.
aoqi@0: */
aoqi@0: final static class TablesPerJAXBContext {
aoqi@0: final int[] elementIndexes;
aoqi@0: final int[] elementIndexPrefixes;
aoqi@0:
aoqi@0: final int[] attributeIndexes;
aoqi@0: final int[] localNameIndexes;
aoqi@0:
aoqi@0: /**
aoqi@0: * The offset of the index
aoqi@0: */
aoqi@0: int indexOffset;
aoqi@0:
aoqi@0: /**
aoqi@0: * The the maximum known value of an index
aoqi@0: */
aoqi@0: int maxIndex;
aoqi@0:
aoqi@0: /**
aoqi@0: * True if the tables require clearing
aoqi@0: */
aoqi@0: boolean requiresClear;
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a new set of tables for a JAXB context.
aoqi@0: *
aoqi@0: * @param content the JAXB context.
aoqi@0: * @param initialIndexOffset the initial index offset to calculate
aoqi@0: * the maximum possible index
aoqi@0: *
aoqi@0: */
aoqi@0: TablesPerJAXBContext(JAXBContextImpl context, int initialIndexOffset) {
aoqi@0: elementIndexes = new int[context.getNumberOfElementNames()];
aoqi@0: elementIndexPrefixes = new int[context.getNumberOfElementNames()];
aoqi@0: attributeIndexes = new int[context.getNumberOfAttributeNames()];
aoqi@0: localNameIndexes = new int[context.getNumberOfLocalNames()];
aoqi@0:
aoqi@0: indexOffset = 1;
aoqi@0: maxIndex = initialIndexOffset + elementIndexes.length + attributeIndexes.length;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Require that tables are cleared.
aoqi@0: */
aoqi@0: public void requireClearTables() {
aoqi@0: requiresClear = true;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Clear or reset the tables.
aoqi@0: *
aoqi@0: * @param initialIndexOffset the initial index offset to calculate
aoqi@0: * the maximum possible index
aoqi@0: */
aoqi@0: public void clearOrResetTables(int intialIndexOffset) {
aoqi@0: if (requiresClear) {
aoqi@0: requiresClear = false;
aoqi@0:
aoqi@0: // Increment offset to new position
aoqi@0: indexOffset += maxIndex;
aoqi@0: // Reset the maximum known value of an index
aoqi@0: maxIndex = intialIndexOffset + elementIndexes.length + attributeIndexes.length;
aoqi@0: // Check if there is enough free space
aoqi@0: // If overflow
aoqi@0: if ((indexOffset + maxIndex) < 0) {
aoqi@0: clearAll();
aoqi@0: }
aoqi@0: } else {
aoqi@0: // Reset the maximum known value of an index
aoqi@0: maxIndex = intialIndexOffset + elementIndexes.length + attributeIndexes.length;
aoqi@0: // Check if there is enough free space
aoqi@0: // If overflow
aoqi@0: if ((indexOffset + maxIndex) < 0) {
aoqi@0: resetAll();
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: private void clearAll() {
aoqi@0: clear(elementIndexes);
aoqi@0: clear(attributeIndexes);
aoqi@0: clear(localNameIndexes);
aoqi@0: indexOffset = 1;
aoqi@0: }
aoqi@0:
aoqi@0: private void clear(int[] array) {
aoqi@0: for (int i = 0; i < array.length; i++) {
aoqi@0: array[i] = 0;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Increment the maximum know index value
aoqi@0: *
aoqi@0: * The indexes are preserved.
aoqi@0: */
aoqi@0: public void incrementMaxIndexValue() {
aoqi@0: // Increment the maximum value of an index
aoqi@0: maxIndex++;
aoqi@0: // Check if there is enough free space
aoqi@0: // If overflow
aoqi@0: if ((indexOffset + maxIndex) < 0) {
aoqi@0: resetAll();
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: private void resetAll() {
aoqi@0: clear(elementIndexes);
aoqi@0: clear(attributeIndexes);
aoqi@0: clear(localNameIndexes);
aoqi@0: indexOffset = 1;
aoqi@0: }
aoqi@0:
aoqi@0: private void reset(int[] array) {
aoqi@0: for (int i = 0; i < array.length; i++) {
aoqi@0: if (array[i] > indexOffset) {
aoqi@0: array[i] = array[i] - indexOffset + 1;
aoqi@0: } else {
aoqi@0: array[i] = 0;
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Holder of JAXB contexts -> tables.
aoqi@0: *
aoqi@0: * An instance will be registered with the
aoqi@0: * {@link LowLevelStAXDocumentSerializer}.
aoqi@0: */
aoqi@0: final static class AppData implements VocabularyApplicationData {
aoqi@0: final Map contexts =
aoqi@0: new WeakHashMap();
aoqi@0: final Collection collectionOfContexts = contexts.values();
aoqi@0:
aoqi@0: /**
aoqi@0: * Clear all the tables.
aoqi@0: */
aoqi@0: public void clear() {
aoqi@0: for(TablesPerJAXBContext c : collectionOfContexts)
aoqi@0: c.requireClearTables();
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public FastInfosetStreamWriterOutput(StAXDocumentSerializer out,
aoqi@0: JAXBContextImpl context) {
aefimov@1443: super(out, NoEscapeHandler.theInstance);
aoqi@0:
aoqi@0: this.fiout = out;
aoqi@0: this.localNames = context.getUTF8NameTable();
aoqi@0:
aoqi@0: final VocabularyApplicationData vocabAppData = fiout.getVocabularyApplicationData();
aoqi@0: AppData appData = null;
aoqi@0: if (vocabAppData == null || !(vocabAppData instanceof AppData)) {
aoqi@0: appData = new AppData();
aoqi@0: fiout.setVocabularyApplicationData(appData);
aoqi@0: } else {
aoqi@0: appData = (AppData)vocabAppData;
aoqi@0: }
aoqi@0:
aoqi@0: final TablesPerJAXBContext tablesPerContext = appData.contexts.get(context);
aoqi@0: if (tablesPerContext != null) {
aoqi@0: tables = tablesPerContext;
aoqi@0: /**
aoqi@0: * Obtain the current local name index. Thus will be used to
aoqi@0: * calculate the maximum index value when serializing for this context
aoqi@0: */
aoqi@0: tables.clearOrResetTables(out.getLocalNameIndex());
aoqi@0: } else {
aoqi@0: tables = new TablesPerJAXBContext(context, out.getLocalNameIndex());
aoqi@0: appData.contexts.put(context, tables);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void startDocument(XMLSerializer serializer, boolean fragment,
aoqi@0: int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext)
aoqi@0: throws IOException, SAXException, XMLStreamException {
aoqi@0: super.startDocument(serializer, fragment, nsUriIndex2prefixIndex, nsContext);
aoqi@0:
aoqi@0: if (fragment)
aoqi@0: fiout.initiateLowLevelWriting();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException {
aoqi@0: super.endDocument(fragment);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void beginStartTag(Name name) throws IOException {
aoqi@0: fiout.writeLowLevelTerminationAndMark();
aoqi@0:
aoqi@0: if (nsContext.getCurrent().count() == 0) {
aoqi@0: final int qNameIndex = tables.elementIndexes[name.qNameIndex] - tables.indexOffset;
aoqi@0: final int prefixIndex = nsUriIndex2prefixIndex[name.nsUriIndex];
aoqi@0:
aoqi@0: if (qNameIndex >= 0 &&
aoqi@0: tables.elementIndexPrefixes[name.qNameIndex] == prefixIndex) {
aoqi@0: fiout.writeLowLevelStartElementIndexed(EncodingConstants.ELEMENT, qNameIndex);
aoqi@0: } else {
aoqi@0: tables.elementIndexes[name.qNameIndex] = fiout.getNextElementIndex() + tables.indexOffset;
aoqi@0: tables.elementIndexPrefixes[name.qNameIndex] = prefixIndex;
aoqi@0: writeLiteral(EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_LITERAL_QNAME_FLAG,
aoqi@0: name,
aoqi@0: nsContext.getPrefix(prefixIndex),
aoqi@0: nsContext.getNamespaceURI(prefixIndex));
aoqi@0: }
aoqi@0: } else {
aoqi@0: beginStartTagWithNamespaces(name);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public void beginStartTagWithNamespaces(Name name) throws IOException {
aoqi@0: final NamespaceContextImpl.Element nse = nsContext.getCurrent();
aoqi@0:
aoqi@0: fiout.writeLowLevelStartNamespaces();
aoqi@0: for (int i = nse.count() - 1; i >= 0; i--) {
aoqi@0: final String uri = nse.getNsUri(i);
aoqi@0: if (uri.length() == 0 && nse.getBase() == 1)
aoqi@0: continue; // no point in definint xmlns='' on the root
aoqi@0: fiout.writeLowLevelNamespace(nse.getPrefix(i), uri);
aoqi@0: }
aoqi@0: fiout.writeLowLevelEndNamespaces();
aoqi@0:
aoqi@0: final int qNameIndex = tables.elementIndexes[name.qNameIndex] - tables.indexOffset;
aoqi@0: final int prefixIndex = nsUriIndex2prefixIndex[name.nsUriIndex];
aoqi@0:
aoqi@0: if (qNameIndex >= 0 &&
aoqi@0: tables.elementIndexPrefixes[name.qNameIndex] == prefixIndex) {
aoqi@0: fiout.writeLowLevelStartElementIndexed(0, qNameIndex);
aoqi@0: } else {
aoqi@0: tables.elementIndexes[name.qNameIndex] = fiout.getNextElementIndex() + tables.indexOffset;
aoqi@0: tables.elementIndexPrefixes[name.qNameIndex] = prefixIndex;
aoqi@0: writeLiteral(EncodingConstants.ELEMENT_LITERAL_QNAME_FLAG,
aoqi@0: name,
aoqi@0: nsContext.getPrefix(prefixIndex),
aoqi@0: nsContext.getNamespaceURI(prefixIndex));
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void attribute(Name name, String value) throws IOException {
aoqi@0: fiout.writeLowLevelStartAttributes();
aoqi@0:
aoqi@0: final int qNameIndex = tables.attributeIndexes[name.qNameIndex] - tables.indexOffset;
aoqi@0: if (qNameIndex >= 0) {
aoqi@0: fiout.writeLowLevelAttributeIndexed(qNameIndex);
aoqi@0: } else {
aoqi@0: tables.attributeIndexes[name.qNameIndex] = fiout.getNextAttributeIndex() + tables.indexOffset;
aoqi@0:
aoqi@0: final int namespaceURIId = name.nsUriIndex;
aoqi@0: if (namespaceURIId == -1) {
aoqi@0: writeLiteral(EncodingConstants.ATTRIBUTE_LITERAL_QNAME_FLAG,
aoqi@0: name,
aoqi@0: "",
aoqi@0: "");
aoqi@0: } else {
aoqi@0: final int prefix = nsUriIndex2prefixIndex[namespaceURIId];
aoqi@0: writeLiteral(EncodingConstants.ATTRIBUTE_LITERAL_QNAME_FLAG,
aoqi@0: name,
aoqi@0: nsContext.getPrefix(prefix),
aoqi@0: nsContext.getNamespaceURI(prefix));
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: fiout.writeLowLevelAttributeValue(value);
aoqi@0: }
aoqi@0:
aoqi@0: private void writeLiteral(int type, Name name, String prefix, String namespaceURI) throws IOException {
aoqi@0: final int localNameIndex = tables.localNameIndexes[name.localNameIndex] - tables.indexOffset;
aoqi@0:
aoqi@0: if (localNameIndex < 0) {
aoqi@0: tables.localNameIndexes[name.localNameIndex] = fiout.getNextLocalNameIndex() + tables.indexOffset;
aoqi@0:
aoqi@0: fiout.writeLowLevelStartNameLiteral(
aoqi@0: type,
aoqi@0: prefix,
aoqi@0: localNames[name.localNameIndex].buf,
aoqi@0: namespaceURI);
aoqi@0: } else {
aoqi@0: fiout.writeLowLevelStartNameLiteral(
aoqi@0: type,
aoqi@0: prefix,
aoqi@0: localNameIndex,
aoqi@0: namespaceURI);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void endStartTag() throws IOException {
aoqi@0: fiout.writeLowLevelEndStartElement();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void endTag(Name name) throws IOException {
aoqi@0: fiout.writeLowLevelEndElement();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void endTag(int prefix, String localName) throws IOException {
aoqi@0: fiout.writeLowLevelEndElement();
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0: @Override
aoqi@0: public void text(Pcdata value, boolean needsSeparatingWhitespace) throws IOException {
aoqi@0: if (needsSeparatingWhitespace)
aoqi@0: fiout.writeLowLevelText(" ");
aoqi@0:
aoqi@0: /*
aoqi@0: * Check if the CharSequence is from a base64Binary data type
aoqi@0: */
aoqi@0: if (!(value instanceof Base64Data)) {
aoqi@0: final int len = value.length();
aoqi@0: if(len 0) {
aoqi@0: final NamespaceContextImpl.Element nse = nsContext.getCurrent();
aoqi@0:
aoqi@0: fiout.writeLowLevelStartNamespaces();
aoqi@0: for (int i = nse.count() - 1; i >= 0; i--) {
aoqi@0: final String uri = nse.getNsUri(i);
aoqi@0: if (uri.length() == 0 && nse.getBase() == 1)
aoqi@0: continue; // no point in definint xmlns='' on the root
aoqi@0: fiout.writeLowLevelNamespace(nse.getPrefix(i), uri);
aoqi@0: }
aoqi@0: fiout.writeLowLevelEndNamespaces();
aoqi@0:
aoqi@0: type= 0;
aoqi@0: }
aoqi@0:
aoqi@0: final boolean isIndexed = fiout.writeLowLevelStartElement(
aoqi@0: type,
aoqi@0: nsContext.getPrefix(prefix),
aoqi@0: localName,
aoqi@0: nsContext.getNamespaceURI(prefix));
aoqi@0:
aoqi@0: if (!isIndexed)
aoqi@0: tables.incrementMaxIndexValue();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void attribute(int prefix, String localName, String value) throws IOException {
aoqi@0: fiout.writeLowLevelStartAttributes();
aoqi@0:
aoqi@0: boolean isIndexed;
aoqi@0: if (prefix == -1)
aoqi@0: isIndexed = fiout.writeLowLevelAttribute("", "", localName);
aoqi@0: else
aoqi@0: isIndexed = fiout.writeLowLevelAttribute(
aoqi@0: nsContext.getPrefix(prefix),
aoqi@0: nsContext.getNamespaceURI(prefix),
aoqi@0: localName);
aoqi@0:
aoqi@0: if (!isIndexed)
aoqi@0: tables.incrementMaxIndexValue();
aoqi@0:
aoqi@0: fiout.writeLowLevelAttributeValue(value);
aoqi@0: }
aoqi@0: }