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>
27 #include <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_get_content(mDOMNodeRef node)
264 g_return_val_if_fail(node != NULL, NULL);
265 return xmlNodeGetContent(node);
269 mdom_node_get_parent(mDOMNodeRef node)
271 g_return_val_if_fail(node != NULL, NULL);
276 mdom_node_get_prev_sibling(mDOMNodeRef node)
278 g_return_val_if_fail(node != NULL, NULL);
283 mdom_node_get_next_sibling(mDOMNodeRef node)
285 g_return_val_if_fail(node != NULL, NULL);
290 mdom_node_get_first_child(mDOMNodeRef node)
292 g_return_val_if_fail(node != NULL, NULL);
294 return node->children;
301 mdom_node_get_first_attribute(mDOMNodeRef node)
303 g_return_val_if_fail(node != NULL, NULL);
304 return node->properties;
308 mdom_node_is_first(mDOMNodeRef node)
310 g_return_val_if_fail(node != NULL, FALSE);
311 return node->prev == NULL;
315 mdom_node_is_last(mDOMNodeRef node)
317 g_return_val_if_fail(node != NULL, FALSE);
318 return node->next == NULL;
322 mdom_attr_get_name(mDOMAttrRef attr)
324 g_return_val_if_fail(attr != NULL, NULL);
329 mdom_attr_get_ns_uri(mDOMAttrRef attr)
331 g_return_val_if_fail(attr != NULL, NULL);
332 if (attr->ns == NULL) return NULL;
333 return attr->ns->href;
337 mdom_attr_get_value(mDOMAttrRef attr)
340 g_return_val_if_fail(attr != NULL, NULL);
341 return xmlNodeListGetString(attr->doc, attr->children, 1);
344 g_return_val_if_fail(attr != NULL, NULL);
347 if (child == NULL) return NULL;
348 else return xmlNodeListGetString(child->doc, attr->val, 1);
353 mdom_attr_get_prev_sibling(mDOMAttrRef attr)
356 g_return_val_if_fail(attr != NULL, NULL);
359 /* in libxml 1.x there is no prev link in attributes */
365 mdom_attr_get_next_sibling(mDOMAttrRef attr)
367 g_return_val_if_fail(attr != NULL, NULL);
372 mdom_attr_get_parent(mDOMAttrRef attr)
374 g_return_val_if_fail(attr != NULL, NULL);