1 /* Copyright (C) 2000, Luca Padovani <luca.padovani@cs.unibo.it>.
3 * This file is part of mlminidom, the Ocaml binding for minidom.
5 * mlminidom is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * mlminidom 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with mlminidom; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * For details, send a mail to the author.
29 #define Val_ptr(p) ((value) (p))
30 #define Val_option(p,f) ((p != NULL) ? ml_some(f(p)) : Val_unit)
31 #define mDOMString_val(v) ((mDOMStringRef) String_val(v))
32 #define mDOMDocRef_val(r) (*((mDOMDocRef *)Data_custom_val(r)))
35 Val_mDOMConstString(mDOMConstStringRef s)
37 return copy_string((char *) s);
41 Val_mDOMString(mDOMStringRef s)
43 value r = copy_string((char *) s);
52 value ret = alloc_small(1,0);
58 ml_string_of_mDOMString(value s)
65 ml_mDOMString_of_string(value s)
72 ml_doc_free(value doc)
74 mdom_doc_free(mDOMDocRef_val(doc));
77 static struct custom_operations ops =
78 {"it.unibo.cs.helm.gtkmathview.mDOMDocRef",
80 custom_compare_default,
82 custom_serialize_default,
83 custom_deserialize_default
87 ml_doc_load(value file_name)
91 CAMLparam1(file_name);
92 CAMLlocal1(val_doc_ref);
94 doc_ref = mdom_load(String_val(file_name), FALSE, NULL);
95 if (doc_ref == NULL) failwith("minidom: could not load document");
96 val_doc_ref = alloc_custom(&ops, sizeof(mDOMDocRef), 1, 1);
98 *((mDOMDocRef *)Data_custom_val(val_doc_ref)) = doc_ref;
100 CAMLreturn(val_doc_ref);
109 CAMLlocal1(val_doc_ref);
111 doc_ref = mdom_doc_new(mDOMString_val(s));
112 if (doc_ref == NULL) failwith("minidom: could not create new document");
113 val_doc_ref = alloc_custom(&ops, sizeof(mDOMDocRef), 1, 1);
115 *((mDOMDocRef *)Data_custom_val(val_doc_ref)) = doc_ref;
117 CAMLreturn(val_doc_ref);
122 ml_doc_get_root_node(value doc)
127 root = mdom_doc_get_root_node(mDOMDocRef_val(doc));
128 if (root == NULL) failwith("minidom: document has no root node!");
130 CAMLreturn((value) root);
134 ml_doc_add_entity(value doc, value name, value content)
138 CAMLparam3(doc, name, content);
139 ent = mdom_doc_add_entity(mDOMDocRef_val(doc), mDOMString_val(name), mDOMString_val(content));
140 if (ent == NULL) failwith("minidom: could not add entity");
142 CAMLreturn((value) ent);
146 ml_doc_get_entity(value doc, value name)
150 CAMLparam2(doc, name);
151 ent = mdom_doc_get_entity(mDOMDocRef_val(doc), mDOMString_val(name));
153 CAMLreturn(Val_option(ent, Val_ptr));
157 ml_doc_get_predefined_entity(value name)
162 ent = mdom_get_predefined_entity(mDOMString_val(name));
164 CAMLreturn(Val_option(ent, Val_ptr));
168 ml_entity_get_content(value ent)
171 CAMLreturn(Val_mDOMConstString(mdom_entity_get_content((mDOMEntityRef) ent)));
175 ml_node_is_text(value node)
178 CAMLreturn(Val_bool(mdom_node_is_text((mDOMNodeRef) node)));
182 ml_node_is_element(value node)
185 CAMLreturn(Val_bool(mdom_node_is_element((mDOMNodeRef) node)));
189 ml_node_is_blank(value node)
192 CAMLreturn(Val_bool(mdom_node_is_blank((mDOMNodeRef) node)));
196 ml_node_is_entity_ref(value node)
199 CAMLreturn(Val_bool(mdom_node_is_entity_ref((mDOMNodeRef) node)));
203 ml_node_get_type(value node)
206 CAMLreturn(Val_int(mdom_node_get_type((mDOMNodeRef) node)));
210 ml_node_get_name(value node)
213 CAMLreturn(Val_option(mdom_node_get_name((mDOMNodeRef) node), Val_mDOMConstString));
217 ml_node_has_attribute(value node, value name)
219 CAMLparam2(node,name);
220 CAMLreturn(Val_bool(mdom_node_has_attribute((mDOMNodeRef) node, String_val(name))));
224 ml_node_has_attribute_ns(value node, value name, value uri)
226 CAMLparam3(node,name,uri);
227 CAMLreturn(Val_bool(mdom_node_has_attribute_ns((mDOMNodeRef) node, String_val(name), String_val(uri))));
231 ml_node_get_content(value node)
234 CAMLreturn(Val_option(mdom_node_get_content((mDOMNodeRef) node), Val_mDOMString));
238 ml_node_get_ns_uri(value node)
241 CAMLreturn(Val_option(mdom_node_get_ns_uri((mDOMNodeRef) node), Val_mDOMConstString));
245 ml_node_get_attribute(value node, value name)
247 CAMLparam2(node,name);
248 CAMLreturn(Val_option(mdom_node_get_attribute((mDOMNodeRef) node, String_val(name)), Val_mDOMString));
252 ml_node_get_attribute_ns(value node, value name, value ns_uri)
254 CAMLparam2(node,name);
255 CAMLreturn(Val_option(mdom_node_get_attribute_ns((mDOMNodeRef) node,
257 String_val(ns_uri)), Val_mDOMString));
261 ml_node_get_parent(value node)
264 CAMLreturn(Val_option(mdom_node_get_parent((mDOMNodeRef) node), Val_ptr));
268 ml_node_get_prev_sibling(value node)
271 CAMLreturn(Val_option(mdom_node_get_prev_sibling((mDOMNodeRef) node), Val_ptr));
275 ml_node_get_next_sibling(value node)
278 CAMLreturn(Val_option(mdom_node_get_next_sibling((mDOMNodeRef) node), Val_ptr));
282 ml_node_get_first_child(value node)
285 CAMLreturn(Val_option(mdom_node_get_first_child((mDOMNodeRef) node), Val_ptr));
289 ml_node_get_first_attribute(value node)
292 CAMLreturn(Val_option(mdom_node_get_first_attribute((mDOMNodeRef) node), Val_ptr));
296 ml_node_is_first(value node)
299 CAMLreturn(Val_bool(mdom_node_is_first((mDOMNodeRef) node)));
303 ml_node_is_last(value node)
306 CAMLreturn(Val_bool(mdom_node_is_last((mDOMNodeRef) node)));
310 ml_attr_get_name(value attr)
313 CAMLreturn(Val_option(mdom_attr_get_name((mDOMAttrRef) attr), Val_mDOMConstString));
317 ml_attr_get_ns_uri(value attr)
320 CAMLreturn(Val_option(mdom_attr_get_ns_uri((mDOMAttrRef) attr), Val_mDOMConstString));
324 ml_attr_get_value(value attr)
327 CAMLreturn(Val_option(mdom_attr_get_value((mDOMAttrRef) attr), Val_mDOMString));
331 ml_attr_get_prev_sibling(value attr)
334 CAMLreturn(Val_option(mdom_attr_get_prev_sibling((mDOMAttrRef) attr), Val_ptr));
338 ml_attr_get_next_sibling(value attr)
341 CAMLreturn(Val_option(mdom_attr_get_next_sibling((mDOMAttrRef) attr), Val_ptr));
345 ml_attr_get_parent(value attr)
348 CAMLreturn(Val_option(mdom_attr_get_parent((mDOMAttrRef) attr), Val_ptr));