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 * This class is used as a structure to pass text contained in the underlying
30 * character buffer of the scanner. The offset and length fields allow the
31 * buffer to be re-used without creating new character arrays.
32 * <p/>
33 * <strong>Note:</strong> Methods that are passed an XMLString structure
34 * should consider the contents read-only and not make any modifications
35 * to the contents of the buffer. The method receiving this structure
36 * should also not modify the offset and length if this structure (or
37 * the values of this structure) are passed to another method.
38 * <p/>
39 * <strong>Note:</strong> Methods that are passed an XMLString structure
40 * are required to copy the information out of the buffer if it is to be
41 * saved for use beyond the scope of the method. The contents of the
42 * structure are volatile and the contents of the character buffer cannot
43 * be assured once the method that is passed this structure returns.
44 * Therefore, methods passed this structure should not save any reference
45 * to the structure or the character array contained in the structure.
46 *
47 * @author Eric Ye, IBM
48 * @author Andy Clark, IBM
49 * @version $Id: XMLString.java 466606 2006-10-21 23:07:12Z markt $
50 */
51 public class XMLString {
52
53
54
55
56
57 /***
58 * The character array.
59 */
60 public char[] ch;
61
62 /***
63 * The offset into the character array.
64 */
65 public int offset;
66
67 /***
68 * The length of characters from the offset.
69 */
70 public int length;
71
72
73
74
75
76 /***
77 * Default constructor.
78 */
79 public XMLString() {
80 }
81
82 /***
83 * Constructs an XMLString structure preset with the specified
84 * values.
85 *
86 * @param ch The character array.
87 * @param offset The offset into the character array.
88 * @param length The length of characters from the offset.
89 */
90 public XMLString(char[] ch, int offset, int length) {
91 setValues(ch, offset, length);
92 }
93
94 /***
95 * Constructs an XMLString structure with copies of the values in
96 * the given structure.
97 * <p/>
98 * <strong>Note:</strong> This does not copy the character array;
99 * only the reference to the array is copied.
100 *
101 * @param string The XMLString to copy.
102 */
103 public XMLString(XMLString string) {
104 setValues(string);
105 }
106
107
108
109
110
111 /***
112 * Initializes the contents of the XMLString structure with the
113 * specified values.
114 *
115 * @param ch The character array.
116 * @param offset The offset into the character array.
117 * @param length The length of characters from the offset.
118 */
119 public void setValues(char[] ch, int offset, int length) {
120 this.ch = ch;
121 this.offset = offset;
122 this.length = length;
123 }
124
125 /***
126 * Initializes the contents of the XMLString structure with copies
127 * of the given string structure.
128 * <p/>
129 * <strong>Note:</strong> This does not copy the character array;
130 * only the reference to the array is copied.
131 *
132 * @param s
133 */
134 public void setValues(XMLString s) {
135 setValues(s.ch, s.offset, s.length);
136 }
137
138 /***
139 * Resets all of the values to their defaults.
140 */
141 public void clear() {
142 this.ch = null;
143 this.offset = 0;
144 this.length = -1;
145 }
146
147 /***
148 * Returns true if the contents of this XMLString structure and
149 * the specified array are equal.
150 *
151 * @param ch The character array.
152 * @param offset The offset into the character array.
153 * @param length The length of characters from the offset.
154 */
155 public boolean equals(char[] ch, int offset, int length) {
156 if (ch == null) {
157 return false;
158 }
159 if (this.length != length) {
160 return false;
161 }
162
163 for (int i = 0; i < length; i++) {
164 if (this.ch[this.offset + i] != ch[offset + i]) {
165 return false;
166 }
167 }
168 return true;
169 }
170
171 /***
172 * Returns true if the contents of this XMLString structure and
173 * the specified string are equal.
174 *
175 * @param s The string to compare.
176 */
177 public boolean equals(String s) {
178 if (s == null) {
179 return false;
180 }
181 if (length != s.length()) {
182 return false;
183 }
184
185
186
187
188 for (int i = 0; i < length; i++) {
189 if (ch[offset + i] != s.charAt(i)) {
190 return false;
191 }
192 }
193
194 return true;
195 }
196
197
198
199
200
201 /***
202 * Returns a string representation of this object.
203 */
204 public String toString() {
205 return length > 0 ? new String(ch, offset, length) : "";
206 }
207
208 }