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