00001 /* 00002 * Copyright (C) 2003-2007 Funambol, Inc 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License version 2 as 00006 * published by the Free Software Foundation. 00007 * 00008 * This program is distributed in the hope that it will be useful, 00009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR 00011 * PURPOSE. See the GNU General Public License for more details. 00012 * 00013 * You should have received a copy of the GNU General Public License 00014 * along with this program; if not, write to the Free Software 00015 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 00016 * 02111-1307 USA 00017 */ 00018 00019 #ifndef INCL_XML_PROCESSOR 00020 #define INCL_XML_PROCESSOR 00021 00023 #include "base/fscapi.h" 00024 #include "base/util/StringBuffer.h" 00025 #include "base/util/KeyValuePair.h" 00026 #include "base/util/ArrayList.h" 00027 00028 #define DIM_TAG 32 00029 00030 00031 /* 00032 * This class implements a minimalist XML processor used to extract 00033 * the content between a known tag, and to make an XML element with 00034 * a specified value. 00035 * 00036 */ 00037 00038 class XMLProcessor { 00039 00040 public: 00041 00042 /* 00043 * Extracts the content of a tag into an XML message. It is supposed that the 00044 * message is a valid XML message. It returns NULL in case the tag is not 00045 * found or the XML fragment is not in the expected form. 00046 * The returned pointer (if not NULL) is allocated with the new operator and 00047 * must be discarded with the operator delete. 00048 * 00049 * @param xml the xml fragment 00050 * @param tag the tag we want the content 00051 * @param pos (OUTPUT) the position where the tag is found (ignored if NULL) 00052 * 00053 */ 00054 static char* copyElementContent(const char* xml, const char* tag, unsigned int* pos = NULL); 00055 00056 /* 00057 * It's like copyElementContent but it doesn't allocate new memory. 00058 * 00059 * @param xml the xml fragment 00060 * @param tag the tag we want the content 00061 * @param pos (OUTPUT) the position where the tag is found (ignored if NULL) 00062 * @param startPos (OUTPUT) the start position of the tag content (ignored if NULL) 00063 * @param endPos (OUTPUT) the end position of the tag content (ignored if NULL) 00064 */ 00065 static const char* getElementContent(const char* xml, const char* tag, 00066 unsigned int* pos, unsigned int* startPos, unsigned int* endPos); 00067 00068 /* 00069 * It returns the number of the tag in the xml string 00070 */ 00071 static int countElementTag(const char* xml, const char* tag); 00072 00073 static int countAnd(const char* token); 00074 00075 static int countChar(const char* token, const char* element); 00076 00077 static const char* getNextTag(const char* xml, int* pos); 00078 /* 00079 * it's as copyElementContent but it doesn't get the content of a tag if 00080 * the parent match except. 00081 * The parent can be more than one. They have to be separated by & 00082 * i.e. 00083 * 00084 * copyElementContentExcept(xmlPtr, "Add", "Sync&Atomic", &post) 00085 * 00086 * The function returns "... to keep ... " content only 00087 * 00088 * <SyncBody> 00089 * <Sync> 00090 * <Add>... to avoid ...</Add> 00091 * </Sync> 00092 * <Add>... to keep ...</Add> 00093 * <Sync> 00094 * <Add>... to avoid ...</Add> 00095 * </Sync> 00096 * <Atomic> 00097 * <Add>... to avoid ...</Add> 00098 * </Atomic> 00099 * </SyncBody> 00100 */ 00101 static char* copyElementContentExcept(const char* xml , 00102 const char* tag , 00103 const char* except , 00104 unsigned int* pos); 00105 00106 static char* copyElementContentLevel(const char* xml, 00107 const char* tag, 00108 unsigned int* pos, 00109 int lev = 0 , // the root value level 00110 int* startLevel = NULL); 00111 00112 /* 00113 * It returns the content of the buffer specified by startPos (initial position) 00114 * and and endPos (the end position) 00115 * 00116 * It allocates new memory that has to be freed by caller with delete []. 00117 * 00118 * @param xml the xml fragment 00119 * @param startPos the start position of the tag content 00120 * @param endPos the end position of the tag content 00121 * 00122 */ 00123 00124 static char* copyContent(const char* xml, unsigned int startPos, unsigned int endPos); 00125 00132 //static StringBuffer makeElement(const WCHAR* tag, const WCHAR* val); 00133 00141 static StringBuffer makeElement(const char* tag, 00142 const char* val, 00143 const char* attr = NULL); 00144 00152 static StringBuffer makeElement(const char* tag, 00153 const char* val, 00154 ArrayList attrList); 00155 00159 static StringBuffer makeElement(const char* tag, bool val) { 00160 return makeElement( tag, (val?"true":"false") ) ; 00161 } 00162 00166 static StringBuffer makeElement(const char* tag, int val) { 00167 return makeElement( tag, StringBuffer().append(val) ) ; 00168 } 00169 00182 static const char* getElementAttributes( 00183 const char* xml, 00184 const char* tag, 00185 unsigned int* startPos, 00186 unsigned int* endPos, 00187 bool escaped = false); 00188 00189 }; 00190 00192 #endif 00193