forked from AravisProject/aravis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharvdomdocument.c
240 lines (188 loc) · 5.91 KB
/
arvdomdocument.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* Aravis - Digital camera library
*
* Copyright © 2009-2022 Emmanuel Pacaud
*
* 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 2 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 Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* Author:
* Emmanuel Pacaud <[email protected]>
*/
/**
* SECTION:arvdomdocument
* @short_description: Base class for DOM document nodes
*/
#include <arvdomdocument.h>
#include <arvdomelement.h>
#include <arvstr.h>
#include <arvdebug.h>
#include <arvdomtext.h>
#include <gio/gio.h>
#include <string.h>
typedef struct {
char * url;
} ArvDomDocumentPrivate;
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (ArvDomDocument, arv_dom_document, ARV_TYPE_DOM_NODE, G_ADD_PRIVATE (ArvDomDocument))
/* ArvDomNode implementation */
static const char *
arv_dom_document_get_node_name (ArvDomNode *node)
{
return "#document";
}
static ArvDomNodeType
arv_dom_document_get_node_type (ArvDomNode *node)
{
return ARV_DOM_NODE_TYPE_DOCUMENT_NODE;
}
/* ArvDomDocument implementation */
/**
* arv_dom_document_get_document_element:
* @self: a #ArvDomDocument
*
* Returns: (transfer none): the top element of @self.
*/
ArvDomElement *
arv_dom_document_get_document_element (ArvDomDocument *self)
{
g_return_val_if_fail (ARV_IS_DOM_DOCUMENT (self), NULL);
return ARV_DOM_ELEMENT (arv_dom_node_get_first_child (ARV_DOM_NODE (self)));
}
/**
* arv_dom_document_create_element:
* @self: a #ArvDomDocument
* @tag_name: node name of the element to create
*
* Create a new element.
*
* Returns: (transfer full): a new orphan #ArvDomElement, NULL on error.
*/
ArvDomElement *
arv_dom_document_create_element (ArvDomDocument *self, const char *tag_name)
{
ArvDomDocumentClass *document_class;
g_return_val_if_fail (ARV_IS_DOM_DOCUMENT (self), NULL);
document_class = ARV_DOM_DOCUMENT_GET_CLASS (self);
if (document_class->create_element != NULL)
return document_class->create_element (self, tag_name);
return NULL;
}
static ArvDomText *
arv_dom_document_create_text_node_base (ArvDomDocument *document, const char *data)
{
return ARV_DOM_TEXT (arv_dom_text_new (data));
}
/**
* arv_dom_document_create_text_node:
* @self: a #ArvDomDocument
* @data: initial content
*
* Create a new text element.
*
* Returns: (transfer full): a new orphan #ArvDomText, NULL on error.
*/
ArvDomText *
arv_dom_document_create_text_node (ArvDomDocument *self, const char *data)
{
g_return_val_if_fail (ARV_IS_DOM_DOCUMENT (self), NULL);
return ARV_DOM_DOCUMENT_GET_CLASS (self)->create_text_node (self, data);
}
const char *
arv_dom_document_get_url (ArvDomDocument *self)
{
ArvDomDocumentPrivate *priv = arv_dom_document_get_instance_private (ARV_DOM_DOCUMENT (self));
g_return_val_if_fail (ARV_IS_DOM_DOCUMENT (self), NULL);
return priv->url;
}
void
arv_dom_document_set_path (ArvDomDocument *self, const char *path)
{
ArvDomDocumentPrivate *priv = arv_dom_document_get_instance_private (ARV_DOM_DOCUMENT (self));
g_return_if_fail (ARV_IS_DOM_DOCUMENT (self));
g_free (priv->url);
if (path == NULL) {
priv->url = NULL;
return;
}
priv->url = arv_str_to_uri (path);
}
void
arv_dom_document_set_url (ArvDomDocument *self, const char *url)
{
ArvDomDocumentPrivate *priv = arv_dom_document_get_instance_private (ARV_DOM_DOCUMENT (self));
g_return_if_fail (ARV_IS_DOM_DOCUMENT (self));
g_return_if_fail (url == NULL || arv_str_is_uri (url));
g_free (priv->url);
priv->url = g_strdup (url);
}
/**
* arv_dom_document_get_href_data:
* @self: a #ArvDomDocument
* @href: document reference
* @size: data size placeholder
*
* Load the content referenced by @href.
*
* Returns: (transfer full): a newly allocated data buffer.
*/
void *
arv_dom_document_get_href_data (ArvDomDocument *self, const char *href, gsize *size)
{
ArvDomDocumentPrivate *priv = arv_dom_document_get_instance_private (ARV_DOM_DOCUMENT (self));
GFile *file;
char *data = NULL;
g_return_val_if_fail (ARV_IS_DOM_DOCUMENT (self), NULL);
g_return_val_if_fail (href != NULL, NULL);
if (strncmp (href, "data:", 5) == 0) {
while (*href != '\0' && *href != ',')
href++;
return g_base64_decode (href, size);
}
file = g_file_new_for_uri (href);
if (!g_file_load_contents (file, NULL, &data, size, NULL, NULL) && priv->url != NULL) {
GFile *document_file;
GFile *parent_file;
g_object_unref (file);
document_file = g_file_new_for_uri (priv->url);
parent_file = g_file_get_parent (document_file);
file = g_file_resolve_relative_path (parent_file, href);
g_object_unref (document_file);
g_object_unref (parent_file);
g_file_load_contents (file, NULL, &data, size, NULL, NULL);
}
g_object_unref (file);
return data;
}
static void
arv_dom_document_init (ArvDomDocument *document)
{
}
static void
arv_dom_document_finalize (GObject *self)
{
ArvDomDocumentPrivate *priv = arv_dom_document_get_instance_private (ARV_DOM_DOCUMENT (self));
g_free (priv->url);
G_OBJECT_CLASS (arv_dom_document_parent_class)->finalize (self);
}
/* ArvDomDocument class */
static void
arv_dom_document_class_init (ArvDomDocumentClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
ArvDomNodeClass *node_class = ARV_DOM_NODE_CLASS (klass);
object_class->finalize = arv_dom_document_finalize;
node_class->get_node_name = arv_dom_document_get_node_name;
node_class->get_node_type = arv_dom_document_get_node_type;
klass->create_text_node = arv_dom_document_create_text_node_base;
}