1 | /*
|
---|
2 | * The Apache Software License, Version 1.1
|
---|
3 | *
|
---|
4 | * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
|
---|
5 | * reserved.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | *
|
---|
11 | * 1. Redistributions of source code must retain the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer.
|
---|
13 | *
|
---|
14 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
15 | * notice, this list of conditions and the following disclaimer in
|
---|
16 | * the documentation and/or other materials provided with the
|
---|
17 | * distribution.
|
---|
18 | *
|
---|
19 | * 3. The end-user documentation included with the redistribution,
|
---|
20 | * if any, must include the following acknowledgment:
|
---|
21 | * "This product includes software developed by the
|
---|
22 | * Apache Software Foundation (http://www.apache.org/)."
|
---|
23 | * Alternately, this acknowledgment may appear in the software itself,
|
---|
24 | * if and wherever such third-party acknowledgments normally appear.
|
---|
25 | *
|
---|
26 | * 4. The names "Xerces" and "Apache Software Foundation" must
|
---|
27 | * not be used to endorse or promote products derived from this
|
---|
28 | * software without prior written permission. For written
|
---|
29 | * permission, please contact apache\@apache.org.
|
---|
30 | *
|
---|
31 | * 5. Products derived from this software may not be called "Apache",
|
---|
32 | * nor may "Apache" appear in their name, without prior written
|
---|
33 | * permission of the Apache Software Foundation.
|
---|
34 | *
|
---|
35 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
---|
36 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
37 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
38 | * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
---|
39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
41 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
---|
42 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
43 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
---|
44 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
---|
45 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
46 | * SUCH DAMAGE.
|
---|
47 | * ====================================================================
|
---|
48 | *
|
---|
49 | * This software consists of voluntary contributions made by many
|
---|
50 | * individuals on behalf of the Apache Software Foundation, and was
|
---|
51 | * originally based on software copyright (c) 1999, International
|
---|
52 | * Business Machines, Inc., http://www.ibm.com . For more information
|
---|
53 | * on the Apache Software Foundation, please see
|
---|
54 | * <http://www.apache.org/>.
|
---|
55 | */
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * $Id: DOMString.hpp,v 1.5 2003/05/22 02:26:50 knoaman Exp $
|
---|
59 | */
|
---|
60 |
|
---|
61 | #ifndef DOMString_HEADER_GUARD_
|
---|
62 | #define DOMString_HEADER_GUARD_
|
---|
63 |
|
---|
64 | #include <xercesc/util/XMemory.hpp>
|
---|
65 |
|
---|
66 | #ifdef XML_DEBUG
|
---|
67 | #include "DOMStringImpl.hpp"
|
---|
68 | XERCES_CPP_NAMESPACE_BEGIN
|
---|
69 | #else
|
---|
70 | XERCES_CPP_NAMESPACE_BEGIN
|
---|
71 | class DOMStringHandle;
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | class DOM_NullPtr;
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * <code>DOMString</code> is the generic string class that stores all strings
|
---|
78 | * used in the DOM C++ API.
|
---|
79 | *
|
---|
80 | * Though this class supports most of the common string operations to manipulate
|
---|
81 | * strings, it is not meant to be a comphrehensive string class.
|
---|
82 | */
|
---|
83 |
|
---|
84 | class CDOM_EXPORT DOMString : public XMemory{
|
---|
85 | public:
|
---|
86 | /** @name Constructors and assignment operator */
|
---|
87 | //@{
|
---|
88 | /**
|
---|
89 | * Default constructor for DOMString. The resulting DOMString
|
---|
90 | * object refers to no string at all; it will compare == 0.
|
---|
91 | *
|
---|
92 | */
|
---|
93 | DOMString();
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Copy constructor.
|
---|
97 | *
|
---|
98 | * @param other The object to be copied.
|
---|
99 | */
|
---|
100 | DOMString(const DOMString &other);
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Constructor to build a DOMString from an XML character array.
|
---|
104 | * (XMLCh is a 16 bit UNICODE character).
|
---|
105 | *
|
---|
106 | * @param other The null-terminated character array to be
|
---|
107 | * that provides the initial value for the DOMString.
|
---|
108 | */
|
---|
109 | DOMString(const XMLCh *other);
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Constructor to build a DOMString from a character array of given length.
|
---|
113 | *
|
---|
114 | * @param other The character array to be imported into the <code>DOMString</code>
|
---|
115 | * @param length The length of the character array to be imported
|
---|
116 | */
|
---|
117 | DOMString(const XMLCh *other, unsigned int length);
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Constructor to build a DOMString from an 8 bit character array.
|
---|
121 | * The char * string will be transcoded to UNICODE using the default
|
---|
122 | * code page on the system where the code is running.
|
---|
123 | *
|
---|
124 | * @param other The character array to be imported into the <code>DOMString</code>
|
---|
125 | */
|
---|
126 | DOMString(const char *other);
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Construct a null DOMString.
|
---|
130 | */
|
---|
131 | DOMString(int nullPointerValue);
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Assignment operator. Make destination DOMString refer to the same
|
---|
135 | * underlying string in memory as the source string.
|
---|
136 | *
|
---|
137 | * @param other the source DOMString.
|
---|
138 | */
|
---|
139 | DOMString & operator = (const DOMString &other);
|
---|
140 |
|
---|
141 |
|
---|
142 |
|
---|
143 | DOMString & operator = (DOM_NullPtr *other);
|
---|
144 |
|
---|
145 | //@}
|
---|
146 | /** @name Destructor. */
|
---|
147 | //@{
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Destructor for DOMString
|
---|
151 | *
|
---|
152 | */
|
---|
153 | ~DOMString();
|
---|
154 |
|
---|
155 | //@}
|
---|
156 | /** @name Operators for string manipulation. */
|
---|
157 | //@{
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Concatenate a DOMString to another.
|
---|
161 | *
|
---|
162 | * @param other The string to be concatenated.
|
---|
163 | * @return The concatenated object
|
---|
164 | */
|
---|
165 | // DOMString operator + (const DOMString &other);
|
---|
166 |
|
---|
167 | //@}
|
---|
168 | /** @name Equality and Inequality operators. */
|
---|
169 | //@{
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Equality operator.
|
---|
173 | *
|
---|
174 | * @param other The object to be compared with.
|
---|
175 | * @return True if the two DOMStrings refer to the same underlying string
|
---|
176 | * in memory.
|
---|
177 | * <p>
|
---|
178 | * WARNING: operator == does NOT compare the contents of
|
---|
179 | * the two strings. To do this, use the <code>DOMString::equals()</code>
|
---|
180 | * This behavior is modelled after the String operations in Java, and
|
---|
181 | * is also similar to operator == on the other DOM_* classes.
|
---|
182 | */
|
---|
183 | bool operator == (const DOMString &other) const;
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Inequality operator.
|
---|
187 | *
|
---|
188 | * @param other The object to be compared with.
|
---|
189 | * @return True if the two DOMStrings refer to different underlying strings in
|
---|
190 | * memory.
|
---|
191 | * <p>
|
---|
192 | * WARNING: operator == does NOT compare the contents of
|
---|
193 | * the two strings. To do this, use the <code>DOMString::equals()</code>
|
---|
194 | * This behavior is modelled after the String operations in Java, and
|
---|
195 | * is also similar to operator == on the other DOM_* classes.
|
---|
196 | */
|
---|
197 | bool operator != (const DOMString &other) const;
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Equality operator. Test for a null DOMString, which is one that does
|
---|
201 | * not refer to any string at all; similar to a null object reference
|
---|
202 | * variable in Java.
|
---|
203 | *
|
---|
204 | * @param other must be 0 or null.
|
---|
205 | * @return
|
---|
206 | */
|
---|
207 | bool operator == (const DOM_NullPtr *other) const;
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Inequality operator, for null test.
|
---|
211 | *
|
---|
212 | * @param other must be 0 or null.
|
---|
213 | * @return True if the two strings are different, false otherwise
|
---|
214 | */
|
---|
215 | bool operator != (const DOM_NullPtr *other) const;
|
---|
216 |
|
---|
217 | //@}
|
---|
218 | /** @name Functions to change the string. */
|
---|
219 | //@{
|
---|
220 |
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Preallocate storage in the string to hold a given number of characters.
|
---|
224 | * A DOMString will grow its buffer on demand, as characters are added,
|
---|
225 | * but it can be more efficient to allocate once in advance, if the size is known.
|
---|
226 | *
|
---|
227 | * @param size The number of 16 bit characters to reserve.
|
---|
228 | */
|
---|
229 | void reserve(unsigned int size);
|
---|
230 |
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Appends the content of another <code>DOMString</code> to this string.
|
---|
234 | *
|
---|
235 | * @param other The object to be appended
|
---|
236 | */
|
---|
237 | void appendData(const DOMString &other);
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Append a single Unicode character to this string.
|
---|
241 | *
|
---|
242 | * @param ch The single character to be appended
|
---|
243 | */
|
---|
244 | void appendData(XMLCh ch);
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Append a null-terminated XMLCh * (Unicode) string to this string.
|
---|
248 | *
|
---|
249 | * @param other The object to be appended
|
---|
250 | */
|
---|
251 | void appendData(const XMLCh *other);
|
---|
252 |
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Appends the content of another <code>DOMString</code> to this string.
|
---|
256 | *
|
---|
257 | * @param other The object to be appended
|
---|
258 | */
|
---|
259 | DOMString& operator +=(const DOMString &other);
|
---|
260 |
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Appends the content of a c-style string to this string.
|
---|
264 | *
|
---|
265 | * @param other The string to be appended
|
---|
266 | */
|
---|
267 | DOMString& operator +=(const XMLCh* other);
|
---|
268 |
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Appends a character to this string.
|
---|
272 | *
|
---|
273 | * @param ch The character to be appended
|
---|
274 | */
|
---|
275 | DOMString& operator +=(XMLCh ch);
|
---|
276 |
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Clears the data of this <code>DOMString</code>.
|
---|
280 | *
|
---|
281 | * @param offset The position from the beginning from which the data must be deleted
|
---|
282 | * @param count The count of characters from the offset that must be deleted
|
---|
283 | */
|
---|
284 | void deleteData(unsigned int offset, unsigned int count);
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Inserts a string within the existing <code>DOMString</code> at an arbitrary position.
|
---|
288 | *
|
---|
289 | * @param offset The offset from the beginning at which the insertion needs to be done
|
---|
290 | * in <code>this</code> object
|
---|
291 | * @param data The <code>DOMString</code> containing the data that needs to be inserted
|
---|
292 | * @return The object to be returned.
|
---|
293 | */
|
---|
294 | void insertData(unsigned int offset, const DOMString &data);
|
---|
295 |
|
---|
296 | //@}
|
---|
297 | /** @name Functions to get properties of the string. */
|
---|
298 | //@{
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Returns the character at the specified position.
|
---|
302 | *
|
---|
303 | * @param index The position at which the character is being requested
|
---|
304 | * @return Returns the character at the specified position.
|
---|
305 | */
|
---|
306 | XMLCh charAt(unsigned int index) const;
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Returns a handle to the raw buffer in the <code>DOMString</code>.
|
---|
310 | *
|
---|
311 | * @return The pointer inside the <code>DOMString</code> containg the string data.
|
---|
312 | * Note: the data is not always null terminated. Do not rely on
|
---|
313 | * a null being there, and do not add one, as several DOMStrings
|
---|
314 | * with different lengths may share the same raw buffer.
|
---|
315 | */
|
---|
316 | const XMLCh *rawBuffer() const;
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Returns a copy of the string, transcoded to the local code page. The
|
---|
320 | * caller owns the (char *) string that is returned, and is responsible
|
---|
321 | * for deleting it.
|
---|
322 | *
|
---|
323 | * Note: The buffer returned is allocated using the global operator new
|
---|
324 | * and users need to make sure to use the corresponding delete [].
|
---|
325 | * This method will be deprecated in later versions, as we move
|
---|
326 | * towards using a memory manager for allocation and deallocation.
|
---|
327 | *
|
---|
328 | * @return A pointer to a newly allocated buffer of char elements, which
|
---|
329 | * represents the original string, but in the local encoding.
|
---|
330 | */
|
---|
331 | char *transcode() const;
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Returns a copy of the string, transcoded to the local code page. The
|
---|
335 | * caller owns the (char *) string that is returned, and is responsible
|
---|
336 | * for deleting it.
|
---|
337 | *
|
---|
338 | * @param manager the memory manager to use for allocating returned
|
---|
339 | * returned buffer.
|
---|
340 | *
|
---|
341 | * @return A pointer to a newly allocated buffer of char elements, which
|
---|
342 | * represents the original string, but in the local encoding.
|
---|
343 | */
|
---|
344 | char *transcode(MemoryManager* const manager) const;
|
---|
345 |
|
---|
346 |
|
---|
347 | /**
|
---|
348 | * Creates a DOMString, transcoded from an input 8 bit char * string
|
---|
349 | * in the local code page.
|
---|
350 | *
|
---|
351 | * @param str The string to be transcoded
|
---|
352 | * @return A new DOMString object
|
---|
353 | */
|
---|
354 | static DOMString transcode(const char* str);
|
---|
355 |
|
---|
356 |
|
---|
357 |
|
---|
358 | /**
|
---|
359 | * Returns a sub-string of the <code>DOMString</code> starting at a specified position.
|
---|
360 | *
|
---|
361 | * @param offset The offset from the beginning from which the sub-string is being requested.
|
---|
362 | * @param count The count of characters in the requested sub-string
|
---|
363 | * @return The sub-string of the <code>DOMString</code> being requested
|
---|
364 | */
|
---|
365 | DOMString substringData(unsigned int offset, unsigned int count) const;
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Returns the length of the DOMString.
|
---|
369 | *
|
---|
370 | * @return The length of the string
|
---|
371 | */
|
---|
372 | unsigned int length() const;
|
---|
373 |
|
---|
374 | //@}
|
---|
375 | /** @name Cloning function. */
|
---|
376 | //@{
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * Makes a clone of a the DOMString.
|
---|
380 | *
|
---|
381 | * @return The object to be cloned.
|
---|
382 | */
|
---|
383 | DOMString clone() const;
|
---|
384 |
|
---|
385 | //@}
|
---|
386 | /** @name Print functions. */
|
---|
387 | //@{
|
---|
388 |
|
---|
389 | /**
|
---|
390 | * Dumps the <code>DOMString</code> on the console.
|
---|
391 | *
|
---|
392 | */
|
---|
393 | void print() const;
|
---|
394 |
|
---|
395 | /**
|
---|
396 | * Dumps the <code>DOMString</code> on the console with a line feed at the end.
|
---|
397 | *
|
---|
398 | */
|
---|
399 | void println() const;
|
---|
400 |
|
---|
401 | //@}
|
---|
402 | /** @name Functions to compare a string with another. */
|
---|
403 | //@{
|
---|
404 |
|
---|
405 | /**
|
---|
406 | * Compares a DOMString with another.
|
---|
407 | *
|
---|
408 | * This compareString does not match the semantics of the standard C strcmp.
|
---|
409 | * All it needs to do is define some less than - equals - greater than
|
---|
410 | * ordering of strings. How doesn't matter.
|
---|
411 | *
|
---|
412 | *
|
---|
413 | * @param other The object to be compared with
|
---|
414 | * @return Either -1, 0, or 1 based on the comparison.
|
---|
415 | */
|
---|
416 | int compareString(const DOMString &other) const;
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * Tells if a <code>DOMString</code> contains the same character data
|
---|
420 | * as another.
|
---|
421 | *
|
---|
422 | * @param other The DOMString to be compared with.
|
---|
423 | * @return True if the two <code>DOMString</code>s are same, false otherwise.
|
---|
424 | */
|
---|
425 | bool equals(const DOMString &other) const;
|
---|
426 |
|
---|
427 |
|
---|
428 | /**
|
---|
429 | * Compare a DOMString with a null-terminated raw 16-bit character
|
---|
430 | * string.
|
---|
431 | *
|
---|
432 | * @param other The character string to be compared with.
|
---|
433 | * @return True if the strings are the same, false otherwise.
|
---|
434 | */
|
---|
435 | bool equals(const XMLCh *other) const;
|
---|
436 |
|
---|
437 |
|
---|
438 | //@}
|
---|
439 | friend class DOMStringData;
|
---|
440 | friend class DOMStringHandle;
|
---|
441 | friend class DomMemDebug;
|
---|
442 | private:
|
---|
443 |
|
---|
444 | DOMStringHandle *fHandle;
|
---|
445 | static int gLiveStringHandleCount;
|
---|
446 | static int gTotalStringHandleCount;
|
---|
447 | static int gLiveStringDataCount;
|
---|
448 | static int gTotalStringDataCount;
|
---|
449 | };
|
---|
450 |
|
---|
451 |
|
---|
452 | /****** Global Helper Functions ******/
|
---|
453 |
|
---|
454 | /**
|
---|
455 | * Concatenate two DOMString's.
|
---|
456 | *
|
---|
457 | * @param lhs the first string
|
---|
458 | * @param rhs the second string
|
---|
459 | * @return The concatenated object
|
---|
460 | */
|
---|
461 | DOMString CDOM_EXPORT operator + (const DOMString &lhs, const DOMString &rhs);
|
---|
462 |
|
---|
463 | /**
|
---|
464 | * Concatenate a null terminated Unicode string to a DOMString.
|
---|
465 | *
|
---|
466 | * @param lhs the DOMString
|
---|
467 | * @param rhs the XMLCh * string
|
---|
468 | * @return The concatenated object
|
---|
469 | */
|
---|
470 | DOMString CDOM_EXPORT operator + (const DOMString &lhs, const XMLCh* rhs);
|
---|
471 |
|
---|
472 |
|
---|
473 | /**
|
---|
474 | * Concatenate a DOMString to a null terminated Unicode string
|
---|
475 | *
|
---|
476 | * @param lhs the null-terminated Unicode string
|
---|
477 | * @param rhs the DOMString
|
---|
478 | * @return The concatenated object
|
---|
479 | */
|
---|
480 | DOMString CDOM_EXPORT operator + (const XMLCh* lhs, const DOMString &rhs);
|
---|
481 |
|
---|
482 |
|
---|
483 | /**
|
---|
484 | * Concatenate a single Unicode character to a DOMString.
|
---|
485 | *
|
---|
486 | * @param lhs the DOMString
|
---|
487 | * @param rhs the character
|
---|
488 | * @return The concatenated object
|
---|
489 | */
|
---|
490 | DOMString CDOM_EXPORT operator + (const DOMString &lhs, XMLCh rhs);
|
---|
491 |
|
---|
492 |
|
---|
493 | /**
|
---|
494 | * Concatenate a DOMString to a single Unicode character.
|
---|
495 | *
|
---|
496 | * @param lhs the character
|
---|
497 | * @param rhs the DOMString
|
---|
498 | * @return The concatenated object
|
---|
499 | */
|
---|
500 | DOMString CDOM_EXPORT operator + (XMLCh lhs, const DOMString &rhs);
|
---|
501 |
|
---|
502 | XERCES_CPP_NAMESPACE_END
|
---|
503 |
|
---|
504 | #endif
|
---|