Added new user api unit test for the new fields.
[libgcal.git] / src / xml_aux.c
blob56305655100ad260e1c9902e973b8e9a2c49d68c
1 /*
2 Copyright (c) 2008 Instituto Nokia de Tecnologia
3 All rights reserved.
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.
30 /**
31 * @file xml_aux.h
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.
44 #include "xml_aux.h"
46 int register_namespaces(xmlXPathContext *xpathCtx, const xmlChar *name_space,
47 const xmlChar* href)
49 int result = -1;
50 if (!xpathCtx)
51 goto exit;
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",
57 name_space, href);
58 goto exit;
61 else {
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,
66 open_search_href) ||
67 register_namespaces(xpathCtx, gContact_ns, gContact_href))
68 goto exit;
71 result = 0;
72 exit:
73 return result;
77 xmlXPathObject* execute_xpath_expression(xmlDoc *doc,
78 const xmlChar* xpathExpr,
79 xmlXPathContext *xpathCtx)
81 unsigned char ownership = 0;
82 xmlXPathObject *xpath_obj = NULL;
83 if (!xpathCtx) {
84 ownership = 1;
85 xpathCtx = xmlXPathNewContext(doc);
86 if (xpathCtx == NULL) {
87 fprintf(stderr,"Error: unable to create new XPath"
88 "context\n");
89 goto exit;
92 if (register_namespaces(xpathCtx, NULL, NULL))
93 goto exit;
97 xpath_obj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
98 if (ownership && (xpathCtx != NULL))
99 xmlXPathFreeContext(xpathCtx);
101 exit:
102 return xpath_obj;
106 int xmlentry_init_resources(xmlTextWriter **writer, xmlBuffer **buffer)
108 int result = -1;
109 *buffer = xmlBufferCreate();
110 if (!buffer)
111 goto exit;
113 *writer = xmlNewTextWriterMemory(*buffer, 0);
114 if (!*writer)
115 goto exit;
117 result = 0;
119 exit:
120 return result;
125 void xmlentry_destroy_resources(xmlTextWriter **writer, xmlBuffer **buffer)
127 if (!writer && !buffer)
128 return;
130 if (writer)
131 if (*writer) {
132 xmlFreeTextWriter(*writer);
133 *writer = NULL;
136 if (buffer)
137 if (*buffer) {
138 xmlBufferFree(*buffer);
139 *buffer = NULL;