/
ruby_xml_schema.c
422 lines (353 loc) · 11.1 KB
/
ruby_xml_schema.c
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include "ruby_libxml.h"
#include "ruby_xml_schema.h"
#include "ruby_xml_schema_type.h"
#include "ruby_xml_schema_element.h"
#include "ruby_xml_schema_attribute.h"
#include "ruby_xml_schema_facet.h"
#include <libxml/xmlschemas.h>
typedef struct _xmlSchemaBucket xmlSchemaBucket;
typedef xmlSchemaBucket *xmlSchemaBucketPtr;
/**
* xmlSchemaSchemaRelation:
*
* Used to create a graph of schema relationships.
*/
typedef struct _xmlSchemaSchemaRelation xmlSchemaSchemaRelation;
typedef xmlSchemaSchemaRelation *xmlSchemaSchemaRelationPtr;
struct _xmlSchemaSchemaRelation {
xmlSchemaSchemaRelationPtr next;
int type;
/* E.g. XML_SCHEMA_SCHEMA_IMPORT */
const xmlChar *importNamespace;
xmlSchemaBucketPtr bucket;
};
struct _xmlSchemaBucket {
int type;
int flags;
const xmlChar *schemaLocation;
const xmlChar *origTargetNamespace;
const xmlChar *targetNamespace;
xmlDocPtr doc;
xmlSchemaSchemaRelationPtr relations;
int located;
int parsed;
int imported;
int preserveDoc;
xmlSchemaItemListPtr globals;
/* Global components. */
xmlSchemaItemListPtr locals; /* Local components. */
};
/**
* xmlSchemaImport:
* (extends xmlSchemaBucket)
*
* Reflects a schema. Holds some information
* about the schema and its toplevel components. Duplicate
* toplevel components are not checked at this level.
*/
typedef struct _xmlSchemaImport xmlSchemaImport;
typedef xmlSchemaImport *xmlSchemaImportPtr;
struct _xmlSchemaImport {
int type;
/* Main OR import OR include. */
int flags;
const xmlChar *schemaLocation; /* The URI of the schema document. */
/* For chameleon includes, @origTargetNamespace will be NULL */
const xmlChar *origTargetNamespace;
/*
* For chameleon includes, @targetNamespace will be the
* targetNamespace of the including schema.
*/
const xmlChar *targetNamespace;
xmlDocPtr doc; /* The schema node-tree. */
/* @relations will hold any included/imported/redefined schemas. */
xmlSchemaSchemaRelationPtr relations;
int located;
int parsed;
int imported;
int preserveDoc;
xmlSchemaItemListPtr globals;
xmlSchemaItemListPtr locals;
/* The imported schema. */
xmlSchemaPtr schema;
};
/*
* Document-class: LibXML::XML::Schema
*
* The XML::Schema class is used to prepare XML Schemas for validation of xml
* documents.
*
* Schemas can be created from XML documents, strinings or URIs using the
* corresponding methods (new for URIs).
*
* Once a schema is prepared, an XML document can be validated by the
* XML::Document#validate_schema method providing the XML::Schema object
* as parameter. The method return true if the document validates, false
* otherwise.
*
* Basic usage:
*
* # parse schema as xml document
* schema_document = XML::Document.file('schema.rng')
*
* # prepare schema for validation
* schema = XML::Schema.document(schema_document)
*
* # parse xml document to be validated
* instance = XML::Document.file('instance.xml')
*
* # validate
* instance.validate_schema(schema)
*/
VALUE cXMLSchema;
static void rxml_schema_free(xmlSchemaPtr xschema)
{
xmlSchemaFree(xschema);
}
VALUE rxml_wrap_schema(xmlSchemaPtr xschema)
{
VALUE result;
if (!xschema)
rb_raise(rb_eArgError, "XML::Schema is required!");
result = Data_Wrap_Struct(cXMLSchema, NULL, rxml_schema_free, xschema);
/*
* Create these as instance variables to provide the output of inspect/to_str some
* idea of what schema this class contains.
*/
rb_iv_set(result, "@target_namespace", QNIL_OR_STRING(xschema->targetNamespace));
rb_iv_set(result, "@name", QNIL_OR_STRING(xschema->name));
rb_iv_set(result, "@id", QNIL_OR_STRING(xschema->id));
rb_iv_set(result, "@version", QNIL_OR_STRING(xschema->name));
return result;
}
static VALUE rxml_schema_init(VALUE class, xmlSchemaParserCtxtPtr xparser)
{
xmlSchemaPtr xschema;
xschema = xmlSchemaParse(xparser);
xmlSchemaFreeParserCtxt(xparser);
if (!xschema)
rxml_raise(xmlGetLastError());
return rxml_wrap_schema(xschema);
}
/*
* call-seq:
* XML::Schema.initialize(schema_uri) -> schema
*
* Create a new schema from the specified URI.
*/
static VALUE rxml_schema_init_from_uri(VALUE class, VALUE uri)
{
xmlSchemaParserCtxtPtr xparser;
Check_Type(uri, T_STRING);
xmlResetLastError();
xparser = xmlSchemaNewParserCtxt(StringValuePtr(uri));
if (!xparser)
rxml_raise(xmlGetLastError());
return rxml_schema_init(class, xparser);
}
/*
* call-seq:
* XML::Schema.document(document) -> schema
*
* Create a new schema from the specified document.
*/
static VALUE rxml_schema_init_from_document(VALUE class, VALUE document)
{
xmlDocPtr xdoc;
xmlSchemaParserCtxtPtr xparser;
Data_Get_Struct(document, xmlDoc, xdoc);
xmlResetLastError();
xparser = xmlSchemaNewDocParserCtxt(xdoc);
if (!xparser)
rxml_raise(xmlGetLastError());
return rxml_schema_init(class, xparser);
}
/*
* call-seq:
* XML::Schema.from_string("schema_data") -> "value"
*
* Create a new schema using the specified string.
*/
static VALUE rxml_schema_init_from_string(VALUE class, VALUE schema_str)
{
xmlSchemaParserCtxtPtr xparser;
Check_Type(schema_str, T_STRING);
xmlResetLastError();
xparser = xmlSchemaNewMemParserCtxt(StringValuePtr(schema_str), (int)strlen(StringValuePtr(schema_str)));
if (!xparser)
rxml_raise(xmlGetLastError());
return rxml_schema_init(class, xparser);
}
/*
* call-seq:
* XML::Schema.document -> document
*
* Return the Schema XML Document
*/
static VALUE rxml_schema_document(VALUE self)
{
xmlSchemaPtr xschema;
Data_Get_Struct(self, xmlSchema, xschema);
return rxml_node_wrap(xmlDocGetRootElement(xschema->doc));
}
static void scan_namespaces(xmlSchemaImportPtr ximport, VALUE array, const xmlChar *nsname)
{
xmlNodePtr xnode;
xmlNsPtr xns;
if (ximport->doc)
{
xnode = xmlDocGetRootElement(ximport->doc);
xns = xnode->nsDef;
while (xns)
{
VALUE namespace = rxml_namespace_wrap(xns);
rb_ary_push(array, namespace);
xns = xns->next;
}
}
}
/*
* call-seq:
* XML::Schema.namespaces -> array
*
* Returns an array of Namespaces defined by the schema
*/
static VALUE rxml_schema_namespaces(VALUE self)
{
VALUE result;
xmlSchemaPtr xschema;
Data_Get_Struct(self, xmlSchema, xschema);
result = rb_ary_new();
xmlHashScan(xschema->schemasImports, (xmlHashScanner)scan_namespaces, (void *)result);
return result;
}
static void scan_schema_element(xmlSchemaElementPtr xelement, VALUE hash, const xmlChar *name)
{
VALUE element = rxml_wrap_schema_element(xelement);
rb_hash_aset(hash, rb_str_new2((const char*)name), element);
}
static VALUE rxml_schema_elements(VALUE self)
{
VALUE result = rb_hash_new();
xmlSchemaPtr xschema;
Data_Get_Struct(self, xmlSchema, xschema);
xmlHashScan(xschema->elemDecl, (xmlHashScanner)scan_schema_element, (void *)result);
return result;
}
static void collect_imported_ns_elements(xmlSchemaImportPtr import, VALUE result, const xmlChar *name)
{
if (import->imported && import->schema)
{
VALUE elements = rb_hash_new();
xmlHashScan(import->schema->elemDecl, (xmlHashScanner)scan_schema_element, (void *)elements);
rb_hash_aset(result, QNIL_OR_STRING(import->schema->targetNamespace), elements);
}
}
/*
* call-seq:
* XML::Schema.imported_ns_elements -> hash
*
* Returns a hash by namespace of a hash of schema elements within the entire schema including imports
*/
static VALUE rxml_schema_imported_ns_elements(VALUE self)
{
xmlSchemaPtr xschema;
VALUE result = rb_hash_new();
Data_Get_Struct(self, xmlSchema, xschema);
if (xschema)
{
xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_ns_elements, (void *)result);
}
return result;
}
static void scan_schema_type(xmlSchemaTypePtr xtype, VALUE hash, const xmlChar *name)
{
VALUE type = rxml_wrap_schema_type(xtype);
rb_hash_aset(hash, rb_str_new2((const char*)name), type);
}
static VALUE rxml_schema_types(VALUE self)
{
VALUE result = rb_hash_new();
xmlSchemaPtr xschema;
Data_Get_Struct(self, xmlSchema, xschema);
if (xschema != NULL && xschema->typeDecl != NULL)
{
xmlHashScan(xschema->typeDecl, (xmlHashScanner)scan_schema_type, (void *)result);
}
return result;
}
static void collect_imported_types(xmlSchemaImportPtr import, VALUE result, const xmlChar *name)
{
if (import->imported && import->schema)
{
xmlHashScan(import->schema->typeDecl, (xmlHashScanner)scan_schema_type, (void *)result);
}
}
/*
* call-seq:
* XML::Schema.imported_types -> hash
*
* Returns a hash of all types within the entire schema including imports
*/
static VALUE rxml_schema_imported_types(VALUE self)
{
xmlSchemaPtr xschema;
VALUE result = rb_hash_new();
Data_Get_Struct(self, xmlSchema, xschema);
if (xschema)
{
xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_types, (void *)result);
}
return result;
}
static void collect_imported_ns_types(xmlSchemaImportPtr import, VALUE result, const xmlChar *name)
{
if (import->imported && import->schema)
{
VALUE types = rb_hash_new();
xmlHashScan(import->schema->typeDecl, (xmlHashScanner)scan_schema_type, (void *)types);
rb_hash_aset(result, QNIL_OR_STRING(import->schema->targetNamespace), types);
}
}
/*
* call-seq:
* XML::Schema.imported_ns_types -> hash
*
* Returns a hash by namespace of a hash of schema types within the entire schema including imports
*/
static VALUE rxml_schema_imported_ns_types(VALUE self)
{
xmlSchemaPtr xschema;
VALUE result = rb_hash_new();
Data_Get_Struct(self, xmlSchema, xschema);
if (xschema)
{
xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_ns_types, (void *)result);
}
return result;
}
void rxml_init_schema(void)
{
cXMLSchema = rb_define_class_under(mXML, "Schema", rb_cObject);
rb_undef_alloc_func(cXMLSchema);
rb_define_singleton_method(cXMLSchema, "new", rxml_schema_init_from_uri, 1);
rb_define_singleton_method(cXMLSchema, "from_string", rxml_schema_init_from_string, 1);
rb_define_singleton_method(cXMLSchema, "document", rxml_schema_init_from_document, 1);
/* Create attr_reader methods for the above instance variables */
rb_define_attr(cXMLSchema, "target_namespace", 1, 0);
rb_define_attr(cXMLSchema, "name", 1, 0);
rb_define_attr(cXMLSchema, "id", 1, 0);
rb_define_attr(cXMLSchema, "version", 1, 0);
// These are just methods so as to hide their values and not overly clutter the output of inspect/to_str
rb_define_method(cXMLSchema, "document", rxml_schema_document, 0);
rb_define_method(cXMLSchema, "namespaces", rxml_schema_namespaces, 0);
rb_define_method(cXMLSchema, "elements", rxml_schema_elements, 0);
rb_define_method(cXMLSchema, "imported_ns_elements", rxml_schema_imported_ns_elements, 0);
rb_define_method(cXMLSchema, "types", rxml_schema_types, 0);
rb_define_method(cXMLSchema, "imported_types", rxml_schema_imported_types, 0);
rb_define_method(cXMLSchema, "imported_ns_types", rxml_schema_imported_ns_types, 0);
rxml_init_schema_facet();
rxml_init_schema_element();
rxml_init_schema_attribute();
rxml_init_schema_type();
}