src/include/common/base/util/XMLProcessor.h

00001 /*
00002  * Funambol is a mobile platform developed by Funambol, Inc. 
00003  * Copyright (C) 2003 - 2007 Funambol, Inc.
00004  * 
00005  * This program is free software; you can redistribute it and/or modify it under
00006  * the terms of the GNU Affero General Public License version 3 as published by
00007  * the Free Software Foundation with the addition of the following permission 
00008  * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
00009  * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
00010  * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
00011  * 
00012  * This program is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015  * details.
00016  * 
00017  * You should have received a copy of the GNU Affero General Public License 
00018  * along with this program; if not, see http://www.gnu.org/licenses or write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00020  * MA 02110-1301 USA.
00021  * 
00022  * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
00023  * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
00024  * 
00025  * The interactive user interfaces in modified source and object code versions
00026  * of this program must display Appropriate Legal Notices, as required under
00027  * Section 5 of the GNU Affero General Public License version 3.
00028  * 
00029  * In accordance with Section 7(b) of the GNU Affero General Public License
00030  * version 3, these Appropriate Legal Notices must retain the display of the
00031  * "Powered by Funambol" logo. If the display of the logo is not reasonably 
00032  * feasible for technical reasons, the Appropriate Legal Notices must display
00033  * the words "Powered by Funambol".
00034  */
00035 
00036 #ifndef INCL_XML_PROCESSOR
00037 #define INCL_XML_PROCESSOR
00038 
00040 #include "base/fscapi.h"
00041 #include "base/util/StringBuffer.h"
00042 #include "base/util/KeyValuePair.h"
00043 #include "base/util/ArrayList.h"
00044 
00045 #define DIM_TAG 32
00046 #include "base/globalsdef.h"
00047 
00048 BEGIN_NAMESPACE
00049 
00050 
00051 /*
00052  * This class implements a minimalist XML processor used to extract
00053  * the content between a known tag, and to make an XML element with
00054  * a specified value.
00055  *
00056  */
00057 
00058 class XMLProcessor  {
00059 
00060 public:
00061 
00062     /*
00063      * Extracts the content of a tag into an XML message. It is supposed that the
00064      * message is a valid XML message. It returns NULL in case the tag is not
00065      * found or the XML fragment is not in the expected form.
00066      * The returned pointer (if not NULL) is allocated with the new operator and
00067      * must be discarded with the operator delete.
00068      *
00069      * @param xml the xml fragment
00070      * @param tag the tag we want the content
00071      * @param pos (OUTPUT) the position where the tag is found (ignored if NULL)
00072      *
00073      */
00074     static char* copyElementContent(const char*  xml, const char*  tag, unsigned int* pos = NULL);
00075 
00076     /*
00077      * It's like copyElementContent but it doesn't allocate new memory.
00078      *
00079      * @param xml the xml fragment
00080      * @param tag the tag we want the content
00081      * @param pos (OUTPUT) the position where the tag is found (ignored if NULL)
00082      * @param startPos (OUTPUT) the start position of the tag content (ignored if NULL)
00083      * @param endPos (OUTPUT) the end position of the tag content (ignored if NULL)
00084      */
00085     static const char*  getElementContent(const char*  xml, const char*  tag,
00086                     unsigned int* pos, unsigned int* startPos, unsigned int* endPos);
00087 
00088     /*
00089     * It returns the number of the tag in the xml string
00090     */
00091     static int countElementTag(const char*  xml, const char*  tag);
00092 
00093     static int countAnd(const char*  token);
00094 
00095     static int countChar(const char*  token, const char*  element);
00096 
00097     static const char* getNextTag(const char* xml, int* pos);
00098    /*
00099     * it's as copyElementContent but it doesn't get the content of a tag if
00100     * the parent match except.
00101     * The parent can be more than one. They have to be separated by &
00102     * i.e.
00103     *
00104     * copyElementContentExcept(xmlPtr, "Add", "Sync&Atomic", &post)
00105     *
00106     * The function returns "... to keep ... " content only
00107     *
00108     * <SyncBody>
00109     *   <Sync>
00110     *     <Add>... to avoid ...</Add>
00111     *   </Sync>
00112     *   <Add>... to keep ...</Add>
00113     *   <Sync>
00114     *     <Add>... to avoid ...</Add>
00115     *   </Sync>
00116     *    <Atomic>
00117     *     <Add>... to avoid ...</Add>
00118     *   </Atomic>
00119     * </SyncBody>
00120     */
00121     static char* copyElementContentExcept(const char* xml       ,
00122                                           const char* tag       ,
00123                                           const char* except    ,
00124                                           unsigned int* pos);
00125 
00126     static char* copyElementContentLevel(const char* xml,
00127                                          const char* tag,
00128                                          unsigned int* pos,
00129                                          int           lev = 0 ,  // the root value level
00130                                          int* startLevel   = NULL);
00131 
00132     /*
00133      * It returns the content of the buffer specified by startPos (initial position)
00134      * and and endPos (the end position)
00135      *
00136      * It allocates new memory that has to be freed by caller with delete [].
00137      *
00138      * @param xml the xml fragment
00139      * @param startPos the start position of the tag content
00140      * @param endPos  the end position of the tag content
00141      *
00142      */
00143 
00144     static char* copyContent(const char*  xml, unsigned int startPos, unsigned int endPos);
00145 
00152     //static StringBuffer makeElement(const WCHAR* tag, const WCHAR* val);
00153 
00161     static StringBuffer makeElement(const char*  tag,
00162                                     const char*  val,
00163                                     const char*  attr = NULL);
00164 
00172     static StringBuffer makeElement(const char*  tag,
00173                                     const char*  val,
00174                                     ArrayList attrList);
00175 
00179     static StringBuffer makeElement(const char*  tag, bool val) {
00180         return makeElement( tag, (val?"true":"false") ) ;
00181     }
00182 
00186         static StringBuffer makeElement(const char*  tag, int val) {
00187         return makeElement( tag, StringBuffer().append(val) ) ;
00188     }
00189 
00202     static const char*  getElementAttributes(
00203                                          const char*  xml,
00204                                          const char*  tag,
00205                                          unsigned int*  startPos,
00206                                          unsigned int*  endPos,
00207                                          bool escaped = false);
00208 
00209 };
00210 
00211 
00212 END_NAMESPACE
00213 
00215 #endif
00216 

Generated on Fri Jul 25 15:04:15 2008 for Funambol C++ Client Library by  doxygen 1.5.1