1 /* Copyright (C) 2006, Index Data ApS
2 * See the file LICENSE for details.
4 * $Id: nfaxml.c,v 1.9 2006-07-14 13:06:38 heikki Exp $
9 * \brief Routines for reading a NFA spec from an XML file
17 /* #include <libxml/parser.h> */
18 #include <libxml/tree.h>
19 #include <libxml/xinclude.h>
23 #include <yaz/yconfig.h>
25 #include <yaz/nfaxml.h>
26 #include <yaz/libxml2_error.h>
28 /** \brief How long strings we are willing to handle here */
29 #define MAXDATALEN 200
31 /** \brief Get content of a node, in utf16, for yaz_nfa */
32 static int utf16_content(xmlNodePtr node, yaz_nfa_char *buf, int maxlen,
33 const char *filename, int rulenumber)
36 xmlChar *content = xmlNodeGetContent(node);
38 int conlen=strlen((char *)content);
41 while (*cp && (bufidx<maxlen) ) {
43 res=xmlGetUTF8Char(cp,&len);
45 /* should be caught earlier */
46 yaz_log(YLOG_FATAL,"Illegal utf-8 sequence "
47 "%d bytes into '%s' in %s, rule %d ",
48 cp-content, content, filename, rulenumber);
61 static int parse_range(xmlNodePtr node,
62 yaz_nfa_char *range_start,
63 yaz_nfa_char *range_end,
64 const char *filename, int rulenumber )
66 xmlChar *content = xmlNodeGetContent(node);
68 int conlen=strlen((char *)content);
72 res=xmlGetUTF8Char(cp,&len);
78 res=xmlGetUTF8Char(cp,&len);
86 res=xmlGetUTF8Char(cp,&len);
93 yaz_log(YLOG_FATAL,"Illegal range. '%s'. Must be like 'a-z' "
95 content, filename, rulenumber);
102 /** \brief Parse a fromstring clause */
103 static yaz_nfa_state *parse_fromstring(yaz_nfa *nfa,
104 xmlNodePtr node, const char *filename, int rulenumber )
106 yaz_nfa_char buf[MAXDATALEN];
107 yaz_nfa_state *state;
108 int bufidx=utf16_content(node, buf, MAXDATALEN, filename, rulenumber);
111 state=yaz_nfa_add_sequence(nfa, 0, buf, bufidx);
113 } /* parse_fromstring */
115 /** \brief Parse a tostring clause */
116 static yaz_nfa_converter *parse_tostring(yaz_nfa *nfa,
117 xmlNodePtr node, const char *filename, int rulenumber )
119 yaz_nfa_char buf[MAXDATALEN];
120 yaz_nfa_converter *conv;
121 int bufidx=utf16_content(node, buf, MAXDATALEN, filename, rulenumber);
124 conv=yaz_nfa_create_string_converter(nfa, buf, bufidx);
126 } /* parse_tostring */
128 static yaz_nfa_state * parse_fromrange(yaz_nfa *nfa,
130 yaz_nfa_char *from_begin,
131 yaz_nfa_char *from_end,
132 const char *filename, int rulenumber )
136 yaz_nfa_state *state;
138 rc=parse_range(node, &begin, &end, filename, rulenumber);
142 *from_end=end; /* save for calculating the to-range */
143 state=yaz_nfa_add_range(nfa, 0, begin, end);
145 } /* parse_fromrange */
147 static yaz_nfa_converter *parse_torange(yaz_nfa *nfa,
148 xmlNodePtr node, yaz_nfa_char from_begin, yaz_nfa_char from_end,
149 const char *filename, int rulenumber )
153 yaz_nfa_converter *conv;
155 rc=parse_range(node, &begin, &end, filename, rulenumber);
158 if ( from_end - from_begin != end - begin ) {
159 yaz_log(YLOG_FATAL,"From-range not as long as to-range: "
160 "from=%x-%x to=%x-%x in rule %d in %s",
161 from_begin, from_end, begin, end, rulenumber, filename);
164 conv=yaz_nfa_create_range_converter(nfa, 0, from_begin, begin);
166 } /* parse_torange */
168 /** \brief Parse one rule from an XML node */
169 static int parse_rule(yaz_nfa *nfa, xmlNodePtr rulenode,
170 const char *filename, int rulenumber )
172 yaz_nfa_state *state=0;
173 yaz_nfa_converter *conv=0;
174 yaz_nfa_char range_begin=0, range_end=0;
177 for (node = rulenode->children; node; node = node->next)
179 if (node->type != XML_ELEMENT_NODE)
182 if (!strcmp((const char *) node->name, "fromstring"))
184 state = parse_fromstring(nfa, node, filename, rulenumber );
187 } else if (!strcmp((const char *) node->name, "tostring"))
189 conv = parse_tostring(nfa, node, filename, rulenumber );
192 } else if (!strcmp((const char *) node->name, "fromrange"))
194 state = parse_fromrange(nfa, node,
195 &range_begin, &range_end, filename, rulenumber );
198 } else if (!strcmp((const char *) node->name, "torange"))
200 conv = parse_torange(nfa, node,
201 range_begin, range_end, filename, rulenumber );
205 yaz_log(YLOG_FATAL,"Unknown clause '%s' in %s rule %d",
206 node->name, filename,rulenumber);
211 yaz_log(YLOG_FATAL,"No 'from' clause in a rule %d in %s",
212 rulenumber,filename);
216 yaz_log(YLOG_FATAL,"No 'to' clause in a rule %d in %s",
217 rulenumber,filename);
221 yaz_log(YLOG_FATAL,"Must have exactly one 'from' and one 'to' clause "
222 "in rule %d in %s", rulenumber,filename);
225 if ( YAZ_NFA_SUCCESS == yaz_nfa_set_result(nfa,state,conv))
227 yaz_log(YLOG_FATAL,"Conflicting rules in %s rule %d",
228 filename, rulenumber);
233 /** \brief Parse the NFA from a XML document
235 yaz_nfa *yaz_nfa_parse_xml_doc(xmlDocPtr doc, const char *filename)
243 libxml2_error_to_yazlog(YLOG_FATAL, "yaz_nfa_parse_doc");
244 node = xmlDocGetRootElement(doc);
245 if (!node || node->type != XML_ELEMENT_NODE ||
246 strcmp((const char *) node->name, "ruleset"))
248 yaz_log(YLOG_FATAL,"nfa_parse_xml: Could not find root element 'ruleset' "
255 yaz_log(YLOG_FATAL,"nfa_parse_xml: Creating nfa failed, can't parse %s",
260 for (node = node->children; node; node = node->next)
262 if (node->type != XML_ELEMENT_NODE)
264 if (!strcmp((const char *) node->name, "rule")) {
265 if (!parse_rule(nfa,node,filename,rulenumber++))
268 yaz_log(YLOG_FATAL,"nfa_parse_xml: "
269 "expected 'rule', found '%s' in %s",
270 (const char *) node->name,filename);
275 } /* yaz_nfa_parse_xml_doc */
278 /** \brief Parse the NFA from a file
280 yaz_nfa *yaz_nfa_parse_xml_file(const char *filepath)
286 yaz_log(YLOG_FATAL,"yaz_nfa_parse_xml_file called with NULL");
289 libxml2_error_to_yazlog(YLOG_FATAL, "yaz_nfa_parse_xml_file");
291 doc = xmlParseFile(filepath);
295 nSubst=xmlXIncludeProcess(doc);
299 return yaz_nfa_parse_xml_doc(doc, filepath);
302 /** \brief Parse the NFA from a memory buffer
304 yaz_nfa *yaz_nfa_parse_xml_memory(const char *xmlbuff, const char *filename) {
308 yaz_log(YLOG_FATAL,"yaz_nfa_parse_memroy called with NULL");
311 libxml2_error_to_yazlog(YLOG_FATAL, "yaz_nfa_parse_xml_memory");
312 doc = xmlParseMemory(xmlbuff, strlen(xmlbuff));
313 return yaz_nfa_parse_xml_doc(doc,filename);
318 #endif /* YAZ_HAVE_XML2 */
324 * indent-tabs-mode: nil
326 * vim: shiftwidth=4 tabstop=8 expandtab