1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 Index Data
3 * See the file LICENSE for details.
7 \brief XML Include (not to be confused with W3C XInclude)
13 #include <yaz/file_glob.h>
15 #include <sys/types.h>
20 #include <yaz/wrbuf.h>
21 #include <yaz/tpath.h>
23 #include <yaz/xml_include.h>
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
30 struct yaz_xml_include_s {
34 typedef struct yaz_xml_include_s *yaz_xml_include_t;
36 static int process_config_includes(yaz_xml_include_t config, xmlNode *n);
38 static void conf_dir_path(yaz_xml_include_t config, WRBUF w, const char *src)
40 if (config->confdir && *config->confdir > 0 &&
43 wrbuf_printf(w, "%s/%s", config->confdir, src);
49 static int config_include_one(yaz_xml_include_t config, xmlNode **sib,
53 if (stat(path, &st) < 0)
55 yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
60 if ((st.st_mode & S_IFMT) == S_IFREG)
62 xmlDoc *doc = xmlParseFile(path);
65 xmlNodePtr t = xmlDocGetRootElement(doc);
66 int ret = process_config_includes(config, t);
67 *sib = xmlAddNextSibling(*sib, xmlCopyNode(t, 1));
74 yaz_log(YLOG_FATAL, "Could not parse %s", path);
82 static int config_include_src(yaz_xml_include_t config, xmlNode **np,
85 int ret = 0; /* return code. OK so far */
86 WRBUF w = wrbuf_alloc();
87 xmlNodePtr sib; /* our sibling that we append */
88 xmlNodePtr c; /* tmp node */
90 wrbuf_printf(w, " begin include src=\"%s\" ", src);
92 /* replace include element with a 'begin' comment */
93 sib = xmlNewComment((const xmlChar *) wrbuf_cstr(w));
94 xmlReplaceNode(*np, sib);
99 conf_dir_path(config, w, src);
102 yaz_glob_res_t glob_res;
104 glob_ret = yaz_file_glob(wrbuf_cstr(w), &glob_res);
110 for (i = 0; (path = yaz_file_glob_get_file(glob_res, i)); i++)
111 ret = config_include_one(config, &sib, path);
112 yaz_file_globfree(&glob_res);
116 wrbuf_printf(w, " end include src=\"%s\" ", src);
117 c = xmlNewComment((const xmlChar *) wrbuf_cstr(w));
118 sib = xmlAddNextSibling(sib, c);
125 static int process_config_includes(yaz_xml_include_t config, xmlNode *n)
127 for (n = n->children; n; n = n->next)
129 if (n->type == XML_ELEMENT_NODE)
131 if (!strcmp((const char *) n->name, "include"))
133 xmlChar *src = xmlGetProp(n, (xmlChar *) "src");
136 int ret = config_include_src(config, &n,
146 if (process_config_includes(config, n))
154 int yaz_xml_include_simple(xmlNode *n, const char *base_path)
156 struct yaz_xml_include_s s;
158 s.confdir = base_path;
159 return process_config_includes(&s, n);
168 * c-file-style: "Stroustrup"
169 * indent-tabs-mode: nil
171 * vim: shiftwidth=4 tabstop=8 expandtab