/* eurephia_xml.c -- Generic helper functions for XML parsing * * GPLv2 only - Copyright (C) 2008, 2009 * David Sommerseth * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; version 2 * of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * */ #ifdef HAVE_LIBXML2 #include #include #include #include #include char *xmlGetAttrValue(xmlAttr *attr, const char *key) { xmlAttr *aptr; xmlChar *x_key = NULL; x_key = xmlCharStrdup(key); assert( x_key != NULL ); for( aptr = attr; aptr != NULL; aptr = aptr->next ) { if( xmlStrcmp(aptr->name, x_key) == 0 ) { // FIXME: Should find a better way to return UTF-8 data free_nullsafe(x_key); return (char *)(aptr->children != NULL ? aptr->children->content : NULL); } } free_nullsafe(x_key); return NULL; } xmlNode *xmlFindNode(xmlNode *node, const char *key) { xmlNode *nptr = NULL; xmlChar *x_key = NULL; if( node->children == NULL ) { return NULL; } x_key = xmlCharStrdup(key); assert( x_key != NULL ); for( nptr = node->children; nptr != NULL; nptr = nptr->next ) { if( xmlStrcmp(nptr->name, x_key) == 0 ) { free_nullsafe(x_key); return nptr; } } free_nullsafe(x_key); return NULL; } int eurephiaXML_CreateDoc(eurephiaCTX *ctx, int format, const char *eurephiaRoot, xmlDoc **doc, xmlNode **root_n) { char tmp[34]; // Create a new XML document *doc = xmlNewDoc((xmlChar *)"1.0"); assert(*doc != NULL); // Set the XML root to be *root_n = xmlNewNode(NULL, (xmlChar *)"eurephia"); assert(*root_n != NULL); // Add eurephia XML document format version id snprintf(tmp, 33, "%i%c", format, '\0'); xmlNewProp(*root_n, (xmlChar *)"format", (xmlChar *)tmp); xmlDocSetRootElement(*doc, *root_n); // Add the eurephia XML root (always inside the tags) *root_n = xmlNewChild(*root_n, NULL, (xmlChar*)eurephiaRoot, NULL); return 1; } xmlNode *eurephiaXML_getRoot(eurephiaCTX *ctx, xmlDoc *doc, const char *nodeset, int req_format) { xmlNode *root = NULL; char *xmlformat_str = NULL; int xmlformat = 0; root = xmlDocGetRootElement(doc); if( (root == NULL) || (xmlStrcmp(root->name, (xmlChar *)"eurephia") != 0) ) { eurephia_log(ctx, LOG_FATAL, 0, "Could not find eurephia XML root element. " "Not a valid eurephia XML document."); return NULL; } xmlformat_str = xmlGetAttrValue(root->properties, "format"); xmlformat = atoi_nullsafe(xmlformat_str); if( xmlformat < req_format ) { eurephia_log(ctx, LOG_ERROR, 0, "eurephia XML document format is not supported. " "The XML document uses '%s', while we need minimum '%i'", xmlformat_str, req_format); return NULL; } return xmlFindNode(root, nodeset); } inline char *xmlExtractContent(xmlNode *n) { // FIXME: Should find better way how to return UTF-8 data return (char *) (((n != NULL) && (n->children != NULL)) ? n->children->content : NULL); } inline char *xmlGetNodeContent(xmlNode *node, const char *key) { return xmlExtractContent(xmlFindNode(node, key)); } #endif