File: userrend.c

package info (click to toggle)
entity 0.7.2-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,352 kB
  • ctags: 5,272
  • sloc: ansic: 61,707; sh: 7,921; makefile: 732; perl: 399
file content (311 lines) | stat: -rw-r--r-- 8,775 bytes parent folder | download
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <glib.h>
#include <string.h>
#include <stdlib.h>

#include "entity.h"


GHashTable *userrend_tag_nodes_ht;

static void
userrend_passthru_parenter (ENode * parent_node, ENode * child_node)
{
    ENode *renderer_node;
    char *onparent;

    renderer_node = g_hash_table_lookup (userrend_tag_nodes_ht,
					 parent_node->element->str);
    if (!renderer_node)
	return;

    onparent = enode_attrib_str (renderer_node, "onparent", NULL);

    /* If they don't implement the parenting, we have to mimic the behavior
     * of the real element parenter */
    if (!onparent) {
	erend_short_curcuit_parent (parent_node, child_node);
	return;
    }

    enode_call_ignore_return (renderer_node, onparent, "nn", parent_node,
			      child_node);
}

static void
userrend_passthru_renderer (ENode * node)
{
    ENode *renderer_node;
    char *onrender;

    renderer_node =
	g_hash_table_lookup (userrend_tag_nodes_ht, node->element->str);
    if (!renderer_node)
	return;

    onrender = enode_attrib_str (renderer_node, "onrender", NULL);
    if (!onrender)
	return;

    EDEBUG (("renderers", "node = %s", node->element->str));

    enode_call_ignore_return (renderer_node, onrender, "n", node);
    /* enode_attribs_sync (node); */
}

static void
userrend_passthru_destroy (ENode * node)
{
    ENode *renderer_node;
    char *ondestroy;

    renderer_node =
	g_hash_table_lookup (userrend_tag_nodes_ht, node->element->str);
    if (!renderer_node)
	return;

    ondestroy = enode_attrib_str (renderer_node, "ondestroy", NULL);
    if (!ondestroy)
	return;

    enode_call_ignore_return (renderer_node, ondestroy, "n", node);
}

static gint
userrend_passthru_attr_set (ENode * node, EBuf * attr, EBuf * value)
{
    ENode *renderer_node;
    ENode *attrib_node;
    GHashTable *attrib_ht;

    char *onset;

    EDEBUG (("renderers", "userrend_passthru_attr_set"));

    renderer_node = g_hash_table_lookup (userrend_tag_nodes_ht,
					 node->element->str);
    if (!renderer_node)
	return FALSE;
    attrib_ht = enode_get_kv (renderer_node, "userrend-renderer-attrib-ht");

    attrib_node = g_hash_table_lookup (attrib_ht, attr->str);
    if (!attrib_node)
	return FALSE;

    onset = enode_attrib_str (attrib_node, "onset", NULL);
    if (!onset)
	return FALSE;

    EDEBUG (("renderers", "node = %s", node->element->str));

    /* Call the onset function. */
    enode_call_ignore_return (renderer_node, onset, "nee", node, attr, value);
    return TRUE;
}

/* TODO: Think about ref'ing these nodes.. */

static void
userrend_renderer_parenter (ENode * parent_node, ENode * child_node)
{
    Element *element;
    ElementAttr *e_attr;

    char *tag;

    char *name;
    char *description;
    char *value_desc;
    char *values;
    ENode *renderer_node;
    GHashTable *attrib_ht;


    EDEBUG (("userrend", "userrend_renderer_parenter"));

    tag = enode_attrib_str (parent_node, "tag", NULL);
    if (!tag)
	return;

    renderer_node = g_hash_table_lookup (userrend_tag_nodes_ht, tag);
    if (!renderer_node)
	return;

    attrib_ht = enode_get_kv (renderer_node, "userrend-renderer-attrib-ht");
    if (!attrib_ht)
	return;

    element = enode_get_kv (parent_node, "userrend-renderer-element");
    if (!element)
	return;

    if (!ebuf_equal_str (child_node->element, "attrib"))
	return;

    name = enode_attrib_str (child_node, "name", NULL);
    if (!name)
	return;

    description = enode_attrib_str (child_node, "description", NULL);
    if (!description)
	g_warning
	    ("Element <%s tag=%s>'s attrib <%s> doesn't have a description.",
	     parent_node->element->str, tag, child_node->element->str);

    value_desc = enode_attrib_str (child_node, "value_desc", NULL);
    values = enode_attrib_str (child_node, "values", NULL);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = name;
    e_attr->description = description;
    e_attr->value_desc = value_desc;
    e_attr->possible_values = values;
    e_attr->set_attr_func = userrend_passthru_attr_set;
    element_register_attrib (element, e_attr);

    g_hash_table_insert (attrib_ht, name, child_node);
}

