1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 \brief MARC map implementation
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
40 struct marcmap *marcmap_load(const char *filename, NMEM nmem)
43 struct marcmap *mmhead;
56 fp = fopen(filename, "r");
58 while ((c = getc(fp) ) != EOF)
60 // allocate some space
65 mm->next = nmem_malloc(nmem, sizeof(struct marcmap));
70 { mm = nmem_malloc(nmem, sizeof(struct marcmap));
75 // whitespace saves and moves on
76 if (c == ' ' || c == '\n' || c == '\t')
86 mm->field = nmem_malloc(nmem, len * sizeof(char));
87 strncpy(mm->field, buf, len);
90 // second, marc subfield, just a char
93 mm->subfield = buf[len-2];
95 // third, pz fieldname
98 mm->pz = nmem_malloc(nmem, len * sizeof(char));
99 strncpy(mm->pz, buf, len);
102 // new line, new record
124 xmlDoc *marcmap_apply(struct marcmap *marcmap, xmlDoc *xml_in)
132 xmlNodePtr xml_out_root;
134 xmlNodePtr meta_node;
135 struct marchash *marchash;
136 struct marcfield *field;
137 struct marcsubfield *subfield;
138 struct marcmap *mmcur;
140 xml_out = xmlNewDoc(BAD_CAST "1.0");
141 xml_out_root = xmlNewNode(NULL, BAD_CAST "record");
142 xmlDocSetRootElement(xml_out, xml_out_root);
143 ns_pz = xmlNewNs(xml_out_root, BAD_CAST "http://www.indexdata.com/pazpar2/1.0", BAD_CAST "pz");
144 nmem = nmem_create();
145 rec_node = xmlDocGetRootElement(xml_in);
146 marchash = marchash_create(nmem);
147 marchash_ingest_marcxml(marchash, rec_node);
150 while (mmcur != NULL)
153 while ((field = marchash_get_field(marchash, mmcur->field, field)) != 0)
156 if ((mmcur->subfield == '$') && (s = field->val))
158 meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", BAD_CAST s);
159 xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST mmcur->pz);
161 // catenate all subfields
162 else if ((mmcur->subfield == '*') && (s = marchash_catenate_subfields(field, " ", nmem)))
164 meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", BAD_CAST s);
165 xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST mmcur->pz);
168 else if (mmcur->subfield)
172 marchash_get_subfield(mmcur->subfield,
173 field, subfield)) != 0)
175 if ((s = subfield->val) != 0)
177 meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", BAD_CAST s);
178 xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST mmcur->pz);
187 // hard coded mappings
190 if ((field = marchash_get_field(marchash, "245", NULL)) && (subfield = marchash_get_subfield('h', field, NULL)))
192 strncpy(medium, subfield->val, 32);
194 else if ((field = marchash_get_field(marchash, "900", NULL)) && (subfield = marchash_get_subfield('a', field, NULL)))
195 strcpy(medium, "electronic resource");
196 else if ((field = marchash_get_field(marchash, "900", NULL)) && (subfield = marchash_get_subfield('b', field, NULL)))
197 strcpy(medium, "electronic resource");
198 else if ((field = marchash_get_field(marchash, "773", NULL)) && (subfield = marchash_get_subfield('t', field, NULL)))
199 strcpy(medium, "article");
201 strcpy(medium, "book");
203 meta_node = xmlNewChild(xml_out_root, ns_pz, BAD_CAST "metadata", BAD_CAST medium);
204 xmlSetProp(meta_node, BAD_CAST "type", BAD_CAST "medium");
207 memset(mergekey, 0, 1024);
208 strcpy(mergekey, "title ");
209 if ((field = marchash_get_field(marchash, "245", NULL)) && (subfield = marchash_get_subfield('a', field, NULL)))
210 strncat(mergekey, subfield->val, 1023 - strlen(mergekey));
211 strncat(mergekey, " author ", 1023 - strlen(mergekey));
212 if ((field = marchash_get_field(marchash, "245", NULL)) && (subfield = marchash_get_subfield('a', field, NULL)))
213 strncat(mergekey, subfield->val, 1023 - strlen(mergekey));
214 strncat(mergekey, " medium ", 1023 - strlen(mergekey));
215 strncat(mergekey, medium, 1023 - strlen(mergekey));
217 xmlSetProp(xml_out_root, BAD_CAST "mergekey", BAD_CAST mergekey);
226 * c-file-style: "Stroustrup"
227 * indent-tabs-mode: nil
229 * vim: shiftwidth=4 tabstop=8 expandtab