Debian Codegraph @

src/common/reference.c

raw symbols SCIP
1 /*
2 * Copyright 2010 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Lukas Kuklinek <[email protected]>
21 */
22
23 /**
24 * @file
25 * Dublin Core references handling
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include "reference_priv.h"
33 #include "list.h"
34 #include "debug_priv.h"
35
36 #define NS_DUBLINCORE BAD_CAST "http://purl.org/dc/elements/1.1/"
37
38 OSCAP_ACCESSOR_SIMPLE(bool,oscap_reference,is_dublincore)
39 OSCAP_ACCESSOR_STRING(oscap_reference,href)
40 OSCAP_ACCESSOR_STRING(oscap_reference,title)
41 OSCAP_ACCESSOR_STRING(oscap_reference,creator)
42 OSCAP_ACCESSOR_STRING(oscap_reference,subject)
43 OSCAP_ACCESSOR_STRING(oscap_reference,description)
44 OSCAP_ACCESSOR_STRING(oscap_reference,publisher)
45 OSCAP_ACCESSOR_STRING(oscap_reference,contributor)
46 OSCAP_ACCESSOR_STRING(oscap_reference,date)
47 OSCAP_ACCESSOR_STRING(oscap_reference,type)
48 OSCAP_ACCESSOR_STRING(oscap_reference,format)
49 OSCAP_ACCESSOR_STRING(oscap_reference,identifier)
50 OSCAP_ACCESSOR_STRING(oscap_reference,source)
51 OSCAP_ACCESSOR_STRING(oscap_reference,language)
52 OSCAP_ACCESSOR_STRING(oscap_reference,relation)
53 OSCAP_ACCESSOR_STRING(oscap_reference,coverage)
54 OSCAP_ACCESSOR_STRING(oscap_reference,rights)
55
56 OSCAP_ITERATOR_GEN(oscap_reference)
57
58 struct oscap_reference *oscap_reference_new(void)
59 {
60 return calloc(1, sizeof(struct oscap_reference));
61 }
62
63 void oscap_reference_free(struct oscap_reference *ref)
64 {
65 if (ref) {
66 free(ref->title);
67 free(ref->creator);
68 free(ref->subject);
69 free(ref->description);
70 free(ref->publisher);
71 free(ref->contributor);
72 free(ref->date);
73 free(ref->type);
74 free(ref->format);
75 free(ref->identifier);
76 free(ref->source);
77 free(ref->language);
78 free(ref->relation);
79 free(ref->coverage);
80 free(ref->rights);
81 free(ref->href);
82 free(ref);
83 }
84 }
85
86 #define DC_ITEM_TO_DOM(ITEM) do { \
87 if (ref->ITEM != NULL) \
88 xmlNewTextChild(ref_node, ns_dc, BAD_CAST #ITEM, BAD_CAST ref->ITEM); \
89 } while(0)
90
91 xmlNode *oscap_reference_to_dom(struct oscap_reference *ref, xmlNode *parent, xmlDoc *doc, const char *elname)
92 {
93 if (!ref) return NULL;
94 xmlNode *ref_node = xmlNewChild(parent, NULL, BAD_CAST elname, NULL);
95
96 if (ref->href != NULL)
97 xmlNewProp(ref_node, BAD_CAST "href", BAD_CAST ref->href);
98
99 if (!ref->is_dublincore) {
100 xmlNodeAddContent(ref_node, BAD_CAST ref->title);
101 return ref_node;
102 }
103
104 xmlNs *ns_dc = xmlSearchNsByHref(doc, parent, NS_DUBLINCORE);
105 if (ns_dc == NULL) // the namespace hasn't been defined in ancestor elements
106 ns_dc = xmlNewNs(ref_node, NS_DUBLINCORE, BAD_CAST "dc");
107
108 DC_ITEM_TO_DOM(title);
109 DC_ITEM_TO_DOM(creator);
110 DC_ITEM_TO_DOM(subject);
111 DC_ITEM_TO_DOM(description);
112 DC_ITEM_TO_DOM(publisher);
113 DC_ITEM_TO_DOM(contributor);
114 DC_ITEM_TO_DOM(date);
115 DC_ITEM_TO_DOM(type);
116 DC_ITEM_TO_DOM(format);
117 DC_ITEM_TO_DOM(identifier);
118 DC_ITEM_TO_DOM(source);
119 DC_ITEM_TO_DOM(language);
120 DC_ITEM_TO_DOM(relation);
121 DC_ITEM_TO_DOM(coverage);
122 DC_ITEM_TO_DOM(rights);
123
124 return ref_node;
125 }
126
127 #define DC_DOM_SCAN(ITEM) do { \
128 if (oscap_streq((const char*)cur->name, #ITEM)) \
129 ref->ITEM = (char*) xmlNodeGetContent(cur); \
130 } while(0)
131
132 struct oscap_reference *oscap_reference_new_parse(xmlTextReaderPtr reader)
133 {
134 assert(reader != NULL);
135
136 struct oscap_reference *ref = calloc(1, sizeof(struct oscap_reference));
137
138 int depth = oscap_element_depth(reader);
139
140 xmlNode* ref_node = xmlTextReaderExpand(reader);
141
142 ref->href = (char*) xmlGetProp(ref_node, BAD_CAST "href");
143
144 for (xmlNode* cur = ref_node->children; cur != NULL; cur = cur->next)
145 if (cur->type == XML_ELEMENT_NODE) { ref->is_dublincore = true; break; }
146
147 if (ref->is_dublincore) {
148 for (xmlNode* cur = ref_node->children; cur != NULL; cur = cur->next) {
149 if (cur->type != XML_ELEMENT_NODE
150 || cur->ns == NULL
151 || !oscap_streq((const char* ) cur->ns->href, (const char *) NS_DUBLINCORE))
152 continue;
153
154 DC_DOM_SCAN(title);
155 DC_DOM_SCAN(creator);
156 DC_DOM_SCAN(subject);
157 DC_DOM_SCAN(description);
158 DC_DOM_SCAN(publisher);
159 DC_DOM_SCAN(contributor);
160 DC_DOM_SCAN(date);
161 DC_DOM_SCAN(type);
162 DC_DOM_SCAN(format);
163 DC_DOM_SCAN(identifier);
164 DC_DOM_SCAN(source);
165 DC_DOM_SCAN(language);
166 DC_DOM_SCAN(relation);
167 DC_DOM_SCAN(coverage);
168 DC_DOM_SCAN(rights);
169 }
170 }
171 else {
172 ref->title = (char*) xmlNodeGetContent(ref_node);
173 }
174
175 if (!oscap_to_start_element(reader, depth))
176 dW("oscap_to_start_element returned `false'");
177
178 return ref;
179 }
180
181 #define DC_ITEM_CLONE(ITEM) do { new_ref->ITEM = oscap_strdup(ref->ITEM); } while(0)
182
183 struct oscap_reference *oscap_reference_clone(const struct oscap_reference *ref)
184 {
185 if (ref == NULL) return NULL;
186
187 struct oscap_reference *new_ref = oscap_reference_new();
188
189 new_ref->is_dublincore = ref->is_dublincore;
190 DC_ITEM_CLONE(title);
191 DC_ITEM_CLONE(creator);
192 DC_ITEM_CLONE(subject);
193 DC_ITEM_CLONE(description);
194 DC_ITEM_CLONE(publisher);
195 DC_ITEM_CLONE(contributor);
196 DC_ITEM_CLONE(date);
197 DC_ITEM_CLONE(type);
198 DC_ITEM_CLONE(format);
199 DC_ITEM_CLONE(identifier);
200 DC_ITEM_CLONE(source);
201 DC_ITEM_CLONE(language);
202 DC_ITEM_CLONE(relation);
203 DC_ITEM_CLONE(coverage);
204 DC_ITEM_CLONE(rights);
205 DC_ITEM_CLONE(href);
206
207 return new_ref;
208 }
209