2 Copyright (c) 2008 Instituto Nokia de Tecnologia
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13 * Neither the name of the INdT nor the names of its contributors
14 may be used to endorse or promote products derived from this software
15 without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
32 * @author Adenilson Cavalcanti da Silva <adenilson.silva@indt.org.br>
33 * @date Fri Mar 28 14:31:21 2008
35 * @brief Auxiliary code to parse XML using XPath, iIt depends on libxml.
37 * I started with 'xpath1.c' libxml example written by Aleksey Sanin
38 * (which uses MIT license).
39 * In the end my code turn out to be rather different from him, but
40 * I decided to keep the same function names.
46 int register_namespaces(xmlXPathContext
*xpathCtx
, const xmlChar
*name_space
,
52 if (name_space
&& href
) {
53 /* do register namespace */
54 if(xmlXPathRegisterNs(xpathCtx
, name_space
, href
) != 0) {
55 fprintf(stderr
,"Error: unable to register NS with"
56 "prefix=\"%s\" and href=\"%s\"\n",
62 if (register_namespaces(xpathCtx
, atom_ns
, atom_href
) ||
63 register_namespaces(xpathCtx
, gd_ns
, gd_href
) ||
64 register_namespaces(xpathCtx
, gContact_ns
, gContact_href
) ||
65 register_namespaces(xpathCtx
, open_search_ns
,
67 register_namespaces(xpathCtx
, gContact_ns
, gContact_href
))
77 xmlXPathObject
* execute_xpath_expression(xmlDoc
*doc
,
78 const xmlChar
* xpathExpr
,
79 xmlXPathContext
*xpathCtx
)
81 unsigned char ownership
= 0;
82 xmlXPathObject
*xpath_obj
= NULL
;
85 xpathCtx
= xmlXPathNewContext(doc
);
86 if (xpathCtx
== NULL
) {
87 fprintf(stderr
,"Error: unable to create new XPath"
92 if (register_namespaces(xpathCtx
, NULL
, NULL
))
97 xpath_obj
= xmlXPathEvalExpression(xpathExpr
, xpathCtx
);
98 if (ownership
&& (xpathCtx
!= NULL
))
99 xmlXPathFreeContext(xpathCtx
);
106 int xmlentry_init_resources(xmlTextWriter
**writer
, xmlBuffer
**buffer
)
109 *buffer
= xmlBufferCreate();
113 *writer
= xmlNewTextWriterMemory(*buffer
, 0);
125 void xmlentry_destroy_resources(xmlTextWriter
**writer
, xmlBuffer
**buffer
)
127 if (!writer
&& !buffer
)
132 xmlFreeTextWriter(*writer
);
138 xmlBufferFree(*buffer
);