JWS C Library
C language utility library
|
00001 /* 00002 * This work is licensed under a Creative Commons 00003 * Attribution-Noncommercial-Share Alike 3.0 United States License. 00004 * 00005 * http://creativecommons.org/licenses/by-nc-sa/3.0/us/ 00006 * 00007 * You are free: 00008 * 00009 * to Share - to copy, distribute, display, and perform the work 00010 * to Remix - to make derivative works 00011 * 00012 * Under the following conditions: 00013 * 00014 * Attribution. You must attribute the work in the manner specified by the 00015 * author or licensor (but not in any way that suggests that they endorse you 00016 * or your use of the work). 00017 * 00018 * Noncommercial. You may not use this work for commercial purposes. 00019 * 00020 * Share Alike. If you alter, transform, or build upon this work, you may 00021 * distribute the resulting work only under the same or similar license to 00022 * this one. 00023 * 00024 * For any reuse or distribution, you must make clear to others the license 00025 * terms of this work. The best way to do this is by including this header. 00026 * 00027 * Any of the above conditions can be waived if you get permission from the 00028 * copyright holder. 00029 * 00030 * Apart from the remix rights granted under this license, nothing in this 00031 * license impairs or restricts the author's moral rights. 00032 */ 00033 00034 00048 #include <jwsc/config.h> 00049 00050 #include <stdlib.h> 00051 #include <string.h> 00052 #include <assert.h> 00053 #include <inttypes.h> 00054 00055 #ifdef JWSC_HAVE_LIBXML2 00056 #include <libxml/tree.h> 00057 #include <libxml/parser.h> 00058 #include <libxml/valid.h> 00059 #endif 00060 00061 #include "jwsc/base/error.h" 00062 #include "jwsc/base/xml.h" 00063 00064 #ifdef JWSC_HAVE_DMALLOC 00065 #include <dmalloc.h> 00066 #endif 00067 00068 00070 void create_xml_doc(xmlDoc** doc_out) 00071 { 00072 if (*doc_out) 00073 { 00074 free_xml_doc(*doc_out); 00075 } 00076 *doc_out = xmlNewDoc((xmlChar*)"1.0"); 00077 } 00078 00079 00081 void free_xml_doc(xmlDoc* doc) 00082 { 00083 xmlFreeDoc(doc); 00084 } 00085 00086 00091 Error* read_xml_doc(xmlDoc** doc_out, const char* fname) 00092 { 00093 xmlParserCtxt* ctx; 00094 Error* e = NULL; 00095 00096 create_xml_doc(doc_out); 00097 assert(ctx = xmlNewParserCtxt()); 00098 00099 if (*doc_out) 00100 { 00101 free_xml_doc(*doc_out); 00102 } 00103 00104 if (!(*doc_out = xmlCtxtReadFile(ctx, fname, NULL, 0))) 00105 { 00106 e = JWSC_EARG("Could not parse file"); 00107 } 00108 00109 xmlFreeParserCtxt(ctx); 00110 00111 return e; 00112 } 00113 00114 00119 Error* read_xml_dtd(xmlDtd** dtd_out, const char* fname) 00120 { 00121 Error* e = NULL; 00122 00123 if (!(*dtd_out = xmlParseDTD(NULL, (xmlChar*)fname))) 00124 { 00125 e = JWSC_EARG("Could not parse file"); 00126 } 00127 00128 return e; 00129 } 00130 00131 00133 void free_xml_dtd(xmlDtd* dtd) 00134 { 00135 xmlFreeDtd(dtd); 00136 } 00137 00138 00145 int validate_xml_doc(xmlDoc* doc, xmlDtd* dtd) 00146 { 00147 xmlValidCtxt* ctx; 00148 int valid; 00149 00150 assert(ctx = xmlNewValidCtxt()); 00151 00152 valid = xmlValidateDtd(ctx, doc, dtd); 00153 00154 xmlFreeValidCtxt(ctx); 00155 00156 return valid; 00157 }