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

00001 
00020 #ifndef INCL_XML_PROCESSOR
00021 #define INCL_XML_PROCESSOR
00022 
00024 #include "base/fscapi.h"
00025 #include "base/util/StringBuffer.h"
00026 #include "base/util/KeyValuePair.h"
00027 #include "base/util/ArrayList.h"
00028 
00029 #define DIM_TAG 32
00030 
00031 
00032 /*
00033  * This class implements a minimalist XML processor used to extract
00034  * the content between a known tag, and to make an XML element with
00035  * a specified value.
00036  *
00037  */
00038 
00039 class XMLProcessor  {
00040 
00041 public:
00042 
00043     /*
00044      * Extracts the content of a tag into an XML message. It is supposed that the
00045      * message is a valid XML message. It returns NULL in case the tag is not
00046      * found or the XML fragment is not in the expected form.
00047      * The returned pointer (if not NULL) is allocated with the new operator and
00048      * must be discarded with the operator delete.
00049      *
00050      * @param xml the xml fragment
00051      * @param tag the tag we want the content
00052      * @param pos (OUTPUT) the position where the tag is found (ignored if NULL)
00053      *
00054      */
00055     static char* copyElementContent(const char*  xml, const char*  tag, unsigned int* pos = NULL) EXTRA_SECTION_00;
00056 
00057     /*
00058      * It's like copyElementContent but it doesn't allocate new memory.
00059      *
00060      * @param xml the xml fragment
00061      * @param tag the tag we want the content
00062      * @param pos (OUTPUT) the position where the tag is found (ignored if NULL)
00063      * @param startPos (OUTPUT) the start position of the tag content (ignored if NULL)
00064      * @param endPos (OUTPUT) the end position of the tag content (ignored if NULL)
00065      */
00066     static const char*  getElementContent(const char*  xml, const char*  tag,
00067                     unsigned int* pos, unsigned int* startPos, unsigned int* endPos) EXTRA_SECTION_00;
00068     
00069     /*
00070     * It returns the number of the tag in the xml string
00071     */
00072     static int countElementTag(const char*  xml, const char*  tag) EXTRA_SECTION_00;
00073     
00074     static int countAnd(const char*  token) EXTRA_SECTION_00;
00075 
00076     static int countChar(const char*  token, const char*  element) EXTRA_SECTION_00;
00077     
00078     static const char* getNextTag(const char* xml, int* pos);
00079    /*
00080     * it's as copyElementContent but it doesn't get the content of a tag if
00081     * the parent match except.
00082     * The parent can be more than one. They have to be separated by &
00083     * i.e.  
00084     *
00085     * copyElementContentExcept(xmlPtr, "Add", "Sync&Atomic", &post)
00086     *
00087     * The function returns "... to keep ... " content only 
00088     *
00089     * <SyncBody>
00090     *   <Sync>
00091     *     <Add>... to avoid ...</Add>
00092     *   </Sync>
00093     *   <Add>... to keep ...</Add>
00094     *   <Sync>
00095     *     <Add>... to avoid ...</Add>
00096     *   </Sync>
00097     *    <Atomic>
00098     *     <Add>... to avoid ...</Add>
00099     *   </Atomic>
00100     * </SyncBody>
00101     */
00102     static char* copyElementContentExcept(const char* xml       ,
00103                                           const char* tag       ,
00104                                           const char* except    ,
00105                                           unsigned int* pos) EXTRA_SECTION_00;
00106 
00107     static char* copyElementContentLevel(const char* xml,
00108                                          const char* tag,
00109                                          unsigned int* pos,
00110                                          int           lev = 0 ,  // the root value level
00111                                          int* startLevel   = NULL) EXTRA_SECTION_00;
00112 
00113     /*
00114      * It returns the content of the buffer specified by startPos (initial position)
00115      * and and endPos (the end position)
00116      *
00117      * It allocates new memory that has to be freed by caller with delete [].
00118      *
00119      * @param xml the xml fragment
00120      * @param startPos the start position of the tag content
00121      * @param endPos  the end position of the tag content
00122      *
00123      */
00124 
00125     static char* copyContent(const char*  xml, unsigned int startPos, unsigned int endPos) EXTRA_SECTION_00;
00126     
00133     //static StringBuffer makeElement(const WCHAR* tag, const WCHAR* val) EXTRA_SECTION_00;
00134 
00142     static StringBuffer makeElement(const char*  tag, 
00143                                     const char*  val,
00144                                     const char*  attr = NULL) EXTRA_SECTION_00;
00145 
00153     static StringBuffer makeElement(const char*  tag, 
00154                                     const char*  val,
00155                                     ArrayList attrList) EXTRA_SECTION_00;
00156 
00160     static StringBuffer makeElement(const char*  tag, bool val) {
00161         return makeElement( tag, (val?"true":"false") ) EXTRA_SECTION_00 ;
00162     }
00163 
00167         static StringBuffer makeElement(const char*  tag, int val) {
00168         return makeElement( tag, StringBuffer().append(val) ) EXTRA_SECTION_00 ;
00169     }
00170     
00183     static const char*  getElementAttributes(
00184                                          const char*  xml,
00185                                          const char*  tag,
00186                                          unsigned int*  startPos, 
00187                                          unsigned int*  endPos,
00188                                          bool escaped = false) EXTRA_SECTION_00;
00189 
00190 };
00191 
00193 #endif
00194 

Generated on Fri Apr 27 12:29:18 2007 for Funambol C++ Client Library by  doxygen 1.5.2