325N/A/*
325N/A * Copyright (c) 2005, 2010, 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
325N/Apackage com.sun.xml.internal.stream.buffer;
325N/A
325N/A/**
325N/A * Base class for classes that creates {@link MutableXMLStreamBuffer}
325N/A * and from infoset in API-specific form.
325N/A */
325N/Apublic class AbstractCreator extends AbstractCreatorProcessor {
325N/A
325N/A protected MutableXMLStreamBuffer _buffer;
325N/A
325N/A public void setXMLStreamBuffer(MutableXMLStreamBuffer buffer) {
325N/A if (buffer == null) {
325N/A throw new NullPointerException("buffer cannot be null");
325N/A }
325N/A setBuffer(buffer);
325N/A }
325N/A
325N/A public MutableXMLStreamBuffer getXMLStreamBuffer() {
325N/A return _buffer;
325N/A }
325N/A
325N/A
325N/A protected final void createBuffer() {
325N/A setBuffer(new MutableXMLStreamBuffer());
325N/A }
325N/A
325N/A /**
325N/A * Should be called whenever a new tree is stored on the buffer.
325N/A */
325N/A protected final void increaseTreeCount() {
325N/A _buffer.treeCount++;
325N/A }
325N/A
325N/A protected final void setBuffer(MutableXMLStreamBuffer buffer) {
325N/A _buffer = buffer;
325N/A
325N/A _currentStructureFragment = _buffer.getStructure();
325N/A _structure = _currentStructureFragment.getArray();
325N/A _structurePtr = 0;
325N/A
325N/A _currentStructureStringFragment = _buffer.getStructureStrings();
325N/A _structureStrings = _currentStructureStringFragment.getArray();
325N/A _structureStringsPtr = 0;
325N/A
325N/A _currentContentCharactersBufferFragment = _buffer.getContentCharactersBuffer();
325N/A _contentCharactersBuffer = _currentContentCharactersBufferFragment.getArray();
325N/A _contentCharactersBufferPtr = 0;
325N/A
325N/A _currentContentObjectFragment = _buffer.getContentObjects();
325N/A _contentObjects = _currentContentObjectFragment.getArray();
325N/A _contentObjectsPtr = 0;
325N/A }
325N/A
325N/A protected final void setHasInternedStrings(boolean hasInternedStrings) {
325N/A _buffer.setHasInternedStrings(hasInternedStrings);
325N/A }
325N/A
325N/A protected final void storeStructure(int b) {
325N/A _structure[_structurePtr++] = (byte)b;
325N/A if (_structurePtr == _structure.length) {
325N/A resizeStructure();
325N/A }
325N/A }
325N/A
325N/A protected final void resizeStructure() {
325N/A _structurePtr = 0;
325N/A if (_currentStructureFragment.getNext() != null) {
325N/A _currentStructureFragment = _currentStructureFragment.getNext();
325N/A _structure = _currentStructureFragment.getArray();
325N/A } else {
325N/A _structure = new byte[_structure.length];
325N/A _currentStructureFragment = new FragmentedArray(_structure, _currentStructureFragment);
325N/A }
325N/A }
325N/A
325N/A protected final void storeStructureString(String s) {
325N/A _structureStrings[_structureStringsPtr++] = s;
325N/A if (_structureStringsPtr == _structureStrings.length) {
325N/A resizeStructureStrings();
325N/A }
325N/A }
325N/A
325N/A protected final void resizeStructureStrings() {
325N/A _structureStringsPtr = 0;
325N/A if (_currentStructureStringFragment.getNext() != null) {
325N/A _currentStructureStringFragment = _currentStructureStringFragment.getNext();
325N/A _structureStrings = _currentStructureStringFragment.getArray();
325N/A } else {
325N/A _structureStrings = new String[_structureStrings.length];
325N/A _currentStructureStringFragment = new FragmentedArray(_structureStrings, _currentStructureStringFragment);
325N/A }
325N/A }
325N/A
325N/A protected final void storeContentString(String s) {
325N/A storeContentObject(s);
325N/A }
325N/A
325N/A protected final void storeContentCharacters(int type, char[] ch, int start, int length) {
325N/A if (_contentCharactersBufferPtr + length >= _contentCharactersBuffer.length) {
325N/A if (length >= 512) {
325N/A storeStructure(type | CONTENT_TYPE_CHAR_ARRAY_COPY);
325N/A storeContentCharactersCopy(ch, start, length);
325N/A return;
325N/A }
325N/A resizeContentCharacters();
325N/A }
325N/A
325N/A if (length < CHAR_ARRAY_LENGTH_SMALL_SIZE) {
325N/A storeStructure(type | CHAR_ARRAY_LENGTH_SMALL);
325N/A storeStructure(length);
325N/A System.arraycopy(ch, start, _contentCharactersBuffer, _contentCharactersBufferPtr, length);
325N/A _contentCharactersBufferPtr += length;
325N/A } else if (length < CHAR_ARRAY_LENGTH_MEDIUM_SIZE) {
325N/A storeStructure(type | CHAR_ARRAY_LENGTH_MEDIUM);
325N/A storeStructure(length >> 8);
325N/A storeStructure(length & 255);
325N/A System.arraycopy(ch, start, _contentCharactersBuffer, _contentCharactersBufferPtr, length);
325N/A _contentCharactersBufferPtr += length;
325N/A } else {
325N/A storeStructure(type | CONTENT_TYPE_CHAR_ARRAY_COPY);
325N/A storeContentCharactersCopy(ch, start, length);
325N/A }
325N/A }
325N/A
325N/A protected final void resizeContentCharacters() {
325N/A _contentCharactersBufferPtr = 0;
325N/A if (_currentContentCharactersBufferFragment.getNext() != null) {
325N/A _currentContentCharactersBufferFragment = _currentContentCharactersBufferFragment.getNext();
325N/A _contentCharactersBuffer = _currentContentCharactersBufferFragment.getArray();
325N/A } else {
325N/A _contentCharactersBuffer = new char[_contentCharactersBuffer.length];
325N/A _currentContentCharactersBufferFragment = new FragmentedArray(_contentCharactersBuffer,
325N/A _currentContentCharactersBufferFragment);
325N/A }
325N/A }
325N/A
325N/A protected final void storeContentCharactersCopy(char[] ch, int start, int length) {
325N/A char[] copyOfCh = new char[length];
325N/A System.arraycopy(ch, start, copyOfCh, 0, length);
325N/A storeContentObject(copyOfCh);
325N/A }
325N/A
325N/A protected final Object peekAtContentObject() {
325N/A return _contentObjects[_contentObjectsPtr];
325N/A }
325N/A
325N/A protected final void storeContentObject(Object s) {
325N/A _contentObjects[_contentObjectsPtr++] = s;
325N/A if (_contentObjectsPtr == _contentObjects.length) {
325N/A resizeContentObjects();
325N/A }
325N/A }
325N/A
325N/A protected final void resizeContentObjects() {
325N/A _contentObjectsPtr = 0;
325N/A if (_currentContentObjectFragment.getNext() != null) {
325N/A _currentContentObjectFragment = _currentContentObjectFragment.getNext();
325N/A _contentObjects = _currentContentObjectFragment.getArray();
325N/A } else {
325N/A _contentObjects = new Object[_contentObjects.length];
325N/A _currentContentObjectFragment = new FragmentedArray(_contentObjects, _currentContentObjectFragment);
325N/A }
325N/A }
325N/A}