xpather 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/ext/xpather/xpather.c +35 -37
- metadata +2 -2
data/ext/xpather/xpather.c
CHANGED
@@ -9,48 +9,58 @@ static void xml_free(void *doc) {
|
|
9
9
|
xmlFreeDoc(doc);
|
10
10
|
}
|
11
11
|
|
12
|
-
|
13
|
-
{
|
14
|
-
VALUE results = rb_ary_new();
|
15
|
-
xmlDocPtr doc;
|
12
|
+
xmlXPathObjectPtr eval_and_search(xmlDocPtr doc, char *query) {
|
16
13
|
xmlXPathContextPtr xpathCtx;
|
17
14
|
xmlXPathObjectPtr xpathObj;
|
18
|
-
xmlNodeSetPtr nodes;
|
19
|
-
xmlNodePtr cur;
|
20
|
-
int size;
|
21
|
-
int i;
|
22
|
-
|
23
|
-
Data_Get_Struct(self, xmlDoc, doc);
|
24
15
|
|
25
16
|
xpathCtx = xmlXPathNewContext(doc);
|
26
17
|
if (xpathCtx == NULL) {
|
27
18
|
rb_raise(rb_eArgError, "Error: unable to create new XPath context\n");
|
28
|
-
return
|
19
|
+
return NULL;
|
29
20
|
}
|
30
21
|
|
31
|
-
xpathObj = xmlXPathEvalExpression(
|
22
|
+
xpathObj = xmlXPathEvalExpression(query, xpathCtx);
|
32
23
|
if (xpathObj == NULL) {
|
33
|
-
rb_raise(rb_eArgError, "Error: unable to evaluate xpath expression \"%s\"\n",
|
24
|
+
rb_raise(rb_eArgError, "Error: unable to evaluate xpath expression \"%s\"\n", query);
|
34
25
|
xmlXPathFreeContext(xpathCtx);
|
35
|
-
return
|
26
|
+
return NULL;
|
36
27
|
}
|
37
28
|
|
29
|
+
xmlXPathFreeContext(xpathCtx);
|
30
|
+
|
31
|
+
return xpathObj;
|
32
|
+
}
|
33
|
+
|
34
|
+
VALUE get(VALUE self, VALUE xpathExpr)
|
35
|
+
{
|
36
|
+
VALUE results = rb_ary_new();
|
37
|
+
xmlDocPtr doc;
|
38
|
+
xmlXPathObjectPtr xpathObj;
|
39
|
+
xmlNodeSetPtr nodes;
|
40
|
+
xmlNodePtr current;
|
41
|
+
int size, i;
|
42
|
+
|
43
|
+
Data_Get_Struct(self, xmlDoc, doc);
|
44
|
+
|
45
|
+
xpathObj = eval_and_search(doc, StringValueCStr(xpathExpr));
|
46
|
+
|
47
|
+
if (xpathObj == NULL) { return Qnil; }
|
48
|
+
|
38
49
|
nodes = xpathObj->nodesetval;
|
39
50
|
size = (nodes) ? nodes->nodeNr : 0;
|
40
51
|
|
52
|
+
if (size == 0) { return Qnil; }
|
53
|
+
|
41
54
|
if (size == 1) {
|
42
55
|
results = rb_str_new2(xmlNodeGetContent(nodes->nodeTab[0]));
|
43
|
-
} else if (size > 1) {
|
44
|
-
for (i = 0; i < size; ++i) {
|
45
|
-
cur = nodes->nodeTab[i];
|
46
|
-
rb_ary_push(results, rb_str_new2(xmlNodeGetContent(cur)));
|
47
|
-
}
|
48
56
|
} else {
|
49
|
-
|
57
|
+
for (i = 0; i < size; i++) {
|
58
|
+
current = nodes->nodeTab[i];
|
59
|
+
rb_ary_push(results, rb_str_new2(xmlNodeGetContent(current)));
|
60
|
+
}
|
50
61
|
}
|
51
62
|
|
52
63
|
xmlXPathFreeObject(xpathObj);
|
53
|
-
xmlXPathFreeContext(xpathCtx);
|
54
64
|
|
55
65
|
return results;
|
56
66
|
}
|
@@ -59,28 +69,17 @@ VALUE search(VALUE self, VALUE xpathExpr)
|
|
59
69
|
{
|
60
70
|
VALUE results = rb_ary_new();
|
61
71
|
xmlDocPtr doc;
|
62
|
-
xmlXPathContextPtr xpathCtx;
|
63
72
|
xmlXPathObjectPtr xpathObj;
|
64
73
|
xmlNodeSetPtr nodes;
|
65
74
|
xmlNodePtr cur;
|
66
75
|
xmlBufferPtr nodeBuffer;
|
67
|
-
int size;
|
68
|
-
int i;
|
76
|
+
int size, i;
|
69
77
|
|
70
78
|
Data_Get_Struct(self, xmlDoc, doc);
|
71
79
|
|
72
|
-
|
73
|
-
if (xpathCtx == NULL) {
|
74
|
-
rb_raise(rb_eArgError, "Error: unable to create new XPath context\n");
|
75
|
-
return Qnil;
|
76
|
-
}
|
80
|
+
xpathObj = eval_and_search(doc, StringValueCStr(xpathExpr));
|
77
81
|
|
78
|
-
xpathObj
|
79
|
-
if (xpathObj == NULL) {
|
80
|
-
rb_raise(rb_eArgError, "Error: unable to evaluate xpath expression \"%s\"\n", StringValueCStr(xpathExpr));
|
81
|
-
xmlXPathFreeContext(xpathCtx);
|
82
|
-
return Qnil;
|
83
|
-
}
|
82
|
+
if (xpathObj == NULL) { return Qnil; }
|
84
83
|
|
85
84
|
nodes = xpathObj->nodesetval;
|
86
85
|
size = (nodes) ? nodes->nodeNr : 0;
|
@@ -93,7 +92,6 @@ VALUE search(VALUE self, VALUE xpathExpr)
|
|
93
92
|
}
|
94
93
|
|
95
94
|
xmlXPathFreeObject(xpathObj);
|
96
|
-
xmlXPathFreeContext(xpathCtx);
|
97
95
|
|
98
96
|
return results;
|
99
97
|
}
|
@@ -111,7 +109,7 @@ VALUE constructor(VALUE self, VALUE xmlStr)
|
|
111
109
|
VALUE argv[1];
|
112
110
|
VALUE t_data;
|
113
111
|
|
114
|
-
doc = xmlParseMemory(xmlCStr, strlen(xmlCStr));
|
112
|
+
doc = xmlParseMemory(xmlCStr, (int)strlen(xmlCStr));
|
115
113
|
if (doc == NULL) {
|
116
114
|
fprintf(stderr, "Error: unable to parse xml\n");
|
117
115
|
return Qnil;
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xpather
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-17 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Quick and painless XPath searching for Ruby using libxml2
|
15
15
|
email:
|