1 | /*
|
---|
2 | * Copyright 1999-2002,2004 The Apache Software Foundation.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 |
|
---|
17 | /*
|
---|
18 | * $Id: DOMString.hpp,v 1.7 2004/09/08 13:55:42 peiyongz Exp $
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef DOMString_HEADER_GUARD_
|
---|
22 | #define DOMString_HEADER_GUARD_
|
---|
23 |
|
---|
24 | #include <xercesc/util/XMemory.hpp>
|
---|
25 |
|
---|
26 | #ifdef XML_DEBUG
|
---|
27 | #include "DOMStringImpl.hpp"
|
---|
28 | XERCES_CPP_NAMESPACE_BEGIN
|
---|
29 | #else
|
---|
30 | XERCES_CPP_NAMESPACE_BEGIN
|
---|
31 | class DOMStringHandle;
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | class DOM_NullPtr;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * <code>DOMString</code> is the generic string class that stores all strings
|
---|
38 | * used in the DOM C++ API.
|
---|
39 | *
|
---|
40 | * Though this class supports most of the common string operations to manipulate
|
---|
41 | * strings, it is not meant to be a comphrehensive string class.
|
---|
42 | */
|
---|
43 |
|
---|
44 | class DEPRECATED_DOM_EXPORT DOMString : public XMemory{
|
---|
45 | public:
|
---|
46 | /** @name Constructors and assignment operator */
|
---|
47 | //@{
|
---|
48 | /**
|
---|
49 | * Default constructor for DOMString. The resulting DOMString
|
---|
50 | * object refers to no string at all; it will compare == 0.
|
---|
51 | *
|
---|
52 | */
|
---|
53 | DOMString();
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Copy constructor.
|
---|
57 | *
|
---|
58 | * @param other The object to be copied.
|
---|
59 | */
|
---|
60 | DOMString(const DOMString &other);
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Constructor to build a DOMString from an XML character array.
|
---|
64 | * (XMLCh is a 16 bit UNICODE character).
|
---|
65 | *
|
---|
66 | * @param other The null-terminated character array to be
|
---|
67 | * that provides the initial value for the DOMString.
|
---|
68 | */
|
---|
69 | DOMString(const XMLCh *other);
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Constructor to build a DOMString from a character array of given length.
|
---|
73 | *
|
---|
74 | * @param other The character array to be imported into the <code>DOMString</code>
|
---|
75 | * @param length The length of the character array to be imported
|
---|
76 | */
|
---|
77 | DOMString(const XMLCh *other, unsigned int length);
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Constructor to build a DOMString from an 8 bit character array.
|
---|
81 | * The char * string will be transcoded to UNICODE using the default
|
---|
82 | * code page on the system where the code is running.
|
---|
83 | *
|
---|
84 | * @param other The character array to be imported into the <code>DOMString</code>
|
---|
85 | */
|
---|
86 | DOMString(const char *other);
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Construct a null DOMString.
|
---|
90 | */
|
---|
91 | DOMString(int nullPointerValue);
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Assignment operator. Make destination DOMString refer to the same
|
---|
95 | * underlying string in memory as the source string.
|
---|
96 | *
|
---|
97 | * @param other the source DOMString.
|
---|
98 | */
|
---|
99 | DOMString & operator = (const DOMString &other);
|
---|
100 |
|
---|
101 |
|
---|
102 |
|
---|
103 | DOMString & operator = (DOM_NullPtr *other);
|
---|
104 |
|
---|
105 | //@}
|
---|
106 | /** @name Destructor. */
|
---|
107 | //@{
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Destructor for DOMString
|
---|
111 | *
|
---|
112 | */
|
---|
113 | ~DOMString();
|
---|
114 |
|
---|
115 | //@}
|
---|
116 | /** @name Operators for string manipulation. */
|
---|
117 | //@{
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Concatenate a DOMString to another.
|
---|
121 | *
|
---|
122 | * @param other The string to be concatenated.
|
---|
123 | * @return The concatenated object
|
---|
124 | */
|
---|
125 | // DOMString operator + (const DOMString &other);
|
---|
126 |
|
---|
127 | //@}
|
---|
128 | /** @name Equality and Inequality operators. */
|
---|
129 | //@{
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Equality operator.
|
---|
133 | *
|
---|
134 | * @param other The object to be compared with.
|
---|
135 | * @return True if the two DOMStrings refer to the same underlying string
|
---|
136 | * in memory.
|
---|
137 | * <p>
|
---|
138 | * WARNING: operator == does NOT compare the contents of
|
---|
139 | * the two strings. To do this, use the <code>DOMString::equals()</code>
|
---|
140 | * This behavior is modelled after the String operations in Java, and
|
---|
141 | * is also similar to operator == on the other DOM_* classes.
|
---|
142 | */
|
---|
143 | bool operator == (const DOMString &other) const;
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Inequality operator.
|
---|
147 | *
|
---|
148 | * @param other The object to be compared with.
|
---|
149 | * @return True if the two DOMStrings refer to different underlying strings in
|
---|
150 | * memory.
|
---|
151 | * <p>
|
---|
152 | * WARNING: operator == does NOT compare the contents of
|
---|
153 | * the two strings. To do this, use the <code>DOMString::equals()</code>
|
---|
154 | * This behavior is modelled after the String operations in Java, and
|
---|
155 | * is also similar to operator == on the other DOM_* classes.
|
---|
156 | */
|
---|
157 | bool operator != (const DOMString &other) const;
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Equality operator. Test for a null DOMString, which is one that does
|
---|
161 | * not refer to any string at all; similar to a null object reference
|
---|
162 | * variable in Java.
|
---|
163 | *
|
---|
164 | * @param other must be 0 or null.
|
---|
165 | * @return
|
---|
166 | */
|
---|
167 | bool operator == (const DOM_NullPtr *other) const;
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Inequality operator, for null test.
|
---|
171 | *
|
---|
172 | * @param other must be 0 or null.
|
---|
173 | * @return True if the two strings are different, false otherwise
|
---|
174 | */
|
---|
175 | bool operator != (const DOM_NullPtr *other) const;
|
---|
176 |
|
---|
177 | //@}
|
---|
178 | /** @name Functions to change the string. */
|
---|
179 | //@{
|
---|
180 |
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Preallocate storage in the string to hold a given number of characters.
|
---|
184 | * A DOMString will grow its buffer on demand, as characters are added,
|
---|
185 | * but it can be more efficient to allocate once in advance, if the size is known.
|
---|
186 | *
|
---|
187 | * @param size The number of 16 bit characters to reserve.
|
---|
188 | */
|
---|
189 | void reserve(unsigned int size);
|
---|
190 |
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Appends the content of another <code>DOMString</code> to this string.
|
---|
194 | *
|
---|
195 | * @param other The object to be appended
|
---|
196 | */
|
---|
197 | void appendData(const DOMString &other);
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Append a single Unicode character to this string.
|
---|
201 | *
|
---|
202 | * @param ch The single character to be appended
|
---|
203 | */
|
---|
204 | void appendData(XMLCh ch);
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Append a null-terminated XMLCh * (Unicode) string to this string.
|
---|
208 | *
|
---|
209 | * @param other The object to be appended
|
---|
210 | */
|
---|
211 | void appendData(const XMLCh *other);
|
---|
212 |
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Appends the content of another <code>DOMString</code> to this string.
|
---|
216 | *
|
---|
217 | * @param other The object to be appended
|
---|
218 | */
|
---|
219 | DOMString& operator +=(const DOMString &other);
|
---|
220 |
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Appends the content of a c-style string to this string.
|
---|
224 | *
|
---|
225 | * @param other The string to be appended
|
---|
226 | */
|
---|
227 | DOMString& operator +=(const XMLCh* other);
|
---|
228 |
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Appends a character to this string.
|
---|
232 | *
|
---|
233 | * @param ch The character to be appended
|
---|
234 | */
|
---|
235 | DOMString& operator +=(XMLCh ch);
|
---|
236 |
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Clears the data of this <code>DOMString</code>.
|
---|
240 | *
|
---|
241 | * @param offset The position from the beginning from which the data must be deleted
|
---|
242 | * @param count The count of characters from the offset that must be deleted
|
---|
243 | */
|
---|
244 | void deleteData(unsigned int offset, unsigned int count);
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Inserts a string within the existing <code>DOMString</code> at an arbitrary position.
|
---|
248 | *
|
---|
249 | * @param offset The offset from the beginning at which the insertion needs to be done
|
---|
250 | * in <code>this</code> object
|
---|
251 | * @param data The <code>DOMString</code> containing the data that needs to be inserted
|
---|
252 | * @return The object to be returned.
|
---|
253 | */
|
---|
254 | void insertData(unsigned int offset, const DOMString &data);
|
---|
255 |
|
---|
256 | //@}
|
---|
257 | /** @name Functions to get properties of the string. */
|
---|
258 | //@{
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Returns the character at the specified position.
|
---|
262 | *
|
---|
263 | * @param index The position at which the character is being requested
|
---|
264 | * @return Returns the character at the specified position.
|
---|
265 | */
|
---|
266 | XMLCh charAt(unsigned int index) const;
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Returns a handle to the raw buffer in the <code>DOMString</code>.
|
---|
270 | *
|
---|
271 | * @return The pointer inside the <code>DOMString</code> containg the string data.
|
---|
272 | * Note: the data is not always null terminated. Do not rely on
|
---|
273 | * a null being there, and do not add one, as several DOMStrings
|
---|
274 | * with different lengths may share the same raw buffer.
|
---|
275 | */
|
---|
276 | const XMLCh *rawBuffer() const;
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Returns a copy of the string, transcoded to the local code page. The
|
---|
280 | * caller owns the (char *) string that is returned, and is responsible
|
---|
281 | * for deleting it.
|
---|
282 | *
|
---|
283 | * Note: The buffer returned is allocated using the global operator new
|
---|
284 | * and users need to make sure to use the corresponding delete [].
|
---|
285 | * This method will be deprecated in later versions, as we move
|
---|
286 | * towards using a memory manager for allocation and deallocation.
|
---|
287 | *
|
---|
288 | * @return A pointer to a newly allocated buffer of char elements, which
|
---|
289 | * represents the original string, but in the local encoding.
|
---|
290 | */
|
---|
291 | char *transcode() const;
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Returns a copy of the string, transcoded to the local code page. The
|
---|
295 | * caller owns the (char *) string that is returned, and is responsible
|
---|
296 | * for deleting it.
|
---|
297 | *
|
---|
298 | * @param manager the memory manager to use for allocating returned
|
---|
299 | * returned buffer.
|
---|
300 | *
|
---|
301 | * @return A pointer to a newly allocated buffer of char elements, which
|
---|
302 | * represents the original string, but in the local encoding.
|
---|
303 | */
|
---|
304 | char *transcode(MemoryManager* const manager) const;
|
---|
305 |
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Creates a DOMString, transcoded from an input 8 bit char * string
|
---|
309 | * in the local code page.
|
---|
310 | *
|
---|
311 | * @param str The string to be transcoded
|
---|
312 | * @return A new DOMString object
|
---|
313 | */
|
---|
314 | static DOMString transcode(const char* str);
|
---|
315 |
|
---|
316 |
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Returns a sub-string of the <code>DOMString</code> starting at a specified position.
|
---|
320 | *
|
---|
321 | * @param offset The offset from the beginning from which the sub-string is being requested.
|
---|
322 | * @param count The count of characters in the requested sub-string
|
---|
323 | * @return The sub-string of the <code>DOMString</code> being requested
|
---|
324 | */
|
---|
325 | DOMString substringData(unsigned int offset, unsigned int count) const;
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Returns the length of the DOMString.
|
---|
329 | *
|
---|
330 | * @return The length of the string
|
---|
331 | */
|
---|
332 | unsigned int length() const;
|
---|
333 |
|
---|
334 | //@}
|
---|
335 | /** @name Cloning function. */
|
---|
336 | //@{
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Makes a clone of a the DOMString.
|
---|
340 | *
|
---|
341 | * @return The object to be cloned.
|
---|
342 | */
|
---|
343 | DOMString clone() const;
|
---|
344 |
|
---|
345 | //@}
|
---|
346 | /** @name Print functions. */
|
---|
347 | //@{
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Dumps the <code>DOMString</code> on the console.
|
---|
351 | *
|
---|
352 | */
|
---|
353 | void print() const;
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Dumps the <code>DOMString</code> on the console with a line feed at the end.
|
---|
357 | *
|
---|
358 | */
|
---|
359 | void println() const;
|
---|
360 |
|
---|
361 | //@}
|
---|
362 | /** @name Functions to compare a string with another. */
|
---|
363 | //@{
|
---|
364 |
|
---|
365 | /**
|
---|
366 | * Compares a DOMString with another.
|
---|
367 | *
|
---|
368 | * This compareString does not match the semantics of the standard C strcmp.
|
---|
369 | * All it needs to do is define some less than - equals - greater than
|
---|
370 | * ordering of strings. How doesn't matter.
|
---|
371 | *
|
---|
372 | *
|
---|
373 | * @param other The object to be compared with
|
---|
374 | * @return Either -1, 0, or 1 based on the comparison.
|
---|
375 | */
|
---|
376 | int compareString(const DOMString &other) const;
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * Tells if a <code>DOMString</code> contains the same character data
|
---|
380 | * as another.
|
---|
381 | *
|
---|
382 | * @param other The DOMString to be compared with.
|
---|
383 | * @return True if the two <code>DOMString</code>s are same, false otherwise.
|
---|
384 | */
|
---|
385 | bool equals(const DOMString &other) const;
|
---|
386 |
|
---|
387 |
|
---|
388 | /**
|
---|
389 | * Compare a DOMString with a null-terminated raw 16-bit character
|
---|
390 | * string.
|
---|
391 | *
|
---|
392 | * @param other The character string to be compared with.
|
---|
393 | * @return True if the strings are the same, false otherwise.
|
---|
394 | */
|
---|
395 | bool equals(const XMLCh *other) const;
|
---|
396 |
|
---|
397 |
|
---|
398 | //@}
|
---|
399 | friend class DOMStringData;
|
---|
400 | friend class DOMStringHandle;
|
---|
401 | friend class DomMemDebug;
|
---|
402 | private:
|
---|
403 |
|
---|
404 | DOMStringHandle *fHandle;
|
---|
405 | static int gLiveStringHandleCount;
|
---|
406 | static int gTotalStringHandleCount;
|
---|
407 | static int gLiveStringDataCount;
|
---|
408 | static int gTotalStringDataCount;
|
---|
409 | };
|
---|
410 |
|
---|
411 |
|
---|
412 | /****** Global Helper Functions ******/
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * Concatenate two DOMString's.
|
---|
416 | *
|
---|
417 | * @param lhs the first string
|
---|
418 | * @param rhs the second string
|
---|
419 | * @return The concatenated object
|
---|
420 | */
|
---|
421 | DOMString DEPRECATED_DOM_EXPORT operator + (const DOMString &lhs, const DOMString &rhs);
|
---|
422 |
|
---|
423 | /**
|
---|
424 | * Concatenate a null terminated Unicode string to a DOMString.
|
---|
425 | *
|
---|
426 | * @param lhs the DOMString
|
---|
427 | * @param rhs the XMLCh * string
|
---|
428 | * @return The concatenated object
|
---|
429 | */
|
---|
430 | DOMString DEPRECATED_DOM_EXPORT operator + (const DOMString &lhs, const XMLCh* rhs);
|
---|
431 |
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * Concatenate a DOMString to a null terminated Unicode string
|
---|
435 | *
|
---|
436 | * @param lhs the null-terminated Unicode string
|
---|
437 | * @param rhs the DOMString
|
---|
438 | * @return The concatenated object
|
---|
439 | */
|
---|
440 | DOMString DEPRECATED_DOM_EXPORT operator + (const XMLCh* lhs, const DOMString &rhs);
|
---|
441 |
|
---|
442 |
|
---|
443 | /**
|
---|
444 | * Concatenate a single Unicode character to a DOMString.
|
---|
445 | *
|
---|
446 | * @param lhs the DOMString
|
---|
447 | * @param rhs the character
|
---|
448 | * @return The concatenated object
|
---|
449 | */
|
---|
450 | DOMString DEPRECATED_DOM_EXPORT operator + (const DOMString &lhs, XMLCh rhs);
|
---|
451 |
|
---|
452 |
|
---|
453 | /**
|
---|
454 | * Concatenate a DOMString to a single Unicode character.
|
---|
455 | *
|
---|
456 | * @param lhs the character
|
---|
457 | * @param rhs the DOMString
|
---|
458 | * @return The concatenated object
|
---|
459 | */
|
---|
460 | DOMString DEPRECATED_DOM_EXPORT operator + (XMLCh lhs, const DOMString &rhs);
|
---|
461 |
|
---|
462 | XERCES_CPP_NAMESPACE_END
|
---|
463 |
|
---|
464 | #endif
|
---|