2 * Copyright (C) 2000, Luca Padovani <luca.padovani@cs.unibo.it>.
4 * This file is part of miniDOM, a minimal implementation of the
5 * Document Object Model
7 * miniDOM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * miniDOM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with miniDOM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * For details, send a mail to <luca.padovani@cs.unibo.it>
26 #include <libxml/parser.h>
27 #include <libxml/parserInternals.h>
32 mdom_string_eq(mDOMConstStringRef s1, mDOMConstStringRef s2)
34 g_return_val_if_fail(s1 != NULL, FALSE);
35 g_return_val_if_fail(s2 != NULL, FALSE);
37 return strcmp(C_CONST_STRING(s1), C_CONST_STRING(s2)) ? FALSE : TRUE;
41 mdom_string_dup(mDOMConstStringRef s)
43 g_return_val_if_fail(s != NULL, NULL);
48 mdom_string_free(mDOMStringRef s)
54 mdom_load(const char* file_name, gboolean subst, mDOMEntityResolver entity_resolver)
56 xmlParserCtxtPtr ctxt;
59 g_return_val_if_fail(file_name != NULL, NULL);
61 xmlSubstituteEntitiesDefault(subst);
63 ctxt = xmlCreateFileParserCtxt(file_name);
64 if (ctxt == NULL) return NULL;
65 g_assert(ctxt->sax != NULL);
66 ctxt->sax->getEntity = entity_resolver;
68 xmlParseDocument(ctxt);
71 xmlFreeParserCtxt(ctxt);
77 mdom_unload(mDOMDocRef doc)
79 g_return_if_fail(doc != NULL);
84 mdom_doc_new(mDOMConstStringRef version)
88 g_return_val_if_fail(version != NULL, NULL);
89 doc = xmlNewDoc(version);
90 g_return_val_if_fail(doc != NULL, NULL);
92 doc->extSubset = xmlNewDtd(doc, DOM_CONST_STRING("Entities"), NULL, NULL);
98 mdom_doc_free(mDOMDocRef doc)
100 g_return_if_fail(doc != NULL);
105 mdom_doc_add_entity(mDOMDocRef doc, mDOMConstStringRef name, mDOMConstStringRef content)
107 g_return_val_if_fail(doc != NULL, NULL);
108 g_return_val_if_fail(name != NULL, NULL);
109 g_return_val_if_fail(content != NULL, NULL);
111 return xmlAddDtdEntity(doc, name, XML_INTERNAL_GENERAL_ENTITY, NULL, NULL, content);
113 xmlAddDtdEntity(doc, name, XML_INTERNAL_GENERAL_ENTITY, NULL, NULL, content);
119 mdom_doc_get_entity(mDOMDocRef doc, mDOMConstStringRef name)
121 g_return_val_if_fail(doc != NULL, NULL);
122 g_return_val_if_fail(name != NULL, NULL);
123 return xmlGetDtdEntity(doc, name);
127 mdom_get_predefined_entity(mDOMConstStringRef name)
129 g_return_val_if_fail(name != NULL, NULL);
130 return xmlGetPredefinedEntity(name);
134 mdom_entity_get_content(mDOMEntityRef entity)
136 g_return_val_if_fail(entity != NULL, NULL);
137 return entity->content;
141 mdom_doc_get_root_node(mDOMDocRef doc)
143 g_return_val_if_fail(doc != NULL, NULL);
144 return xmlDocGetRootElement(doc);
148 mdom_node_set_user_data(mDOMNodeRef node, gpointer user_data)
150 g_return_if_fail(node != NULL);
151 #ifndef XML_WITHOUT_CORBA
152 node->_private = user_data;
154 g_assert_not_reached();
159 mdom_node_get_user_data(mDOMNodeRef node)
161 g_return_val_if_fail(node != NULL, NULL);
162 #ifndef XML_WITHOUT_CORBA
163 return node->_private;
165 g_assert_not_reached();
170 mdom_node_is_text(mDOMNodeRef node)
172 g_return_val_if_fail(node != NULL, FALSE);
173 return xmlNodeIsText(node) ? TRUE : FALSE;
177 mdom_node_is_element(mDOMNodeRef node)
179 g_return_val_if_fail(node != NULL, FALSE);
180 return node->type == XML_ELEMENT_NODE;
184 mdom_node_is_blank(mDOMNodeRef node)
186 g_return_val_if_fail(node != NULL, FALSE);
188 return xmlIsBlankNode(node) ? TRUE : FALSE;
195 mdom_node_is_entity_ref(mDOMNodeRef node)
197 g_return_val_if_fail(node != NULL, FALSE);
198 return node->type == XML_ENTITY_REF_NODE;
202 mdom_node_get_type(mDOMNodeRef node)
204 g_return_val_if_fail(node != NULL, 0);
209 mdom_node_get_name(mDOMNodeRef node)
211 g_return_val_if_fail(node != NULL, NULL);
216 mdom_node_get_ns_uri(mDOMNodeRef node)
218 g_return_val_if_fail(node != NULL, NULL);
219 if (node->ns == NULL) return NULL;
220 return node->ns->href;
224 mdom_node_get_attribute(mDOMNodeRef node, mDOMConstStringRef name)
226 g_return_val_if_fail(node != NULL, NULL);
227 g_return_val_if_fail(name != NULL, NULL);
228 return xmlGetProp(node, name);
232 mdom_node_get_attribute_ns(mDOMNodeRef node, mDOMConstStringRef name, mDOMConstStringRef uri)
234 g_return_val_if_fail(node != NULL, NULL);
235 g_return_val_if_fail(uri != NULL, NULL);
236 g_return_val_if_fail(name != NULL, NULL);
237 g_return_val_if_fail(mdom_node_is_element(node), NULL);
238 return xmlGetNsProp(node, name, uri);
242 mdom_node_has_attribute(mDOMNodeRef node, mDOMConstStringRef name)
245 mDOMStringRef s = NULL;
246 #endif /* HAVE_LIBXML2 */
247 g_return_val_if_fail(node != NULL, FALSE);
248 g_return_val_if_fail(name != NULL, FALSE);
250 return (xmlHasProp(node, name) != NULL) ? TRUE : FALSE;
252 s = xmlGetProp(node, name);
258 #endif /* HAVE_LIBXML2 */
262 mdom_node_has_attribute_ns(mDOMNodeRef node, mDOMConstStringRef name, mDOMConstStringRef uri)
264 g_return_val_if_fail(node != NULL, FALSE);
265 g_return_val_if_fail(name != NULL, FALSE);
266 g_return_val_if_fail(uri != NULL, FALSE);
268 return (xmlHasNsProp(node, name, uri) != NULL) ? TRUE : FALSE;
271 #endif /* HAVE_LIBXML2 */
275 mdom_node_get_content(mDOMNodeRef node)
277 g_return_val_if_fail(node != NULL, NULL);
278 return xmlNodeGetContent(node);
282 mdom_node_get_parent(mDOMNodeRef node)
284 g_return_val_if_fail(node != NULL, NULL);
289 mdom_node_get_prev_sibling(mDOMNodeRef node)
291 g_return_val_if_fail(node != NULL, NULL);
296 mdom_node_get_next_sibling(mDOMNodeRef node)
298 g_return_val_if_fail(node != NULL, NULL);
303 mdom_node_get_first_child(mDOMNodeRef node)
305 g_return_val_if_fail(node != NULL, NULL);
307 return node->children;
314 mdom_node_get_first_attribute(mDOMNodeRef node)
316 g_return_val_if_fail(node != NULL, NULL);
317 return node->properties;
321 mdom_node_is_first(mDOMNodeRef node)
323 g_return_val_if_fail(node != NULL, FALSE);
324 return node->prev == NULL;
328 mdom_node_is_last(mDOMNodeRef node)
330 g_return_val_if_fail(node != NULL, FALSE);
331 return node->next == NULL;
335 mdom_attr_get_name(mDOMAttrRef attr)
337 g_return_val_if_fail(attr != NULL, NULL);
342 mdom_attr_get_ns_uri(mDOMAttrRef attr)
344 g_return_val_if_fail(attr != NULL, NULL);
345 if (attr->ns == NULL) return NULL;
346 return attr->ns->href;
350 mdom_attr_get_value(mDOMAttrRef attr)
353 g_return_val_if_fail(attr != NULL, NULL);
354 return xmlNodeListGetString(attr->doc, attr->children, 1);
357 g_return_val_if_fail(attr != NULL, NULL);
360 if (child == NULL) return NULL;
361 else return xmlNodeListGetString(child->doc, attr->val, 1);
366 mdom_attr_get_prev_sibling(mDOMAttrRef attr)
369 g_return_val_if_fail(attr != NULL, NULL);
372 /* in libxml 1.x there is no prev link in attributes */
378 mdom_attr_get_next_sibling(mDOMAttrRef attr)
380 g_return_val_if_fail(attr != NULL, NULL);
385 mdom_attr_get_parent(mDOMAttrRef attr)
387 g_return_val_if_fail(attr != NULL, NULL);