GME  13
XMLStringTokenizer.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: XMLStringTokenizer.cpp 555320 2007-07-11 16:05:13Z amassari $
00020  */
00021 
00022 // ---------------------------------------------------------------------------
00023 //  Includes
00024 // ---------------------------------------------------------------------------
00025 #include <xercesc/util/Janitor.hpp>
00026 #include <xercesc/util/XMLStringTokenizer.hpp>
00027 #include <xercesc/util/XMLUniDefs.hpp>
00028 #include <xercesc/util/OutOfMemoryException.hpp>
00029 
00030 XERCES_CPP_NAMESPACE_BEGIN
00031 
00032 // ---------------------------------------------------------------------------
00033 //  XMLStringTokenizer: Local const data
00034 // ---------------------------------------------------------------------------
00035 const XMLCh fgDelimeters[] =
00036 {
00037     chSpace, chHTab, chCR, chLF, chNull
00038 };
00039 
00040 // ---------------------------------------------------------------------------
00041 //  XMLStringTokenizer: Constructors and Destructor
00042 // ---------------------------------------------------------------------------
00043 
00044 typedef JanitorMemFunCall<XMLStringTokenizer>   CleanupType;
00045 
00046 XMLStringTokenizer::XMLStringTokenizer( const XMLCh* const srcStr
00047                                       , MemoryManager* const manager)
00048     : fOffset(0)
00049     , fStringLen(XMLString::stringLen(srcStr))
00050     , fString(XMLString::replicate(srcStr, manager))
00051     , fDelimeters(fgDelimeters)
00052     , fTokens(0)
00053     , fMemoryManager(manager)
00054 {
00055     CleanupType cleanup(this, &XMLStringTokenizer::cleanUp);
00056 
00057         try {
00058         if (fStringLen > 0) {
00059             fTokens = new (fMemoryManager) RefArrayVectorOf<XMLCh>(4, true, fMemoryManager);
00060         }
00061     }
00062     catch(const OutOfMemoryException&)
00063     {
00064         cleanup.release();
00065 
00066         throw;
00067     }
00068 
00069     cleanup.release();
00070 }
00071 
00072 XMLStringTokenizer::XMLStringTokenizer(const XMLCh* const srcStr,
00073                                        const XMLCh* const delim,
00074                                        MemoryManager* const manager)
00075     : fOffset(0)
00076     , fStringLen(XMLString::stringLen(srcStr))
00077     , fString(XMLString::replicate(srcStr, manager))
00078     , fDelimeters(XMLString::replicate(delim, manager))
00079     , fTokens(0)
00080     , fMemoryManager(manager)
00081 {
00082     CleanupType cleanup(this, &XMLStringTokenizer::cleanUp);
00083 
00084         try {
00085         if (fStringLen > 0) {
00086             fTokens = new (fMemoryManager) RefArrayVectorOf<XMLCh>(4, true, fMemoryManager);
00087         }
00088     }
00089     catch(const OutOfMemoryException&)
00090     {
00091         cleanup.release();
00092 
00093         throw;
00094     }
00095 
00096     cleanup.release();
00097 }
00098 
00099 XMLStringTokenizer::~XMLStringTokenizer()
00100 {
00101         cleanUp();
00102 }
00103 
00104 // ---------------------------------------------------------------------------
00105 //  XMLStringTokenizer: CleanUp methods
00106 // ---------------------------------------------------------------------------
00107 void XMLStringTokenizer::cleanUp() {
00108 
00109         fMemoryManager->deallocate(fString);//delete [] fString;
00110     if (fDelimeters != fgDelimeters) {
00111         fMemoryManager->deallocate((void*)fDelimeters);//delete [] fDelimeters;
00112     }
00113     delete fTokens;
00114 }
00115 
00116 
00117 // ---------------------------------------------------------------------------
00118 //  XMLStringTokenizer: Management methods
00119 // ---------------------------------------------------------------------------
00120 XMLCh* XMLStringTokenizer::nextToken() {
00121 
00122     if (fOffset >= fStringLen) {
00123         return 0;
00124     }
00125 
00126     bool tokFound = false;
00127     XMLSize_t startIndex = fOffset;
00128     XMLSize_t endIndex = fOffset;
00129 
00130     for (; endIndex < fStringLen; endIndex++) {
00131 
00132         if (isDelimeter(fString[endIndex])) {
00133 
00134                         if (tokFound) {
00135                 break;
00136             }
00137 
00138                         startIndex++;
00139                         continue;
00140         }
00141 
00142         tokFound = true;
00143     }
00144 
00145     fOffset = endIndex;
00146 
00147     if (tokFound) {
00148 
00149         XMLCh* tokStr = (XMLCh*) fMemoryManager->allocate
00150         (
00151             (endIndex - startIndex + 1) * sizeof(XMLCh)
00152         );//new XMLCh[(endIndex - startIndex) + 1];
00153 
00154         XMLString::subString(tokStr, fString, startIndex, endIndex, fMemoryManager);
00155         fTokens->addElement(tokStr);
00156 
00157         return tokStr;
00158         }
00159 
00160     return 0;
00161 }
00162 
00163 
00164 bool XMLStringTokenizer::hasMoreTokens() {
00165 
00166     if (countTokens() > 0)
00167         return true;
00168 
00169         return false;
00170 }
00171 
00172 XERCES_CPP_NAMESPACE_END
00173