00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef _WIN32
00041 #include "config.h"
00042 #else
00043 #include "winconfig.h"
00044 #endif
00045
00046 #include <stdio.h>
00047 #include <string.h>
00048 #include <stdlib.h>
00049 #include <mxml.h>
00050 #include "include/xml_helper.h"
00051 #include "include/macros.h"
00052
00053 mxml_node_t *
00054 xml_find_sibling(const mxml_node_t *node, const char *sibling_name)
00055 {
00056 if ( !strcmp(
00057 xml_get_element_name(node),
00058 sibling_name
00059 )
00060 ) {
00061 return (mxml_node_t *)node;
00062 } else {
00063 return mxmlFindElement( (mxml_node_t *)node,
00064 (mxml_node_t *)node->parent,
00065 sibling_name,
00066 NULL,
00067 NULL,
00068 MXML_NO_DESCEND );
00069 }
00070 }
00071
00072
00073
00074
00075 char *
00076 xml_get_cdata(const mxml_node_t *node) {
00077 char *cdata_str;
00078 char *tmp;
00079 char *buf;
00080 if (node == NULL) {
00081 return NULL;
00082 } else if ( node->type != MXML_ELEMENT) {
00083 return NULL;
00084 }
00085
00086 buf = xml_get_element_name(node);
00087 cdata_str = (char *)malloc(sizeof(char) * (strlen(buf) + 1));
00088 tmp = strstr(buf, "![CDATA[");
00089 if (tmp == NULL) {
00090 return NULL;
00091 }
00092 tmp = tmp + strlen("![CDATA[");
00093 strcpy(cdata_str, tmp);
00094 for (tmp = cdata_str; *(tmp+2) != '\0'; tmp++) {
00095 if (*tmp == '\0') {
00096 return NULL;
00097 }
00098 }
00099
00100 if ( strcmp(tmp, "]]") ) {
00101 return NULL;
00102 }
00103 CHECK_NULL(tmp, return NULL);
00104 *tmp = '\0';
00105 return cdata_str;
00106 }
00107
00108 mxml_node_t *
00109 xml_get_child(const mxml_node_t *node, const char *child_name, int descend)
00110 {
00111 return mxmlFindElement( (mxml_node_t *)node,
00112 (mxml_node_t *)node,
00113 child_name,
00114 NULL,
00115 NULL,
00116 descend );
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 mxml_node_t *
00128 xml_get_deep_child(const mxml_node_t *parent, const char **child_path)
00129 {
00130 int i;
00131 mxml_node_t *node;
00132 node = (mxml_node_t *)parent;
00133 for (i=0; child_path[i] != NULL; i++) {
00134 if ( (node = xml_get_child(node, child_path[i], MXML_NO_DESCEND)) == NULL) {
00135 return NULL;
00136 }
00137 }
00138 return node;
00139 }
00140
00141 mxml_node_t *
00142 xml_get_next_element(const mxml_node_t *node)
00143 {
00144 node = node->next;
00145 while (node != NULL) {
00146 if (node->type == MXML_ELEMENT) {
00147 break;
00148 }
00149 node = node->next;
00150 }
00151 return (mxml_node_t *)node;
00152 }
00153
00154
00155
00156
00157
00158
00159
00160 char * xml_get_text(const mxml_node_t *node)
00161 {
00162 char *ret;
00163 char *tmp;
00164 mxml_node_t *it;
00165 int len = 0;
00166 if (node->child) {
00167 node = node->child;
00168 } else {
00169 return NULL;
00170 }
00171
00172 if (node->type == MXML_TEXT || node->type == MXML_ELEMENT) {
00173
00174 it = (mxml_node_t *)node;
00175 while (it != NULL &&
00176 (it->type == MXML_TEXT || it->type == MXML_ELEMENT)
00177 ){
00178 if (it->type == MXML_TEXT) {
00179 len += strlen(it->value.text.string);
00180 len++;
00181 it = it->next;
00182 } else if (it->type == MXML_ELEMENT) {
00183 if ((tmp = strstr(it->value.element.name, "![CDATA["))){
00184 len += strlen(tmp);
00185 it = it->next;
00186 } else {
00187 break;
00188 }
00189 } else {
00190 break;
00191 }
00192 }
00193 } else {
00194 return NULL;
00195 }
00196 ret = (char*)malloc( sizeof(char) * (len + 1));
00197 *ret = '\0';
00198 for (
00199 it = (mxml_node_t *)node;
00200 it != NULL && (it->type == MXML_TEXT || it->type == MXML_ELEMENT);
00201 it = it->next) {
00202 if (it->type == MXML_TEXT) {
00203 if (it->value.text.whitespace == 1) {
00204 strcat(ret, " ");
00205 }
00206 strcat(ret, it->value.text.string);
00207 } else if (it->type == MXML_ELEMENT) {
00208 if ((tmp = xml_get_cdata(it))) {
00209
00210 strcat(ret, tmp);
00211 free(tmp);
00212 } else {
00213 break;
00214 }
00215 } else {
00216 CHECK_NULL( NULL, exit(0) );
00217 }
00218 }
00219 return ret;
00220 }
00221
00222 char* xml_get_element_name(const mxml_node_t *node)
00223 {
00224 if (node->type != MXML_ELEMENT) {
00225 return NULL;
00226 } else {
00227 return node->value.element.name;
00228 }
00229 }
00230
00231
00232
00233
00234 mxml_node_t *
00235 xml_new_cdata(mxml_node_t* parent, const char* text) {
00236 char *tmp;
00237 int namelen;
00238 mxml_node_t * node;
00239 namelen = (strlen(text) + strlen("![CDATA[]]")+1) * sizeof(char);
00240 tmp = (char*)malloc(namelen);
00241 CHECK_NULL(tmp, exit(0) );
00242 *tmp = '\0';
00243 strcat(tmp, "![CDATA[");
00244 strcat(tmp, text);
00245 strcat(tmp, "]]");
00246 node = mxmlNewElement(
00247 parent,
00248 (const char*)tmp );
00249 free((char*)tmp);
00250 return node;
00251 }
00252
00253 const char*
00254 whitespace_cb( mxml_node_t *node,
00255 int where )
00256 {
00257 if (where == MXML_WS_BEFORE_OPEN || where == MXML_WS_AFTER_CLOSE) {
00258 return("\n");
00259 } else {
00260 return NULL;
00261 }
00262 }