-
Notifications
You must be signed in to change notification settings - Fork 1
/
xml.c
175 lines (157 loc) · 4.14 KB
/
xml.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* @file xml.c
* XML based database schema support for Apteryx.
*
* Copyright 2016, Allied Telesis Labs New Zealand, Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>
*/
#include "internal.h"
#include <libxml/parser.h>
#include <libxml/tree.h>
/* Convert an XML node to apteryx_schema_node */
static struct apteryx_schema_node *
xml_to_node (xmlNode *xml, int depth)
{
struct apteryx_schema_node *node;
char *field;
/* NULL */
if (!xml)
{
ERROR ("XML: Node NULL\n");
return NULL;
}
/* Create a new node */
field = (char *) xmlGetProp (xml, (xmlChar *) "name");
if (!field)
{
ERROR ("XML: Node has no name\n");
return NULL;
}
node = node_create (field);
xmlFree (field);
/* Default */
field = (char *) xmlGetProp (xml, (xmlChar *) "default");
if (field)
{
node->defvalue = g_strdup (field);
xmlFree (field);
}
/* Pattern */
field = (char *) xmlGetProp (xml, (xmlChar *) "pattern");
if (field)
{
node->pattern = g_strdup (field);
xmlFree (field);
}
/* Mode */
field = (char *) xmlGetProp (xml, (xmlChar *) "mode");
if (field)
{
if (strchr (field, 'r') != NULL)
{
node->flags |= NODE_FLAGS_READ;
}
if (strchr (field, 'w') != NULL)
{
node->flags |= NODE_FLAGS_WRITE;
}
//TODO config
//TODO hidden
xmlFree (field);
}
/* Handle ENUMs */
if (g_strcmp0 (xml->name, "VALUE") == 0)
{
/* Value */
field = (char *) xmlGetProp (xml, (xmlChar *) "value");
if (field)
{
node->value = g_strdup (field);
node->flags |= NODE_FLAGS_ENUM;
xmlFree (field);
}
}
/* Description */
field = (char *) xmlGetProp (xml, (xmlChar *) "help");
if (field)
{
node->description = g_strdup (field);
xmlFree (field);
}
/* Leaf */
if (!xml->children || g_strcmp0 (xml->children->name, "VALUE") == 0)
{
node->flags |= NODE_FLAGS_LEAF;
}
/* Process children */
for (xmlNode *child = xml->children; child; child = child->next)
{
struct apteryx_schema_node *cn = xml_to_node (child, depth + 1);
if (cn)
node->children = g_list_append (node->children, cn);
}
return node;
}
/* Remove unwanted nodes and attributes from a parsed tree */
static void
cleanup_nodes (xmlNode *node)
{
xmlNode *n, *next;
n = node;
while (n)
{
next = n->next;
if (n->type == XML_ELEMENT_NODE)
{
cleanup_nodes (n->children);
xmlSetNs (n, NULL);
}
else
{
xmlUnlinkNode (n);
xmlFreeNode (n);
}
n = next;
}
}
/* Load an Apteryx schema in XML format */
struct apteryx_schema_node *
xml_schema_load (const char *filename)
{
struct apteryx_schema_node *schema;
xmlDoc *doc;
xmlNode *root;
/* Parse document */
doc = xmlParseFile (filename);
if (!doc)
{
ERROR ("XML: Invalid XML\n");
return NULL;
}
/* Find root node */
root = xmlDocGetRootElement (doc);
if (!root || g_strcmp0 (root->name, "MODULE") != 0)
{
ERROR ("XML: No root MODULE element\n");
xmlFreeDoc (doc);
return NULL;
}
/* Remove TEXT etc */
cleanup_nodes (root->children);
/* Convert to apteryx_schema_node */
schema = xml_to_node (root->children, 0);
xmlFreeDoc (doc);
return schema;
}