netcdf-c/libncxml/ncxml_xml2.c
Dennis Heimbigner 53464e8963 Allow optional use of libxml2
re: https://github.com/Unidata/netcdf-c/issues/2119

H/T to [Egbert Eich](https://github.com/e4t) and [Bas Couwenberg](https://github.com/sebastic) for this PR.

It is undesirable to make netcdf be dependent on the availability
of libxml2, but it is desirable to allow its use if available.

In order to do this, a wrapper API (include/ncxml.h) was constructed
that supports either ezxml or libxml2 as the implementation.
Additionally, the xml support code was moved to a new directory
netcdf-c/libncxml.

Primary changes:
* Create a new sub-directory named netcdf-c/libncxml to hold all the xml implementation code.
* Move ezxml.c and ezxml.h to libncxml
* Create a wrapper API -- include/ncxml.h
* Create an implementation, ncxml_ezxml.c to support use of ezxml.
* Create an implementation, ncxml_xml2.c to support use of libxml2.
* Add a check for libxml2 in configure.ac and CMakeLists.txt
* Modify libdap to use the wrapper API instead of ezxml directly.

Misc. Other Changes:
* Change include/netcdf_json.h from built source to be part of the distribution.
2021-11-01 22:37:05 -06:00

162 lines
3.3 KiB
C

/* Copyright 2018-2018 University Corporation for Atmospheric Research/Unidata. */
#include <stdlib.h>
#include <string.h>
#include <libxml2/libxml/parser.h>
#include <libxml/tree.h>
#include "ncxml.h"
#ifndef nulldup
#define nulldup(s) ((s)?strdup(s):NULL)
#endif
static int ncxml_initialized = 0;
void
ncxml_initialize(void)
{
ncxml_initialized = 1;
}
void
ncxml_finalize(void)
{
ncxml_initialized = 0;
xmlCleanupParser();
}
ncxml_doc_t
ncxml_parse(char* contents, size_t len)
{
xmlDocPtr doc; /* the resulting document tree */
doc = xmlReadMemory(contents, (int)len, "dap4.xml", NULL, 0);
return (ncxml_doc_t)doc;
}
void
ncxml_free(ncxml_doc_t doc0)
{
xmlDoc *doc = (xmlDoc*)doc0;
xmlFreeDoc(doc);
}
ncxml_t
ncxml_root(ncxml_doc_t doc0)
{
xmlDoc *doc = (xmlDoc*)doc0;
return (ncxml_t)xmlDocGetRootElement(doc);
}
const char*
ncxml_name(ncxml_t xml0)
{
xmlNode* xml = (xmlNode*)xml0;
return (xml?xml->name:NULL);
}
char*
ncxml_attr(ncxml_t xml0, const char* key)
{
xmlNode* xml = (xmlNode*)xml0;
xmlChar* value = NULL;
char* s = NULL;
value = xmlGetProp(xml,key);
s = nulldup((char*)value);
xmlFree(value);
return s;
}
/* First child by name */
ncxml_t
ncxml_child(ncxml_t xml0, const char* name)
{
xmlNode* xml = (xmlNode*)xml0;
xmlNode* child = NULL;
for(child=xml->children;child; child = child->next) {
if(child->type == XML_ELEMENT_NODE && strcmp(child->name,name)==0)
return (ncxml_t)child;
}
return NULL;
}
ncxml_t
ncxml_next(ncxml_t xml0, const char* name)
{
xmlNode* xml = (xmlNode*)xml0;
xmlNode* next = NULL;
for(next=xml->next;next; next = next->next) {
if(next->type == XML_ELEMENT_NODE && strcmp(next->name,name)==0)
return (ncxml_t)next;
}
return NULL;
}
char*
ncxml_text(ncxml_t xml0)
{
xmlNode* xml = (xmlNode*)xml0;
xmlChar* txt = NULL;
char* s = NULL;
if(xml == NULL) return NULL;
txt = xmlNodeGetContent(xml);
s = nulldup((char*)txt);
xmlFree(txt);
return s;
}
/* Nameless versions of child and next */
ncxml_t
ncxml_child_first(ncxml_t xml0)
{
xmlNode* xml = (xmlNode*)xml0;
xmlNode* child = NULL;
if(xml == NULL) return NULL;
for(child=xml->children;child; child = child->next) {
if(child->type == XML_ELEMENT_NODE) return child;
}
return NULL;
}
ncxml_t
ncxml_child_next(ncxml_t xml0)
{
xmlNode* xml = (xmlNode*)xml0;
if(xml == NULL) return NULL;
for(xml=xml->next;xml; xml = xml->next) {
if(xml->type == XML_ELEMENT_NODE) return xml;
}
return NULL;
}
int
ncxml_attr_pairs(ncxml_t xml0, char*** pairsp)
{
char** pairs = NULL;
xmlNode* xml = (xmlNode*)xml0;
xmlAttr* attr = NULL;
int i,count = 0;
if(xml == NULL) return 0;
/* First count */
for(attr=xml->properties;attr;attr=attr->next) count++;
/* Allocate */
pairs = (char**)malloc(sizeof(char*)*((2*count)+1));
if(pairs == NULL) return 0;
/* Collect */
for(i=0,attr=xml->properties;attr;i+=2,attr=attr->next) {
xmlChar* value;
pairs[i] = nulldup((char*)attr->name);
value = xmlNodeListGetString(xml->doc, attr->children, 1);
pairs[i+1] = nulldup((char*)value);
xmlFree(value);
}
pairs[2*count] = NULL;
if(pairsp) *pairsp = pairs;
return 1;
}