1 /* 2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.stream.buffer; 27 28 /** 29 * Base class for classes that creates {@link MutableXMLStreamBuffer} 30 * and from infoset in API-specific form. 31 */ 32 public class AbstractCreator extends AbstractCreatorProcessor { 33 34 protected MutableXMLStreamBuffer _buffer; 35 setXMLStreamBuffer(MutableXMLStreamBuffer buffer)36 public void setXMLStreamBuffer(MutableXMLStreamBuffer buffer) { 37 if (buffer == null) { 38 throw new NullPointerException("buffer cannot be null"); 39 } 40 setBuffer(buffer); 41 } 42 getXMLStreamBuffer()43 public MutableXMLStreamBuffer getXMLStreamBuffer() { 44 return _buffer; 45 } 46 47 createBuffer()48 protected final void createBuffer() { 49 setBuffer(new MutableXMLStreamBuffer()); 50 } 51 52 /** 53 * Should be called whenever a new tree is stored on the buffer. 54 */ increaseTreeCount()55 protected final void increaseTreeCount() { 56 _buffer.treeCount++; 57 } 58 setBuffer(MutableXMLStreamBuffer buffer)59 protected final void setBuffer(MutableXMLStreamBuffer buffer) { 60 _buffer = buffer; 61 62 _currentStructureFragment = _buffer.getStructure(); 63 _structure = _currentStructureFragment.getArray(); 64 _structurePtr = 0; 65 66 _currentStructureStringFragment = _buffer.getStructureStrings(); 67 _structureStrings = _currentStructureStringFragment.getArray(); 68 _structureStringsPtr = 0; 69 70 _currentContentCharactersBufferFragment = _buffer.getContentCharactersBuffer(); 71 _contentCharactersBuffer = _currentContentCharactersBufferFragment.getArray(); 72 _contentCharactersBufferPtr = 0; 73 74 _currentContentObjectFragment = _buffer.getContentObjects(); 75 _contentObjects = _currentContentObjectFragment.getArray(); 76 _contentObjectsPtr = 0; 77 } 78 setHasInternedStrings(boolean hasInternedStrings)79 protected final void setHasInternedStrings(boolean hasInternedStrings) { 80 _buffer.setHasInternedStrings(hasInternedStrings); 81 } 82 storeStructure(int b)83 protected final void storeStructure(int b) { 84 _structure[_structurePtr++] = (byte)b; 85 if (_structurePtr == _structure.length) { 86 resizeStructure(); 87 } 88 } 89 resizeStructure()90 protected final void resizeStructure() { 91 _structurePtr = 0; 92 if (_currentStructureFragment.getNext() != null) { 93 _currentStructureFragment = _currentStructureFragment.getNext(); 94 _structure = _currentStructureFragment.getArray(); 95 } else { 96 _structure = new byte[_structure.length]; 97 _currentStructureFragment = new FragmentedArray(_structure, _currentStructureFragment); 98 } 99 } 100 storeStructureString(String s)101 protected final void storeStructureString(String s) { 102 _structureStrings[_structureStringsPtr++] = s; 103 if (_structureStringsPtr == _structureStrings.length) { 104 resizeStructureStrings(); 105 } 106 } 107 resizeStructureStrings()108 protected final void resizeStructureStrings() { 109 _structureStringsPtr = 0; 110 if (_currentStructureStringFragment.getNext() != null) { 111 _currentStructureStringFragment = _currentStructureStringFragment.getNext(); 112 _structureStrings = _currentStructureStringFragment.getArray(); 113 } else { 114 _structureStrings = new String[_structureStrings.length]; 115 _currentStructureStringFragment = new FragmentedArray(_structureStrings, _currentStructureStringFragment); 116 } 117 } 118 storeContentString(String s)119 protected final void storeContentString(String s) { 120 storeContentObject(s); 121 } 122 storeContentCharacters(int type, char[] ch, int start, int length)123 protected final void storeContentCharacters(int type, char[] ch, int start, int length) { 124 if (_contentCharactersBufferPtr + length >= _contentCharactersBuffer.length) { 125 if (length >= 512) { 126 storeStructure(type | CONTENT_TYPE_CHAR_ARRAY_COPY); 127 storeContentCharactersCopy(ch, start, length); 128 return; 129 } 130 resizeContentCharacters(); 131 } 132 133 if (length < CHAR_ARRAY_LENGTH_SMALL_SIZE) { 134 storeStructure(type); 135 storeStructure(length); 136 System.arraycopy(ch, start, _contentCharactersBuffer, _contentCharactersBufferPtr, length); 137 _contentCharactersBufferPtr += length; 138 } else if (length < CHAR_ARRAY_LENGTH_MEDIUM_SIZE) { 139 storeStructure(type | CHAR_ARRAY_LENGTH_MEDIUM); 140 storeStructure(length >> 8); 141 storeStructure(length & 255); 142 System.arraycopy(ch, start, _contentCharactersBuffer, _contentCharactersBufferPtr, length); 143 _contentCharactersBufferPtr += length; 144 } else { 145 storeStructure(type | CONTENT_TYPE_CHAR_ARRAY_COPY); 146 storeContentCharactersCopy(ch, start, length); 147 } 148 } 149 resizeContentCharacters()150 protected final void resizeContentCharacters() { 151 _contentCharactersBufferPtr = 0; 152 if (_currentContentCharactersBufferFragment.getNext() != null) { 153 _currentContentCharactersBufferFragment = _currentContentCharactersBufferFragment.getNext(); 154 _contentCharactersBuffer = _currentContentCharactersBufferFragment.getArray(); 155 } else { 156 _contentCharactersBuffer = new char[_contentCharactersBuffer.length]; 157 _currentContentCharactersBufferFragment = new FragmentedArray(_contentCharactersBuffer, 158 _currentContentCharactersBufferFragment); 159 } 160 } 161 storeContentCharactersCopy(char[] ch, int start, int length)162 protected final void storeContentCharactersCopy(char[] ch, int start, int length) { 163 char[] copyOfCh = new char[length]; 164 System.arraycopy(ch, start, copyOfCh, 0, length); 165 storeContentObject(copyOfCh); 166 } 167 peekAtContentObject()168 protected final Object peekAtContentObject() { 169 return _contentObjects[_contentObjectsPtr]; 170 } 171 storeContentObject(Object s)172 protected final void storeContentObject(Object s) { 173 _contentObjects[_contentObjectsPtr++] = s; 174 if (_contentObjectsPtr == _contentObjects.length) { 175 resizeContentObjects(); 176 } 177 } 178 resizeContentObjects()179 protected final void resizeContentObjects() { 180 _contentObjectsPtr = 0; 181 if (_currentContentObjectFragment.getNext() != null) { 182 _currentContentObjectFragment = _currentContentObjectFragment.getNext(); 183 _contentObjects = _currentContentObjectFragment.getArray(); 184 } else { 185 _contentObjects = new Object[_contentObjects.length]; 186 _currentContentObjectFragment = new FragmentedArray(_contentObjects, _currentContentObjectFragment); 187 } 188 } 189 } 190