00001
00002
00003
00004
00005
00010 #include <stdio.h>
00011 #include <string.h>
00012 #include <assert.h>
00013
00014 #include <yaz/ccl_xml.h>
00015
00016 #if YAZ_HAVE_XML2
00017
00018 static int ccl_xml_config_attr(CCL_bibset bibset, const char *default_set,
00019 WRBUF wrbuf,
00020 const xmlNode *ptr,
00021 const char **addinfo)
00022 {
00023 struct _xmlAttr *attr;
00024 const char *type = 0;
00025 const char *value = 0;
00026 const char *attrset = default_set;
00027 for (attr = ptr->properties; attr; attr = attr->next)
00028 {
00029 if (!xmlStrcmp(attr->name, BAD_CAST "type") &&
00030 attr->children && attr->children->type == XML_TEXT_NODE)
00031 type = (const char *) attr->children->content;
00032 else if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
00033 attr->children && attr->children->type == XML_TEXT_NODE)
00034 value = (const char *) attr->children->content;
00035 else if (!xmlStrcmp(attr->name, BAD_CAST "attrset") &&
00036 attr->children && attr->children->type == XML_TEXT_NODE)
00037 attrset = (const char *) attr->children->content;
00038 else
00039 {
00040 *addinfo = "bad attribute for 'attr'. "
00041 "Expecting 'type', 'value', or 'attrset'";
00042 return 1;
00043 }
00044 }
00045 if (!type)
00046 {
00047 *addinfo = "missing attribute for 'type' for element 'attr'";
00048 return 1;
00049 }
00050 if (!value)
00051 {
00052 *addinfo = "missing attribute for 'value' for element 'attr'";
00053 return 1;
00054 }
00055 if (attrset)
00056 wrbuf_printf(wrbuf, "%s,%s=%s", attrset, type, value);
00057 else
00058 wrbuf_printf(wrbuf, "%s=%s", type, value);
00059 return 0;
00060 }
00061
00062 static int ccl_xml_config_qual(CCL_bibset bibset, const char *default_set,
00063 WRBUF wrbuf,
00064 const xmlNode *ptr,
00065 const char **addinfo)
00066 {
00067 struct _xmlAttr *attr;
00068 const char *name = 0;
00069 const xmlNode *a_ptr = ptr->children;
00070 for (attr = ptr->properties; attr; attr = attr->next)
00071 {
00072 if (!xmlStrcmp(attr->name, BAD_CAST "name") &&
00073 attr->children && attr->children->type == XML_TEXT_NODE)
00074 name = (const char *) attr->children->content;
00075 else
00076 {
00077 *addinfo = "bad attribute for 'qual'. Expecting 'name' only";
00078 return 1;
00079 }
00080 }
00081 if (!name)
00082 {
00083 *addinfo = "missing attribute 'name' for 'qual' element";
00084 return 1;
00085 }
00086 for (; a_ptr; a_ptr = a_ptr->next)
00087 {
00088 if (a_ptr->type == XML_ELEMENT_NODE)
00089 {
00090 if (!xmlStrcmp(a_ptr->name, BAD_CAST "attr"))
00091 {
00092 int r = ccl_xml_config_attr(bibset, default_set, wrbuf,
00093 a_ptr, addinfo);
00094 if (r)
00095 return r;
00096 wrbuf_printf(wrbuf, " ");
00097 }
00098 else
00099 {
00100 *addinfo = "bad element: expecting 'attr'";
00101 return 1;
00102 }
00103 }
00104 }
00105 ccl_qual_fitem(bibset, wrbuf_cstr(wrbuf), name);
00106 return 0;
00107 }
00108
00109 int ccl_xml_config_directive(CCL_bibset bibset, const xmlNode *ptr,
00110 const char **addinfo)
00111 {
00112 struct _xmlAttr *attr;
00113 const char *name = 0;
00114 const char *value = 0;
00115 for (attr = ptr->properties; attr; attr = attr->next)
00116 {
00117 if (!xmlStrcmp(attr->name, BAD_CAST "name") &&
00118 attr->children && attr->children->type == XML_TEXT_NODE)
00119 name = (const char *) attr->children->content;
00120 else if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
00121 attr->children && attr->children->type == XML_TEXT_NODE)
00122 value = (const char *) attr->children->content;
00123 else
00124 {
00125 *addinfo = "bad attribute for 'diretive'. "
00126 "Expecting 'name' or 'value'";
00127 return 1;
00128 }
00129 }
00130 if (!name)
00131 {
00132 *addinfo = "missing attribute 'name' for 'directive' element";
00133 return 1;
00134 }
00135 if (!value)
00136 {
00137 *addinfo = "missing attribute 'name' for 'value' element";
00138 return 1;
00139 }
00140 ccl_qual_add_special(bibset, name, value);
00141 return 0;
00142 }
00143
00144 int ccl_xml_config(CCL_bibset bibset, const xmlNode *ptr, const char **addinfo)
00145 {
00146 if (ptr && ptr->type == XML_ELEMENT_NODE &&
00147 !xmlStrcmp(ptr->name, BAD_CAST "cclmap"))
00148 {
00149 const xmlNode *c_ptr;
00150 const char *set = 0;
00151 struct _xmlAttr *attr;
00152 for (attr = ptr->properties; attr; attr = attr->next)
00153 {
00154 if (!xmlStrcmp(attr->name, BAD_CAST "defaultattrset") &&
00155 attr->children && attr->children->type == XML_TEXT_NODE)
00156 set = (const char *) attr->children->content;
00157 else
00158 {
00159 *addinfo = "bad attribute for 'cclmap'. "
00160 "expecting 'defaultattrset'";
00161 return 1;
00162 }
00163 }
00164 for (c_ptr = ptr->children; c_ptr; c_ptr = c_ptr->next)
00165 {
00166 if (c_ptr->type == XML_ELEMENT_NODE)
00167 {
00168 if (!xmlStrcmp(c_ptr->name, BAD_CAST "qual"))
00169 {
00170 WRBUF wrbuf = wrbuf_alloc();
00171 int r = ccl_xml_config_qual(bibset, set,
00172 wrbuf, c_ptr, addinfo);
00173 wrbuf_destroy(wrbuf);
00174 if (r)
00175 return r;
00176 }
00177 else if (!xmlStrcmp(c_ptr->name, BAD_CAST "directive"))
00178 {
00179 int r = ccl_xml_config_directive(bibset, c_ptr, addinfo);
00180 if (r)
00181 return r;
00182 }
00183 else
00184 {
00185 *addinfo = "bad element for 'cclmap'. "
00186 "expecting 'directive' or 'qual'";
00187 return 1;
00188 }
00189 }
00190 }
00191 }
00192 return 0;
00193 }
00194 #endif
00195
00196
00197
00198
00199
00200
00201
00202