1 /* Lasem
2  *
3  * Copyright © 2007-2008 Emmanuel Pacaud
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 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
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author:
21  * 	Emmanuel Pacaud <emmanuel@gnome.org>
22  */
23 
24 /**
25  * SECTION:lsmdomelement
26  * @short_description: Base class for DOM element nodes
27  */
28 
29 #include <lsmdomelement.h>
30 #include <string.h>
31 
32 static GObjectClass *parent_class = NULL;
33 
34 /* LsmDomNode implementation */
35 
36 static const char *
lsm_dom_element_get_node_value(LsmDomNode * node)37 lsm_dom_element_get_node_value (LsmDomNode *node)
38 {
39 	return NULL;
40 }
41 
42 static LsmDomNodeType
lsm_dom_element_get_node_type(LsmDomNode * node)43 lsm_dom_element_get_node_type (LsmDomNode *node)
44 {
45 	return LSM_DOM_NODE_TYPE_ELEMENT_NODE;
46 }
47 
48 static void
lsm_dom_element_write_to_stream(LsmDomNode * self,GOutputStream * stream,GError ** error)49 lsm_dom_element_write_to_stream (LsmDomNode *self, GOutputStream *stream, GError **error)
50 {
51 	LsmDomElementClass *element_class;
52 	char *string;
53 	char *attributes = NULL;
54 
55 	element_class = LSM_DOM_ELEMENT_GET_CLASS (self);
56 	if (element_class->get_serialized_attributes != NULL)
57 		attributes = element_class->get_serialized_attributes (LSM_DOM_ELEMENT (self));
58 
59 	if (attributes != NULL)
60 		string = g_strdup_printf ("<%s %s>", lsm_dom_node_get_node_name (self), attributes);
61 	else
62 		string = g_strdup_printf ("<%s>", lsm_dom_node_get_node_name (self));
63 
64 	g_output_stream_write (stream, string, strlen (string), NULL, error);
65 	g_free (string);
66 	g_free (attributes);
67 
68 	LSM_DOM_NODE_CLASS (parent_class)->write_to_stream (self, stream, error);
69 
70 	string = g_strdup_printf ("</\%s>\n", lsm_dom_node_get_node_name (self));
71 	g_output_stream_write (stream, string, strlen (string), NULL, error);
72 	g_free (string);
73 }
74 
75 /* LsmDomElement implementation */
76 
77 /**
78  * lsm_dom_element_get_attribute:
79  * @self: a #LsmDomElement
80  * @name: attribute name
81  *
82  * Returns: (transfer none): attribute value, as string.
83  */
84 
85 const char *
lsm_dom_element_get_attribute(LsmDomElement * self,const char * name)86 lsm_dom_element_get_attribute (LsmDomElement* self, const char* name)
87 {
88 	g_return_val_if_fail (LSM_IS_DOM_ELEMENT (self), NULL);
89 	g_return_val_if_fail (name != NULL, NULL);
90 
91 	return LSM_DOM_ELEMENT_GET_CLASS (self)->get_attribute (self, name);
92 }
93 
94 /**
95  * lsm_dom_element_set_attribute:
96  * @self: a #LsmDomElement
97  * @name: attribute name
98  * @attribute_value: attribute value as string
99  *
100  * Set the atribute value.
101  */
102 
103 void
lsm_dom_element_set_attribute(LsmDomElement * self,const char * name,const char * attribute_value)104 lsm_dom_element_set_attribute (LsmDomElement* self, const char* name, const char* attribute_value)
105 {
106 	g_return_if_fail (LSM_IS_DOM_ELEMENT (self));
107 	g_return_if_fail (name != NULL);
108 
109 	LSM_DOM_ELEMENT_GET_CLASS (self)->set_attribute (self, name, attribute_value);
110 
111 	lsm_dom_node_changed (LSM_DOM_NODE (self));
112 }
113 
114 /**
115  * lsm_dom_element_get_tag_name:
116  * @self: a #LsmDomElement
117  *
118  * Returns: (transfer none): element tag name.
119  */
120 
121 const char *
lsm_dom_element_get_tag_name(LsmDomElement * self)122 lsm_dom_element_get_tag_name (LsmDomElement *self)
123 {
124 	g_return_val_if_fail (LSM_IS_DOM_ELEMENT (self), NULL);
125 
126 	return lsm_dom_node_get_node_name (LSM_DOM_NODE (self));
127 }
128 
129 static void
lsm_dom_element_init(LsmDomElement * element)130 lsm_dom_element_init (LsmDomElement *element)
131 {
132 }
133 
134 /* LsmDomElement class */
135 
136 static void
lsm_dom_element_class_init(LsmDomElementClass * klass)137 lsm_dom_element_class_init (LsmDomElementClass *klass)
138 {
139 	LsmDomNodeClass *node_class = LSM_DOM_NODE_CLASS (klass);
140 
141 	parent_class = g_type_class_peek_parent (klass);
142 
143 	node_class->get_node_value = lsm_dom_element_get_node_value;
144 	node_class->get_node_type = lsm_dom_element_get_node_type;
145 	node_class->write_to_stream = lsm_dom_element_write_to_stream;
146 }
147 
148 G_DEFINE_ABSTRACT_TYPE (LsmDomElement, lsm_dom_element, LSM_TYPE_DOM_NODE)
149