libxml-ruby 4.1.0 → 4.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HISTORY +13 -0
- data/ext/libxml/extconf.rb +67 -61
- data/ext/libxml/ruby_xml_attr_decl.c +154 -153
- data/ext/libxml/ruby_xml_attributes.c +276 -275
- data/ext/libxml/ruby_xml_error.c +2 -2
- data/ext/libxml/ruby_xml_html_parser_context.c +0 -1
- data/ext/libxml/ruby_xml_input_cbg.c +4 -7
- data/ext/libxml/ruby_xml_reader.c +1245 -1242
- data/ext/libxml/ruby_xml_relaxng.c +113 -112
- data/ext/libxml/ruby_xml_schema.c +422 -420
- data/ext/libxml/ruby_xml_schema_attribute.c +108 -107
- data/ext/libxml/ruby_xml_schema_element.c +70 -69
- data/ext/libxml/ruby_xml_schema_type.c +252 -251
- data/ext/libxml/ruby_xml_version.h +2 -2
- data/ext/libxml/ruby_xml_writer.c +1138 -1137
- data/ext/libxml/ruby_xml_xpath_expression.c +81 -81
- data/ext/libxml/ruby_xml_xpath_object.c +340 -339
- data/lib/libxml/error.rb +7 -7
- data/lib/libxml/schema/element.rb +27 -19
- data/test/test_error.rb +173 -157
- data/test/test_schema.rb +237 -231
- metadata +3 -3
@@ -1,420 +1,422 @@
|
|
1
|
-
#include "ruby_libxml.h"
|
2
|
-
#include "ruby_xml_schema.h"
|
3
|
-
|
4
|
-
#include "ruby_xml_schema_type.h"
|
5
|
-
#include "ruby_xml_schema_element.h"
|
6
|
-
#include "ruby_xml_schema_attribute.h"
|
7
|
-
#include "ruby_xml_schema_facet.h"
|
8
|
-
|
9
|
-
#include <libxml/xmlschemas.h>
|
10
|
-
|
11
|
-
typedef struct _xmlSchemaBucket xmlSchemaBucket;
|
12
|
-
typedef xmlSchemaBucket *xmlSchemaBucketPtr;
|
13
|
-
|
14
|
-
/**
|
15
|
-
* xmlSchemaSchemaRelation:
|
16
|
-
*
|
17
|
-
* Used to create a graph of schema relationships.
|
18
|
-
*/
|
19
|
-
typedef struct _xmlSchemaSchemaRelation xmlSchemaSchemaRelation;
|
20
|
-
typedef xmlSchemaSchemaRelation *xmlSchemaSchemaRelationPtr;
|
21
|
-
struct _xmlSchemaSchemaRelation {
|
22
|
-
xmlSchemaSchemaRelationPtr next;
|
23
|
-
int type;
|
24
|
-
/* E.g. XML_SCHEMA_SCHEMA_IMPORT */
|
25
|
-
const xmlChar *importNamespace;
|
26
|
-
xmlSchemaBucketPtr bucket;
|
27
|
-
};
|
28
|
-
|
29
|
-
struct _xmlSchemaBucket {
|
30
|
-
int type;
|
31
|
-
int flags;
|
32
|
-
const xmlChar *schemaLocation;
|
33
|
-
const xmlChar *origTargetNamespace;
|
34
|
-
const xmlChar *targetNamespace;
|
35
|
-
xmlDocPtr doc;
|
36
|
-
xmlSchemaSchemaRelationPtr relations;
|
37
|
-
int located;
|
38
|
-
int parsed;
|
39
|
-
int imported;
|
40
|
-
int preserveDoc;
|
41
|
-
xmlSchemaItemListPtr globals;
|
42
|
-
/* Global components. */
|
43
|
-
xmlSchemaItemListPtr locals; /* Local components. */
|
44
|
-
};
|
45
|
-
|
46
|
-
/**
|
47
|
-
* xmlSchemaImport:
|
48
|
-
* (extends xmlSchemaBucket)
|
49
|
-
*
|
50
|
-
* Reflects a schema. Holds some information
|
51
|
-
* about the schema and its toplevel components. Duplicate
|
52
|
-
* toplevel components are not checked at this level.
|
53
|
-
*/
|
54
|
-
typedef struct _xmlSchemaImport xmlSchemaImport;
|
55
|
-
typedef xmlSchemaImport *xmlSchemaImportPtr;
|
56
|
-
struct _xmlSchemaImport {
|
57
|
-
int type;
|
58
|
-
/* Main OR import OR include. */
|
59
|
-
int flags;
|
60
|
-
const xmlChar *schemaLocation; /* The URI of the schema document. */
|
61
|
-
/* For chameleon includes, @origTargetNamespace will be NULL */
|
62
|
-
const xmlChar *origTargetNamespace;
|
63
|
-
/*
|
64
|
-
* For chameleon includes, @targetNamespace will be the
|
65
|
-
* targetNamespace of the including schema.
|
66
|
-
*/
|
67
|
-
const xmlChar *targetNamespace;
|
68
|
-
xmlDocPtr doc; /* The schema node-tree. */
|
69
|
-
/* @relations will hold any included/imported/redefined schemas. */
|
70
|
-
xmlSchemaSchemaRelationPtr relations;
|
71
|
-
int located;
|
72
|
-
int parsed;
|
73
|
-
int imported;
|
74
|
-
int preserveDoc;
|
75
|
-
xmlSchemaItemListPtr globals;
|
76
|
-
xmlSchemaItemListPtr locals;
|
77
|
-
/* The imported schema. */
|
78
|
-
xmlSchemaPtr schema;
|
79
|
-
};
|
80
|
-
|
81
|
-
/*
|
82
|
-
* Document-class: LibXML::XML::Schema
|
83
|
-
*
|
84
|
-
* The XML::Schema class is used to prepare XML Schemas for validation of xml
|
85
|
-
* documents.
|
86
|
-
*
|
87
|
-
* Schemas can be created from XML documents, strinings or URIs using the
|
88
|
-
* corresponding methods (new for URIs).
|
89
|
-
*
|
90
|
-
* Once a schema is prepared, an XML document can be validated by the
|
91
|
-
* XML::Document#validate_schema method providing the XML::Schema object
|
92
|
-
* as parameter. The method return true if the document validates, false
|
93
|
-
* otherwise.
|
94
|
-
*
|
95
|
-
* Basic usage:
|
96
|
-
*
|
97
|
-
* # parse schema as xml document
|
98
|
-
* schema_document = XML::Document.file('schema.rng')
|
99
|
-
*
|
100
|
-
* # prepare schema for validation
|
101
|
-
* schema = XML::Schema.document(schema_document)
|
102
|
-
*
|
103
|
-
* # parse xml document to be validated
|
104
|
-
* instance = XML::Document.file('instance.xml')
|
105
|
-
*
|
106
|
-
* # validate
|
107
|
-
* instance.validate_schema(schema)
|
108
|
-
*/
|
109
|
-
|
110
|
-
VALUE cXMLSchema;
|
111
|
-
|
112
|
-
static void rxml_schema_free(xmlSchemaPtr xschema)
|
113
|
-
{
|
114
|
-
xmlSchemaFree(xschema);
|
115
|
-
}
|
116
|
-
|
117
|
-
VALUE rxml_wrap_schema(xmlSchemaPtr xschema)
|
118
|
-
{
|
119
|
-
VALUE result;
|
120
|
-
|
121
|
-
if (!xschema)
|
122
|
-
rb_raise(rb_eArgError, "XML::Schema is required!");
|
123
|
-
|
124
|
-
result = Data_Wrap_Struct(cXMLSchema, NULL, rxml_schema_free, xschema);
|
125
|
-
|
126
|
-
/*
|
127
|
-
* Create these as instance variables to provide the output of inspect/to_str some
|
128
|
-
* idea of what schema this class contains.
|
129
|
-
*/
|
130
|
-
rb_iv_set(result, "@target_namespace", QNIL_OR_STRING(xschema->targetNamespace));
|
131
|
-
rb_iv_set(result, "@name", QNIL_OR_STRING(xschema->name));
|
132
|
-
rb_iv_set(result, "@id", QNIL_OR_STRING(xschema->id));
|
133
|
-
rb_iv_set(result, "@version", QNIL_OR_STRING(xschema->name));
|
134
|
-
|
135
|
-
return result;
|
136
|
-
}
|
137
|
-
|
138
|
-
static VALUE rxml_schema_init(VALUE class, xmlSchemaParserCtxtPtr xparser)
|
139
|
-
{
|
140
|
-
xmlSchemaPtr xschema;
|
141
|
-
|
142
|
-
xschema = xmlSchemaParse(xparser);
|
143
|
-
xmlSchemaFreeParserCtxt(xparser);
|
144
|
-
|
145
|
-
if (!xschema)
|
146
|
-
rxml_raise(&xmlLastError);
|
147
|
-
|
148
|
-
return rxml_wrap_schema(xschema);
|
149
|
-
}
|
150
|
-
|
151
|
-
/*
|
152
|
-
* call-seq:
|
153
|
-
* XML::Schema.initialize(schema_uri) -> schema
|
154
|
-
*
|
155
|
-
* Create a new schema from the specified URI.
|
156
|
-
*/
|
157
|
-
static VALUE rxml_schema_init_from_uri(VALUE class, VALUE uri)
|
158
|
-
{
|
159
|
-
xmlSchemaParserCtxtPtr xparser;
|
160
|
-
|
161
|
-
Check_Type(uri, T_STRING);
|
162
|
-
|
163
|
-
xmlResetLastError();
|
164
|
-
xparser = xmlSchemaNewParserCtxt(StringValuePtr(uri));
|
165
|
-
if (!xparser)
|
166
|
-
rxml_raise(&xmlLastError);
|
167
|
-
|
168
|
-
return rxml_schema_init(class, xparser);
|
169
|
-
}
|
170
|
-
|
171
|
-
/*
|
172
|
-
* call-seq:
|
173
|
-
* XML::Schema.document(document) -> schema
|
174
|
-
*
|
175
|
-
* Create a new schema from the specified document.
|
176
|
-
*/
|
177
|
-
static VALUE rxml_schema_init_from_document(VALUE class, VALUE document)
|
178
|
-
{
|
179
|
-
xmlDocPtr xdoc;
|
180
|
-
xmlSchemaParserCtxtPtr xparser;
|
181
|
-
|
182
|
-
Data_Get_Struct(document, xmlDoc, xdoc);
|
183
|
-
|
184
|
-
xmlResetLastError();
|
185
|
-
xparser = xmlSchemaNewDocParserCtxt(xdoc);
|
186
|
-
if (!xparser)
|
187
|
-
rxml_raise(&xmlLastError);
|
188
|
-
|
189
|
-
return rxml_schema_init(class, xparser);
|
190
|
-
}
|
191
|
-
|
192
|
-
/*
|
193
|
-
* call-seq:
|
194
|
-
* XML::Schema.from_string("schema_data") -> "value"
|
195
|
-
*
|
196
|
-
* Create a new schema using the specified string.
|
197
|
-
*/
|
198
|
-
static VALUE rxml_schema_init_from_string(VALUE class, VALUE schema_str)
|
199
|
-
{
|
200
|
-
xmlSchemaParserCtxtPtr xparser;
|
201
|
-
|
202
|
-
Check_Type(schema_str, T_STRING);
|
203
|
-
|
204
|
-
xmlResetLastError();
|
205
|
-
xparser = xmlSchemaNewMemParserCtxt(StringValuePtr(schema_str), (int)strlen(StringValuePtr(schema_str)));
|
206
|
-
if (!xparser)
|
207
|
-
rxml_raise(&xmlLastError);
|
208
|
-
|
209
|
-
return rxml_schema_init(class, xparser);
|
210
|
-
}
|
211
|
-
|
212
|
-
/*
|
213
|
-
* call-seq:
|
214
|
-
* XML::Schema.document -> document
|
215
|
-
*
|
216
|
-
* Return the Schema XML Document
|
217
|
-
*/
|
218
|
-
static VALUE rxml_schema_document(VALUE self)
|
219
|
-
{
|
220
|
-
xmlSchemaPtr xschema;
|
221
|
-
|
222
|
-
Data_Get_Struct(self, xmlSchema, xschema);
|
223
|
-
|
224
|
-
return rxml_node_wrap(xmlDocGetRootElement(xschema->doc));
|
225
|
-
}
|
226
|
-
|
227
|
-
static void scan_namespaces(xmlSchemaImportPtr ximport, VALUE array, const xmlChar *nsname)
|
228
|
-
{
|
229
|
-
xmlNodePtr xnode;
|
230
|
-
xmlNsPtr xns;
|
231
|
-
|
232
|
-
if (ximport->doc)
|
233
|
-
{
|
234
|
-
xnode = xmlDocGetRootElement(ximport->doc);
|
235
|
-
xns = xnode->nsDef;
|
236
|
-
|
237
|
-
while (xns)
|
238
|
-
{
|
239
|
-
VALUE namespace = rxml_namespace_wrap(xns);
|
240
|
-
rb_ary_push(array, namespace);
|
241
|
-
xns = xns->next;
|
242
|
-
}
|
243
|
-
}
|
244
|
-
}
|
245
|
-
|
246
|
-
/*
|
247
|
-
* call-seq:
|
248
|
-
* XML::Schema.namespaces -> array
|
249
|
-
*
|
250
|
-
* Returns an array of Namespaces defined by the schema
|
251
|
-
*/
|
252
|
-
static VALUE rxml_schema_namespaces(VALUE self)
|
253
|
-
{
|
254
|
-
VALUE result;
|
255
|
-
xmlSchemaPtr xschema;
|
256
|
-
|
257
|
-
Data_Get_Struct(self, xmlSchema, xschema);
|
258
|
-
|
259
|
-
result = rb_ary_new();
|
260
|
-
xmlHashScan(xschema->schemasImports, (xmlHashScanner)scan_namespaces, (void *)result);
|
261
|
-
|
262
|
-
return result;
|
263
|
-
}
|
264
|
-
|
265
|
-
static void scan_schema_element(xmlSchemaElementPtr xelement, VALUE hash, const xmlChar *name)
|
266
|
-
{
|
267
|
-
VALUE element = rxml_wrap_schema_element(xelement);
|
268
|
-
rb_hash_aset(hash, rb_str_new2((const char*)name), element);
|
269
|
-
}
|
270
|
-
|
271
|
-
static VALUE rxml_schema_elements(VALUE self)
|
272
|
-
{
|
273
|
-
VALUE result = rb_hash_new();
|
274
|
-
xmlSchemaPtr xschema;
|
275
|
-
|
276
|
-
Data_Get_Struct(self, xmlSchema, xschema);
|
277
|
-
xmlHashScan(xschema->elemDecl, (xmlHashScanner)scan_schema_element, (void *)result);
|
278
|
-
|
279
|
-
return result;
|
280
|
-
}
|
281
|
-
|
282
|
-
static void collect_imported_ns_elements(xmlSchemaImportPtr import, VALUE result, const xmlChar *name)
|
283
|
-
{
|
284
|
-
if (import->imported && import->schema)
|
285
|
-
{
|
286
|
-
VALUE elements = rb_hash_new();
|
287
|
-
xmlHashScan(import->schema->elemDecl, (xmlHashScanner)scan_schema_element, (void *)elements);
|
288
|
-
rb_hash_aset(result, QNIL_OR_STRING(import->schema->targetNamespace), elements);
|
289
|
-
}
|
290
|
-
}
|
291
|
-
|
292
|
-
/*
|
293
|
-
* call-seq:
|
294
|
-
* XML::Schema.imported_ns_elements -> hash
|
295
|
-
*
|
296
|
-
* Returns a hash by namespace of a hash of schema elements within the entire schema including imports
|
297
|
-
*/
|
298
|
-
static VALUE rxml_schema_imported_ns_elements(VALUE self)
|
299
|
-
{
|
300
|
-
xmlSchemaPtr xschema;
|
301
|
-
VALUE result = rb_hash_new();
|
302
|
-
|
303
|
-
Data_Get_Struct(self, xmlSchema, xschema);
|
304
|
-
|
305
|
-
if (xschema)
|
306
|
-
{
|
307
|
-
xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_ns_elements, (void *)result);
|
308
|
-
}
|
309
|
-
|
310
|
-
return result;
|
311
|
-
}
|
312
|
-
|
313
|
-
static void scan_schema_type(xmlSchemaTypePtr xtype, VALUE hash, const xmlChar *name)
|
314
|
-
{
|
315
|
-
VALUE type = rxml_wrap_schema_type(xtype);
|
316
|
-
rb_hash_aset(hash, rb_str_new2((const char*)name), type);
|
317
|
-
}
|
318
|
-
|
319
|
-
static VALUE rxml_schema_types(VALUE self)
|
320
|
-
{
|
321
|
-
VALUE result = rb_hash_new();
|
322
|
-
xmlSchemaPtr xschema;
|
323
|
-
|
324
|
-
Data_Get_Struct(self, xmlSchema, xschema);
|
325
|
-
|
326
|
-
if (xschema != NULL && xschema->typeDecl != NULL)
|
327
|
-
{
|
328
|
-
xmlHashScan(xschema->typeDecl, (xmlHashScanner)scan_schema_type, (void *)result);
|
329
|
-
}
|
330
|
-
|
331
|
-
return result;
|
332
|
-
}
|
333
|
-
|
334
|
-
static void collect_imported_types(xmlSchemaImportPtr import, VALUE result, const xmlChar *name)
|
335
|
-
{
|
336
|
-
if (import->imported && import->schema)
|
337
|
-
{
|
338
|
-
xmlHashScan(import->schema->typeDecl, (xmlHashScanner)scan_schema_type, (void *)result);
|
339
|
-
}
|
340
|
-
}
|
341
|
-
|
342
|
-
/*
|
343
|
-
* call-seq:
|
344
|
-
* XML::Schema.imported_types -> hash
|
345
|
-
*
|
346
|
-
* Returns a hash of all types within the entire schema including imports
|
347
|
-
*/
|
348
|
-
static VALUE rxml_schema_imported_types(VALUE self)
|
349
|
-
{
|
350
|
-
xmlSchemaPtr xschema;
|
351
|
-
VALUE result = rb_hash_new();
|
352
|
-
|
353
|
-
Data_Get_Struct(self, xmlSchema, xschema);
|
354
|
-
|
355
|
-
if (xschema)
|
356
|
-
{
|
357
|
-
xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_types, (void *)result);
|
358
|
-
}
|
359
|
-
|
360
|
-
return result;
|
361
|
-
}
|
362
|
-
|
363
|
-
static void collect_imported_ns_types(xmlSchemaImportPtr import, VALUE result, const xmlChar *name)
|
364
|
-
{
|
365
|
-
if (import->imported && import->schema)
|
366
|
-
{
|
367
|
-
VALUE types = rb_hash_new();
|
368
|
-
xmlHashScan(import->schema->typeDecl, (xmlHashScanner)scan_schema_type, (void *)types);
|
369
|
-
rb_hash_aset(result, QNIL_OR_STRING(import->schema->targetNamespace), types);
|
370
|
-
}
|
371
|
-
}
|
372
|
-
|
373
|
-
/*
|
374
|
-
* call-seq:
|
375
|
-
* XML::Schema.imported_ns_types -> hash
|
376
|
-
*
|
377
|
-
* Returns a hash by namespace of a hash of schema types within the entire schema including imports
|
378
|
-
*/
|
379
|
-
static VALUE rxml_schema_imported_ns_types(VALUE self)
|
380
|
-
{
|
381
|
-
xmlSchemaPtr xschema;
|
382
|
-
VALUE result = rb_hash_new();
|
383
|
-
|
384
|
-
Data_Get_Struct(self, xmlSchema, xschema);
|
385
|
-
|
386
|
-
if (xschema)
|
387
|
-
{
|
388
|
-
xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_ns_types, (void *)result);
|
389
|
-
}
|
390
|
-
|
391
|
-
return result;
|
392
|
-
}
|
393
|
-
|
394
|
-
void rxml_init_schema(void)
|
395
|
-
{
|
396
|
-
cXMLSchema = rb_define_class_under(mXML, "Schema", rb_cObject);
|
397
|
-
|
398
|
-
|
399
|
-
rb_define_singleton_method(cXMLSchema, "
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
rb_define_attr(cXMLSchema, "
|
405
|
-
rb_define_attr(cXMLSchema, "
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
rb_define_method(cXMLSchema, "
|
411
|
-
rb_define_method(cXMLSchema, "
|
412
|
-
rb_define_method(cXMLSchema, "
|
413
|
-
rb_define_method(cXMLSchema, "
|
414
|
-
rb_define_method(cXMLSchema, "
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
1
|
+
#include "ruby_libxml.h"
|
2
|
+
#include "ruby_xml_schema.h"
|
3
|
+
|
4
|
+
#include "ruby_xml_schema_type.h"
|
5
|
+
#include "ruby_xml_schema_element.h"
|
6
|
+
#include "ruby_xml_schema_attribute.h"
|
7
|
+
#include "ruby_xml_schema_facet.h"
|
8
|
+
|
9
|
+
#include <libxml/xmlschemas.h>
|
10
|
+
|
11
|
+
typedef struct _xmlSchemaBucket xmlSchemaBucket;
|
12
|
+
typedef xmlSchemaBucket *xmlSchemaBucketPtr;
|
13
|
+
|
14
|
+
/**
|
15
|
+
* xmlSchemaSchemaRelation:
|
16
|
+
*
|
17
|
+
* Used to create a graph of schema relationships.
|
18
|
+
*/
|
19
|
+
typedef struct _xmlSchemaSchemaRelation xmlSchemaSchemaRelation;
|
20
|
+
typedef xmlSchemaSchemaRelation *xmlSchemaSchemaRelationPtr;
|
21
|
+
struct _xmlSchemaSchemaRelation {
|
22
|
+
xmlSchemaSchemaRelationPtr next;
|
23
|
+
int type;
|
24
|
+
/* E.g. XML_SCHEMA_SCHEMA_IMPORT */
|
25
|
+
const xmlChar *importNamespace;
|
26
|
+
xmlSchemaBucketPtr bucket;
|
27
|
+
};
|
28
|
+
|
29
|
+
struct _xmlSchemaBucket {
|
30
|
+
int type;
|
31
|
+
int flags;
|
32
|
+
const xmlChar *schemaLocation;
|
33
|
+
const xmlChar *origTargetNamespace;
|
34
|
+
const xmlChar *targetNamespace;
|
35
|
+
xmlDocPtr doc;
|
36
|
+
xmlSchemaSchemaRelationPtr relations;
|
37
|
+
int located;
|
38
|
+
int parsed;
|
39
|
+
int imported;
|
40
|
+
int preserveDoc;
|
41
|
+
xmlSchemaItemListPtr globals;
|
42
|
+
/* Global components. */
|
43
|
+
xmlSchemaItemListPtr locals; /* Local components. */
|
44
|
+
};
|
45
|
+
|
46
|
+
/**
|
47
|
+
* xmlSchemaImport:
|
48
|
+
* (extends xmlSchemaBucket)
|
49
|
+
*
|
50
|
+
* Reflects a schema. Holds some information
|
51
|
+
* about the schema and its toplevel components. Duplicate
|
52
|
+
* toplevel components are not checked at this level.
|
53
|
+
*/
|
54
|
+
typedef struct _xmlSchemaImport xmlSchemaImport;
|
55
|
+
typedef xmlSchemaImport *xmlSchemaImportPtr;
|
56
|
+
struct _xmlSchemaImport {
|
57
|
+
int type;
|
58
|
+
/* Main OR import OR include. */
|
59
|
+
int flags;
|
60
|
+
const xmlChar *schemaLocation; /* The URI of the schema document. */
|
61
|
+
/* For chameleon includes, @origTargetNamespace will be NULL */
|
62
|
+
const xmlChar *origTargetNamespace;
|
63
|
+
/*
|
64
|
+
* For chameleon includes, @targetNamespace will be the
|
65
|
+
* targetNamespace of the including schema.
|
66
|
+
*/
|
67
|
+
const xmlChar *targetNamespace;
|
68
|
+
xmlDocPtr doc; /* The schema node-tree. */
|
69
|
+
/* @relations will hold any included/imported/redefined schemas. */
|
70
|
+
xmlSchemaSchemaRelationPtr relations;
|
71
|
+
int located;
|
72
|
+
int parsed;
|
73
|
+
int imported;
|
74
|
+
int preserveDoc;
|
75
|
+
xmlSchemaItemListPtr globals;
|
76
|
+
xmlSchemaItemListPtr locals;
|
77
|
+
/* The imported schema. */
|
78
|
+
xmlSchemaPtr schema;
|
79
|
+
};
|
80
|
+
|
81
|
+
/*
|
82
|
+
* Document-class: LibXML::XML::Schema
|
83
|
+
*
|
84
|
+
* The XML::Schema class is used to prepare XML Schemas for validation of xml
|
85
|
+
* documents.
|
86
|
+
*
|
87
|
+
* Schemas can be created from XML documents, strinings or URIs using the
|
88
|
+
* corresponding methods (new for URIs).
|
89
|
+
*
|
90
|
+
* Once a schema is prepared, an XML document can be validated by the
|
91
|
+
* XML::Document#validate_schema method providing the XML::Schema object
|
92
|
+
* as parameter. The method return true if the document validates, false
|
93
|
+
* otherwise.
|
94
|
+
*
|
95
|
+
* Basic usage:
|
96
|
+
*
|
97
|
+
* # parse schema as xml document
|
98
|
+
* schema_document = XML::Document.file('schema.rng')
|
99
|
+
*
|
100
|
+
* # prepare schema for validation
|
101
|
+
* schema = XML::Schema.document(schema_document)
|
102
|
+
*
|
103
|
+
* # parse xml document to be validated
|
104
|
+
* instance = XML::Document.file('instance.xml')
|
105
|
+
*
|
106
|
+
* # validate
|
107
|
+
* instance.validate_schema(schema)
|
108
|
+
*/
|
109
|
+
|
110
|
+
VALUE cXMLSchema;
|
111
|
+
|
112
|
+
static void rxml_schema_free(xmlSchemaPtr xschema)
|
113
|
+
{
|
114
|
+
xmlSchemaFree(xschema);
|
115
|
+
}
|
116
|
+
|
117
|
+
VALUE rxml_wrap_schema(xmlSchemaPtr xschema)
|
118
|
+
{
|
119
|
+
VALUE result;
|
120
|
+
|
121
|
+
if (!xschema)
|
122
|
+
rb_raise(rb_eArgError, "XML::Schema is required!");
|
123
|
+
|
124
|
+
result = Data_Wrap_Struct(cXMLSchema, NULL, rxml_schema_free, xschema);
|
125
|
+
|
126
|
+
/*
|
127
|
+
* Create these as instance variables to provide the output of inspect/to_str some
|
128
|
+
* idea of what schema this class contains.
|
129
|
+
*/
|
130
|
+
rb_iv_set(result, "@target_namespace", QNIL_OR_STRING(xschema->targetNamespace));
|
131
|
+
rb_iv_set(result, "@name", QNIL_OR_STRING(xschema->name));
|
132
|
+
rb_iv_set(result, "@id", QNIL_OR_STRING(xschema->id));
|
133
|
+
rb_iv_set(result, "@version", QNIL_OR_STRING(xschema->name));
|
134
|
+
|
135
|
+
return result;
|
136
|
+
}
|
137
|
+
|
138
|
+
static VALUE rxml_schema_init(VALUE class, xmlSchemaParserCtxtPtr xparser)
|
139
|
+
{
|
140
|
+
xmlSchemaPtr xschema;
|
141
|
+
|
142
|
+
xschema = xmlSchemaParse(xparser);
|
143
|
+
xmlSchemaFreeParserCtxt(xparser);
|
144
|
+
|
145
|
+
if (!xschema)
|
146
|
+
rxml_raise(&xmlLastError);
|
147
|
+
|
148
|
+
return rxml_wrap_schema(xschema);
|
149
|
+
}
|
150
|
+
|
151
|
+
/*
|
152
|
+
* call-seq:
|
153
|
+
* XML::Schema.initialize(schema_uri) -> schema
|
154
|
+
*
|
155
|
+
* Create a new schema from the specified URI.
|
156
|
+
*/
|
157
|
+
static VALUE rxml_schema_init_from_uri(VALUE class, VALUE uri)
|
158
|
+
{
|
159
|
+
xmlSchemaParserCtxtPtr xparser;
|
160
|
+
|
161
|
+
Check_Type(uri, T_STRING);
|
162
|
+
|
163
|
+
xmlResetLastError();
|
164
|
+
xparser = xmlSchemaNewParserCtxt(StringValuePtr(uri));
|
165
|
+
if (!xparser)
|
166
|
+
rxml_raise(&xmlLastError);
|
167
|
+
|
168
|
+
return rxml_schema_init(class, xparser);
|
169
|
+
}
|
170
|
+
|
171
|
+
/*
|
172
|
+
* call-seq:
|
173
|
+
* XML::Schema.document(document) -> schema
|
174
|
+
*
|
175
|
+
* Create a new schema from the specified document.
|
176
|
+
*/
|
177
|
+
static VALUE rxml_schema_init_from_document(VALUE class, VALUE document)
|
178
|
+
{
|
179
|
+
xmlDocPtr xdoc;
|
180
|
+
xmlSchemaParserCtxtPtr xparser;
|
181
|
+
|
182
|
+
Data_Get_Struct(document, xmlDoc, xdoc);
|
183
|
+
|
184
|
+
xmlResetLastError();
|
185
|
+
xparser = xmlSchemaNewDocParserCtxt(xdoc);
|
186
|
+
if (!xparser)
|
187
|
+
rxml_raise(&xmlLastError);
|
188
|
+
|
189
|
+
return rxml_schema_init(class, xparser);
|
190
|
+
}
|
191
|
+
|
192
|
+
/*
|
193
|
+
* call-seq:
|
194
|
+
* XML::Schema.from_string("schema_data") -> "value"
|
195
|
+
*
|
196
|
+
* Create a new schema using the specified string.
|
197
|
+
*/
|
198
|
+
static VALUE rxml_schema_init_from_string(VALUE class, VALUE schema_str)
|
199
|
+
{
|
200
|
+
xmlSchemaParserCtxtPtr xparser;
|
201
|
+
|
202
|
+
Check_Type(schema_str, T_STRING);
|
203
|
+
|
204
|
+
xmlResetLastError();
|
205
|
+
xparser = xmlSchemaNewMemParserCtxt(StringValuePtr(schema_str), (int)strlen(StringValuePtr(schema_str)));
|
206
|
+
if (!xparser)
|
207
|
+
rxml_raise(&xmlLastError);
|
208
|
+
|
209
|
+
return rxml_schema_init(class, xparser);
|
210
|
+
}
|
211
|
+
|
212
|
+
/*
|
213
|
+
* call-seq:
|
214
|
+
* XML::Schema.document -> document
|
215
|
+
*
|
216
|
+
* Return the Schema XML Document
|
217
|
+
*/
|
218
|
+
static VALUE rxml_schema_document(VALUE self)
|
219
|
+
{
|
220
|
+
xmlSchemaPtr xschema;
|
221
|
+
|
222
|
+
Data_Get_Struct(self, xmlSchema, xschema);
|
223
|
+
|
224
|
+
return rxml_node_wrap(xmlDocGetRootElement(xschema->doc));
|
225
|
+
}
|
226
|
+
|
227
|
+
static void scan_namespaces(xmlSchemaImportPtr ximport, VALUE array, const xmlChar *nsname)
|
228
|
+
{
|
229
|
+
xmlNodePtr xnode;
|
230
|
+
xmlNsPtr xns;
|
231
|
+
|
232
|
+
if (ximport->doc)
|
233
|
+
{
|
234
|
+
xnode = xmlDocGetRootElement(ximport->doc);
|
235
|
+
xns = xnode->nsDef;
|
236
|
+
|
237
|
+
while (xns)
|
238
|
+
{
|
239
|
+
VALUE namespace = rxml_namespace_wrap(xns);
|
240
|
+
rb_ary_push(array, namespace);
|
241
|
+
xns = xns->next;
|
242
|
+
}
|
243
|
+
}
|
244
|
+
}
|
245
|
+
|
246
|
+
/*
|
247
|
+
* call-seq:
|
248
|
+
* XML::Schema.namespaces -> array
|
249
|
+
*
|
250
|
+
* Returns an array of Namespaces defined by the schema
|
251
|
+
*/
|
252
|
+
static VALUE rxml_schema_namespaces(VALUE self)
|
253
|
+
{
|
254
|
+
VALUE result;
|
255
|
+
xmlSchemaPtr xschema;
|
256
|
+
|
257
|
+
Data_Get_Struct(self, xmlSchema, xschema);
|
258
|
+
|
259
|
+
result = rb_ary_new();
|
260
|
+
xmlHashScan(xschema->schemasImports, (xmlHashScanner)scan_namespaces, (void *)result);
|
261
|
+
|
262
|
+
return result;
|
263
|
+
}
|
264
|
+
|
265
|
+
static void scan_schema_element(xmlSchemaElementPtr xelement, VALUE hash, const xmlChar *name)
|
266
|
+
{
|
267
|
+
VALUE element = rxml_wrap_schema_element(xelement);
|
268
|
+
rb_hash_aset(hash, rb_str_new2((const char*)name), element);
|
269
|
+
}
|
270
|
+
|
271
|
+
static VALUE rxml_schema_elements(VALUE self)
|
272
|
+
{
|
273
|
+
VALUE result = rb_hash_new();
|
274
|
+
xmlSchemaPtr xschema;
|
275
|
+
|
276
|
+
Data_Get_Struct(self, xmlSchema, xschema);
|
277
|
+
xmlHashScan(xschema->elemDecl, (xmlHashScanner)scan_schema_element, (void *)result);
|
278
|
+
|
279
|
+
return result;
|
280
|
+
}
|
281
|
+
|
282
|
+
static void collect_imported_ns_elements(xmlSchemaImportPtr import, VALUE result, const xmlChar *name)
|
283
|
+
{
|
284
|
+
if (import->imported && import->schema)
|
285
|
+
{
|
286
|
+
VALUE elements = rb_hash_new();
|
287
|
+
xmlHashScan(import->schema->elemDecl, (xmlHashScanner)scan_schema_element, (void *)elements);
|
288
|
+
rb_hash_aset(result, QNIL_OR_STRING(import->schema->targetNamespace), elements);
|
289
|
+
}
|
290
|
+
}
|
291
|
+
|
292
|
+
/*
|
293
|
+
* call-seq:
|
294
|
+
* XML::Schema.imported_ns_elements -> hash
|
295
|
+
*
|
296
|
+
* Returns a hash by namespace of a hash of schema elements within the entire schema including imports
|
297
|
+
*/
|
298
|
+
static VALUE rxml_schema_imported_ns_elements(VALUE self)
|
299
|
+
{
|
300
|
+
xmlSchemaPtr xschema;
|
301
|
+
VALUE result = rb_hash_new();
|
302
|
+
|
303
|
+
Data_Get_Struct(self, xmlSchema, xschema);
|
304
|
+
|
305
|
+
if (xschema)
|
306
|
+
{
|
307
|
+
xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_ns_elements, (void *)result);
|
308
|
+
}
|
309
|
+
|
310
|
+
return result;
|
311
|
+
}
|
312
|
+
|
313
|
+
static void scan_schema_type(xmlSchemaTypePtr xtype, VALUE hash, const xmlChar *name)
|
314
|
+
{
|
315
|
+
VALUE type = rxml_wrap_schema_type(xtype);
|
316
|
+
rb_hash_aset(hash, rb_str_new2((const char*)name), type);
|
317
|
+
}
|
318
|
+
|
319
|
+
static VALUE rxml_schema_types(VALUE self)
|
320
|
+
{
|
321
|
+
VALUE result = rb_hash_new();
|
322
|
+
xmlSchemaPtr xschema;
|
323
|
+
|
324
|
+
Data_Get_Struct(self, xmlSchema, xschema);
|
325
|
+
|
326
|
+
if (xschema != NULL && xschema->typeDecl != NULL)
|
327
|
+
{
|
328
|
+
xmlHashScan(xschema->typeDecl, (xmlHashScanner)scan_schema_type, (void *)result);
|
329
|
+
}
|
330
|
+
|
331
|
+
return result;
|
332
|
+
}
|
333
|
+
|
334
|
+
static void collect_imported_types(xmlSchemaImportPtr import, VALUE result, const xmlChar *name)
|
335
|
+
{
|
336
|
+
if (import->imported && import->schema)
|
337
|
+
{
|
338
|
+
xmlHashScan(import->schema->typeDecl, (xmlHashScanner)scan_schema_type, (void *)result);
|
339
|
+
}
|
340
|
+
}
|
341
|
+
|
342
|
+
/*
|
343
|
+
* call-seq:
|
344
|
+
* XML::Schema.imported_types -> hash
|
345
|
+
*
|
346
|
+
* Returns a hash of all types within the entire schema including imports
|
347
|
+
*/
|
348
|
+
static VALUE rxml_schema_imported_types(VALUE self)
|
349
|
+
{
|
350
|
+
xmlSchemaPtr xschema;
|
351
|
+
VALUE result = rb_hash_new();
|
352
|
+
|
353
|
+
Data_Get_Struct(self, xmlSchema, xschema);
|
354
|
+
|
355
|
+
if (xschema)
|
356
|
+
{
|
357
|
+
xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_types, (void *)result);
|
358
|
+
}
|
359
|
+
|
360
|
+
return result;
|
361
|
+
}
|
362
|
+
|
363
|
+
static void collect_imported_ns_types(xmlSchemaImportPtr import, VALUE result, const xmlChar *name)
|
364
|
+
{
|
365
|
+
if (import->imported && import->schema)
|
366
|
+
{
|
367
|
+
VALUE types = rb_hash_new();
|
368
|
+
xmlHashScan(import->schema->typeDecl, (xmlHashScanner)scan_schema_type, (void *)types);
|
369
|
+
rb_hash_aset(result, QNIL_OR_STRING(import->schema->targetNamespace), types);
|
370
|
+
}
|
371
|
+
}
|
372
|
+
|
373
|
+
/*
|
374
|
+
* call-seq:
|
375
|
+
* XML::Schema.imported_ns_types -> hash
|
376
|
+
*
|
377
|
+
* Returns a hash by namespace of a hash of schema types within the entire schema including imports
|
378
|
+
*/
|
379
|
+
static VALUE rxml_schema_imported_ns_types(VALUE self)
|
380
|
+
{
|
381
|
+
xmlSchemaPtr xschema;
|
382
|
+
VALUE result = rb_hash_new();
|
383
|
+
|
384
|
+
Data_Get_Struct(self, xmlSchema, xschema);
|
385
|
+
|
386
|
+
if (xschema)
|
387
|
+
{
|
388
|
+
xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_ns_types, (void *)result);
|
389
|
+
}
|
390
|
+
|
391
|
+
return result;
|
392
|
+
}
|
393
|
+
|
394
|
+
void rxml_init_schema(void)
|
395
|
+
{
|
396
|
+
cXMLSchema = rb_define_class_under(mXML, "Schema", rb_cObject);
|
397
|
+
rb_undef_alloc_func(cXMLSchema);
|
398
|
+
|
399
|
+
rb_define_singleton_method(cXMLSchema, "new", rxml_schema_init_from_uri, 1);
|
400
|
+
rb_define_singleton_method(cXMLSchema, "from_string", rxml_schema_init_from_string, 1);
|
401
|
+
rb_define_singleton_method(cXMLSchema, "document", rxml_schema_init_from_document, 1);
|
402
|
+
|
403
|
+
/* Create attr_reader methods for the above instance variables */
|
404
|
+
rb_define_attr(cXMLSchema, "target_namespace", 1, 0);
|
405
|
+
rb_define_attr(cXMLSchema, "name", 1, 0);
|
406
|
+
rb_define_attr(cXMLSchema, "id", 1, 0);
|
407
|
+
rb_define_attr(cXMLSchema, "version", 1, 0);
|
408
|
+
|
409
|
+
// These are just methods so as to hide their values and not overly clutter the output of inspect/to_str
|
410
|
+
rb_define_method(cXMLSchema, "document", rxml_schema_document, 0);
|
411
|
+
rb_define_method(cXMLSchema, "namespaces", rxml_schema_namespaces, 0);
|
412
|
+
rb_define_method(cXMLSchema, "elements", rxml_schema_elements, 0);
|
413
|
+
rb_define_method(cXMLSchema, "imported_ns_elements", rxml_schema_imported_ns_elements, 0);
|
414
|
+
rb_define_method(cXMLSchema, "types", rxml_schema_types, 0);
|
415
|
+
rb_define_method(cXMLSchema, "imported_types", rxml_schema_imported_types, 0);
|
416
|
+
rb_define_method(cXMLSchema, "imported_ns_types", rxml_schema_imported_ns_types, 0);
|
417
|
+
|
418
|
+
rxml_init_schema_facet();
|
419
|
+
rxml_init_schema_element();
|
420
|
+
rxml_init_schema_attribute();
|
421
|
+
rxml_init_schema_type();
|
422
|
+
}
|