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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/*
* gretl -- Gnu Regression, Econometrics and Time-series Library
* Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* parsing of XML buffer using XPath via libxml2 */
#include "libgretl.h"
#include "version.h"
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#define SDMX_NS 1
#if SDMX_NS
# include <libxml/xpathInternals.h>
#endif
static void report_xml_error (xmlError *xerr)
{
if (xerr->code) {
fprintf(stderr, "xmlError: domain %d, code %d, '%s'\n",
xerr->domain, xerr->code, xerr->message);
}
}
static xmlXPathObjectPtr getnodeset (xmlDocPtr doc, xmlChar *xpath,
xmlXPathContextPtr context)
{
xmlXPathObjectPtr result;
result = xmlXPathEvalExpression(xpath, context);
if (result == NULL || xmlXPathNodeSetIsEmpty(result->nodesetval)) {
report_xml_error(&context->lastError);
xmlXPathFreeObject(result);
gretl_errmsg_set("xmlget: got no results");
return NULL;
}
return result;
}
static int real_xml_get (xmlDocPtr doc, xmlXPathObjectPtr op,
int *nobj, PRN *prn)
{
xmlNodeSetPtr ns = op->nodesetval;
xmlNodePtr np;
xmlChar *str;
int i, err = 0;
for (i=0; i<ns->nodeNr && !err; i++) {
np = ns->nodeTab[i];
str = xmlNodeListGetString(doc, np->xmlChildrenNode, 1);
if (str != NULL) {
pprintf(prn, "%s\n", str);
xmlFree(str);
} else {
err = E_DATA;
}
}
if (!err) {
*nobj = ns->nodeNr;
}
return err;
}
static int xml_get_multi (xmlDocPtr doc,
xmlXPathObjectPtr *oparr,
int nop, int *nobj,
PRN *prn)
{
xmlNodeSetPtr ns;
xmlNodePtr np;
xmlChar *str;
int nrmax = 0;
int i, j, err = 0;
for (j=0; j<nop; j++) {
ns = oparr[j]->nodesetval;
if (ns->nodeNr > nrmax) {
nrmax = ns->nodeNr;
}
}
if (nrmax == 0) {
return 0;
}
for (i=0; i<nrmax && !err; i++) {
for (j=0; j<nop; j++) {
ns = oparr[j]->nodesetval;
if (i < ns->nodeNr) {
np = ns->nodeTab[i];
str = xmlNodeListGetString(doc, np->xmlChildrenNode, 1);
if (str != NULL) {
if (strchr((char *) str, ',') || strchr((char *) str, ' ')) {
pprintf(prn, "\"%s\"", str);
} else {
pprintf(prn, "%s", str);
}
xmlFree(str);
} else {
err = E_DATA;
}
}
pputc(prn, (j == nop - 1)? '\n' : ',');
}
}
if (!err) {
*nobj = nrmax;
}
return err;
}
#if SDMX_NS
/* register the SDMX "message", "data", "com" and "str" namespaces */
#define SDMX_SCHEMAS "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/"
static int add_sdmx_namespaces (xmlXPathContextPtr ctx)
{
int err = 0;
err += xmlXPathRegisterNs(ctx, (const xmlChar *) "message",
(const xmlChar *) SDMX_SCHEMAS "message");
err += xmlXPathRegisterNs(ctx, (const xmlChar *) "com",
(const xmlChar *)SDMX_SCHEMAS "common");
err += xmlXPathRegisterNs(ctx, (const xmlChar *) "data",
(const xmlChar *)SDMX_SCHEMAS "data/structurespecific");
err += xmlXPathRegisterNs(ctx, (const xmlChar *) "str",
(const xmlChar *) SDMX_SCHEMAS "structure");
return err;
}
#endif
/*
@data: XML buffer.
@ppath: either a single string containing an XPath specification,
or an array of such strings
@ptype: either GRETL_TYPE_STRING or GRETL_TYPE_STRINGS, depending
on the type of the second argument.
@n_objects: location to receive the number of pieces
of information retrieved, or NULL.
@err: location to receive error code.
On success, returns an allocated string. If the "target"
is an array, the members are printed one per line. This
function handles target types of double, int or string;
in the case of doubles or ints, their string representation
is returned (using the C locale for doubles).
*/
char *xml_get (const char *data, void *ppath,
GretlType ptype, int *n_objects,
int *err)
{
xmlXPathContextPtr context;
xmlDocPtr doc = NULL;
int allow_fail;
char *ret = NULL;
PRN *prn = NULL;
int n = 0;
if (data == NULL || ppath == NULL) {
if (n_objects != NULL) {
*n_objects = 0;
}
*err = E_DATA;
return NULL;
}
doc = xmlParseMemory(data, strlen(data));
if (doc == NULL) {
gretl_errmsg_set("xmlParseMemory returned NULL");
*err = 1;
return NULL;
}
context = xmlXPathNewContext(doc);
if (context == NULL) {
gretl_errmsg_set("xmlXPathNewContext returned NULL");
*err = 1;
xmlFreeDoc(doc);
return NULL;
}
#if SDMX_NS /* experimental: may or may not really be needed */
if (strstr(data, "message:DataSet") || strstr(data, "str:Codelist")) {
add_sdmx_namespaces(context);
}
#endif
allow_fail = (n_objects != NULL);
prn = gretl_print_new(GRETL_PRINT_BUFFER, err);
if (!*err && ptype == GRETL_TYPE_STRING) {
/* a single XPath spec */
xmlXPathObjectPtr optr;
char *path = (char *) ppath;
optr = getnodeset(doc, (xmlChar *) path, context);
if (optr != NULL) {
*err = real_xml_get(doc, optr, &n, prn);
if (!*err) {
ret = gretl_print_steal_buffer(prn);
}
xmlXPathFreeObject(optr);
} else if (!allow_fail) {
*err = E_DATA;
}
} else if (!*err) {
/* an array of XPath specs */
xmlXPathObjectPtr *oparr = NULL;
gretl_array *a = (gretl_array *) ppath;
char **paths;
int i, ns;
paths = gretl_array_get_strings(a, &ns);
if (paths == NULL) {
*err = E_DATA;
} else {
oparr = malloc(ns * sizeof *oparr);
if (oparr == NULL) {
*err = E_ALLOC;
} else {
for (i=0; i<ns; i++) {
oparr[i] = NULL;
}
}
}
for (i=0; i<ns && !*err; i++) {
oparr[i] = getnodeset(doc, (xmlChar *) paths[i], context);
if (oparr[i] == NULL) {
*err = 1;
}
}
if (!*err) {
*err = xml_get_multi(doc, oparr, ns, &n, prn);
if (!*err && n > 0) {
ret = gretl_print_steal_buffer(prn);
}
}
for (i=0; i<ns; i++) {
xmlXPathFreeObject(oparr[i]);
}
free(oparr);
}
gretl_print_destroy(prn);
if (n_objects != NULL) {
*n_objects = n;
}
xmlXPathFreeContext(context);
xmlFreeDoc(doc);
if (!*err && n == 0) {
ret = gretl_strdup("");
}
return ret;
}
|