1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
#include "ruby_libxml.h"
#include "ruby_xml_dtd.h"
/*
* Document-class: LibXML::XML::Dtd
*
* The XML::Dtd class is used to prepare DTD's for validation of xml
* documents.
*
* DTDs can be created from a string or a pair of public and system identifiers.
* Once a Dtd object is instantiated, an XML document can be validated by the
* XML::Document#validate method providing the XML::Dtd object as parameeter.
* The method will raise an exception if the document is
* not valid.
*
* Basic usage:
*
* # parse DTD
* dtd = XML::Dtd.new(<<EOF)
* <!ELEMENT root (item*) >
* <!ELEMENT item (#PCDATA) >
* EOF
*
* # parse xml document to be validated
* instance = XML::Document.file('instance.xml')
*
* # validate
* instance.validate(dtd)
*/
VALUE cXMLDtd;
void rxml_dtd_free(xmlDtdPtr xdtd)
{
/* Set _private to NULL so that we won't reuse the
same, freed, Ruby wrapper object later.*/
xdtd->_private = NULL;
if (xdtd->doc == NULL && xdtd->parent == NULL)
xmlFreeDtd(xdtd);
}
void rxml_dtd_mark(xmlDtdPtr xdtd)
{
if (xdtd == NULL)
return;
if (xdtd->_private == NULL)
{
return;
}
rxml_node_mark((xmlNodePtr) xdtd);
}
static VALUE rxml_dtd_alloc(VALUE klass)
{
return Data_Wrap_Struct(klass, rxml_dtd_mark, rxml_dtd_free, NULL);
}
VALUE rxml_dtd_wrap(xmlDtdPtr xdtd)
{
VALUE result;
// This node is already wrapped
if (xdtd->_private != NULL)
return (VALUE) xdtd->_private;
result = Data_Wrap_Struct(cXMLDtd, NULL, NULL, xdtd);
xdtd->_private = (void*) result;
return result;
}
/*
* call-seq:
* dtd.external_id -> "string"
*
* Obtain this dtd's external identifer (for a PUBLIC DTD).
*/
static VALUE rxml_dtd_external_id_get(VALUE self)
{
xmlDtdPtr xdtd;
Data_Get_Struct(self, xmlDtd, xdtd);
if (xdtd->ExternalID == NULL)
return (Qnil);
else
return (rxml_new_cstr((const char*) xdtd->ExternalID, NULL));
}
/*
* call-seq:
* dtd.name -> "string"
*
* Obtain this dtd's name.
*/
static VALUE rxml_dtd_name_get(VALUE self)
{
xmlDtdPtr xdtd;
Data_Get_Struct(self, xmlDtd, xdtd);
if (xdtd->name == NULL)
return (Qnil);
else
return (rxml_new_cstr((const char*) xdtd->name, NULL));
}
/*
* call-seq:
* dtd.uri -> "string"
*
* Obtain this dtd's URI (for a SYSTEM or PUBLIC DTD).
*/
static VALUE rxml_dtd_uri_get(VALUE self)
{
xmlDtdPtr xdtd;
Data_Get_Struct(self, xmlDtd, xdtd);
if (xdtd->SystemID == NULL)
return (Qnil);
else
return (rxml_new_cstr((const char*) xdtd->SystemID, NULL));
}
/*
* call-seq:
* node.type -> num
*
* Obtain this node's type identifier.
*/
static VALUE rxml_dtd_type(VALUE self)
{
xmlDtdPtr xdtd;
Data_Get_Struct(self, xmlDtd, xdtd);
return (INT2NUM(xdtd->type));
}
/*
* call-seq:
* XML::Dtd.new("DTD string") -> dtd
* XML::Dtd.new("public", "system") -> dtd
* XML::Dtd.new("name", "public", "system", document) -> external subset dtd
* XML::Dtd.new("name", "public", "system", document, false) -> internal subset dtd
* XML::Dtd.new("name", "public", "system", document, true) -> internal subset dtd
*
* Create a new Dtd from the specified public and system
* identifiers.
*/
static VALUE rxml_dtd_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE external, system, dtd_string;
xmlParserInputBufferPtr buffer;
xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
xmlChar *new_string;
xmlDtdPtr xdtd;
// 1 argument -- string --> parsujeme jako dtd
// 2 arguments -- public, system --> bude se hledat
// 3 arguments -- public, system, name --> creates an external subset (any parameter may be nil)
// 4 arguments -- public, system, name, doc --> creates an external subset (any parameter may be nil)
// 5 arguments -- public, system, name, doc, true --> creates an internal subset (all but last parameter may be nil)
switch (argc)
{
case 3:
case 4:
case 5: {
VALUE name, doc, internal;
const xmlChar *xname = NULL, *xpublic = NULL, *xsystem = NULL;
xmlDocPtr xdoc = NULL;
rb_scan_args(argc, argv, "32", &external, &system, &name, &doc, &internal);
if (external != Qnil) {
Check_Type(external, T_STRING);
xpublic = (const xmlChar*) StringValuePtr(external);
}
if (system != Qnil) {
Check_Type(system, T_STRING);
xsystem = (const xmlChar*) StringValuePtr(system);
}
if (name != Qnil) {
Check_Type(name, T_STRING);
xname = (const xmlChar*) StringValuePtr(name);
}
if (doc != Qnil) {
if (rb_obj_is_kind_of(doc, cXMLDocument) == Qfalse)
rb_raise(rb_eTypeError, "Must pass an XML::Document object");
Data_Get_Struct(doc, xmlDoc, xdoc);
}
if (internal == Qnil || internal == Qfalse)
xdtd = xmlNewDtd(xdoc, xname, xpublic, xsystem);
else
xdtd = xmlCreateIntSubset(xdoc, xname, xpublic, xsystem);
if (xdtd == NULL)
rxml_raise(&xmlLastError);
/* Document will free this dtd now. */
RDATA(self)->dfree = NULL;
DATA_PTR(self) = xdtd;
xmlSetTreeDoc((xmlNodePtr) xdtd, xdoc);
}
break;
case 2:
rb_scan_args(argc, argv, "20", &external, &system);
Check_Type(external, T_STRING);
Check_Type(system, T_STRING);
xdtd = xmlParseDTD((xmlChar*) StringValuePtr(external),
(xmlChar*) StringValuePtr(system));
if (xdtd == NULL)
rxml_raise(&xmlLastError);
DATA_PTR(self) = xdtd;
xmlSetTreeDoc((xmlNodePtr) xdtd, NULL);
break;
case 1:
rb_scan_args(argc, argv, "10", &dtd_string);
Check_Type(dtd_string, T_STRING);
/* Note that buffer is freed by xmlParserInputBufferPush*/
buffer = xmlAllocParserInputBuffer(enc);
new_string = xmlStrdup((xmlChar*) StringValuePtr(dtd_string));
xmlParserInputBufferPush(buffer, xmlStrlen(new_string),
(const char*) new_string);
xdtd = xmlIOParseDTD(NULL, buffer, enc);
if (xdtd == NULL)
rxml_raise(&xmlLastError);
xmlFree(new_string);
DATA_PTR(self) = xdtd;
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments");
}
return self;
}
void rxml_init_dtd()
{
cXMLDtd = rb_define_class_under(mXML, "Dtd", rb_cObject);
rb_define_alloc_func(cXMLDtd, rxml_dtd_alloc);
rb_define_method(cXMLDtd, "initialize", rxml_dtd_initialize, -1);
rb_define_method(cXMLDtd, "external_id", rxml_dtd_external_id_get, 0);
rb_define_method(cXMLDtd, "name", rxml_dtd_name_get, 0);
rb_define_method(cXMLDtd, "uri", rxml_dtd_uri_get, 0);
rb_define_method(cXMLDtd, "node_type", rxml_dtd_type, 0);
rb_define_alias(cXMLDtd, "system_id", "uri");
}
|