X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fnfaxml.c;h=94ef5e67846a7ca648521996f582d684f2367bcd;hp=84a8b0b7d4137af6e0ae45f01b9f9b75434dfb01;hb=fd3a0b7302ceed6425c05b6537cb654201cb1494;hpb=5ed3b6561d90d13f64be7f9ae8beaf4a0b3f8e32 diff --git a/src/nfaxml.c b/src/nfaxml.c index 84a8b0b..94ef5e6 100644 --- a/src/nfaxml.c +++ b/src/nfaxml.c @@ -1,7 +1,7 @@ /* Copyright (C) 2006, Index Data ApS * See the file LICENSE for details. * - * $Id: nfaxml.c,v 1.8 2006-07-06 14:06:17 heikki Exp $ + * $Id: nfaxml.c,v 1.9 2006-07-14 13:06:38 heikki Exp $ */ /** @@ -25,25 +25,267 @@ #include #include +/** \brief How long strings we are willing to handle here */ +#define MAXDATALEN 200 + +/** \brief Get content of a node, in utf16, for yaz_nfa */ +static int utf16_content(xmlNodePtr node, yaz_nfa_char *buf, int maxlen, + const char *filename, int rulenumber) +{ + int bufidx=0; + xmlChar *content = xmlNodeGetContent(node); + xmlChar *cp=content; + int conlen=strlen((char *)content); + int len; + int res; + while (*cp && (bufidxchildren; node; node = node->next) + { + if (node->type != XML_ELEMENT_NODE) + continue; + clauses++; + if (!strcmp((const char *) node->name, "fromstring")) + { + state = parse_fromstring(nfa, node, filename, rulenumber ); + if (!state) + return 0; + } else if (!strcmp((const char *) node->name, "tostring")) + { + conv = parse_tostring(nfa, node, filename, rulenumber ); + if (!conv) + return 0; + } else if (!strcmp((const char *) node->name, "fromrange")) + { + state = parse_fromrange(nfa, node, + &range_begin, &range_end, filename, rulenumber ); + if (!state) + return 0; + } else if (!strcmp((const char *) node->name, "torange")) + { + conv = parse_torange(nfa, node, + range_begin, range_end, filename, rulenumber ); + if (!conv) + return 0; + } else { + yaz_log(YLOG_FATAL,"Unknown clause '%s' in %s rule %d", + node->name, filename,rulenumber); + return 0; + } + } /* for child */ + if (!state) { + yaz_log(YLOG_FATAL,"No 'from' clause in a rule %d in %s", + rulenumber,filename); + return 0; + } + if (!conv) { + yaz_log(YLOG_FATAL,"No 'to' clause in a rule %d in %s", + rulenumber,filename); + return 0; + } + if (clauses != 2) { + yaz_log(YLOG_FATAL,"Must have exactly one 'from' and one 'to' clause " + "in rule %d in %s", rulenumber,filename); + return 0; + } + if ( YAZ_NFA_SUCCESS == yaz_nfa_set_result(nfa,state,conv)) + return 1; + yaz_log(YLOG_FATAL,"Conflicting rules in %s rule %d", + filename, rulenumber); + return 0; +} /* parse_rule */ + + /** \brief Parse the NFA from a XML document */ -yaz_nfa *yaz_nfa_parse_xml_doc(xmlDocPtr doc) +yaz_nfa *yaz_nfa_parse_xml_doc(xmlDocPtr doc, const char *filename) { - libxml2_error_to_yazlog(YLOG_FATAL, "yaz_nfa_parse_doc"); + xmlNodePtr node; + yaz_nfa *nfa; + int rulenumber=0; if (!doc) return 0; - - return 0; -} + libxml2_error_to_yazlog(YLOG_FATAL, "yaz_nfa_parse_doc"); + node = xmlDocGetRootElement(doc); + if (!node || node->type != XML_ELEMENT_NODE || + strcmp((const char *) node->name, "ruleset")) + { + yaz_log(YLOG_FATAL,"nfa_parse_xml: Could not find root element 'ruleset' " + "in %s", filename); + return 0; + } + nfa= yaz_nfa_init(); + if (!nfa) + { + yaz_log(YLOG_FATAL,"nfa_parse_xml: Creating nfa failed, can't parse %s", + filename); + return 0; + } + + for (node = node->children; node; node = node->next) + { + if (node->type != XML_ELEMENT_NODE) + continue; + if (!strcmp((const char *) node->name, "rule")) { + if (!parse_rule(nfa,node,filename,rulenumber++)) + return 0; + } else { + yaz_log(YLOG_FATAL,"nfa_parse_xml: " + "expected 'rule', found '%s' in %s", + (const char *) node->name,filename); + return 0; + } + } /* for */ + return nfa; +} /* yaz_nfa_parse_xml_doc */ /** \brief Parse the NFA from a file */ -yaz_nfa *yaz_nfa_parse_xml_file(const char *filepath) { +yaz_nfa *yaz_nfa_parse_xml_file(const char *filepath) +{ int nSubst; - xmlDocPtr doc; + if (!filepath) + { + yaz_log(YLOG_FATAL,"yaz_nfa_parse_xml_file called with NULL"); + return 0; + } libxml2_error_to_yazlog(YLOG_FATAL, "yaz_nfa_parse_xml_file"); doc = xmlParseFile(filepath); @@ -54,16 +296,21 @@ yaz_nfa *yaz_nfa_parse_xml_file(const char *filepath) { if (nSubst==-1) { return 0; } - return yaz_nfa_parse_xml_doc(doc); + return yaz_nfa_parse_xml_doc(doc, filepath); } /** \brief Parse the NFA from a memory buffer */ -yaz_nfa *yaz_nfa_parse_xml_memory(const char *xmlbuff) { +yaz_nfa *yaz_nfa_parse_xml_memory(const char *xmlbuff, const char *filename) { xmlDocPtr doc; + if (!xmlbuff) + { + yaz_log(YLOG_FATAL,"yaz_nfa_parse_memroy called with NULL"); + return 0; + } libxml2_error_to_yazlog(YLOG_FATAL, "yaz_nfa_parse_xml_memory"); doc = xmlParseMemory(xmlbuff, strlen(xmlbuff)); - return yaz_nfa_parse_xml_doc(doc); + return yaz_nfa_parse_xml_doc(doc,filename); }