001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: package com.sun.xml.stream.buffer;
021:
022: /**
023: * Base class for classes that creates {@link MutableXMLStreamBuffer}
024: * and from infoset in API-specific form.
025: */
026: public class AbstractCreator extends AbstractCreatorProcessor {
027:
028: protected MutableXMLStreamBuffer _buffer;
029:
030: public void setXMLStreamBuffer(MutableXMLStreamBuffer buffer) {
031: if (buffer == null) {
032: throw new NullPointerException("buffer cannot be null");
033: }
034: setBuffer(buffer);
035: }
036:
037: public MutableXMLStreamBuffer getXMLStreamBuffer() {
038: return _buffer;
039: }
040:
041: protected final void createBuffer() {
042: setBuffer(new MutableXMLStreamBuffer());
043: }
044:
045: /**
046: * Should be called whenever a new tree is stored on the buffer.
047: */
048: protected final void increaseTreeCount() {
049: _buffer.treeCount++;
050: }
051:
052: protected final void setBuffer(MutableXMLStreamBuffer buffer) {
053: _buffer = buffer;
054:
055: _currentStructureFragment = _buffer.getStructure();
056: _structure = _currentStructureFragment.getArray();
057: _structurePtr = 0;
058:
059: _currentStructureStringFragment = _buffer.getStructureStrings();
060: _structureStrings = _currentStructureStringFragment.getArray();
061: _structureStringsPtr = 0;
062:
063: _currentContentCharactersBufferFragment = _buffer
064: .getContentCharactersBuffer();
065: _contentCharactersBuffer = _currentContentCharactersBufferFragment
066: .getArray();
067: _contentCharactersBufferPtr = 0;
068:
069: _currentContentObjectFragment = _buffer.getContentObjects();
070: _contentObjects = _currentContentObjectFragment.getArray();
071: _contentObjectsPtr = 0;
072: }
073:
074: protected final void setHasInternedStrings(
075: boolean hasInternedStrings) {
076: _buffer.setHasInternedStrings(hasInternedStrings);
077: }
078:
079: protected final void storeStructure(int b) {
080: _structure[_structurePtr++] = (byte) b;
081: if (_structurePtr == _structure.length) {
082: resizeStructure();
083: }
084: }
085:
086: protected final void resizeStructure() {
087: _structurePtr = 0;
088: if (_currentStructureFragment.getNext() != null) {
089: _currentStructureFragment = _currentStructureFragment
090: .getNext();
091: _structure = _currentStructureFragment.getArray();
092: } else {
093: _structure = new byte[_structure.length];
094: _currentStructureFragment = new FragmentedArray(_structure,
095: _currentStructureFragment);
096: }
097: }
098:
099: protected final void storeStructureString(String s) {
100: _structureStrings[_structureStringsPtr++] = s;
101: if (_structureStringsPtr == _structureStrings.length) {
102: resizeStructureStrings();
103: }
104: }
105:
106: protected final void resizeStructureStrings() {
107: _structureStringsPtr = 0;
108: if (_currentStructureStringFragment.getNext() != null) {
109: _currentStructureStringFragment = _currentStructureStringFragment
110: .getNext();
111: _structureStrings = _currentStructureStringFragment
112: .getArray();
113: } else {
114: _structureStrings = new String[_structureStrings.length];
115: _currentStructureStringFragment = new FragmentedArray(
116: _structureStrings, _currentStructureStringFragment);
117: }
118: }
119:
120: protected final void storeContentString(String s) {
121: storeContentObject(s);
122: }
123:
124: protected final void storeContentCharacters(int type, char[] ch,
125: int start, int length) {
126: if (_contentCharactersBufferPtr + length >= _contentCharactersBuffer.length) {
127: if (length >= 512) {
128: storeStructure(type | CONTENT_TYPE_CHAR_ARRAY_COPY);
129: storeContentCharactersCopy(ch, start, length);
130: return;
131: }
132: resizeContentCharacters();
133: }
134:
135: if (length < CHAR_ARRAY_LENGTH_SMALL_SIZE) {
136: storeStructure(type | CHAR_ARRAY_LENGTH_SMALL);
137: storeStructure(length);
138: System.arraycopy(ch, start, _contentCharactersBuffer,
139: _contentCharactersBufferPtr, length);
140: _contentCharactersBufferPtr += length;
141: } else if (length < CHAR_ARRAY_LENGTH_MEDIUM_SIZE) {
142: storeStructure(type | CHAR_ARRAY_LENGTH_MEDIUM);
143: storeStructure(length >> 8);
144: storeStructure(length & 255);
145: System.arraycopy(ch, start, _contentCharactersBuffer,
146: _contentCharactersBufferPtr, length);
147: _contentCharactersBufferPtr += length;
148: } else {
149: storeStructure(type | CONTENT_TYPE_CHAR_ARRAY_COPY);
150: storeContentCharactersCopy(ch, start, length);
151: }
152: }
153:
154: protected final void resizeContentCharacters() {
155: _contentCharactersBufferPtr = 0;
156: if (_currentContentCharactersBufferFragment.getNext() != null) {
157: _currentContentCharactersBufferFragment = _currentContentCharactersBufferFragment
158: .getNext();
159: _contentCharactersBuffer = _currentContentCharactersBufferFragment
160: .getArray();
161: } else {
162: _contentCharactersBuffer = new char[_contentCharactersBuffer.length];
163: _currentContentCharactersBufferFragment = new FragmentedArray(
164: _contentCharactersBuffer,
165: _currentContentCharactersBufferFragment);
166: }
167: }
168:
169: protected final void storeContentCharactersCopy(char[] ch,
170: int start, int length) {
171: char[] copyOfCh = new char[length];
172: System.arraycopy(ch, start, copyOfCh, 0, length);
173: storeContentObject(copyOfCh);
174: }
175:
176: protected final Object peekAtContentObject() {
177: return _contentObjects[_contentObjectsPtr];
178: }
179:
180: protected final void storeContentObject(Object s) {
181: _contentObjects[_contentObjectsPtr++] = s;
182: if (_contentObjectsPtr == _contentObjects.length) {
183: resizeContentObjects();
184: }
185: }
186:
187: protected final void resizeContentObjects() {
188: _contentObjectsPtr = 0;
189: if (_currentContentObjectFragment.getNext() != null) {
190: _currentContentObjectFragment = _currentContentObjectFragment
191: .getNext();
192: _contentObjects = _currentContentObjectFragment.getArray();
193: } else {
194: _contentObjects = new Object[_contentObjects.length];
195: _currentContentObjectFragment = new FragmentedArray(
196: _contentObjects, _currentContentObjectFragment);
197: }
198: }
199: }
|