1 /* $Id: misc_text_node.c,v 1.0 2005/10/14 15:17:55 fpeters Exp $
2 *
3 * Lasso - A free implementation of the Liberty Alliance specifications.
4 *
5 * Copyright (C) 2004-2007 Entr'ouvert
6 * http://lasso.entrouvert.org
7 *
8 * Authors: See AUTHORS file in top-level directory.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include <libxml/tree.h>
25 #include "../utils.h"
26 #include "private.h"
27 #include "misc_text_node.h"
28
29 /*****************************************************************************/
30 /* private methods */
31 /*****************************************************************************/
32
33 typedef struct {
34 xmlNode *xml_content;
35 GHashTable *any_attributes;
36 } LassoMiscTextNodePrivate;
37
38 #define LASSO_MISC_TEXT_NODE_GET_PRIVATE(o) \
39 (G_TYPE_INSTANCE_GET_PRIVATE ((o), LASSO_TYPE_MISC_TEXT_NODE, LassoMiscTextNodePrivate))
40
41 static struct XmlSnippet schema_snippets[] = {
42 { "content", SNIPPET_TEXT_CHILD,
43 G_STRUCT_OFFSET(LassoMiscTextNode, content), NULL, NULL, NULL},
44 { "any_attributes", SNIPPET_ATTRIBUTE | SNIPPET_ANY | SNIPPET_PRIVATE,
45 G_STRUCT_OFFSET(LassoMiscTextNodePrivate, any_attributes), NULL, NULL, NULL},
46 {NULL, 0, 0, NULL, NULL, NULL}
47 };
48
49 static LassoNodeClass *parent_class = NULL;
50
51 static xmlNode*
get_xmlNode(LassoNode * node,gboolean lasso_dump)52 get_xmlNode(LassoNode *node, gboolean lasso_dump)
53 {
54 xmlNode *xmlnode;
55 xmlNs *ns;
56 LassoMiscTextNode *mtnode = (LassoMiscTextNode*)node;
57 LassoMiscTextNodePrivate *private;
58
59 private = LASSO_MISC_TEXT_NODE_GET_PRIVATE(node);
60 if (private->xml_content) {
61 return xmlCopyNode(private->xml_content, 1);
62 }
63
64 if (LASSO_MISC_TEXT_NODE(node)->text_child) {
65 return xmlNewText((xmlChar*)(LASSO_MISC_TEXT_NODE(node)->content));
66 }
67
68 xmlnode = parent_class->get_xmlNode(node, lasso_dump);
69 xmlNodeSetName(xmlnode, BAD_CAST mtnode->name);
70 if (! lasso_strisempty(mtnode->ns_href) && ! lasso_strisempty(mtnode->ns_href)) {
71 ns = xmlNewNs(xmlnode, BAD_CAST mtnode->ns_href,
72 BAD_CAST mtnode->ns_prefix);
73 xmlSetNs(xmlnode, ns);
74 }
75
76 return xmlnode;
77 }
78
79 static int
init_from_xml(LassoNode * node,xmlNode * xmlnode)80 init_from_xml(LassoNode *node, xmlNode *xmlnode)
81 {
82 LassoMiscTextNode *n = LASSO_MISC_TEXT_NODE(node);
83 int rc = 0;
84
85 if (xmlnode->type == XML_TEXT_NODE) {
86 n->text_child = TRUE;
87 n->content = g_strdup((char*)(xmlnode->content));
88 return 0;
89 } else if (xmlnode->type == XML_ELEMENT_NODE && xmlnode->properties == NULL &&
90 (xmlnode->children == NULL
91 || (xmlnode->children != NULL && xmlnode->children->next == NULL &&
92 xmlnode->children->type == XML_TEXT_NODE)))
93 {
94 rc = parent_class->init_from_xml(node, xmlnode);
95 if (rc) return rc;
96
97 n->ns_href = g_strdup((char*)xmlnode->ns->href);
98 n->ns_prefix = g_strdup((char*)xmlnode->ns->prefix);
99 n->name = g_strdup((char*)xmlnode->name);
100 } else {
101 lasso_misc_text_node_set_xml_content(n, xmlnode);
102 }
103
104 return 0;
105 }
106
107 static void
finalize(GObject * object)108 finalize(GObject *object)
109 {
110 LassoMiscTextNode *t = LASSO_MISC_TEXT_NODE(object);
111 LassoMiscTextNodePrivate *private;
112
113 private = LASSO_MISC_TEXT_NODE_GET_PRIVATE(object);
114 lasso_release_xml_node(private->xml_content);
115 lasso_release_string(t->name);
116 lasso_release_string(t->ns_href);
117 lasso_release_string(t->ns_prefix);
118
119 G_OBJECT_CLASS(parent_class)->finalize(G_OBJECT(t));
120 }
121
122
123 /*****************************************************************************/
124 /* instance and class init functions */
125 /*****************************************************************************/
126
127
128 static void
class_init(LassoMiscTextNodeClass * klass,void * unused G_GNUC_UNUSED)129 class_init(LassoMiscTextNodeClass *klass, void *unused G_GNUC_UNUSED)
130 {
131 LassoNodeClass *nclass = LASSO_NODE_CLASS(klass);
132
133 parent_class = g_type_class_peek_parent(klass);
134 nclass->get_xmlNode = get_xmlNode;
135 nclass->init_from_xml = init_from_xml;
136 nclass->node_data = g_new0(LassoNodeClassData, 1);
137
138 G_OBJECT_CLASS(nclass)->finalize = finalize;
139
140 lasso_node_class_set_nodename(nclass, "XXX");
141 lasso_node_class_add_snippets(nclass, schema_snippets);
142 g_type_class_add_private(klass, sizeof(LassoMiscTextNodePrivate));
143 }
144
145 GType
lasso_misc_text_node_get_type()146 lasso_misc_text_node_get_type()
147 {
148 static GType this_type = 0;
149
150 if (!this_type) {
151 static const GTypeInfo this_info = {
152 sizeof (LassoMiscTextNodeClass),
153 NULL,
154 NULL,
155 (GClassInitFunc) class_init,
156 NULL,
157 NULL,
158 sizeof(LassoMiscTextNode),
159 0,
160 NULL,
161 NULL
162 };
163
164 this_type = g_type_register_static(LASSO_TYPE_NODE,
165 "LassoMiscTextNode", &this_info, 0);
166 }
167 return this_type;
168 }
169
170 /**
171 * lasso_misc_text_node_get_xml_content:
172 * @misc_text_node: a #LassoMiscTextNode
173 *
174 * Return the xml content in this node.
175 *
176 * Return value:(transfer none): an #xmlNode or NULL.
177 */
178 xmlNode*
lasso_misc_text_node_get_xml_content(LassoMiscTextNode * misc_text_node)179 lasso_misc_text_node_get_xml_content(LassoMiscTextNode *misc_text_node)
180 {
181 LassoMiscTextNodePrivate *private_data;
182
183 private_data = LASSO_MISC_TEXT_NODE_GET_PRIVATE(misc_text_node);
184 return private_data->xml_content;
185 }
186
187 /**
188 * lasso_misc_text_node_set_xml_content:
189 * @misc_text_node: a #LassoMiscTextNode
190 *
191 * Set the xml content of this #LassoMiscTextNode
192 */
193 void
lasso_misc_text_node_set_xml_content(LassoMiscTextNode * misc_text_node,xmlNode * node)194 lasso_misc_text_node_set_xml_content(LassoMiscTextNode *misc_text_node, xmlNode *node)
195 {
196 LassoMiscTextNodePrivate *private_data;
197
198 private_data = LASSO_MISC_TEXT_NODE_GET_PRIVATE(misc_text_node);
199 lasso_assign_xml_node(private_data->xml_content, node);
200 }
201
202 /**
203 * lasso_misc_text_node_new:
204 *
205 * Creates a new #LassoMiscTextNode object.
206 *
207 * Return value: a newly created #LassoMiscTextNode object
208 **/
209 LassoNode*
lasso_misc_text_node_new()210 lasso_misc_text_node_new()
211 {
212 return g_object_new(LASSO_TYPE_MISC_TEXT_NODE, NULL);
213 }
214
215
216 /**
217 * lasso_misc_text_node_new_with_string:
218 * @content: the content of newly created #LassoMiscTextNode
219 *
220 * Creates a new #LassoMiscTextNode object and initializes it with @content. Beware that no
221 * nodename, so it would create a text child, not an element.
222 *
223 * Return value: a newly created #LassoMiscTextNode object
224 **/
225 LassoMiscTextNode*
lasso_misc_text_node_new_with_string(const char * content)226 lasso_misc_text_node_new_with_string(const char *content)
227 {
228 LassoMiscTextNode *object;
229 object = g_object_new(LASSO_TYPE_MISC_TEXT_NODE, NULL);
230 object->content = g_strdup(content);
231 return (LassoMiscTextNode*)LASSO_NODE(object);
232 }
233
234 /**
235 * lasso_misc_text_node_new_with_xml_node:
236 * @xml_node: an #xmlNode
237 *
238 * Creates a new #LassoMiscTextNode object and initialize it with @xml_node.
239 *
240 * Return value: a newly created #LassoMiscTextNode object
241 */
242 LassoMiscTextNode*
lasso_misc_text_node_new_with_xml_node(xmlNode * xml_node)243 lasso_misc_text_node_new_with_xml_node(xmlNode *xml_node)
244 {
245 LassoMiscTextNode *object;
246 object = (LassoMiscTextNode*)g_object_new(LASSO_TYPE_MISC_TEXT_NODE, NULL);
247 lasso_misc_text_node_set_xml_content(object, xml_node);
248 return object;
249 }
250