X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fmarcdisp.c;h=5cda0385a8b5dd6e2dbdd5ef4d91c4f9d700f8ac;hp=75e6d3c71bbbd1d797f8fee549401b5e216aca3c;hb=c0a7048ecd7a2dd9bede0edb27dbe1547be2e9d4;hpb=cde148647135f1a06f5b5ebf769430a9615d86bc diff --git a/src/marcdisp.c b/src/marcdisp.c index 75e6d3c..5cda038 100644 --- a/src/marcdisp.c +++ b/src/marcdisp.c @@ -91,7 +91,6 @@ struct yaz_marc_t_ { int output_format; int debug; int write_using_libxml2; - int turbo_format; enum yaz_collection_state enable_collection; yaz_iconv_t iconv_cd; char subfield_str[8]; @@ -164,7 +163,7 @@ void yaz_marc_add_controlfield_xml(yaz_marc_t mt, const xmlNode *ptr_tag, n->u.controlfield.data = nmem_text_node_cdata(ptr_data, mt->nmem); } -void yaz_marc_add_controlfield_turbo_xml(yaz_marc_t mt, const char *tag, +void yaz_marc_add_controlfield_turbo_xml(yaz_marc_t mt, char *tag, const xmlNode *ptr_data) { struct yaz_marc_node *n = yaz_marc_add_node(mt); @@ -242,6 +241,36 @@ void yaz_marc_add_datafield(yaz_marc_t mt, const char *tag, mt->subfield_pp = &n->u.datafield.subfields; } +// Magic function: adds a attribute value to the element name if it is plain characters. +// if not, and if the attribute name is not null, it will append a attribute element with the value +// if attribute name is null it will return a non-zero value meaning it couldnt handle the value. + +int element_name_append_attribute_value(yaz_marc_t mt, WRBUF buffer, const char *attribute_name, char *code_data, size_t code_len) { + // TODO Map special codes to something possible for XML ELEMENT names + + int encode = 0; + int index = 0; + for (index = 0; index < code_len; index++) { + if (!((code_data[index] >= '0' && code_data[index] <= '9') || + (code_data[index] >= 'a' && code_data[index] <= 'z') || + (code_data[index] >= 'A' && code_data[index] <= 'Z'))) + encode = 1; + } + int success = 0; + // Add as attribute + if (encode && attribute_name) + wrbuf_printf(buffer, " %s=\"", attribute_name); + + if (!encode || attribute_name) + wrbuf_iconv_write_cdata(buffer, mt->iconv_cd, code_data, code_len); + else + success = -1; + + if (encode && attribute_name) + wrbuf_printf(buffer, "\""); // return error if we couldn't handle it. + return success; +} + #if YAZ_HAVE_XML2 void yaz_marc_add_datafield_xml(yaz_marc_t mt, const xmlNode *ptr_tag, const char *indicator, size_t indicator_len) @@ -257,17 +286,16 @@ void yaz_marc_add_datafield_xml(yaz_marc_t mt, const xmlNode *ptr_tag, mt->subfield_pp = &n->u.datafield.subfields; } -struct yaz_marc_node* yaz_marc_add_datafield_turbo_xml(yaz_marc_t mt, char *tag_value) +void yaz_marc_add_datafield_turbo_xml(yaz_marc_t mt, char *tag_value, char *indicators) { struct yaz_marc_node *n = yaz_marc_add_node(mt); n->which = YAZ_MARC_DATAFIELD; n->u.datafield.tag = tag_value; - n->u.datafield.indicator = 0; + n->u.datafield.indicator = indicators; n->u.datafield.subfields = 0; - /* make subfield_pp the current (last one) */ + // make subfield_pp the current (last one) mt->subfield_pp = &n->u.datafield.subfields; - return n; } void yaz_marc_datafield_set_indicators(struct yaz_marc_node *n, char *indicator) @@ -572,6 +600,15 @@ int yaz_marc_write_mode(yaz_marc_t mt, WRBUF wr) return -1; } +const char *collection_name[2] = { "collection", "collection"}; +const char *record_name[2] = { "record", "r"}; +const char *leader_name[2] = { "leader", "l"}; +const char *controlfield_name[2]= { "controlfield", "c"}; +const char *datafield_name[2] = { "datafield", "d"}; +const char *indicator_name[2] = { "ind", "i"}; +const char *subfield_name[2] = { "subfield", "s"}; + + /** \brief common MARC XML/Xchange writer \param mt handle \param wr WRBUF output @@ -588,6 +625,145 @@ static int yaz_marc_write_marcxml_ns1(yaz_marc_t mt, WRBUF wr, int identifier_length; const char *leader = 0; + int turbo = yaz_marc_get_write_format(mt) == YAZ_MARC_TMARCXML; + + for (n = mt->nodes; n; n = n->next) + if (n->which == YAZ_MARC_LEADER) + { + leader = n->u.leader; + break; + } + + if (!leader) + return -1; + if (!atoi_n_check(leader+11, 1, &identifier_length)) + return -1; + + if (mt->enable_collection != no_collection) + { + if (mt->enable_collection == collection_first) { + wrbuf_printf(wr, "<%s xmlns=\"%s\">\n", collection_name[turbo], ns); + mt->enable_collection = collection_second; + } + wrbuf_printf(wr, "<%s", record_name[turbo]); + } + else + { + wrbuf_printf(wr, "<%s xmlns=\"%s\"", record_name[turbo], ns); + } + if (format) + wrbuf_printf(wr, " format=\"%.80s\"", format); + if (type) + wrbuf_printf(wr, " type=\"%.80s\"", type); + wrbuf_printf(wr, ">\n"); + for (n = mt->nodes; n; n = n->next) + { + struct yaz_marc_subfield *s; + + switch(n->which) + { + case YAZ_MARC_DATAFIELD: + + wrbuf_printf(wr, " <%s", datafield_name[turbo]); + if (!turbo) + wrbuf_printf(wr, " tag=\""); + wrbuf_iconv_write_cdata(wr, mt->iconv_cd, n->u.datafield.tag, + strlen(n->u.datafield.tag)); + if (!turbo) + wrbuf_printf(wr, "\""); + if (n->u.datafield.indicator) + { + int i; + for (i = 0; n->u.datafield.indicator[i]; i++) + { + wrbuf_printf(wr, " %s%d=\"", indicator_name[turbo], i+1); + wrbuf_iconv_write_cdata(wr, mt->iconv_cd, + n->u.datafield.indicator+i, 1); + wrbuf_iconv_puts(wr, mt->iconv_cd, "\""); + } + } + wrbuf_printf(wr, ">\n"); + for (s = n->u.datafield.subfields; s; s = s->next) + { + size_t using_code_len = get_subfield_len(mt, s->code_data, + identifier_length); + wrbuf_printf(wr, " <%s", subfield_name[turbo]); + if (!turbo) { + wrbuf_printf(wr, " code=\""); + wrbuf_iconv_write_cdata(wr, mt->iconv_cd, + s->code_data, using_code_len); + wrbuf_iconv_puts(wr, mt->iconv_cd, "\">"); + } else { + element_name_append_attribute_value(mt, wr, "code", s->code_data, using_code_len); + wrbuf_puts(wr, ">"); + } + wrbuf_iconv_write_cdata(wr, mt->iconv_cd, + s->code_data + using_code_len, + strlen(s->code_data + using_code_len)); + marc_iconv_reset(mt, wr); + wrbuf_printf(wr, "code_data, using_code_len); + wrbuf_puts(wr, ">\n"); + } + wrbuf_printf(wr, " iconv_cd, n->u.datafield.tag, + strlen(n->u.datafield.tag)); + wrbuf_printf(wr, ">\n", datafield_name[turbo]); + break; + case YAZ_MARC_CONTROLFIELD: + wrbuf_printf(wr, " <%s", controlfield_name[turbo]); + if (!turbo) { + wrbuf_printf(wr, " tag=\""); + wrbuf_iconv_write_cdata(wr, mt->iconv_cd, n->u.controlfield.tag, + strlen(n->u.controlfield.tag)); + wrbuf_iconv_puts(wr, mt->iconv_cd, "\">"); + } + else { + //TODO convert special + wrbuf_iconv_write_cdata(wr, mt->iconv_cd, n->u.controlfield.tag, + strlen(n->u.controlfield.tag)); + wrbuf_iconv_puts(wr, mt->iconv_cd, ">"); + } + wrbuf_iconv_write_cdata(wr, mt->iconv_cd, + n->u.controlfield.data, + strlen(n->u.controlfield.data)); + marc_iconv_reset(mt, wr); + wrbuf_printf(wr, "iconv_cd, n->u.controlfield.tag, + strlen(n->u.controlfield.tag)); + wrbuf_puts(wr, ">\n"); + break; + case YAZ_MARC_COMMENT: + wrbuf_printf(wr, "\n"); + break; + case YAZ_MARC_LEADER: + wrbuf_printf(wr, " <%s>", leader_name[turbo]); + wrbuf_iconv_write_cdata(wr, + 0 , /* no charset conversion for leader */ + n->u.leader, strlen(n->u.leader)); + wrbuf_printf(wr, "\n", leader_name[turbo]); + } + } + wrbuf_printf(wr, "\n", record_name[turbo]); + return 0; +} + +static int yaz_marc_write_marcxml_ns2(yaz_marc_t mt, WRBUF wr, + const char *ns, + const char *format, + const char *type) +{ + struct yaz_marc_node *n; + int identifier_length; + const char *leader = 0; + for (n = mt->nodes; n; n = n->next) if (n->which == YAZ_MARC_LEADER) { @@ -686,6 +862,7 @@ static int yaz_marc_write_marcxml_ns1(yaz_marc_t mt, WRBUF wr, return 0; } + static int yaz_marc_write_marcxml_ns(yaz_marc_t mt, WRBUF wr, const char *ns, const char *format, @@ -697,9 +874,9 @@ static int yaz_marc_write_marcxml_ns(yaz_marc_t mt, WRBUF wr, int ret; xmlNode *root_ptr; - if (!mt->turbo_format) + if (yaz_marc_get_write_format(mt) == YAZ_MARC_MARCXML) ret = yaz_marc_write_xml(mt, &root_ptr, ns, format, type); - else + else // Check for Turbo XML ret = yaz_marc_write_turbo_xml(mt, &root_ptr, ns, format, type); if (ret == 0) { @@ -752,7 +929,7 @@ void add_marc_datafield_turbo_xml(yaz_marc_t mt, struct yaz_marc_node *n, xmlNod { xmlNode *ptr; struct yaz_marc_subfield *s; - int turbo = mt->turbo_format; + int turbo = mt->output_format == YAZ_MARC_TMARCXML; if (!turbo) { ptr = xmlNewChild(record_ptr, ns_record, BAD_CAST "datafield", 0); xmlNewProp(ptr, BAD_CAST "tag", BAD_CAST n->u.datafield.tag); @@ -775,14 +952,8 @@ void add_marc_datafield_turbo_xml(yaz_marc_t mt, struct yaz_marc_node *n, xmlNod ind_val[0] = n->u.datafield.indicator[i]; ind_val[1] = '\0'; - if (!turbo) { - sprintf(ind_str, "ind%d", i+1); - xmlNewProp(ptr, BAD_CAST ind_str, BAD_CAST ind_val); - } - else { - sprintf(ind_str, "i%d", i+1); - xmlNewTextChild(ptr, ns_record, BAD_CAST ind_str, BAD_CAST ind_val); - } + sprintf(ind_str, "%s%d", indicator_name[turbo], i+1); + xmlNewProp(ptr, BAD_CAST ind_str, BAD_CAST ind_val); } } WRBUF subfield_name = wrbuf_alloc(); @@ -799,6 +970,7 @@ void add_marc_datafield_turbo_xml(yaz_marc_t mt, struct yaz_marc_node *n, xmlNod ptr_subfield = xmlNewTextChild( ptr, ns_record, BAD_CAST "subfield", BAD_CAST wrbuf_cstr(wr_cdata)); + // Generate code attribute value and add wrbuf_rewind(wr_cdata); wrbuf_iconv_write(wr_cdata, mt->iconv_cd,s->code_data, using_code_len); xmlNewProp(ptr_subfield, BAD_CAST "code", @@ -807,27 +979,16 @@ void add_marc_datafield_turbo_xml(yaz_marc_t mt, struct yaz_marc_node *n, xmlNod else { // Turbo format wrbuf_rewind(subfield_name); wrbuf_puts(subfield_name, "s"); - // TODO Map special codes to something possible for XML ELEMENT names - if ((s->code_data[0] >= '0' && s->code_data[0] <= '9') || - (s->code_data[0] >= 'a' && s->code_data[0] <= 'z') || - (s->code_data[0] >= 'A' && s->code_data[0] <= 'Z')) - { - wrbuf_iconv_write(subfield_name, mt->iconv_cd,s->code_data, using_code_len); - } - else { - char buffer[2*using_code_len + 1]; - int index; - for (index = 0; index < using_code_len; index++) { - sprintf(buffer + 2*index, "%02X", (unsigned char) s->code_data[index] & 0xFF); - }; - buffer[2*(index+1)] = 0; - wrbuf_puts(subfield_name, "-"); - wrbuf_puts(subfield_name, buffer); - yaz_log(YLOG_WARN, "Using numeric value in element name: %s", buffer); - } + int not_written = element_name_append_attribute_value(mt, subfield_name, 0, s->code_data, using_code_len) != 0; ptr_subfield = xmlNewTextChild(ptr, ns_record, BAD_CAST wrbuf_cstr(subfield_name), BAD_CAST wrbuf_cstr(wr_cdata)); + if (not_written) { + // Generate code attribute value and add + wrbuf_rewind(wr_cdata); + wrbuf_iconv_write(wr_cdata, mt->iconv_cd,s->code_data, using_code_len); + xmlNewProp(ptr_subfield, BAD_CAST "code", BAD_CAST wrbuf_cstr(wr_cdata)); + } } } wrbuf_destroy(subfield_name); @@ -844,7 +1005,7 @@ int yaz_marc_write_turbo_xml(yaz_marc_t mt, xmlNode **root_ptr, xmlNode *record_ptr; xmlNsPtr ns_record; WRBUF wr_cdata = 0; - int turbo = mt->turbo_format; + int turbo = mt->output_format == YAZ_MARC_TMARCXML; for (n = mt->nodes; n; n = n->next) if (n->which == YAZ_MARC_LEADER) { @@ -859,7 +1020,7 @@ int yaz_marc_write_turbo_xml(yaz_marc_t mt, xmlNode **root_ptr, wr_cdata = wrbuf_alloc(); - record_ptr = xmlNewNode(0, BAD_CAST "record"); + record_ptr = xmlNewNode(0, BAD_CAST "r"); *root_ptr = record_ptr; ns_record = xmlNewNs(record_ptr, BAD_CAST ns, 0); @@ -1207,8 +1368,9 @@ int yaz_marc_get_read_format(yaz_marc_t mt) void yaz_marc_set_write_format(yaz_marc_t mt, int format) { - if (mt) + if (mt) { mt->output_format = format; + } } int yaz_marc_get_write_format(yaz_marc_t mt) @@ -1224,8 +1386,7 @@ int yaz_marc_get_write_format(yaz_marc_t mt) */ void yaz_marc_xml(yaz_marc_t mt, int xmlmode) { - if (mt) - mt->output_format = xmlmode; + yaz_marc_set_write_format(mt, xmlmode); } @@ -1337,14 +1498,9 @@ void yaz_marc_write_using_libxml2(yaz_marc_t mt, int enable) mt->write_using_libxml2 = enable; } -void yaz_marc_write_turbo_format(yaz_marc_t mt, int enable) -{ - mt->turbo_format = enable; -} - int yaz_marc_is_turbo_format(yaz_marc_t mt) { - return mt->turbo_format; + return mt->output_format == YAZ_MARC_TMARCXML; }