static void			/* Silly name but thats what this function
				 * is. */
userrend_renderer_renderer (ENode * node)
{
    Element *element;
    GHashTable *attrib_ht;

    char *tag;

    EDEBUG (("userrend", "in userrend_renderer_renderer"));

    tag = enode_attrib_str (node, "tag", NULL);
    if (!tag)
	return;

    EDEBUG (("userrend", "rendering (%s)", tag));

    enode_set_kv (node, "userrend_renderer_renderer-do", "true");

    element = g_new0 (Element, 1);
    element->render_func = userrend_passthru_renderer;
    element->destroy_func = userrend_passthru_destroy;
    element->parent_func = userrend_passthru_parenter;
    element->tag = tag;
    element_register (element);

    enode_set_kv (node, "userrend-renderer-element", element);

    attrib_ht = g_hash_table_new (g_str_hash, g_str_equal);
    enode_set_kv (node, "userrend-renderer-attrib-ht", attrib_ht);

    g_hash_table_insert (userrend_tag_nodes_ht, tag, node);

    /* xml_node_set_all_attr (node); */
}

static void
userrend_attrib_renderer (ENode * node)
{
    enode_set_kv (node, "userrend-attrib-renderer-do", "true");

    /* xml_node_set_all_attr (node); */
}


void
userrend_renderer_register (void)
{
    Element *element;
    ElementAttr *e_attr;

    userrend_tag_nodes_ht = g_hash_table_new (g_str_hash, g_str_equal);

    element = g_new0 (Element, 1);
    element->render_func = userrend_renderer_renderer;
    /* element->destroy_func = userrend_renderer_destroy; */
    element->parent_func = userrend_renderer_parenter;
    element->tag = "renderer";
    element->description = "Define a new element in any supported langauge.";
    element_register (element);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "tag";
    e_attr->description = "The name of the tag for the new renderer.";
    e_attr->value_desc = "string";
    e_attr->set_attr_func = NULL;
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "lang";
    e_attr->description = "The default language of the renderer.";
    e_attr->value_desc = "string";
    e_attr->possible_values = "language";
    e_attr->set_attr_func = NULL;
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "onrender";
    e_attr->description = "The function or method used to render the tag.";
    e_attr->value_desc = "function";
    e_attr->set_attr_func = NULL;
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "ondestroy";
    e_attr->description = "The function or method used to remove the tag.";
    e_attr->value_desc = "function";
    e_attr->set_attr_func = NULL;
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "onparent";
    e_attr->description = "The function or method used to parent child tags.";
    e_attr->value_desc = "function";
    e_attr->set_attr_func = NULL;
    element_register_attrib (element, e_attr);


    /* Attrib tag element. */
    element = g_new0 (Element, 1);
    element->render_func = userrend_attrib_renderer;
    /* element->destroy_func = userrend_attrib_destroy; */
    /* element->parent_func = userrend_attrib_parenter; */
    element->tag = "attrib";
    element->description = "Define an attribute of a new renderer.";
    element_register (element);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "name";
    e_attr->description = "The attribute's name.";
    e_attr->value_desc = "string";
    e_attr->set_attr_func = NULL;
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "description";
    e_attr->description = "The description of the attribute.";
    e_attr->value_desc = "string";
    e_attr->set_attr_func = NULL;
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "value_desc";
    e_attr->description = "The type of value accepted.";
    e_attr->value_desc = "string";
    e_attr->set_attr_func = NULL;
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "values";
    e_attr->description = "The possible values for this attribute.";
    e_attr->value_desc = "string";
    e_attr->set_attr_func = NULL;
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "onset";
    e_attr->description =
	"The function or method to call when this attribute is set.";
    e_attr->value_desc = "string";
    e_attr->set_attr_func = NULL;
    element_register_attrib (element, e_attr);

}