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