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, 00075 unsigned int* pos = NULL); 00076 static void copyElementContent(StringBuffer& res, const char* xml, const char* tag, 00077 unsigned int* pos = NULL); 00078 00079 /* 00080 * It's like copyElementContent but it doesn't allocate new memory. 00081 * 00082 * @param xml the xml fragment 00083 * @param tag the tag we want the content 00084 * @param pos (OUTPUT) the position where the tag is found (ignored if NULL) 00085 * @param startPos (OUTPUT) the start position of the tag content (ignored if NULL) 00086 * @param endPos (OUTPUT) the end position of the tag content (ignored if NULL) 00087 */ 00088 static const char* getElementContent(const char* xml, const char* tag, 00089 unsigned int* pos, unsigned int* startPos, unsigned int* endPos); 00090 00091 00092 static void getElementContent(const char* xml, const char* tag, 00093 unsigned int* pos, unsigned int* startPos, 00094 unsigned int* endPos, StringBuffer& res); 00095 00096 /* 00097 * It returns the number of the tag in the xml string 00098 */ 00099 static int countElementTag(const char* xml, const char* tag); 00100 00101 static int countAnd(const char* token); 00102 00103 static int countChar(const char* token, const char* element); 00104 00105 static const char* getNextTag(const char* xml, int* pos); 00106 /* 00107 * it's as copyElementContent but it doesn't get the content of a tag if 00108 * the parent match except. 00109 * The parent can be more than one. They have to be separated by & 00110 * i.e. 00111 * 00112 * copyElementContentExcept(xmlPtr, "Add", "Sync&Atomic", &post) 00113 * 00114 * The function returns "... to keep ... " content only 00115 * 00116 * <SyncBody> 00117 * <Sync> 00118 * <Add>... to avoid ...</Add> 00119 * </Sync> 00120 * <Add>... to keep ...</Add> 00121 * <Sync> 00122 * <Add>... to avoid ...</Add> 00123 * </Sync> 00124 * <Atomic> 00125 * <Add>... to avoid ...</Add> 00126 * </Atomic> 00127 * </SyncBody> 00128 */ 00129 static char* copyElementContentExcept(const char* xml , 00130 const char* tag , 00131 const char* except , 00132 unsigned int* pos); 00133 00134 00135 static char* copyElementContentLevel(const char* xml, 00136 const char* tag, 00137 unsigned int* pos, 00138 int lev = 0 , // the root value level 00139 int* startLevel = NULL); 00140 00141 static void copyElementContentLevel(StringBuffer& res, 00142 const char* xml, 00143 const char* tag, 00144 unsigned int* pos, 00145 int lev = 0 , // the root value level 00146 int* startLevel = NULL); 00147 00148 00149 00150 /* 00151 * It returns the content of the buffer specified by startPos (initial position) 00152 * and and endPos (the end position) 00153 * 00154 * It allocates new memory that has to be freed by caller with delete []. 00155 * 00156 * @param xml the xml fragment 00157 * @param startPos the start position of the tag content 00158 * @param endPos the end position of the tag content 00159 * 00160 */ 00161 00162 static char* copyContent(const char* xml, unsigned int startPos, unsigned int endPos); 00163 static void copyContent(const char* xml, unsigned int startPos, 00164 unsigned int endPos, StringBuffer& res); 00165 00172 //static StringBuffer makeElement(const WCHAR* tag, const WCHAR* val); 00173 00181 static StringBuffer makeElement(const char* tag, 00182 const char* val, 00183 const char* attr = NULL); 00184 00192 static StringBuffer makeElement(const char* tag, 00193 const char* val, 00194 ArrayList attrList); 00195 00199 static StringBuffer makeElement(const char* tag, bool val) { 00200 return makeElement( tag, (val?"true":"false") ) ; 00201 } 00202 00206 static StringBuffer makeElement(const char* tag, int val) { 00207 return makeElement( tag, StringBuffer().append(val) ) ; 00208 } 00209 00222 static const char* getElementAttributes( 00223 const char* xml, 00224 const char* tag, 00225 unsigned int* startPos, 00226 unsigned int* endPos, 00227 bool escaped = false); 00228 00229 }; 00230 00231 00232 END_NAMESPACE 00233 00235 #endif 00236