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 
00047 
00048 /*
00049  * This class implements a minimalist XML processor used to extract
00050  * the content between a known tag, and to make an XML element with
00051  * a specified value.
00052  *
00053  */
00054 
00055 class XMLProcessor  {
00056 
00057 public:
00058 
00059     /*
00060      * Extracts the content of a tag into an XML message. It is supposed that the
00061      * message is a valid XML message. It returns NULL in case the tag is not
00062      * found or the XML fragment is not in the expected form.
00063      * The returned pointer (if not NULL) is allocated with the new operator and
00064      * must be discarded with the operator delete.
00065      *
00066      * @param xml the xml fragment
00067      * @param tag the tag we want the content
00068      * @param pos (OUTPUT) the position where the tag is found (ignored if NULL)
00069      *
00070      */
00071     static char* copyElementContent(const char*  xml, const char*  tag, unsigned int* pos = NULL);
00072 
00073     /*
00074      * It's like copyElementContent but it doesn't allocate new memory.
00075      *
00076      * @param xml the xml fragment
00077      * @param tag the tag we want the content
00078      * @param pos (OUTPUT) the position where the tag is found (ignored if NULL)
00079      * @param startPos (OUTPUT) the start position of the tag content (ignored if NULL)
00080      * @param endPos (OUTPUT) the end position of the tag content (ignored if NULL)
00081      */
00082     static const char*  getElementContent(const char*  xml, const char*  tag,
00083                     unsigned int* pos, unsigned int* startPos, unsigned int* endPos);
00084 
00085     /*
00086     * It returns the number of the tag in the xml string
00087     */
00088     static int countElementTag(const char*  xml, const char*  tag);
00089 
00090     static int countAnd(const char*  token);
00091 
00092     static int countChar(const char*  token, const char*  element);
00093 
00094     static const char* getNextTag(const char* xml, int* pos);
00095    /*
00096     * it's as copyElementContent but it doesn't get the content of a tag if
00097     * the parent match except.
00098     * The parent can be more than one. They have to be separated by &
00099     * i.e.
00100     *
00101     * copyElementContentExcept(xmlPtr, "Add", "Sync&Atomic", &post)
00102     *
00103     * The function returns "... to keep ... " content only
00104     *
00105     * <SyncBody>
00106     *   <Sync>
00107     *     <Add>... to avoid ...</Add>
00108     *   </Sync>
00109     *   <Add>... to keep ...</Add>
00110     *   <Sync>
00111     *     <Add>... to avoid ...</Add>
00112     *   </Sync>
00113     *    <Atomic>
00114     *     <Add>... to avoid ...</Add>
00115     *   </Atomic>
00116     * </SyncBody>
00117     */
00118     static char* copyElementContentExcept(const char* xml       ,
00119                                           const char* tag       ,
00120                                           const char* except    ,
00121                                           unsigned int* pos);
00122 
00123     static char* copyElementContentLevel(const char* xml,
00124                                          const char* tag,
00125                                          unsigned int* pos,
00126                                          int           lev = 0 ,  // the root value level
00127                                          int* startLevel   = NULL);
00128 
00129     /*
00130      * It returns the content of the buffer specified by startPos (initial position)
00131      * and and endPos (the end position)
00132      *
00133      * It allocates new memory that has to be freed by caller with delete [].
00134      *
00135      * @param xml the xml fragment
00136      * @param startPos the start position of the tag content
00137      * @param endPos  the end position of the tag content
00138      *
00139      */
00140 
00141     static char* copyContent(const char*  xml, unsigned int startPos, unsigned int endPos);
00142 
00149     //static StringBuffer makeElement(const WCHAR* tag, const WCHAR* val);
00150 
00158     static StringBuffer makeElement(const char*  tag,
00159                                     const char*  val,
00160                                     const char*  attr = NULL);
00161 
00169     static StringBuffer makeElement(const char*  tag,
00170                                     const char*  val,
00171                                     ArrayList attrList);
00172 
00176     static StringBuffer makeElement(const char*  tag, bool val) {
00177         return makeElement( tag, (val?"true":"false") ) ;
00178     }
00179 
00183         static StringBuffer makeElement(const char*  tag, int val) {
00184         return makeElement( tag, StringBuffer().append(val) ) ;
00185     }
00186 
00199     static const char*  getElementAttributes(
00200                                          const char*  xml,
00201                                          const char*  tag,
00202                                          unsigned int*  startPos,
00203                                          unsigned int*  endPos,
00204                                          bool escaped = false);
00205 
00206 };
00207 
00209 #endif
00210 

Generated on Thu Mar 6 14:25:04 2008 for Funambol C++ Client Library by  doxygen 1.5.2