1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.apache.struts2.jasper.xmlparser;
27
28 /***
29 * XMLString is a structure used to pass character arrays. However,
30 * XMLStringBuffer is a buffer in which characters can be appended
31 * and extends XMLString so that it can be passed to methods
32 * expecting an XMLString object. This is a safe operation because
33 * it is assumed that any callee will <strong>not</strong> modify
34 * the contents of the XMLString structure.
35 * <p/>
36 * The contents of the string are managed by the string buffer. As
37 * characters are appended, the string buffer will grow as needed.
38 * <p/>
39 * <strong>Note:</strong> Never set the <code>ch</code>,
40 * <code>offset</code>, and <code>length</code> fields directly.
41 * These fields are managed by the string buffer. In order to reset
42 * the buffer, call <code>clear()</code>.
43 *
44 * @author Andy Clark, IBM
45 * @author Eric Ye, IBM
46 * @version $Id: XMLStringBuffer.java 466606 2006-10-21 23:07:12Z markt $
47 */
48 public class XMLStringBuffer
49 extends XMLString {
50
51
52
53
54
55 /***
56 * Default buffer size (32).
57 */
58 public static final int DEFAULT_SIZE = 32;
59
60
61
62
63
64 /***
65 *
66 */
67 public XMLStringBuffer() {
68 this(DEFAULT_SIZE);
69 }
70
71 /***
72 * @param size
73 */
74 public XMLStringBuffer(int size) {
75 ch = new char[size];
76 }
77
78 /***
79 * Constructs a string buffer from a char.
80 */
81 public XMLStringBuffer(char c) {
82 this(1);
83 append(c);
84 }
85
86 /***
87 * Constructs a string buffer from a String.
88 */
89 public XMLStringBuffer(String s) {
90 this(s.length());
91 append(s);
92 }
93
94 /***
95 * Constructs a string buffer from the specified character array.
96 */
97 public XMLStringBuffer(char[] ch, int offset, int length) {
98 this(length);
99 append(ch, offset, length);
100 }
101
102 /***
103 * Constructs a string buffer from the specified XMLString.
104 */
105 public XMLStringBuffer(XMLString s) {
106 this(s.length);
107 append(s);
108 }
109
110
111
112
113
114 /***
115 * Clears the string buffer.
116 */
117 public void clear() {
118 offset = 0;
119 length = 0;
120 }
121
122 /***
123 * append
124 *
125 * @param c
126 */
127 public void append(char c) {
128 if (this.length + 1 > this.ch.length) {
129 int newLength = this.ch.length * 2;
130 if (newLength < this.ch.length + DEFAULT_SIZE)
131 newLength = this.ch.length + DEFAULT_SIZE;
132 char[] newch = new char[newLength];
133 System.arraycopy(this.ch, 0, newch, 0, this.length);
134 this.ch = newch;
135 }
136 this.ch[this.length] = c;
137 this.length++;
138 }
139
140 /***
141 * append
142 *
143 * @param s
144 */
145 public void append(String s) {
146 int length = s.length();
147 if (this.length + length > this.ch.length) {
148 int newLength = this.ch.length * 2;
149 if (newLength < this.length + length + DEFAULT_SIZE)
150 newLength = this.ch.length + length + DEFAULT_SIZE;
151 char[] newch = new char[newLength];
152 System.arraycopy(this.ch, 0, newch, 0, this.length);
153 this.ch = newch;
154 }
155 s.getChars(0, length, this.ch, this.length);
156 this.length += length;
157 }
158
159 /***
160 * append
161 *
162 * @param ch
163 * @param offset
164 * @param length
165 */
166 public void append(char[] ch, int offset, int length) {
167 if (this.length + length > this.ch.length) {
168 char[] newch = new char[this.ch.length + length + DEFAULT_SIZE];
169 System.arraycopy(this.ch, 0, newch, 0, this.length);
170 this.ch = newch;
171 }
172 System.arraycopy(ch, offset, this.ch, this.length, length);
173 this.length += length;
174 }
175
176 /***
177 * append
178 *
179 * @param s
180 */
181 public void append(XMLString s) {
182 append(s.ch, s.offset, s.length);
183 }
184
185 }