1 | /*
|
---|
2 | * The Apache Software License, Version 1.1
|
---|
3 | *
|
---|
4 | * Copyright (c) 2001 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) 2001, 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: XMLStringTokenizer.hpp,v 1.6 2004/01/29 11:48:47 cargilld Exp $
|
---|
59 | */
|
---|
60 |
|
---|
61 | #if !defined(XMLSTRINGTOKENIZER_HPP)
|
---|
62 | #define XMLSTRINGTOKENIZER_HPP
|
---|
63 |
|
---|
64 | #include <xercesc/util/RefArrayVectorOf.hpp>
|
---|
65 | #include <xercesc/util/XMLString.hpp>
|
---|
66 |
|
---|
67 | XERCES_CPP_NAMESPACE_BEGIN
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * The string tokenizer class breaks a string into tokens.
|
---|
71 | *
|
---|
72 | * The XMLStringTokenizer methods do not distinguish among identifiers,
|
---|
73 | * numbers, and quoted strings, nor do they recognize and skip comments
|
---|
74 | *
|
---|
75 | * A XMLStringTokenizer object internally maintains a current position within
|
---|
76 | * the string to be tokenized. Some operations advance this current position
|
---|
77 | * past the characters processed.
|
---|
78 | */
|
---|
79 |
|
---|
80 |
|
---|
81 | class XMLUTIL_EXPORT XMLStringTokenizer :public XMemory
|
---|
82 | {
|
---|
83 | public:
|
---|
84 | // -----------------------------------------------------------------------
|
---|
85 | // Public Constructors
|
---|
86 | // -----------------------------------------------------------------------
|
---|
87 | /** @name Constructors */
|
---|
88 | //@{
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Constructs a string tokenizer for the specified string. The tokenizer
|
---|
92 | * uses the default delimiter set, which is "\t\n\r\f": the space
|
---|
93 | * character, the tab character, the newline character, the
|
---|
94 | * carriage-return character, and the form-feed character. Delimiter
|
---|
95 | * characters themselves will not be treated as tokens.
|
---|
96 | *
|
---|
97 | * @param srcStr The string to be parsed.
|
---|
98 | * @param manager Pointer to the memory manager to be used to
|
---|
99 | * allocate objects.
|
---|
100 | *
|
---|
101 | */
|
---|
102 | XMLStringTokenizer(const XMLCh* const srcStr,
|
---|
103 | MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Constructs a string tokenizer for the specified string. The characters
|
---|
107 | * in the delim argument are the delimiters for separating tokens.
|
---|
108 | * Delimiter characters themselves will not be treated as tokens.
|
---|
109 | *
|
---|
110 | * @param srcStr The string to be parsed.
|
---|
111 | * @param delim The set of delimiters.
|
---|
112 | * @param manager Pointer to the memory manager to be used to
|
---|
113 | * allocate objects.
|
---|
114 | */
|
---|
115 | XMLStringTokenizer(const XMLCh* const srcStr
|
---|
116 | , const XMLCh* const delim
|
---|
117 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
|
---|
118 |
|
---|
119 | //@}
|
---|
120 |
|
---|
121 | // -----------------------------------------------------------------------
|
---|
122 | // Public Destructor
|
---|
123 | // -----------------------------------------------------------------------
|
---|
124 | /** @name Destructor. */
|
---|
125 | //@{
|
---|
126 |
|
---|
127 | ~XMLStringTokenizer();
|
---|
128 |
|
---|
129 | //@}
|
---|
130 |
|
---|
131 | // -----------------------------------------------------------------------
|
---|
132 | // Management methods
|
---|
133 | // -----------------------------------------------------------------------
|
---|
134 | /** @name Management Function */
|
---|
135 | //@{
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Tests if there are more tokens available from this tokenizer's string.
|
---|
139 | *
|
---|
140 | * Returns true if and only if there is at least one token in the string
|
---|
141 | * after the current position; false otherwise.
|
---|
142 | */
|
---|
143 | bool hasMoreTokens();
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Calculates the number of times that this tokenizer's nextToken method
|
---|
147 | * can be called to return a valid token. The current position is not
|
---|
148 | * advanced.
|
---|
149 | *
|
---|
150 | * Returns the number of tokens remaining in the string using the current
|
---|
151 | * delimiter set.
|
---|
152 | */
|
---|
153 | int countTokens();
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Returns the next token from this string tokenizer.
|
---|
157 | *
|
---|
158 | * Function allocated, function managed (fafm). The calling function
|
---|
159 | * does not need to worry about deleting the returned pointer.
|
---|
160 | */
|
---|
161 | XMLCh* nextToken();
|
---|
162 |
|
---|
163 | //@}
|
---|
164 |
|
---|
165 | private:
|
---|
166 | // -----------------------------------------------------------------------
|
---|
167 | // Unimplemented constructors and operators
|
---|
168 | // -----------------------------------------------------------------------
|
---|
169 | XMLStringTokenizer(const XMLStringTokenizer&);
|
---|
170 | XMLStringTokenizer& operator=(const XMLStringTokenizer&);
|
---|
171 |
|
---|
172 | // -----------------------------------------------------------------------
|
---|
173 | // CleanUp methods
|
---|
174 | // -----------------------------------------------------------------------
|
---|
175 | void cleanUp();
|
---|
176 |
|
---|
177 | // -----------------------------------------------------------------------
|
---|
178 | // Helper methods
|
---|
179 | // -----------------------------------------------------------------------
|
---|
180 | bool isDelimeter(const XMLCh ch);
|
---|
181 |
|
---|
182 | // -----------------------------------------------------------------------
|
---|
183 | // Private data members
|
---|
184 | //
|
---|
185 | // fOffset
|
---|
186 | // The current position in the parsed string.
|
---|
187 | //
|
---|
188 | // fStringLen
|
---|
189 | // The length of the string parsed (for convenience).
|
---|
190 | //
|
---|
191 | // fString
|
---|
192 | // The string to be parsed
|
---|
193 | //
|
---|
194 | // fDelimeters
|
---|
195 | // A set of delimeter characters
|
---|
196 | //
|
---|
197 | // fTokens
|
---|
198 | // A vector of the token strings
|
---|
199 | // -----------------------------------------------------------------------
|
---|
200 | int fOffset;
|
---|
201 | int fStringLen;
|
---|
202 | XMLCh* fString;
|
---|
203 | XMLCh* fDelimeters;
|
---|
204 | RefArrayVectorOf<XMLCh>* fTokens;
|
---|
205 | MemoryManager* fMemoryManager;
|
---|
206 | };
|
---|
207 |
|
---|
208 |
|
---|
209 | // ---------------------------------------------------------------------------
|
---|
210 | // XMLStringTokenizer: CleanUp methods
|
---|
211 | // ---------------------------------------------------------------------------
|
---|
212 | inline void XMLStringTokenizer::cleanUp() {
|
---|
213 |
|
---|
214 | fMemoryManager->deallocate(fString);//delete [] fString;
|
---|
215 | fMemoryManager->deallocate(fDelimeters);//delete [] fDelimeters;
|
---|
216 | delete fTokens;
|
---|
217 | }
|
---|
218 |
|
---|
219 | // ---------------------------------------------------------------------------
|
---|
220 | // XMLStringTokenizer: Helper methods
|
---|
221 | // ---------------------------------------------------------------------------
|
---|
222 | inline bool XMLStringTokenizer::isDelimeter(const XMLCh ch) {
|
---|
223 |
|
---|
224 | return XMLString::indexOf(fDelimeters, ch) == -1 ? false : true;
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | // ---------------------------------------------------------------------------
|
---|
229 | // XMLStringTokenizer: Management methods
|
---|
230 | // ---------------------------------------------------------------------------
|
---|
231 | inline int XMLStringTokenizer::countTokens() {
|
---|
232 |
|
---|
233 | if (fStringLen == 0)
|
---|
234 | return 0;
|
---|
235 |
|
---|
236 | int tokCount = 0;
|
---|
237 | bool inToken = false;
|
---|
238 |
|
---|
239 | for (int i= fOffset; i< fStringLen; i++) {
|
---|
240 |
|
---|
241 | if (isDelimeter(fString[i])) {
|
---|
242 |
|
---|
243 | if (inToken) {
|
---|
244 | inToken = false;
|
---|
245 | }
|
---|
246 |
|
---|
247 | continue;
|
---|
248 | }
|
---|
249 |
|
---|
250 | if (!inToken) {
|
---|
251 |
|
---|
252 | tokCount++;
|
---|
253 | inToken = true;
|
---|
254 | }
|
---|
255 |
|
---|
256 | } // end for
|
---|
257 |
|
---|
258 | return tokCount;
|
---|
259 | }
|
---|
260 |
|
---|
261 | XERCES_CPP_NAMESPACE_END
|
---|
262 |
|
---|
263 | #endif
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * End of file XMLStringTokenizer.hpp
|
---|
267 | */
|
---|
268 |
|
---|