Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2005, 2012, 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 | |
aoqi@0 | 26 | package com.sun.xml.internal.stream.buffer; |
aoqi@0 | 27 | |
aoqi@0 | 28 | /** |
aoqi@0 | 29 | * Base class for classes that creates {@link MutableXMLStreamBuffer} |
aoqi@0 | 30 | * and from infoset in API-specific form. |
aoqi@0 | 31 | */ |
aoqi@0 | 32 | public class AbstractCreator extends AbstractCreatorProcessor { |
aoqi@0 | 33 | |
aoqi@0 | 34 | protected MutableXMLStreamBuffer _buffer; |
aoqi@0 | 35 | |
aoqi@0 | 36 | public void setXMLStreamBuffer(MutableXMLStreamBuffer buffer) { |
aoqi@0 | 37 | if (buffer == null) { |
aoqi@0 | 38 | throw new NullPointerException("buffer cannot be null"); |
aoqi@0 | 39 | } |
aoqi@0 | 40 | setBuffer(buffer); |
aoqi@0 | 41 | } |
aoqi@0 | 42 | |
aoqi@0 | 43 | public MutableXMLStreamBuffer getXMLStreamBuffer() { |
aoqi@0 | 44 | return _buffer; |
aoqi@0 | 45 | } |
aoqi@0 | 46 | |
aoqi@0 | 47 | |
aoqi@0 | 48 | protected final void createBuffer() { |
aoqi@0 | 49 | setBuffer(new MutableXMLStreamBuffer()); |
aoqi@0 | 50 | } |
aoqi@0 | 51 | |
aoqi@0 | 52 | /** |
aoqi@0 | 53 | * Should be called whenever a new tree is stored on the buffer. |
aoqi@0 | 54 | */ |
aoqi@0 | 55 | protected final void increaseTreeCount() { |
aoqi@0 | 56 | _buffer.treeCount++; |
aoqi@0 | 57 | } |
aoqi@0 | 58 | |
aoqi@0 | 59 | protected final void setBuffer(MutableXMLStreamBuffer buffer) { |
aoqi@0 | 60 | _buffer = buffer; |
aoqi@0 | 61 | |
aoqi@0 | 62 | _currentStructureFragment = _buffer.getStructure(); |
aoqi@0 | 63 | _structure = _currentStructureFragment.getArray(); |
aoqi@0 | 64 | _structurePtr = 0; |
aoqi@0 | 65 | |
aoqi@0 | 66 | _currentStructureStringFragment = _buffer.getStructureStrings(); |
aoqi@0 | 67 | _structureStrings = _currentStructureStringFragment.getArray(); |
aoqi@0 | 68 | _structureStringsPtr = 0; |
aoqi@0 | 69 | |
aoqi@0 | 70 | _currentContentCharactersBufferFragment = _buffer.getContentCharactersBuffer(); |
aoqi@0 | 71 | _contentCharactersBuffer = _currentContentCharactersBufferFragment.getArray(); |
aoqi@0 | 72 | _contentCharactersBufferPtr = 0; |
aoqi@0 | 73 | |
aoqi@0 | 74 | _currentContentObjectFragment = _buffer.getContentObjects(); |
aoqi@0 | 75 | _contentObjects = _currentContentObjectFragment.getArray(); |
aoqi@0 | 76 | _contentObjectsPtr = 0; |
aoqi@0 | 77 | } |
aoqi@0 | 78 | |
aoqi@0 | 79 | protected final void setHasInternedStrings(boolean hasInternedStrings) { |
aoqi@0 | 80 | _buffer.setHasInternedStrings(hasInternedStrings); |
aoqi@0 | 81 | } |
aoqi@0 | 82 | |
aoqi@0 | 83 | protected final void storeStructure(int b) { |
aoqi@0 | 84 | _structure[_structurePtr++] = (byte)b; |
aoqi@0 | 85 | if (_structurePtr == _structure.length) { |
aoqi@0 | 86 | resizeStructure(); |
aoqi@0 | 87 | } |
aoqi@0 | 88 | } |
aoqi@0 | 89 | |
aoqi@0 | 90 | protected final void resizeStructure() { |
aoqi@0 | 91 | _structurePtr = 0; |
aoqi@0 | 92 | if (_currentStructureFragment.getNext() != null) { |
aoqi@0 | 93 | _currentStructureFragment = _currentStructureFragment.getNext(); |
aoqi@0 | 94 | _structure = _currentStructureFragment.getArray(); |
aoqi@0 | 95 | } else { |
aoqi@0 | 96 | _structure = new byte[_structure.length]; |
aoqi@0 | 97 | _currentStructureFragment = new FragmentedArray(_structure, _currentStructureFragment); |
aoqi@0 | 98 | } |
aoqi@0 | 99 | } |
aoqi@0 | 100 | |
aoqi@0 | 101 | protected final void storeStructureString(String s) { |
aoqi@0 | 102 | _structureStrings[_structureStringsPtr++] = s; |
aoqi@0 | 103 | if (_structureStringsPtr == _structureStrings.length) { |
aoqi@0 | 104 | resizeStructureStrings(); |
aoqi@0 | 105 | } |
aoqi@0 | 106 | } |
aoqi@0 | 107 | |
aoqi@0 | 108 | protected final void resizeStructureStrings() { |
aoqi@0 | 109 | _structureStringsPtr = 0; |
aoqi@0 | 110 | if (_currentStructureStringFragment.getNext() != null) { |
aoqi@0 | 111 | _currentStructureStringFragment = _currentStructureStringFragment.getNext(); |
aoqi@0 | 112 | _structureStrings = _currentStructureStringFragment.getArray(); |
aoqi@0 | 113 | } else { |
aoqi@0 | 114 | _structureStrings = new String[_structureStrings.length]; |
aoqi@0 | 115 | _currentStructureStringFragment = new FragmentedArray(_structureStrings, _currentStructureStringFragment); |
aoqi@0 | 116 | } |
aoqi@0 | 117 | } |
aoqi@0 | 118 | |
aoqi@0 | 119 | protected final void storeContentString(String s) { |
aoqi@0 | 120 | storeContentObject(s); |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | protected final void storeContentCharacters(int type, char[] ch, int start, int length) { |
aoqi@0 | 124 | if (_contentCharactersBufferPtr + length >= _contentCharactersBuffer.length) { |
aoqi@0 | 125 | if (length >= 512) { |
aoqi@0 | 126 | storeStructure(type | CONTENT_TYPE_CHAR_ARRAY_COPY); |
aoqi@0 | 127 | storeContentCharactersCopy(ch, start, length); |
aoqi@0 | 128 | return; |
aoqi@0 | 129 | } |
aoqi@0 | 130 | resizeContentCharacters(); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | |
aoqi@0 | 133 | if (length < CHAR_ARRAY_LENGTH_SMALL_SIZE) { |
aoqi@0 | 134 | storeStructure(type); |
aoqi@0 | 135 | storeStructure(length); |
aoqi@0 | 136 | System.arraycopy(ch, start, _contentCharactersBuffer, _contentCharactersBufferPtr, length); |
aoqi@0 | 137 | _contentCharactersBufferPtr += length; |
aoqi@0 | 138 | } else if (length < CHAR_ARRAY_LENGTH_MEDIUM_SIZE) { |
aoqi@0 | 139 | storeStructure(type | CHAR_ARRAY_LENGTH_MEDIUM); |
aoqi@0 | 140 | storeStructure(length >> 8); |
aoqi@0 | 141 | storeStructure(length & 255); |
aoqi@0 | 142 | System.arraycopy(ch, start, _contentCharactersBuffer, _contentCharactersBufferPtr, length); |
aoqi@0 | 143 | _contentCharactersBufferPtr += length; |
aoqi@0 | 144 | } else { |
aoqi@0 | 145 | storeStructure(type | CONTENT_TYPE_CHAR_ARRAY_COPY); |
aoqi@0 | 146 | storeContentCharactersCopy(ch, start, length); |
aoqi@0 | 147 | } |
aoqi@0 | 148 | } |
aoqi@0 | 149 | |
aoqi@0 | 150 | protected final void resizeContentCharacters() { |
aoqi@0 | 151 | _contentCharactersBufferPtr = 0; |
aoqi@0 | 152 | if (_currentContentCharactersBufferFragment.getNext() != null) { |
aoqi@0 | 153 | _currentContentCharactersBufferFragment = _currentContentCharactersBufferFragment.getNext(); |
aoqi@0 | 154 | _contentCharactersBuffer = _currentContentCharactersBufferFragment.getArray(); |
aoqi@0 | 155 | } else { |
aoqi@0 | 156 | _contentCharactersBuffer = new char[_contentCharactersBuffer.length]; |
aoqi@0 | 157 | _currentContentCharactersBufferFragment = new FragmentedArray(_contentCharactersBuffer, |
aoqi@0 | 158 | _currentContentCharactersBufferFragment); |
aoqi@0 | 159 | } |
aoqi@0 | 160 | } |
aoqi@0 | 161 | |
aoqi@0 | 162 | protected final void storeContentCharactersCopy(char[] ch, int start, int length) { |
aoqi@0 | 163 | char[] copyOfCh = new char[length]; |
aoqi@0 | 164 | System.arraycopy(ch, start, copyOfCh, 0, length); |
aoqi@0 | 165 | storeContentObject(copyOfCh); |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | protected final Object peekAtContentObject() { |
aoqi@0 | 169 | return _contentObjects[_contentObjectsPtr]; |
aoqi@0 | 170 | } |
aoqi@0 | 171 | |
aoqi@0 | 172 | protected final void storeContentObject(Object s) { |
aoqi@0 | 173 | _contentObjects[_contentObjectsPtr++] = s; |
aoqi@0 | 174 | if (_contentObjectsPtr == _contentObjects.length) { |
aoqi@0 | 175 | resizeContentObjects(); |
aoqi@0 | 176 | } |
aoqi@0 | 177 | } |
aoqi@0 | 178 | |
aoqi@0 | 179 | protected final void resizeContentObjects() { |
aoqi@0 | 180 | _contentObjectsPtr = 0; |
aoqi@0 | 181 | if (_currentContentObjectFragment.getNext() != null) { |
aoqi@0 | 182 | _currentContentObjectFragment = _currentContentObjectFragment.getNext(); |
aoqi@0 | 183 | _contentObjects = _currentContentObjectFragment.getArray(); |
aoqi@0 | 184 | } else { |
aoqi@0 | 185 | _contentObjects = new Object[_contentObjects.length]; |
aoqi@0 | 186 | _currentContentObjectFragment = new FragmentedArray(_contentObjects, _currentContentObjectFragment); |
aoqi@0 | 187 | } |
aoqi@0 | 188 | } |
aoqi@0 | 189 | } |