~ubuntu-branches/ubuntu/hardy/steam/hardy

« back to all changes in this revision

Viewing changes to sources/libxslt/xml_dom.c

  • Committer: Bazaar Package Importer
  • Author(s): Alain Schroeder
  • Date: 2006-11-21 16:03:12 UTC
  • mfrom: (2.1.4 feisty)
  • Revision ID: james.westby@ubuntu.com-20061121160312-nf96y6nihzsyd2uv
Tags: 2.2.31-3
Add patch to prevent inconsistent data after shutdown.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <string.h>
 
2
#include <stdarg.h>
 
3
#include <libxml/xmlmemory.h>
 
4
#include <libxml/debugXML.h>
 
5
#include <libxml/HTMLtree.h>
 
6
#include <libxml/xmlIO.h>
 
7
#include <libxml/DOCBparser.h>
 
8
#include <libxml/xinclude.h>
 
9
#include <libxml/catalog.h>
 
10
#include <libxml/xmlversion.h>
 
11
#include <libxslt/xslt.h>
 
12
#include <libxslt/xsltInternals.h>
 
13
#include <libxslt/transform.h>
 
14
#include <libxslt/xsltutils.h>
 
15
#include <libxslt/xsltconfig.h>
 
16
 
 
17
#include "global.h"
 
18
#include "interpret.h"
 
19
#include "stralloc.h"
 
20
#include "pike_macros.h"
 
21
#include "module_support.h"
 
22
#include "mapping.h"
 
23
#include "threads.h"
 
24
 
 
25
#include "xslt.h"
 
26
 
 
27
 
 
28
#define safe_push_text(_txt_) if ((_txt_)) push_text((_txt_)); else push_int(0)
 
29
 
 
30
static void init_dom(struct object * o);
 
31
static void exit_dom(struct object * o);
 
32
 
 
33
static void init_node(struct object * o);
 
34
static void exit_node(struct object * o);
 
35
 
 
36
 
 
37
 
 
38
void f_create_dom(INT32 args)
 
39
{
 
40
    xmlDocPtr doc;
 
41
    xmlNodePtr node;
 
42
    struct pike_string *rootStr = NULL;
 
43
 
 
44
    if ( args != 1 )
 
45
        Pike_error("Wrong number of arguments for creation !");
 
46
    else if ( ARG(1).type != T_STRING )
 
47
        Pike_error("Incorrect type for argument 1: expected a string !");
 
48
    rootStr = ARG(1).u.string;
 
49
 
 
50
    doc = xmlNewDoc("1.0");
 
51
    THISDOM->domDoc = doc;
 
52
 
 
53
    if(doc == NULL)
 
54
    {
 
55
        Pike_error("Unable to create new XML document.\n");
 
56
    }  
 
57
    node = xmlNewNode(NULL, (xmlChar *)rootStr->str);
 
58
    xmlDocSetRootElement(doc, node);
 
59
    THISDOM->rootNode = node;
 
60
    if(node == NULL)
 
61
    {
 
62
        xmlFreeDoc(doc);
 
63
        Pike_error("Unable to find Root Node.\n");
 
64
    }
 
65
    pop_n_elems(args);
 
66
}
 
67
 
 
68
void f_get_root(INT32 args)
 
69
{
 
70
    struct object *o;
 
71
    xmlNodePtr node = THISDOM->rootNode;
 
72
    push_text(node->name);
 
73
    o = NEW_NODE();
 
74
    OBJ2_NODE(o)->node = node;
 
75
    push_object(o);
 
76
}
 
77
 
 
78
void f_create_node(INT32 args)
 
79
{
 
80
    struct mapping *attributes = NULL;
 
81
    struct pike_string *name = NULL;
 
82
    xmlNodePtr                 node;
 
83
    struct keypair               *k;
 
84
 
 
85
    switch(args) {
 
86
    case 2:
 
87
        if ( ARG(2).type != T_MAPPING )
 
88
            Pike_error("second argument is attribute mapping of node !");
 
89
        attributes = ARG(2).u.mapping;
 
90
    case 1:
 
91
        break;
 
92
    default:
 
93
        Pike_error("invalid number of arguments to create node !");
 
94
    }
 
95
    if ( ARG(1).type != T_STRING )
 
96
        Pike_error("first argument needs to be name of the node !");
 
97
    name = ARG(1).u.string;
 
98
 
 
99
    
 
100
    node = xmlNewNode(NULL, (xmlChar *)name->str);
 
101
    THISNODE->node = node;
 
102
 
 
103
    if ( attributes != NULL ) {
 
104
        struct svalue sind, sval;
 
105
        int count;
 
106
        
 
107
        MY_MAPPING_LOOP(attributes, count, k) 
 
108
        {
 
109
            sind = k->ind;
 
110
            sval = k->val;
 
111
            if(!(sind.type == T_STRING && sval.type == T_STRING)) {
 
112
                continue;
 
113
            }
 
114
            xmlNewProp(node, sind.u.string->str, sval.u.string->str);
 
115
        }
 
116
    }
 
117
 
 
118
 
 
119
    pop_n_elems(args);
 
120
}
 
