-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaddml-parser.c
136 lines (126 loc) · 3.78 KB
/
addml-parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* $id$
*
* noark5-parser
*
* Copyright (C) 2017 Ole Aamot
*
* Author: Ole Aamot <oka@oka.no>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
typedef struct _Noark5DataObject {
char *name;
} Noark5DataObject;
typedef struct _Noark5DataObjects {
Noark5DataObject *curr;
Noark5DataObject *prev;
Noark5DataObject *next;
} Noark5DataObjects;
typedef struct _Noark5Reference {
char *recordCreator;
char *systemType;
char *systemName;
char *archive;
} Noark5Reference;
typedef struct _Noark5Dataset {
char *name;
char *description;
Noark5Reference *reference;
Noark5DataObjects *dataObjects;
} Noark5Dataset;
static void
noark5_dataobjects_parser(Noark5Dataset *dataset, xmlDocPtr doc, xmlNodePtr cur)
{
xmlNodePtr sub;
sub = cur->xmlChildrenNode;
while (sub != NULL) {
if ((!xmlStrcmp(sub->name, (const xmlChar *) "dataObject"))) {
dataset->dataObjects->prev = dataset->dataObjects->curr;
dataset->dataObjects->curr = g_new0(Noark5DataObject, 1);
fprintf(stdout,"Found new dataObject!\n");
dataset->dataObjects->curr->name = (gchar *) xmlGetProp(sub, (const xmlChar *)"name");
printf("dataObject:%s\n", dataset->dataObjects->curr->name);
}
sub = sub->next;
}
}
static void
noark5_reference_parser(Noark5Dataset *dataset, xmlDocPtr doc, xmlNodePtr cur)
{
xmlNodePtr sub;
sub = cur->xmlChildrenNode;
while (sub != NULL) {
if ((!xmlStrcmp(sub->name, (const xmlChar *) "reference"))) {
dataset->reference = g_new0(Noark5Reference, 1);
fprintf(stdout,"Found new reference!\n");
}
if ((!xmlStrcmp(sub->name, (const xmlChar *) "dataObjects"))) {
dataset->dataObjects = g_new0(Noark5DataObjects, 1);
fprintf(stdout,"Found new dataObjects!\n");
noark5_dataobjects_parser(dataset, doc, sub);
}
sub = sub->next;
}
}
int main (int argc, char **argv)
{
xmlDocPtr doc = NULL;
xmlNodePtr cur = NULL;
xmlNodePtr sub = NULL;
Noark5Dataset *dataset, *curr;
Noark5Reference *reference;
Noark5DataObjects *dataObjects;
if (argc > 1) {
doc = xmlReadFile(argv[1], NULL, 0);
if (doc == NULL) {
perror("xmlParseFile");
xmlFreeDoc(doc);
return 0;
}
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr, "Empty document\n");
xmlFreeDoc(doc);
return 1;
}
if (xmlStrcmp(cur->name, (const xmlChar *) "addml")) {
fprintf(stderr, "Document of wrong type, root node != addml\n");
xmlFreeDoc(doc);
return 2;
}
curr = g_new0(Noark5Dataset, 1);
curr->name = (gchar *) xmlGetProp(cur, (const xmlChar *)"name");
printf("%s\n", curr->name);
sub = cur->xmlChildrenNode;
while (sub != NULL) {
if ((!xmlStrcmp(sub->name, (const xmlChar *) "dataset"))) {
curr->reference = g_new0(Noark5Reference, 1);
fprintf(stdout,"Found new dataset!\n");
noark5_reference_parser(curr, doc, sub);
}
sub = sub->next;
}
free(curr->dataObjects);
free(curr->reference);
free(curr->name);
free(curr);
} else {
fprintf(stdout, "noark5-parser FILE\n");
}
return 0;
}