/xmlbench/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/xmlbench/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <sys/time.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>

#if LIBXML_VERSION > 20600
#define READ_MEMORY
//xmlParserCtxtPtr ctxt = NULL;
#endif

#ifdef READ_MEMORY
#define DTD_VALIDATION_SUPPORTED
#endif

#include "tools.h"


inline double walker_process_node(xmlNode *cur_node) {
    double res;
    xmlChar *value;
    xmlAttr *cur_attr;
    
    if ((cur_node->children)&&(cur_node->children->type == XML_TEXT_NODE)&&(cur_node->children->content)) {
//	puts(cur_node->children->content);

	res = get_value(cur_node->children->content);
	if (res) return res;
    } 

    for (cur_attr = cur_node->properties; cur_attr; cur_attr = cur_attr->next) {
//	value = xmlGetProp(cur_node, cur_attr->name);
	value = (char*)cur_attr->children->content;
	if (value) {
	    res = get_value(value);
//	    xmlFree(value);
	    if (res) return res;
	}
    }
    
    return 0;
}

inline double xpath_walker(xmlNodeSetPtr nodes) {
    int i;
    double res = 0;
    int size = (nodes) ? nodes->nodeNr : 0;
    
    for(i = 0; i < size; ++i) {
	res += walker_process_node(nodes->nodeTab[i]);
    }
    
    return res;
}

double walker(xmlNode *node) {
    int i = 0;
    double res = 0;
    xmlNode *cur_node;

    for (cur_node = node; cur_node; cur_node = cur_node->next) {
	if((cur_node->type == XML_ELEMENT_NODE)&&(++i>3)) break;
    }
    
    if (i<=3) i = 0;

    for (cur_node = node; cur_node; cur_node = cur_node->next) {
	if (cur_node->type == XML_ELEMENT_NODE) {
	    if (i) {
		res += walker_process_node(cur_node);
	    }
	    if (cur_node->children) {
		res+=walker(cur_node->children);
	    }
	}
    }
    return res;
}


void initXML(struct TestData *td) {
    xmlInitParser();
#ifdef READ_MEMORY
/*    ctxt = xmlNewParserCtxt();
    if (!ctxt) {
	fprintf(stderr, "Can't create parser context!\n");
	exit(0);
    }*/
#endif
}

void releaseXML(struct TestData *td) {
#ifdef READ_MEMORY
//    if (ctxt) xmlFreeParserCtxt(ctxt);
#endif
    xmlCleanupParser();
}

void parseXML(struct TestData *td, unsigned long iter) {
    int err;
    xmlDocPtr doc;
#ifdef READ_MEMORY
#ifdef DTD_VALIDATION
    doc=xmlReadMemory(td->xml,td->xmllen,"xml",NULL,XML_PARSE_DTDVALID);
#else
    doc=xmlReadMemory(td->xml,td->xmllen,"xml",NULL,0);
#endif
/*    doc=xmlCtxtReadMemory(ctxt,td->xml,td->xmllen,"xml",NULL,XML_PARSE_DTDVALID);
    printf("%u\n",ctxt->valid); */


#else
    doc=xmlParseMemory(td->xml,td->xmllen);
#endif
    if (!doc) {
        printf("Error parsing document!\n");
        exit(0);
    }


    if (walk_tree) {
	double res;

	if (force_xpath) {
	    xmlXPathContextPtr xpathCtx; 
	    xmlXPathObjectPtr xpathObj; 

	    xpathCtx = xmlXPathNewContext(doc);
	    xpathObj = xmlXPathEvalExpression(walk_xpath, xpathCtx);
	    if (xpathObj) {
		res = xpath_walker(xpathObj->nodesetval);
	        xmlXPathFreeObject(xpathObj);
		xmlXPathFreeContext(xpathCtx); 
	    }
	} else {
	    res = walker(xmlDocGetRootElement(doc));
	}
//	printf("Sum: %lf\n", res);
    }

    xmlFreeDoc(doc);    

}

int main(int argc, char *argv[]) {
    return Test(argc,argv);
}