121
 
 
122
void f_add_prop(INT32 args)
 
123
{
 
124
    xmlNodePtr node, current;
 
125
    
 
126
    if ( args != 2 ) 
 
127
      Pike_error("add_prop: invalid number of arguments : expected key/value");
 
128
    if ( ARG(1).type != T_STRING || ARG(2).type != T_STRING )
 
129
        Pike_error("Incorrect type for arguments: expected string, string !");
 
130
    current = THISNODE->node;
 
131
    
 
132
    xmlNewProp(current, ARG(1).u.string->str, ARG(2).u.string->str);
 
133
    pop_n_elems(args);
 
134
    push_int(1);
 
135
}
 
136
 
 
137
 
 
138
void f_add_data(INT32 args)
 
139
{
 
140
    xmlNodePtr node;
 
141
    if ( args != 1 )
 
142
        Pike_error("invalid number of arguments to add_data: expected string");
 
143
    if ( ARG(1).type != T_STRING )
 
144
        Pike_error("Incorrect type for argument 1: expected string");
 
145
    node = xmlNewText(ARG(1).u.string->str);
 
146
    xmlAddChild(THISNODE->node, node);
 
147
    pop_n_elems(args);
 
148
    push_int(1);
 
149
}
 
150
 
 
151
void f_add_child(INT32 args)
 
152
{
 
153
    xmlNodePtr current;
 
154
    
 
155
    if ( args != 1 ) 
 
156
      Pike_error("invalid number of arguments for add_child: expected object");
 
157
    if ( ARG(1).type != T_OBJECT )
 
158
        Pike_error("Incorrect type for argument 1: expected an object !");
 
159
    struct object* node = ARG(1).u.object;
 
160
    current = THISNODE->node;
 
161
    
 
162
    xmlAddChild(current, OBJ2_NODE(node)->node);
 
163
    pop_n_elems(args);
 
164
    push_int(1);
 
165
}
 
166
 
 
167
void f_render_xml(INT32 args)
 
168
{
 
169
    int dumped;
 
170
    xmlBufferPtr buf;
 
171
    char * str;
 
172
 
 
173
    buf = xmlBufferCreate();
 
174
    dumped = xmlNodeDump(buf, THISDOM->domDoc, THISDOM->rootNode, 1, 1);
 
175
    pop_n_elems(args);
 
176
 
 
177
    if(dumped>0)
 
178
    {
 
179
        str = (char *)xmlStrdup(buf->content);
 
180
        push_text(str);
 
181
        xmlBufferFree(buf);
 
182
    }
 
183
    else
 
184
        push_text("");
 
185
}
 
186
 
 
187
int _init_xml_dom(void)
 
188
{
 
189
    start_new_program();
 
190
    ADD_STORAGE(dom_storage);
 
191
 
 
192
    set_init_callback(init_dom);
 
193
    set_exit_callback(exit_dom);
 
194
    
 
195
    ADD_FUNCTION("create", f_create_dom, tFunc(tString, tVoid), 0);
 
196
    ADD_FUNCTION("render_xml", f_render_xml, tFunc(tVoid, tString), 0);
 
197
    ADD_FUNCTION("get_root", f_get_root, tFunc(tVoid, tObj), 0);
 
198
    dom_program = end_program();
 
199
    
 
200
    add_program_constant("DOM", dom_program, 0);
 
201
 
 
202
    start_new_program();
 
203
    ADD_STORAGE(node_storage);
 
204
 
 
205
    set_init_callback(init_node);
 
206
    set_exit_callback(exit_node);
 
207
    ADD_FUNCTION("create", f_create_node, tFunc(tString tOr(tMapping, tVoid), tVoid), 0);
 
208
    ADD_FUNCTION("add_data", f_add_data, tFunc(tString, tInt), 0);
 
209
    ADD_FUNCTION("add_prop", f_add_prop, tFunc(tString tString, tInt), 0);
 
210
    ADD_FUNCTION("add_child", f_add_child, tFunc(tObj, tInt), 0);
 
211
    node_program = end_program();
 
212
    add_program_constant("Node", node_program, 0);
 
213
}
 
214
 
 
215
int _shutdown_xml_dom(void)
 
216
{
 
217
  return 1;
 
218
}
 
219
 
 
220
static void init_dom(struct object *o)
 
221
{
 
222
  if (!THIS)
 
223
    return;
 
224
 
 
225
  THISDOM->domDoc = NULL;
 
226
  THISDOM->rootNode = NULL;
 
227
}
 
228
 
 
229
static void init_node(struct object *o)
 
230
{
 
231
  if (!THISNODE)
 
232
    return;
 
233
 
 
234
  THISNODE->node = NULL;
 
235
}
 
236
 
 
237
static void exit_dom(struct object *o)
 
238
{
 
239
  if (!THIS)
 
240
    return;
 
241
 
 
242
}
 
243
 
 
244
static void exit_node(struct object *o)
 
245
{
 
246
  if (!THIS)
 
247
    return;
 
248
 
 
249
}