5 #include <libxml/parser.h>
6 #include <libxml/tree.h>
13 struct marcmap *marcmap_load(char *filename, NMEM nmem) {
15 struct marcmap *mmhead;
28 fp = fopen(filename, "r");
30 while ((c = getc(fp) ) != EOF)
32 // allocate some space
37 mm->next = nmem_malloc(nmem, sizeof(struct marcmap));
42 { mm = nmem_malloc(nmem, sizeof(struct marcmap));
47 // whitespace saves and moves on
48 if (c == ' ' || c == '\n' || c == '\t')
58 mm->field = nmem_malloc(nmem, len * sizeof(char));
59 strncpy(mm->field, buf, len);
62 // second, marc subfield, just a char
65 mm->subfield = buf[len-2];
67 // third, pz fieldname
70 mm->pz = nmem_malloc(nmem, len * sizeof(char));
71 strncpy(mm->pz, buf, len);
74 // new line, new record
96 xmlDoc *marcmap_apply(struct marcmap *marcmap, xmlDoc *xml_in)
104 xmlNodePtr xml_out_root;
106 xmlNodePtr meta_node;
107 struct marchash *marchash;
108 struct marcfield *field;
109 struct marcsubfield *subfield;
110 struct marcmap *mmcur;
112 xml_out = xmlNewDoc(BAD_CAST "1.0");
113 xml_out_root = xmlNewNode(NULL, BAD_CAST "record");
114 xmlDocSetRootElement(xml_out, xml_out_root);
115 ns_pz = xmlNewNs(xml_out_root, BAD_CAST "http://www.indexdata.com/pazpar2/1.0", BAD_CAST "pz");
116 nmem = nmem_create();
117 rec_node = xmlDocGetRootElement(xml_in);
118 marchash = marchash_create(nmem);
119 marchash_ingest_marcxml(marchash, rec_node);
122 while (mmcur != NULL)
124 if (field = marchash_get_field(marchash, mmcur->field, NULL))
128 if ((mmcur->subfield == '$') && (s = field->val))
130 meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", s);
131 xmlSetProp(meta_node, BAD_CAST "type", mmcur->pz);
133 // catenate all subfields
134 else if ((mmcur->subfield == '*') && (s = marchash_catenate_subfields(field, " ", nmem)))
136 meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", s);
137 xmlSetProp(meta_node, BAD_CAST "type", mmcur->pz);
140 else if (mmcur->subfield)
142 if (subfield = marchash_get_subfield(mmcur->subfield, field, NULL))
144 if (s = subfield->val)
146 meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", s);
147 xmlSetProp(meta_node, BAD_CAST "type", mmcur->pz);
149 while (subfield = marchash_get_subfield(mmcur->subfield, field, subfield));
153 while (field = marchash_get_field(marchash, mmcur->field, field));
157 // hard coded mappings
160 if ((field = marchash_get_field(marchash, "245", NULL)) && (subfield = marchash_get_subfield('h', field, NULL)))
162 strncpy(medium, subfield->val, 32);
164 else if ((field = marchash_get_field(marchash, "900", NULL)) && (subfield = marchash_get_subfield('a', field, NULL)))
165 strcpy(medium, "electronic resource");
166 else if ((field = marchash_get_field(marchash, "900", NULL)) && (subfield = marchash_get_subfield('b', field, NULL)))
167 strcpy(medium, "electronic resource");
168 else if ((field = marchash_get_field(marchash, "773", NULL)) && (subfield = marchash_get_subfield('t', field, NULL)))
169 strcpy(medium, "article");
171 strcpy(medium, "book");
173 meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", BAD_CAST medium);
174 xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST "medium");
177 memset(mergekey, 0, 1024);
178 strcpy(mergekey, "title ");
179 if ((field = marchash_get_field(marchash, "245", NULL)) && (subfield = marchash_get_subfield('a', field, NULL)))
180 strncat(mergekey, subfield->val, 1023 - strlen(mergekey));
181 strncat(mergekey, " author ", 1023 - strlen(mergekey));
182 if ((field = marchash_get_field(marchash, "245", NULL)) && (subfield = marchash_get_subfield('a', field, NULL)))
183 strncat(mergekey, subfield->val, 1023 - strlen(mergekey));
184 strncat(mergekey, " medium ", 1023 - strlen(mergekey));
185 strncat(mergekey, medium, 1023 - strlen(mergekey));
187 xmlSetProp(xml_out_root, BAD_CAST "mergekey", BAD_CAST mergekey);