0
|
1 /*--
|
|
2
|
|
3 $Id: TextBuffer.java,v 1.10 2007/11/10 05:29:00 jhunter Exp $
|
|
4
|
|
5 Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
|
|
6 All rights reserved.
|
|
7
|
|
8 Redistribution and use in source and binary forms, with or without
|
|
9 modification, are permitted provided that the following conditions
|
|
10 are met:
|
|
11
|
|
12 1. Redistributions of source code must retain the above copyright
|
|
13 notice, this list of conditions, and the following disclaimer.
|
|
14
|
|
15 2. Redistributions in binary form must reproduce the above copyright
|
|
16 notice, this list of conditions, and the disclaimer that follows
|
|
17 these conditions in the documentation and/or other materials
|
|
18 provided with the distribution.
|
|
19
|
|
20 3. The name "JDOM" must not be used to endorse or promote products
|
|
21 derived from this software without prior written permission. For
|
|
22 written permission, please contact <request_AT_jdom_DOT_org>.
|
|
23
|
|
24 4. Products derived from this software may not be called "JDOM", nor
|
|
25 may "JDOM" appear in their name, without prior written permission
|
|
26 from the JDOM Project Management <request_AT_jdom_DOT_org>.
|
|
27
|
|
28 In addition, we request (but do not require) that you include in the
|
|
29 end-user documentation provided with the redistribution and/or in the
|
|
30 software itself an acknowledgement equivalent to the following:
|
|
31 "This product includes software developed by the
|
|
32 JDOM Project (http://www.jdom.org/)."
|
|
33 Alternatively, the acknowledgment may be graphical using the logos
|
|
34 available at http://www.jdom.org/images/logos.
|
|
35
|
|
36 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
37 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
38 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
39 DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
|
|
40 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
41 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
42 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
43 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
44 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
45 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
46 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
47 SUCH DAMAGE.
|
|
48
|
|
49 This software consists of voluntary contributions made by many
|
|
50 individuals on behalf of the JDOM Project and was originally
|
|
51 created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
|
|
52 Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
|
|
53 on the JDOM Project, please see <http://www.jdom.org/>.
|
|
54
|
|
55 */
|
|
56
|
|
57 package org.jdom.input;
|
|
58
|
|
59 import org.jdom.*;
|
|
60
|
|
61 /**
|
|
62 * A non-public utility class similar to StringBuffer but optimized for XML
|
|
63 * parsing where the common case is that you get only one chunk of characters
|
|
64 * per text section. TextBuffer stores the first chunk of characters in a
|
|
65 * String, which can just be returned directly if no second chunk is received.
|
|
66 * Subsequent chunks are stored in a supplemental char array (like StringBuffer
|
|
67 * uses). In this case, the returned text will be the first String chunk,
|
|
68 * concatenated with the subsequent chunks stored in the char array. This
|
|
69 * provides optimal performance in the common case, while still providing very
|
|
70 * good performance in the uncommon case. Furthermore, avoiding StringBuffer
|
|
71 * means that no extra unused char array space will be kept around after parsing
|
|
72 * is through.
|
|
73 *
|
|
74 * @version $Revision: 1.10 $, $Date: 2007/11/10 05:29:00 $
|
|
75 * @author Bradley S. Huffman
|
|
76 * @author Alex Rosen
|
|
77 */
|
|
78 class TextBuffer {
|
|
79
|
|
80 private static final String CVS_ID =
|
|
81 "@(#) $RCSfile: TextBuffer.java,v $ $Revision: 1.10 $ $Date: 2007/11/10 05:29:00 $ $Name: jdom_1_1_1 $";
|
|
82
|
|
83 /** The first part of the text value (the "prefix"). If null, the
|
|
84 * text value is the empty string. */
|
|
85 private String prefixString;
|
|
86
|
|
87 /** The rest of the text value (the "suffix"). Only the first
|
|
88 * code>arraySize</code> characters are valid. */
|
|
89 private char[] array;
|
|
90
|
|
91 /** The size of the rest of the text value. If zero, then only
|
|
92 * code>prefixString</code> contains the text value. */
|
|
93 private int arraySize;
|
|
94
|
|
95 /** Constructor */
|
|
96 TextBuffer() {
|
|
97 array = new char[4096]; // initial capacity
|
|
98 arraySize = 0;
|
|
99 }
|
|
100
|
|
101 /** Append the specified text to the text value of this buffer. */
|
|
102 void append(char[] source, int start, int count) {
|
|
103 if (prefixString == null) {
|
|
104 // This is the first chunk, so we'll store it in the prefix string
|
|
105 prefixString = new String(source, start, count);
|
|
106 }
|
|
107 else {
|
|
108 // This is a subsequent chunk, so we'll add it to the char array
|
|
109 ensureCapacity(arraySize + count);
|
|
110 System.arraycopy(source, start, array, arraySize, count);
|
|
111 arraySize += count;
|
|
112 }
|
|
113 }
|
|
114
|
|
115 /** Returns the size of the text value. */
|
|
116 int size() {
|
|
117 if (prefixString == null) {
|
|
118 return 0;
|
|
119 }
|
|
120 else {
|
|
121 return prefixString.length() + arraySize;
|
|
122 }
|
|
123 }
|
|
124
|
|
125 /** Clears the text value and prepares the TextBuffer for reuse. */
|
|
126 void clear() {
|
|
127 arraySize = 0;
|
|
128 prefixString = null;
|
|
129 }
|
|
130
|
|
131 boolean isAllWhitespace() {
|
|
132 if ((prefixString == null) || (prefixString.length() == 0)) {
|
|
133 return true;
|
|
134 }
|
|
135
|
|
136 int size = prefixString.length();
|
|
137 for(int i = 0; i < size; i++) {
|
|
138 if ( !Verifier.isXMLWhitespace(prefixString.charAt(i))) {
|
|
139 return false;
|
|
140 }
|
|
141 }
|
|
142
|
|
143 for(int i = 0; i < arraySize; i++) {
|
|
144 if ( !Verifier.isXMLWhitespace(array[i])) {
|
|
145 return false;
|
|
146 }
|
|
147 }
|
|
148 return true;
|
|
149 }
|
|
150
|
|
151 /** Returns the text value stored in the buffer. */
|
|
152 public String toString() {
|
|
153 if (prefixString == null) {
|
|
154 return "";
|
|
155 }
|
|
156
|
|
157 String str = "";
|
|
158 if (arraySize == 0) {
|
|
159 // Char array is empty, so the text value is just prefixString.
|
|
160 str = prefixString;
|
|
161 }
|
|
162 else {
|
|
163 // Char array is not empty, so the text value is prefixString
|
|
164 // plus the char array.
|
|
165 str = new StringBuffer(prefixString.length() + arraySize)
|
|
166 .append(prefixString)
|
|
167 .append(array, 0, arraySize)
|
|
168 .toString();
|
|
169 }
|
|
170 return str;
|
|
171 }
|
|
172
|
|
173 // Ensure that the char array has room for at least "csize" characters.
|
|
174 private void ensureCapacity(int csize) {
|
|
175 int capacity = array.length;
|
|
176 if (csize > capacity) {
|
|
177 char[] old = array;
|
|
178 int nsize = capacity;
|
|
179 while (csize > nsize) {
|
|
180 nsize += (capacity/2);
|
|
181 }
|
|
182 array = new char[nsize];
|
|
183 System.arraycopy(old, 0, array, 0, arraySize);
|
|
184 }
|
|
185 }
|
|
186 }
|