X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fzeerex.c;h=82ac08de303f660217088f21769c23787f04c633;hb=40da7ed5f74bb4ad3bbce81943622bc2cd9ab524;hp=d2b7dc7636653ec4a5a90ded3322a5808c6c1de8;hpb=1c67f9fb6408afadc117241e01d4963c4ba7e8c1;p=pazpar2-moved-to-github.git diff --git a/src/zeerex.c b/src/zeerex.c index d2b7dc7..82ac08d 100644 --- a/src/zeerex.c +++ b/src/zeerex.c @@ -1,4 +1,23 @@ -/* $Id: zeerex.c,v 1.1 2007-01-29 03:19:25 quinn Exp $ */ +/* This file is part of Pazpar2. + Copyright (C) 2006-2010 Index Data + +Pazpar2 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 2, or (at your option) any later +version. + +Pazpar2 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, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +// Reads Zeerex records into a set of structures #include @@ -12,13 +31,14 @@ // Replace this with something that will take a callback static void fail(const char *s, xmlNode *n) { - yaz_log(YLOG_WARN, "Zeerex Err '%s' in elem '%s/%s'", s, n->parent->name, n->name); + yaz_log(YLOG_WARN, "Zeerex Err '%s'; elem '%s/%s'", + s, n->parent->name, n->name); } // returns an nmem-allocated string if attr is present, or null static char *attrtostr(NMEM m, xmlNode *n, const char *name) { - char *s = xmlGetProp(n, name); + char *s = (char *) xmlGetProp(n, (xmlChar *) name); if (s) { char *r = nmem_strdup(m, s); @@ -31,7 +51,7 @@ static char *attrtostr(NMEM m, xmlNode *n, const char *name) static int attrtoint(xmlNode *n, const char *name) { - char *s = xmlGetProp(n, name); + char *s = (char *)xmlGetProp(n, (xmlChar *) name); if (s) { int val = atoi(s); @@ -44,7 +64,7 @@ static int attrtoint(xmlNode *n, const char *name) static Zr_bool attrtobool(xmlNode *node, const char *name) { - char *v = xmlGetProp(node, name); + char *v = (char *) xmlGetProp(node, (xmlChar *) name); if (v) { Zr_bool res; @@ -63,7 +83,7 @@ static Zr_bool attrtobool(xmlNode *node, const char *name) static char *valuetostr(NMEM m, xmlNode *n) { - char *val = xmlNodeGetContent(n); + char *val = (char *) xmlNodeGetContent(n); if (val) { char *res = nmem_strdup(m, val); @@ -76,7 +96,7 @@ static char *valuetostr(NMEM m, xmlNode *n) static int valuetoint(xmlNode *n) { - char *s = xmlNodeGetContent(n); + char *s = (char *) xmlNodeGetContent(n); if (s) { int res = atoi(s); @@ -93,7 +113,8 @@ static Zr_langstr *findlangstr(NMEM m, xmlNode *node, const char *name) Zr_langstr *res = 0; for (n = node->children; n; n = n->next) { - if (n->type == XML_ELEMENT_NODE && !strcmp(n->name, name)) + if (n->type == XML_ELEMENT_NODE + && !strcmp((const char *) n->name, name)) { Zr_langstr *new = nmem_malloc(m, sizeof(*new)); memset(new, 0, sizeof(*new)); @@ -107,6 +128,16 @@ static Zr_langstr *findlangstr(NMEM m, xmlNode *node, const char *name) return res; } +const char *zr_langstr(Zr_langstr *s, const char *lang) +{ + Zr_langstr *p; + for (p = s; p; p = p->next) + if ((!lang && p->primary == Zr_bool_true) || + (lang && p->lang && !strcmp(lang, p->lang))) + return p->str; + return s->str; +} + static struct zr_authentication *authentication(NMEM m, xmlNode *node) { xmlNode *n; @@ -117,13 +148,13 @@ static struct zr_authentication *authentication(NMEM m, xmlNode *node) { if (n->type != XML_ELEMENT_NODE) continue; - if (!strcmp(n->name, "open")) + if (!strcmp((const char *) n->name, "open")) r->open = valuetostr(m, n); - else if (!strcmp(n->name, "user")) + else if (!strcmp((const char *) n->name, "user")) r->user = valuetostr(m, n); - else if (!strcmp(n->name, "group")) + else if (!strcmp((const char *) n->name, "group")) r->group = valuetostr(m, n); - else if (!strcmp(n->name, "password")) + else if (!strcmp((const char *) n->name, "password")) r->password = valuetostr(m, n); else { @@ -141,23 +172,25 @@ static struct zr_serverInfo *serverInfo(NMEM m, xmlNode *node) struct zr_serverInfo *r = nmem_malloc(m, sizeof(*r)); memset(r, 0, sizeof(*r)); - r->protocol = attrtostr(m, n, "protocol"); - r->version = attrtostr(m, n, "version"); - r->transport = attrtostr(m, n, "transport"); - r->method = attrtostr(m, n, "method"); + r->protocol = attrtostr(m, node, "protocol"); + r->version = attrtostr(m, node, "version"); + r->transport = attrtostr(m, node, "transport"); + r->method = attrtostr(m, node, "method"); for (n = node->children; n; n = n->next) { if (n->type != XML_ELEMENT_NODE) continue; - if (!strcmp(n->name, "host")) + if (!strcmp((const char *) n->name, "host")) r->host = valuetostr(m, n); - else if (!strcmp(n->name, "port")) + else if (!strcmp((const char *) n->name, "port")) r->port = valuetoint(n); - else if (!strcmp(n->name, "database")) + else if (!strcmp((const char *) n->name, "database")) r->database = valuetostr(m, n); - else if (!strcmp(n->name, "authentication") && !(r->authentication = - authentication(m, n))) - return 0; + else if (!strcmp((const char *) n->name, "authentication")) + { + if (!(r->authentication = authentication(m, n))) + return 0; + } else { fail("Unexpected element", n); @@ -189,7 +222,7 @@ static struct zr_implementation *implementation(NMEM m, xmlNode *node) { if (n->type != XML_ELEMENT_NODE) continue; - if (!strcmp(n->name, "agent")) + if (!strcmp((const char *) n->name, "agent")) { struct zr_agent *ag = agent(m, node); if (!ag) @@ -203,9 +236,9 @@ static struct zr_implementation *implementation(NMEM m, xmlNode *node) struct zr_databaseInfo *databaseInfo(NMEM m, xmlNode *node) { + xmlNode *n; struct zr_databaseInfo *r = nmem_malloc(m, sizeof(*r)); memset(r, 0, sizeof(*r)); - xmlNode *n; r->title = findlangstr(m, node, "title"); r->description = findlangstr(m, node, "description"); @@ -218,14 +251,14 @@ struct zr_databaseInfo *databaseInfo(NMEM m, xmlNode *node) { if (n->type != XML_ELEMENT_NODE) continue; - if (!strcmp(n->name, "agents")) + if (!strcmp((const char *) n->name, "agents")) { xmlNode *n2; for (n2 = n->children; n2; n2 = n2->next) { if (n2->type != XML_ELEMENT_NODE) continue; - if (strcmp(n2->name, "agent")) + if (strcmp((const char *) n2->name, "agent")) continue; else { @@ -237,17 +270,19 @@ struct zr_databaseInfo *databaseInfo(NMEM m, xmlNode *node) } } } - else if (!strcmp(n->name, "implementation") && - !(r->implementation = implementation(m, n))) - return 0; - else if (!strcmp(n->name, "links")) + else if (!strcmp((const char *) n->name, "implementation")) + { + if (!(r->implementation = implementation(m, n))) + return 0; + } + else if (!strcmp((const char *) n->name, "links")) { xmlNode *n2; for (n2 = n->children; n2; n2 = n2->next) { - if (!n2->type != XML_ELEMENT_NODE) + if (n2->type != XML_ELEMENT_NODE) continue; - if (!strcmp(n2->name, "link")) + if (!strcmp((const char *) n2->name, "link")) continue; else { @@ -260,11 +295,11 @@ struct zr_databaseInfo *databaseInfo(NMEM m, xmlNode *node) } } } - else if (!strcmp(n->name, "history") && !r->lastUpdate) + else if (!strcmp((const char *) n->name, "history") && !r->lastUpdate) r->lastUpdate = attrtostr(m, n, "lastUpdate"); - else if (!strcmp(n->name, "extent") && !r->numberOfRecords) + else if (!strcmp((const char *) n->name, "extent") && !r->numberOfRecords) r->numberOfRecords = attrtoint(n, "numberOfRecords"); - else if (!strcmp(n->name, "langUsage") && !r->codes) + else if (!strcmp((const char *) n->name, "langUsage") && !r->codes) r->codes = attrtostr(m, n, "codes"); } return r; @@ -278,13 +313,13 @@ struct zr_metaInfo *metaInfo(NMEM m, xmlNode *node) for (n = node->children; n; n = n->next) { - if (!n->type == XML_ELEMENT_NODE) + if (n->type != XML_ELEMENT_NODE) continue; - if (!strcmp(n->name, "dateModified")) + if (!strcmp((const char *) n->name, "dateModified")) r->dateModified = valuetostr(m, n); - else if (!strcmp(n->name, "dateAggregated")) + else if (!strcmp((const char *) n->name, "dateAggregated")) r->dateAggregated = valuetostr(m, n); - else if (!strcmp(n->name, "aggregatedFrom")) + else if (!strcmp((const char *) n->name, "aggregatedFrom")) r->aggregatedFrom = valuetostr(m, n); else { @@ -326,12 +361,12 @@ static struct zr_map *map(NMEM m, xmlNode *node) { if (n->type != XML_ELEMENT_NODE) continue; - if (!strcmp(n->name, "name")) + if (!strcmp((const char *) n->name, "name")) { r->set = attrtostr(m, n, "set"); r->name = valuetostr(m, n); } - else if (!strcmp(n->name, "attr")) + else if (!strcmp((const char *) n->name, "attr")) { struct zr_attr *new = attr(m, n); if (!new) @@ -350,11 +385,11 @@ static struct zr_map *map(NMEM m, xmlNode *node) static Zr_setting *findsetting(NMEM m, xmlNode *node, char *name) { - static Zr_setting *r = 0; + static Zr_setting *r = 0; /* thread pr */ xmlNode *n; for (n = node->children; n; n = n->next) { - if (node->type == XML_ELEMENT_NODE && !strcmp(n->name, name)) + if (node->type == XML_ELEMENT_NODE && !strcmp((const char *) n->name, name)) { xmlNode *n2; struct zr_setting *new = nmem_malloc(m, sizeof(*new)); @@ -362,7 +397,7 @@ static Zr_setting *findsetting(NMEM m, xmlNode *node, char *name) new->type = attrtostr(m, n, "type"); for (n2 = n->children; n2; n2 = n2->next) { - if (n2->type == XML_ELEMENT_NODE && !strcmp(n2->name, "map")) + if (n2->type == XML_ELEMENT_NODE && !strcmp((const char *) n2->name, "map")) { new->map = map(m, n2); if (!new) @@ -371,7 +406,7 @@ static Zr_setting *findsetting(NMEM m, xmlNode *node, char *name) } } if (!new->map) - new->value = xmlNodeGetContent(n); + new->value = (char *) xmlNodeGetContent(n); new->next = r; r = new; } @@ -389,7 +424,7 @@ static struct zr_configInfo *configInfo(NMEM m, xmlNode *node) return r; } -static struct zr_index *index(NMEM m, xmlNode *node) +static struct zr_index *parse_index(NMEM m, xmlNode *node) { xmlNode *n; struct zr_index *r = nmem_malloc(m, sizeof(*r)); @@ -403,7 +438,9 @@ static struct zr_index *index(NMEM m, xmlNode *node) for (n = node->children; n; n = n->next) { - if (!strcmp(n->name, "map")) + if (n->type != XML_ELEMENT_NODE) + continue; + if (!strcmp((const char *) n->name, "map")) { struct zr_map *new = map(m, n); if (!new) @@ -411,9 +448,12 @@ static struct zr_index *index(NMEM m, xmlNode *node) new->next = r->maps; r->maps = new; } - else if (!strcmp(n->name, "configInfo") && !(r->configInfo = configInfo(m, n))) - return 0; - else if (strcmp(n->name, "title")) + else if (!strcmp((const char *) n->name, "configInfo")) + { + if (!(r->configInfo = configInfo(m, n))) + return 0; + } + else if (strcmp((const char *) n->name, "title")) { fail("Unknown child element", n); return 0; @@ -440,7 +480,7 @@ static struct zr_indexInfo *indexInfo(NMEM m , xmlNode *node) { if (n->type != XML_ELEMENT_NODE) continue; - if (!strcmp(n->name, "set")) + if (!strcmp((const char *) n->name, "set")) { struct zr_set *new = set(m, n); if (!new) @@ -448,15 +488,15 @@ static struct zr_indexInfo *indexInfo(NMEM m , xmlNode *node) new->next = r->sets; r->sets = new; } - else if (!strcmp(n->name, "index")) + else if (!strcmp((const char *) n->name, "index")) { - struct zr_index *new = index(m, n); + struct zr_index *new = parse_index(m, n); if (!new) return 0; new->next = r->indexes; r->indexes = new; } - else if (!strcmp(n->name, "sortKeyword")) + else if (!strcmp((const char *) n->name, "sortKeyword")) { struct zr_sortKeyword *new = sortKeyword(m, n); if (!new) @@ -464,8 +504,11 @@ static struct zr_indexInfo *indexInfo(NMEM m , xmlNode *node) new->next = r->sortKeywords; r->sortKeywords = new; } - else if (!strcmp(n->name, "sortKeyword") && !(r->configInfo = configInfo(m, n))) - return 0; + else if (!strcmp((const char *) n->name, "sortKeyword")) + { + if (!(r->configInfo = configInfo(m, n))) + return 0; + } else { fail("Unknown child element", n); @@ -489,16 +532,16 @@ static struct zr_recordSyntax *recordSyntax(NMEM m, xmlNode *node) { xmlNode *n; struct zr_recordSyntax *r = nmem_malloc(m, sizeof(*r)); - memset(r, 0, sizeof(*r)); struct zr_elementSet **elementp = &r->elementSets; + memset(r, 0, sizeof(*r)); r->name = attrtostr(m, node, "name"); r->identifier = attrtostr(m, node, "identifier"); for (n = node->children; n; n = n->next) { if (n->type != XML_ELEMENT_NODE) continue; - if (!strcmp(n->name, "elementSet")) + if (!strcmp((const char *) n->name, "elementSet")) { if (!(*elementp = elementSet(m, n))) return 0; @@ -517,14 +560,14 @@ static struct zr_recordInfo *recordInfo(NMEM m, xmlNode *node) { xmlNode *n; struct zr_recordInfo *r = nmem_malloc(m, sizeof(*r)); - memset(r, 0, sizeof(*r)); struct zr_recordSyntax **syntaxp = &r->recordSyntaxes; + memset(r, 0, sizeof(*r)); for (n = node->children; n; n = n->next) { if (n->type != XML_ELEMENT_NODE) continue; - if (!strcmp(n->name, "recordSyntax")) + if (!strcmp((const char *) n->name, "recordSyntax")) { if (!(*syntaxp = recordSyntax(m, n))) return 0; @@ -558,14 +601,14 @@ static struct zr_schemaInfo *schemaInfo(NMEM m, xmlNode *node) { xmlNode *n; struct zr_schemaInfo *r = nmem_malloc(m, sizeof(*r)); - memset(r, 0, sizeof(*r)); struct zr_schema **schemap = &r->schemas; + memset(r, 0, sizeof(*r)); for (n = node->children; n; n = n->next) { if (n->type != XML_ELEMENT_NODE) continue; - if (!strcmp(n->name, "schema")) + if (!strcmp((const char *) n->name, "schema")) { if (!(*schemap = schema(m, n))) return 0; @@ -590,23 +633,46 @@ static struct zr_explain *explain(NMEM m, xmlNode *node) { if (n->type != XML_ELEMENT_NODE) continue; - if (!strcmp(n->name, "serverInfo") && !(r->serverInfo = serverInfo(m, n))) - return 0; - else if (!strcmp(n->name, "databaseInfo") && !(r->databaseInfo = databaseInfo(m, n))) - return 0; - else if (!strcmp(n->name, "metaInfo") && !(r->metaInfo = metaInfo(m, n))) - return 0; - else if (!strcmp(n->name, "indexInfo") && !(r->indexInfo = indexInfo(m, n))) - return 0; - else if (!strcmp(n->name, "recordInfo") && !(r->recordInfo = recordInfo(m, n))) - return 0; - else if (!strcmp(n->name, "schemaInfo") && !(r->schemaInfo = schemaInfo(m, n))) - return 0; - else if (!strcmp(n->name, "configInfo") && !(r->configInfo = configInfo(m, n))) - return 0; + if (!strcmp((const char *) n->name, "serverInfo")) + { + if (!(r->serverInfo = serverInfo(m, n))) + return 0; + } + else if (!strcmp((const char *) n->name, "databaseInfo")) + { + if (!(r->databaseInfo = databaseInfo(m, n))) + return 0; + } + else if (!strcmp((const char *) n->name, "metaInfo")) + { + if (!(r->metaInfo = metaInfo(m, n))) + return 0; + } + else if (!strcmp((const char *) n->name, "indexInfo")) + { + if (!(r->indexInfo = indexInfo(m, n))) + return 0; + } + else if (!strcmp((const char *) n->name, "recordInfo")) + { + if (!(r->recordInfo = recordInfo(m, n))) + return 0; + } + else if (!strcmp((const char *) n->name, "schemaInfo")) + { + if (!(r->schemaInfo = schemaInfo(m, n))) + return 0; + } + else if (!strcmp((const char *) n->name, "configInfo")) + { + if (!(r->configInfo = configInfo(m, n))) + return 0; + } + else if (!strcmp((const char *) n->name, "status")) + continue; else { - fail("Unknown child element", n); + fail("Unknown child element of root node", n); return 0; } } @@ -635,7 +701,9 @@ struct zr_explain *zr_read_file(NMEM m, const char *fn) /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil * End: * vim: shiftwidth=4 tabstop=8 expandtab */ +