libxml-ruby 3.2.2 → 3.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40cfe66ffd84495bc2bd079978d06abd01be932513d0c04160453fe04c33db6c
4
- data.tar.gz: 1c45a468970af35413989b9af9f516efb1fc7aae17b401fa83626a762141d04e
3
+ metadata.gz: 5173bcced46009decd7e7a710aaf20b4005cf8baa5e8ee55148cb28733fd3c97
4
+ data.tar.gz: 3649de749c1064ec31c3d563a1328abfb43e401372beb6af4b0b5ad891af1ae3
5
5
  SHA512:
6
- metadata.gz: 6352fc0f0414020fbd48118313e6557e18f6dbccbd1a67270ef04cf693c3abd3a054ff68548e4235266acb41182b0907df079ba99e0b461afc6f320e1b3b923f
7
- data.tar.gz: e19986fa3ee2298dd6ed3d00f300e5ce25b956562cf6609b27fb976b9acc5df6e6f3ddcf22926de76cd9d9eb97d6d95d05d8c281dc4df0a53ec27b81a16552dd
6
+ metadata.gz: 3f5e71808404d9553073ae483c15a55b4d72e7a0e5b40939c7f2c72c8867386e7c7489769f6da2ce7da13cbbbd3329f0851d949f6dc48d7c5b40d9ec5784b630
7
+ data.tar.gz: 731d9152dca3cf219af862eb67171b9e5cce8df6c8bde4262eae3c53f1a5528e211188a50b472be67e3a294cf4b4f0d2a239317b5c08a3b2886b5e9c5fece14a
data/HISTORY CHANGED
@@ -1,5 +1,16 @@
1
1
  = Release History
2
2
 
3
+ == 3.2.3 / 2022-05-22
4
+
5
+ * Change some getter methods to instance variables with attr_accessors for easier debuggability (David Hansen)
6
+ * Add a number of tests related to schemas (David Hansen)
7
+ * Add schema.imported_elements so we can find imported elements (David Hansen)
8
+ * Fix segfault under windows when dereferencing a pre-existing error where the node has already been freed (David Hansen)
9
+ * Update to change name from imported_elements to imported_ns_elements and return a hash of hashes for namespaced elements (David Hansen)
10
+ * Only call xmlGetFeaturesList if LIBXML_LEGACY_ENABLED is defined. Most distros still ship libxml2 with legacy features enabled, but this will change (Nick Wellnhofer)
11
+ * Update GitHub Actions to use ruby/setup-ruby (Greg)
12
+ * Fix memory leak in rxml_node_path, node.path (vovanmozg)
13
+
3
14
  == 3.2.2 / 2022-01-15
4
15
  * Switch to Github actions for CI/CD (Greg)
5
16
  * Test fixes (Greg, Sergio Durigan Junior)
data/Rakefile CHANGED
@@ -21,7 +21,7 @@ Rake::ExtensionTask.new do |ext|
21
21
  ext.name = SO_NAME
22
22
  ext.ext_dir = "ext/libxml"
23
23
  ext.lib_dir = "lib/#{RUBY_VERSION.sub(/\.\d$/, '')}"
24
- if RUBY_PLATFORM.match(/mswin32|mswin64|mingw32|ucrt/)
24
+ if RUBY_PLATFORM.match(/mswin|mingw/)
25
25
  ext.config_options <<
26
26
  if (dir = ENV['WINDOWS_XML2_INCLUDE'])
27
27
  "--with-xml2-include=#{dir}"
@@ -49,7 +49,7 @@ Gem::PackageTask.new(spec) do |pkg|
49
49
  end
50
50
 
51
51
  # Setup Windows Gem
52
- if RUBY_PLATFORM.match(/mswin32|mswin64|mingw32/)
52
+ if RUBY_PLATFORM.match(/mswin|mingw/)
53
53
  binaries = (FileList['lib/**/*.so',
54
54
  'lib/**/*dll'])
55
55
 
@@ -764,6 +764,9 @@ static VALUE rxml_default_save_no_empty_tags_set(VALUE klass, VALUE value)
764
764
  */
765
765
  static VALUE rxml_features(VALUE klass)
766
766
  {
767
+ #ifndef LIBXML_LEGACY_ENABLED
768
+ return Qnil;
769
+ #else
767
770
  VALUE arr, str;
768
771
  int i, len = MAX_LIBXML_FEATURES_LEN;
769
772
  char **list = NULL;
@@ -788,6 +791,7 @@ static VALUE rxml_features(VALUE klass)
788
791
 
789
792
  ruby_xfree(list);
790
793
  return (arr);
794
+ #endif /* LIBXML_LEGACY_ENABLED */
791
795
  }
792
796
 
793
797
  /*
@@ -52,6 +52,7 @@ static VALUE rxml_namespace_initialize(VALUE self, VALUE node, VALUE prefix,
52
52
 
53
53
  Check_Type(node, T_DATA);
54
54
  Data_Get_Struct(node, xmlNode, xnode);
55
+ xmlResetLastError();
55
56
 
56
57
  /* Prefix can be null - that means its the default namespace */
57
58
  xmlPrefix = NIL_P(prefix) ? NULL : (xmlChar *)StringValuePtr(prefix);
@@ -983,14 +983,18 @@ static VALUE rxml_node_path(VALUE self)
983
983
  {
984
984
  xmlNodePtr xnode;
985
985
  xmlChar *path;
986
+ VALUE result = Qnil;
986
987
 
987
988
  xnode = rxml_get_xnode(self);
988
989
  path = xmlGetNodePath(xnode);
989
990
 
990
- if (path == NULL)
991
- return (Qnil);
992
- else
993
- return (rxml_new_cstr( path, NULL));
991
+ if (path)
992
+ {
993
+ result = rxml_new_cstr( path, NULL);
994
+ xmlFree(path);
995
+ }
996
+
997
+ return result;
994
998
  }
995
999
 
996
1000
  /*
@@ -46,9 +46,37 @@ static void rxml_schema_free(xmlSchemaPtr xschema)
46
46
 
47
47
  VALUE rxml_wrap_schema(xmlSchemaPtr xschema)
48
48
  {
49
- return Data_Wrap_Struct(cXMLSchema, NULL, rxml_schema_free, xschema);
49
+ VALUE result;
50
+
51
+ if (!xschema)
52
+ rb_raise(rb_eArgError, "XML::Schema is required!");
53
+
54
+ result = Data_Wrap_Struct(cXMLSchema, NULL, rxml_schema_free, xschema);
55
+
56
+ /*
57
+ * Create these as instance variables to provide the output of inspect/to_str some
58
+ * idea of what schema this class contains.
59
+ */
60
+ rb_iv_set(result, "@target_namespace", QNIL_OR_STRING(xschema->targetNamespace));
61
+ rb_iv_set(result, "@name", QNIL_OR_STRING(xschema->name));
62
+ rb_iv_set(result, "@id", QNIL_OR_STRING(xschema->id));
63
+ rb_iv_set(result, "@version", QNIL_OR_STRING(xschema->name));
64
+
65
+ return result;
50
66
  }
51
67
 
68
+ static VALUE rxml_schema_init(VALUE class, xmlSchemaParserCtxtPtr xparser)
69
+ {
70
+ xmlSchemaPtr xschema;
71
+
72
+ xschema = xmlSchemaParse(xparser);
73
+ xmlSchemaFreeParserCtxt(xparser);
74
+
75
+ if (!xschema)
76
+ rxml_raise(&xmlLastError);
77
+
78
+ return rxml_wrap_schema(xschema);
79
+ }
52
80
 
53
81
  /*
54
82
  * call-seq:
@@ -59,15 +87,15 @@ VALUE rxml_wrap_schema(xmlSchemaPtr xschema)
59
87
  static VALUE rxml_schema_init_from_uri(VALUE class, VALUE uri)
60
88
  {
61
89
  xmlSchemaParserCtxtPtr xparser;
62
- xmlSchemaPtr xschema;
63
90
 
64
91
  Check_Type(uri, T_STRING);
65
92
 
93
+ xmlResetLastError();
66
94
  xparser = xmlSchemaNewParserCtxt(StringValuePtr(uri));
67
- xschema = xmlSchemaParse(xparser);
68
- xmlSchemaFreeParserCtxt(xparser);
95
+ if (!xparser)
96
+ rxml_raise(&xmlLastError);
69
97
 
70
- return Data_Wrap_Struct(cXMLSchema, NULL, rxml_schema_free, xschema);
98
+ return rxml_schema_init(class, xparser);
71
99
  }
72
100
 
73
101
  /*
@@ -79,19 +107,16 @@ static VALUE rxml_schema_init_from_uri(VALUE class, VALUE uri)
79
107
  static VALUE rxml_schema_init_from_document(VALUE class, VALUE document)
80
108
  {
81
109
  xmlDocPtr xdoc;
82
- xmlSchemaPtr xschema;
83
110
  xmlSchemaParserCtxtPtr xparser;
84
111
 
85
112
  Data_Get_Struct(document, xmlDoc, xdoc);
86
113
 
114
+ xmlResetLastError();
87
115
  xparser = xmlSchemaNewDocParserCtxt(xdoc);
88
- xschema = xmlSchemaParse(xparser);
89
- xmlSchemaFreeParserCtxt(xparser);
116
+ if (!xparser)
117
+ rxml_raise(&xmlLastError);
90
118
 
91
- if (xschema == NULL)
92
- return Qnil;
93
-
94
- return Data_Wrap_Struct(cXMLSchema, NULL, rxml_schema_free, xschema);
119
+ return rxml_schema_init(class, xparser);
95
120
  }
96
121
 
97
122
  /*
@@ -100,57 +125,26 @@ static VALUE rxml_schema_init_from_document(VALUE class, VALUE document)
100
125
  *
101
126
  * Create a new schema using the specified string.
102
127
  */
103
- static VALUE rxml_schema_init_from_string(VALUE self, VALUE schema_str)
128
+ static VALUE rxml_schema_init_from_string(VALUE class, VALUE schema_str)
104
129
  {
105
130
  xmlSchemaParserCtxtPtr xparser;
106
- xmlSchemaPtr xschema;
107
131
 
108
132
  Check_Type(schema_str, T_STRING);
109
133
 
134
+ xmlResetLastError();
110
135
  xparser = xmlSchemaNewMemParserCtxt(StringValuePtr(schema_str), (int)strlen(StringValuePtr(schema_str)));
111
- xschema = xmlSchemaParse(xparser);
112
- xmlSchemaFreeParserCtxt(xparser);
113
-
114
- return Data_Wrap_Struct(cXMLSchema, NULL, rxml_schema_free, xschema);
115
- }
116
-
117
-
118
- static VALUE rxml_schema_target_namespace(VALUE self)
119
- {
120
- xmlSchemaPtr xschema;
121
-
122
- Data_Get_Struct(self, xmlSchema, xschema);
123
-
124
- QNIL_OR_STRING(xschema->targetNamespace)
125
- }
126
-
127
- static VALUE rxml_schema_name(VALUE self)
128
- {
129
- xmlSchemaPtr xschema;
130
-
131
- Data_Get_Struct(self, xmlSchema, xschema);
136
+ if (!xparser)
137
+ rxml_raise(&xmlLastError);
132
138
 
133
- QNIL_OR_STRING(xschema->name)
134
- }
135
-
136
- static VALUE rxml_schema_version(VALUE self)
137
- {
138
- xmlSchemaPtr xschema;
139
-
140
- Data_Get_Struct(self, xmlSchema, xschema);
141
-
142
- QNIL_OR_STRING(xschema->version)
143
- }
144
-
145
- static VALUE rxml_schema_id(VALUE self)
146
- {
147
- xmlSchemaPtr xschema;
148
-
149
- Data_Get_Struct(self, xmlSchema, xschema);
150
-
151
- QNIL_OR_STRING(xschema->id)
139
+ return rxml_schema_init(class, xparser);
152
140
  }
153
141
 
142
+ /*
143
+ * call-seq:
144
+ * XML::Schema.document -> document
145
+ *
146
+ * Return the Schema XML Document
147
+ */
154
148
  static VALUE rxml_schema_document(VALUE self)
155
149
  {
156
150
  xmlSchemaPtr xschema;
@@ -160,7 +154,7 @@ static VALUE rxml_schema_document(VALUE self)
160
154
  return rxml_node_wrap(xmlDocGetRootElement(xschema->doc));
161
155
  }
162
156
 
163
- static void scan_namespaces(xmlSchemaImportPtr ximport, VALUE array, xmlChar *nsname)
157
+ static void scan_namespaces(xmlSchemaImportPtr ximport, VALUE array, const xmlChar *nsname)
164
158
  {
165
159
  xmlNodePtr xnode;
166
160
  xmlNsPtr xns;
@@ -171,7 +165,7 @@ static void scan_namespaces(xmlSchemaImportPtr ximport, VALUE array, xmlChar *ns
171
165
  xns = xnode->nsDef;
172
166
 
173
167
  while (xns)
174
- {
168
+ {
175
169
  VALUE namespace = rxml_namespace_wrap(xns);
176
170
  rb_ary_push(array, namespace);
177
171
  xns = xns->next;
@@ -179,6 +173,12 @@ static void scan_namespaces(xmlSchemaImportPtr ximport, VALUE array, xmlChar *ns
179
173
  }
180
174
  }
181
175
 
176
+ /*
177
+ * call-seq:
178
+ * XML::Schema.namespaces -> array
179
+ *
180
+ * Returns an array of Namespaces defined by the schema
181
+ */
182
182
  static VALUE rxml_schema_namespaces(VALUE self)
183
183
  {
184
184
  VALUE result;
@@ -192,7 +192,7 @@ static VALUE rxml_schema_namespaces(VALUE self)
192
192
  return result;
193
193
  }
194
194
 
195
- static void scan_element(xmlSchemaElementPtr xelement, VALUE hash, xmlChar *name)
195
+ static void scan_schema_element(xmlSchemaElementPtr xelement, VALUE hash, const xmlChar *name)
196
196
  {
197
197
  VALUE element = rxml_wrap_schema_element(xelement);
198
198
  rb_hash_aset(hash, rb_str_new2((const char*)name), element);
@@ -204,40 +204,77 @@ static VALUE rxml_schema_elements(VALUE self)
204
204
  xmlSchemaPtr xschema;
205
205
 
206
206
  Data_Get_Struct(self, xmlSchema, xschema);
207
- xmlHashScan(xschema->elemDecl, (xmlHashScanner)scan_element, (void *)result);
207
+ xmlHashScan(xschema->elemDecl, (xmlHashScanner)scan_schema_element, (void *)result);
208
208
 
209
209
  return result;
210
210
  }
211
211
 
212
- static void scan_type(xmlSchemaTypePtr xtype, VALUE hash, xmlChar *name)
212
+ static void collect_imported_ns_elements(xmlSchemaImportPtr import, VALUE result, const xmlChar *name)
213
213
  {
214
- VALUE type = rxml_wrap_schema_type(xtype);
215
- rb_hash_aset(hash, rb_str_new2((const char*)name), type);
214
+ if (import->imported && import->schema)
215
+ {
216
+ VALUE elements = rb_hash_new();
217
+ xmlHashScan(import->schema->elemDecl, (xmlHashScanner)scan_schema_element, (void *)elements);
218
+ rb_hash_aset(result, QNIL_OR_STRING(import->schema->targetNamespace), elements);
219
+ }
220
+ }
221
+
222
+ /*
223
+ * call-seq:
224
+ * XML::Schema.imported_ns_elements -> hash
225
+ *
226
+ * Returns a hash by namespace of a hash of schema elements within the entire schema including imports
227
+ */
228
+ static VALUE rxml_schema_imported_ns_elements(VALUE self)
229
+ {
230
+ xmlSchemaPtr xschema;
231
+ VALUE result = rb_hash_new();
232
+
233
+ Data_Get_Struct(self, xmlSchema, xschema);
234
+
235
+ if (xschema)
236
+ {
237
+ xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_ns_elements, (void *)result);
238
+ }
239
+
240
+ return result;
241
+ }
242
+
243
+ static void scan_schema_type(xmlSchemaTypePtr xtype, VALUE hash, const xmlChar *name)
244
+ {
245
+ VALUE type = rxml_wrap_schema_type(xtype);
246
+ rb_hash_aset(hash, rb_str_new2((const char*)name), type);
216
247
  }
217
248
 
218
249
  static VALUE rxml_schema_types(VALUE self)
219
250
  {
220
- VALUE result = rb_hash_new();
221
- xmlSchemaPtr xschema;
251
+ VALUE result = rb_hash_new();
252
+ xmlSchemaPtr xschema;
222
253
 
223
- Data_Get_Struct(self, xmlSchema, xschema);
254
+ Data_Get_Struct(self, xmlSchema, xschema);
224
255
 
225
- if (xschema != NULL && xschema->typeDecl != NULL)
226
- {
227
- xmlHashScan(xschema->typeDecl, (xmlHashScanner)scan_type, (void *)result);
228
- }
256
+ if (xschema != NULL && xschema->typeDecl != NULL)
257
+ {
258
+ xmlHashScan(xschema->typeDecl, (xmlHashScanner)scan_schema_type, (void *)result);
259
+ }
229
260
 
230
- return result;
261
+ return result;
231
262
  }
232
263
 
233
- static void collect_imported_types(xmlSchemaImportPtr import, VALUE result)
264
+ static void collect_imported_types(xmlSchemaImportPtr import, VALUE result, const xmlChar *name)
234
265
  {
235
266
  if (import->imported && import->schema)
236
267
  {
237
- xmlHashScan(import->schema->typeDecl, (xmlHashScanner)scan_type, (void *)result);
268
+ xmlHashScan(import->schema->typeDecl, (xmlHashScanner)scan_schema_type, (void *)result);
238
269
  }
239
270
  }
240
271
 
272
+ /*
273
+ * call-seq:
274
+ * XML::Schema.imported_types -> hash
275
+ *
276
+ * Returns a hash of all types within the entire schema including imports
277
+ */
241
278
  static VALUE rxml_schema_imported_types(VALUE self)
242
279
  {
243
280
  xmlSchemaPtr xschema;
@@ -247,7 +284,38 @@ static VALUE rxml_schema_imported_types(VALUE self)
247
284
 
248
285
  if (xschema)
249
286
  {
250
- xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_types, (void *)result);
287
+ xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_types, (void *)result);
288
+ }
289
+
290
+ return result;
291
+ }
292
+
293
+ static void collect_imported_ns_types(xmlSchemaImportPtr import, VALUE result, const xmlChar *name)
294
+ {
295
+ if (import->imported && import->schema)
296
+ {
297
+ VALUE types = rb_hash_new();
298
+ xmlHashScan(import->schema->typeDecl, (xmlHashScanner)scan_schema_type, (void *)types);
299
+ rb_hash_aset(result, QNIL_OR_STRING(import->schema->targetNamespace), types);
300
+ }
301
+ }
302
+
303
+ /*
304
+ * call-seq:
305
+ * XML::Schema.imported_ns_types -> hash
306
+ *
307
+ * Returns a hash by namespace of a hash of schema types within the entire schema including imports
308
+ */
309
+ static VALUE rxml_schema_imported_ns_types(VALUE self)
310
+ {
311
+ xmlSchemaPtr xschema;
312
+ VALUE result = rb_hash_new();
313
+
314
+ Data_Get_Struct(self, xmlSchema, xschema);
315
+
316
+ if (xschema)
317
+ {
318
+ xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_ns_types, (void *)result);
251
319
  }
252
320
 
253
321
  return result;
@@ -260,16 +328,20 @@ void rxml_init_schema(void)
260
328
  rb_define_singleton_method(cXMLSchema, "from_string", rxml_schema_init_from_string, 1);
261
329
  rb_define_singleton_method(cXMLSchema, "document", rxml_schema_init_from_document, 1);
262
330
 
263
- rb_define_method(cXMLSchema, "target_namespace", rxml_schema_target_namespace, 0);
264
- rb_define_method(cXMLSchema, "name", rxml_schema_name, 0);
265
- rb_define_method(cXMLSchema, "id", rxml_schema_id, 0);
266
- rb_define_method(cXMLSchema, "version", rxml_schema_version, 0);
267
- rb_define_method(cXMLSchema, "document", rxml_schema_document, 0);
331
+ /* Create attr_reader methods for the above instance variables */
332
+ rb_define_attr(cXMLSchema, "target_namespace", 1, 0);
333
+ rb_define_attr(cXMLSchema, "name", 1, 0);
334
+ rb_define_attr(cXMLSchema, "id", 1, 0);
335
+ rb_define_attr(cXMLSchema, "version", 1, 0);
268
336
 
269
- rb_define_method(cXMLSchema, "elements", rxml_schema_elements, 0);
270
- rb_define_method(cXMLSchema, "imported_types", rxml_schema_imported_types, 0);
337
+ // These are just methods so as to hide their values and not overly clutter the output of inspect/to_str
338
+ rb_define_method(cXMLSchema, "document", rxml_schema_document, 0);
271
339
  rb_define_method(cXMLSchema, "namespaces", rxml_schema_namespaces, 0);
340
+ rb_define_method(cXMLSchema, "elements", rxml_schema_elements, 0);
341
+ rb_define_method(cXMLSchema, "imported_ns_elements", rxml_schema_imported_ns_elements, 0);
272
342
  rb_define_method(cXMLSchema, "types", rxml_schema_types, 0);
343
+ rb_define_method(cXMLSchema, "imported_types", rxml_schema_imported_types, 0);
344
+ rb_define_method(cXMLSchema, "imported_ns_types", rxml_schema_imported_ns_types, 0);
273
345
 
274
346
  rxml_init_schema_facet();
275
347
  rxml_init_schema_element();
@@ -9,11 +9,8 @@ extern VALUE cXMLSchema;
9
9
 
10
10
  void rxml_init_schema(void);
11
11
 
12
- #define QNIL_OR_STRING(slot) \
13
- if (slot == NULL) \
14
- return Qnil; \
15
- else \
16
- return rb_str_new2((const char *)slot);
12
+ #define QNIL_OR_STRING(slot) \
13
+ (slot == NULL) ? Qnil : rb_str_new2((const char *)slot)
17
14
 
18
15
  #define SUBSET_RESTRICTION 1<<0
19
16
  #define SUBSET_EXTENSION 1<<1
@@ -12,62 +12,31 @@ static void rxml_schema_attribute_free(xmlSchemaAttributeUsePtr attr)
12
12
 
13
13
  VALUE rxml_wrap_schema_attribute(xmlSchemaAttributeUsePtr attr)
14
14
  {
15
- return Data_Wrap_Struct(cXMLSchemaAttribute, NULL, rxml_schema_attribute_free, attr);
16
- }
17
-
18
- static VALUE rxml_schema_attribute_namespace(VALUE self)
19
- {
20
- xmlSchemaAttributeUsePtr attr;
21
- const xmlChar *tns;
15
+ VALUE result;
16
+ const xmlChar *tns_str, *name_str;
22
17
 
23
- Data_Get_Struct(self, xmlSchemaAttributeUse, attr);
18
+ if (!attr)
19
+ rb_raise(rb_eArgError, "XML::Schema::Attribute required!");
24
20
 
25
- if (attr == NULL)
26
- return Qnil;
21
+ result = Data_Wrap_Struct(cXMLSchemaAttribute, NULL, rxml_schema_attribute_free, attr);
27
22
 
28
23
  if (attr->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
29
- tns = ((xmlSchemaAttributeUseProhibPtr) attr)->targetNamespace;
24
+ tns_str = ((xmlSchemaAttributeUseProhibPtr) attr)->targetNamespace;
25
+ name_str = ((xmlSchemaAttributeUseProhibPtr) attr)->name;
30
26
  } else if (attr->type == XML_SCHEMA_EXTRA_QNAMEREF) {
31
- tns = ((xmlSchemaQNameRefPtr) attr)->targetNamespace;
27
+ tns_str = ((xmlSchemaQNameRefPtr) attr)->targetNamespace;
28
+ name_str = ((xmlSchemaQNameRefPtr) attr)->name;
32
29
  } else {
33
- tns = ((xmlSchemaAttributePtr) ((xmlSchemaAttributeUsePtr) (attr))->attrDecl)->targetNamespace;
30
+ tns_str = ((xmlSchemaAttributePtr) (attr->attrDecl))->targetNamespace;
31
+ name_str = ((xmlSchemaAttributePtr) (attr->attrDecl))->name;
34
32
  }
33
+ rb_iv_set(result, "@target_namespace", QNIL_OR_STRING(tns_str));
34
+ rb_iv_set(result, "@name", QNIL_OR_STRING(name_str));
35
+ rb_iv_set(result, "@type", rxml_wrap_schema_type((xmlSchemaTypePtr)attr->attrDecl->subtypes));
36
+ rb_iv_set(result, "@value", QNIL_OR_STRING(attr->defValue));
37
+ rb_iv_set(result, "@occurs", INT2NUM(attr->occurs));
35
38
 
36
- QNIL_OR_STRING(tns)
37
- }
38
-
39
- static VALUE rxml_schema_attribute_name(VALUE self)
40
- {
41
- xmlSchemaAttributeUsePtr attr;
42
- const xmlChar *name;
43
-
44
- Data_Get_Struct(self, xmlSchemaAttributeUse, attr);
45
-
46
- if (attr == NULL)
47
- return Qnil;
48
-
49
- if (attr->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
50
- name = ((xmlSchemaAttributeUseProhibPtr) attr)->name;
51
- } else if (attr->type == XML_SCHEMA_EXTRA_QNAMEREF) {
52
- name = ((xmlSchemaQNameRefPtr) attr)->name;
53
- } else {
54
- xmlSchemaAttributePtr attrDecl = ((xmlSchemaAttributeUsePtr) attr)->attrDecl;
55
- name = attrDecl->name;
56
- }
57
-
58
- QNIL_OR_STRING(name)
59
- }
60
-
61
- static VALUE rxml_schema_attribute_type(VALUE self)
62
- {
63
- xmlSchemaAttributeUsePtr attr;
64
- xmlSchemaTypePtr xtype;
65
-
66
- Data_Get_Struct(self, xmlSchemaAttributeUse, attr);
67
-
68
- xtype = attr->attrDecl->subtypes;
69
-
70
- return rxml_wrap_schema_type((xmlSchemaTypePtr) xtype);
39
+ return result;
71
40
  }
72
41
 
73
42
  static VALUE rxml_schema_attribute_node(VALUE self)
@@ -79,31 +48,14 @@ static VALUE rxml_schema_attribute_node(VALUE self)
79
48
  return rxml_node_wrap(attr->node);
80
49
  }
81
50
 
82
- static VALUE rxml_schema_attribute_value(VALUE self)
83
- {
84
- xmlSchemaAttributeUsePtr attr;
85
-
86
- Data_Get_Struct(self, xmlSchemaAttributeUse, attr);
87
-
88
- QNIL_OR_STRING(attr->defValue)
89
- }
90
-
91
- static VALUE rxml_schema_attribute_occurs(VALUE self)
92
- {
93
- xmlSchemaAttributeUsePtr attr;
94
-
95
- Data_Get_Struct(self, xmlSchemaAttributeUse, attr);
96
-
97
- return INT2NUM(attr->occurs);
98
- }
99
-
100
51
  void rxml_init_schema_attribute(void)
101
52
  {
102
53
  cXMLSchemaAttribute = rb_define_class_under(cXMLSchema, "Attribute", rb_cObject);
103
- rb_define_method(cXMLSchemaAttribute, "namespace", rxml_schema_attribute_namespace, 0);
104
- rb_define_method(cXMLSchemaAttribute, "name", rxml_schema_attribute_name, 0);
105
- rb_define_method(cXMLSchemaAttribute, "type", rxml_schema_attribute_type, 0);
54
+ rb_define_attr(cXMLSchemaAttribute, "name", 1, 0);
55
+ rb_define_attr(cXMLSchemaAttribute, "type", 1, 0);
56
+ rb_define_attr(cXMLSchemaAttribute, "namespace", 1, 0);
57
+ rb_define_attr(cXMLSchemaAttribute, "value", 1, 0);
58
+ rb_define_attr(cXMLSchemaAttribute, "occurs", 1, 0);
59
+
106
60
  rb_define_method(cXMLSchemaAttribute, "node", rxml_schema_attribute_node, 0);
107
- rb_define_method(cXMLSchemaAttribute, "value", rxml_schema_attribute_value, 0);
108
- rb_define_method(cXMLSchemaAttribute, "occurs", rxml_schema_attribute_occurs, 0);
109
61
  }
@@ -10,40 +10,21 @@ static void rxml_schema_element_free(xmlSchemaElementPtr xschema_element)
10
10
  xmlFree(xschema_element);
11
11
  }
12
12
 
13
- VALUE rxml_wrap_schema_element(xmlSchemaElementPtr xelement)
13
+ VALUE rxml_wrap_schema_element(xmlSchemaElementPtr xelem)
14
14
  {
15
- return Data_Wrap_Struct(cXMLSchemaElement, NULL, rxml_schema_element_free, xelement);
16
- }
15
+ VALUE result;
17
16
 
18
- static VALUE rxml_schema_element_namespace(VALUE self)
19
- {
20
- xmlSchemaElementPtr xelem;
17
+ if (!xelem)
18
+ rb_raise(rb_eArgError, "XML::Schema::Element is required!");
21
19
 
22
- Data_Get_Struct(self, xmlSchemaElement, xelem);
20
+ result = Data_Wrap_Struct(cXMLSchemaElement, NULL, rxml_schema_element_free, xelem);
23
21
 
24
- QNIL_OR_STRING(xelem->targetNamespace)
25
- }
22
+ rb_iv_set(result, "@name", QNIL_OR_STRING(xelem->name));
23
+ rb_iv_set(result, "@value", QNIL_OR_STRING(xelem->value));
24
+ rb_iv_set(result, "@namespace", QNIL_OR_STRING(xelem->targetNamespace));
25
+ rb_iv_set(result, "@type", rxml_wrap_schema_type((xmlSchemaTypePtr) (xelem->subtypes)));
26
26
 
27
- static VALUE rxml_schema_element_name(VALUE self)
28
- {
29
- xmlSchemaElementPtr xelem;
30
-
31
- Data_Get_Struct(self, xmlSchemaElement, xelem);
32
-
33
-
34
- QNIL_OR_STRING(xelem->name)
35
- }
36
-
37
- static VALUE rxml_schema_element_type(VALUE self)
38
- {
39
- xmlSchemaElementPtr xelem;
40
- xmlSchemaTypePtr xtype;
41
-
42
- Data_Get_Struct(self, xmlSchemaElement, xelem);
43
-
44
- xtype = xelem->subtypes;
45
-
46
- return rxml_wrap_schema_type((xmlSchemaTypePtr) xtype);
27
+ return result;
47
28
  }
48
29
 
49
30
  static VALUE rxml_schema_element_node(VALUE self)
@@ -55,41 +36,34 @@ static VALUE rxml_schema_element_node(VALUE self)
55
36
  return rxml_node_wrap(xelem->node);
56
37
  }
57
38
 
58
- static VALUE rxml_schema_element_value(VALUE self)
39
+ static VALUE rxml_schema_element_annot(VALUE self)
59
40
  {
60
41
  xmlSchemaElementPtr xelem;
42
+ VALUE annotation = Qnil;
61
43
 
62
44
  Data_Get_Struct(self, xmlSchemaElement, xelem);
63
45
 
64
- QNIL_OR_STRING(xelem->value)
65
- }
66
-
67
- static VALUE rxml_schema_element_annot(VALUE self)
68
- {
69
- VALUE result = Qnil;
70
- xmlSchemaElementPtr xelem;
71
-
72
- Data_Get_Struct(self, xmlSchemaElement, xelem);
73
-
74
- if (xelem != NULL && xelem->annot != NULL && xelem->annot->content != NULL)
75
- {
76
- xmlChar *content = xmlNodeGetContent(xelem->annot->content);
77
- if (content)
78
- {
79
- result = rxml_new_cstr(content, NULL);
80
- xmlFree(content);
81
- }
82
- }
83
- return result;
46
+ if ((xelem->annot != NULL) && (xelem->annot->content != NULL))
47
+ {
48
+ xmlChar *content = xmlNodeGetContent(xelem->annot->content);
49
+ if (content)
50
+ {
51
+ annotation = rxml_new_cstr(content, NULL);
52
+ xmlFree(content);
53
+ }
54
+ }
55
+
56
+ return annotation;
84
57
  }
85
58
 
86
59
  void rxml_init_schema_element(void)
87
60
  {
88
61
  cXMLSchemaElement = rb_define_class_under(cXMLSchema, "Element", rb_cObject);
89
- rb_define_method(cXMLSchemaElement, "namespace", rxml_schema_element_namespace, 0);
90
- rb_define_method(cXMLSchemaElement, "name", rxml_schema_element_name, 0);
91
- rb_define_method(cXMLSchemaElement, "type", rxml_schema_element_type, 0);
62
+ rb_define_attr(cXMLSchemaElement, "name", 1, 0);
63
+ rb_define_attr(cXMLSchemaElement, "value", 1, 0);
64
+ rb_define_attr(cXMLSchemaElement, "namespace", 1, 0);
65
+ rb_define_attr(cXMLSchemaElement, "type", 1, 0);
66
+
92
67
  rb_define_method(cXMLSchemaElement, "node", rxml_schema_element_node, 0);
93
- rb_define_method(cXMLSchemaElement, "value", rxml_schema_element_value, 0);
94
68
  rb_define_method(cXMLSchemaElement, "annotation", rxml_schema_element_annot, 0);
95
69
  }
@@ -9,44 +9,38 @@ static void rxml_schema_facet_free(xmlSchemaFacetPtr xschema_type)
9
9
  xmlFree(xschema_type);
10
10
  }
11
11
 
12
- /* START FACET*/
13
-
14
- static VALUE rxml_schema_facet_node(VALUE self)
12
+ VALUE rxml_wrap_schema_facet(xmlSchemaFacetPtr facet)
15
13
  {
16
- xmlSchemaFacetPtr facet;
14
+ VALUE result;
17
15
 
18
- Data_Get_Struct(self, xmlSchemaFacet, facet);
16
+ if (!facet)
17
+ rb_raise(rb_eArgError, "XML::Schema::Facet required!");
19
18
 
20
- return rxml_node_wrap(facet->node);
21
- }
19
+ result = Data_Wrap_Struct(cXMLSchemaFacet, NULL, rxml_schema_facet_free, facet);
22
20
 
23
- static VALUE rxml_schema_facet_value(VALUE self)
24
- {
25
- xmlSchemaFacetPtr facet;
21
+ rb_iv_set(result, "@kind", INT2NUM(facet->type));
22
+ rb_iv_set(result, "@value", QNIL_OR_STRING(facet->value));
26
23
 
27
- Data_Get_Struct(self, xmlSchemaFacet, facet);
24
+ return result;
28
25
 
29
- QNIL_OR_STRING(facet->value)
30
26
  }
31
27
 
32
- static VALUE rxml_schema_facet_kind(VALUE self)
28
+ /* START FACET*/
29
+
30
+ static VALUE rxml_schema_facet_node(VALUE self)
33
31
  {
34
32
  xmlSchemaFacetPtr facet;
35
33
 
36
34
  Data_Get_Struct(self, xmlSchemaFacet, facet);
37
35
 
38
- return INT2NUM(facet->type);
39
- }
40
-
41
- VALUE rxml_wrap_schema_facet(xmlSchemaFacetPtr facet)
42
- {
43
- return Data_Wrap_Struct(cXMLSchemaFacet, NULL, rxml_schema_facet_free, facet);
36
+ return rxml_node_wrap(facet->node);
44
37
  }
45
38
 
46
39
  void rxml_init_schema_facet(void)
47
40
  {
48
41
  cXMLSchemaFacet = rb_define_class_under(cXMLSchema, "Facet", rb_cObject);
49
- rb_define_method(cXMLSchemaFacet, "value", rxml_schema_facet_value, 0);
42
+ rb_define_attr(cXMLSchemaFacet, "kind", 1, 0);
43
+ rb_define_attr(cXMLSchemaFacet, "value", 1, 0);
44
+
50
45
  rb_define_method(cXMLSchemaFacet, "node", rxml_schema_facet_node, 0);
51
- rb_define_method(cXMLSchemaFacet, "kind", rxml_schema_facet_kind, 0);
52
46
  }
@@ -17,34 +17,36 @@ static void rxml_schema_type_free(xmlSchemaTypePtr xschema_type)
17
17
 
18
18
  VALUE rxml_wrap_schema_type(xmlSchemaTypePtr xtype)
19
19
  {
20
- return Data_Wrap_Struct(cXMLSchemaType, NULL, rxml_schema_type_free, xtype);
21
- }
20
+ VALUE result;
22
21
 
23
- static VALUE rxml_schema_type_namespace(VALUE self)
24
- {
25
- xmlSchemaTypePtr xtype;
22
+ if (!xtype)
23
+ rb_raise(rb_eArgError, "XML::Schema::Type required!");
26
24
 
27
- Data_Get_Struct(self, xmlSchemaType, xtype);
25
+ result = Data_Wrap_Struct(cXMLSchemaType, NULL, rxml_schema_type_free, xtype);
28
26
 
29
- QNIL_OR_STRING(xtype->targetNamespace)
27
+ rb_iv_set(result, "@name", QNIL_OR_STRING(xtype->name));
28
+ rb_iv_set(result, "@namespace", QNIL_OR_STRING(xtype->targetNamespace));
29
+ rb_iv_set(result, "@kind", INT2NUM(xtype->type));
30
+
31
+ return result;
30
32
  }
31
33
 
32
- static VALUE rxml_schema_type_name(VALUE self)
34
+ static VALUE rxml_schema_type_base(VALUE self)
33
35
  {
34
36
  xmlSchemaTypePtr xtype;
35
37
 
36
38
  Data_Get_Struct(self, xmlSchemaType, xtype);
37
39
 
38
- QNIL_OR_STRING(xtype->name)
40
+ return (xtype->baseType != xtype) ? rxml_wrap_schema_type(xtype->baseType) : Qnil;
39
41
  }
40
42
 
41
- static VALUE rxml_schema_type_base(VALUE self)
43
+ static VALUE rxml_schema_type_node(VALUE self)
42
44
  {
43
45
  xmlSchemaTypePtr xtype;
44
46
 
45
47
  Data_Get_Struct(self, xmlSchemaType, xtype);
46
48
 
47
- return Data_Wrap_Struct(cXMLSchemaType, NULL, rxml_schema_type_free, xtype->baseType);
49
+ return (xtype->node != NULL) ? rxml_node_wrap(xtype->node) : Qnil;
48
50
  }
49
51
 
50
52
  static VALUE rxml_schema_type_facets(VALUE self)
@@ -68,27 +70,6 @@ static VALUE rxml_schema_type_facets(VALUE self)
68
70
  return result;
69
71
  }
70
72
 
71
- static VALUE rxml_schema_type_node(VALUE self)
72
- {
73
- xmlSchemaTypePtr xtype;
74
-
75
- Data_Get_Struct(self, xmlSchemaType, xtype);
76
-
77
- if(xtype->node != NULL)
78
- return rxml_node_wrap(xtype->node);
79
- else
80
- return Qnil;
81
- }
82
-
83
- static VALUE rxml_schema_type_kind(VALUE self)
84
- {
85
- xmlSchemaTypePtr xtype;
86
-
87
- Data_Get_Struct(self, xmlSchemaType, xtype);
88
-
89
- return INT2NUM(xtype->type);
90
- }
91
-
92
73
  static VALUE rxml_schema_type_annot(VALUE self)
93
74
  {
94
75
  VALUE result = Qnil;
@@ -220,13 +201,14 @@ void rxml_init_schema_type(void)
220
201
 
221
202
  cXMLSchemaType = rb_define_class_under(cXMLSchema, "Type", rb_cObject);
222
203
 
223
- rb_define_method(cXMLSchemaType, "namespace", rxml_schema_type_namespace, 0);
224
- rb_define_method(cXMLSchemaType, "name", rxml_schema_type_name, 0);
225
- rb_define_method(cXMLSchemaType, "elements", rxml_schema_type_elements, 0);
226
- rb_define_method(cXMLSchemaType, "attributes", rxml_schema_type_attributes, 0);
204
+ rb_define_attr(cXMLSchemaType, "namespace", 1, 0);
205
+ rb_define_attr(cXMLSchemaType, "name", 1, 0);
206
+ rb_define_attr(cXMLSchemaType, "kind", 1, 0);
207
+
227
208
  rb_define_method(cXMLSchemaType, "base", rxml_schema_type_base, 0);
228
- rb_define_method(cXMLSchemaType, "kind", rxml_schema_type_kind, 0);
229
209
  rb_define_method(cXMLSchemaType, "node", rxml_schema_type_node, 0);
210
+ rb_define_method(cXMLSchemaType, "elements", rxml_schema_type_elements, 0);
211
+ rb_define_method(cXMLSchemaType, "attributes", rxml_schema_type_attributes, 0);
230
212
  rb_define_method(cXMLSchemaType, "facets", rxml_schema_type_facets, 0);
231
213
  rb_define_method(cXMLSchemaType, "annotation", rxml_schema_type_annot, 0);
232
214
  }
@@ -1,9 +1,9 @@
1
1
  /* Don't nuke this block! It is used for automatically updating the
2
2
  * versions below. VERSION = string formatting, VERNUM = numbered
3
3
  * version for inline testing: increment both or none at all.*/
4
- #define RUBY_LIBXML_VERSION "3.2.2"
5
- #define RUBY_LIBXML_VERNUM 321
4
+ #define RUBY_LIBXML_VERSION "3.2.3"
5
+ #define RUBY_LIBXML_VERNUM 323
6
6
  #define RUBY_LIBXML_VER_MAJ 3
7
7
  #define RUBY_LIBXML_VER_MIN 2
8
- #define RUBY_LIBXML_VER_MIC 2
8
+ #define RUBY_LIBXML_VER_MIC 3
9
9
  #define RUBY_LIBXML_VER_PATCH 0
@@ -5,9 +5,9 @@
5
5
  #
6
6
  namespace xsi = "http://www.w3.org/2001/XMLSchema-instance"
7
7
 
8
- start = shiporder
8
+ start = shiporderType
9
9
 
10
- shiporder = element shiporder { attribute orderid { text },
10
+ shiporderType = element shiporder { attribute orderid { text },
11
11
  attribute xsi:noNamespaceSchemaLocation { text },
12
12
  orderperson, shipto, item* }
13
13
 
@@ -7,9 +7,9 @@
7
7
  -->
8
8
  <grammar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://relaxng.org/ns/structure/1.0">
9
9
  <start>
10
- <ref name="shiporder"/>
10
+ <ref name="shiporderType"/>
11
11
  </start>
12
- <define name="shiporder">
12
+ <define name="shiporderType">
13
13
  <element name="shiporder">
14
14
  <attribute name="orderid"/>
15
15
  <attribute name="xsi:noNamespaceSchemaLocation"/>
@@ -1,8 +1,12 @@
1
1
  <?xml version="1.0" encoding="iso-8859-1" ?>
2
2
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3
- <xs:element name="shiporder" type="shiporder"/>
4
3
 
5
- <xs:complexType name="shiporder">
4
+ <xs:import namespace="http://xml4r.org/ibxml-ruby/test/shiporder"
5
+ schemaLocation="shiporder_import.xsd"/>
6
+
7
+ <xs:element name="shiporder" type="shiporderType"/>
8
+
9
+ <xs:complexType name="shiporderType">
6
10
  <xs:annotation>
7
11
  <xs:documentation>Shiporder type documentation</xs:documentation>
8
12
  </xs:annotation>
@@ -37,4 +41,4 @@
37
41
  <xs:attribute name="foo" default="1" type="xs:integer" use="optional"/>
38
42
  <xs:attribute name="bar" use="prohibited"/>
39
43
  </xs:complexType>
40
- </xs:schema>
44
+ </xs:schema>
@@ -0,0 +1,40 @@
1
+ <?xml version="1.0" encoding="iso-8859-1" ?>
2
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3
+ <xs:element name="shiporder" type="shiporderType"/>
4
+
5
+ <xs:complexType name="shiporderType">
6
+ <xs:anntation>
7
+ <xs:documentation>Shiporder type documentation</xs:documentation>
8
+ </xs:anntation>
9
+ <xs:sequence>
10
+ <xs:element name="orderperson" type="xs:string">
11
+ <xs:annotation>
12
+ <xs:documentation>orderperson element documentation</xs:documentation>
13
+ </xs:annotation>
14
+ </xs:element>
15
+ <xs:element name="shipto">
16
+ <xs:complexType>
17
+ <xs:sequence>
18
+ <xs:element name="name" type="xs:string"/>
19
+ <xs:element name="address" type="xs:string"/>
20
+ <xs:element name="city" type="xs:string"/>
21
+ <xs:element name="country" type="xs:string"/>
22
+ </xs:sequence>
23
+ </xs:complexType>
24
+ </xs:element>
25
+ <xs:element name="item" maxOccurs="unbounded">
26
+ <xs:complexType>
27
+ <xs:seq>
28
+ <xs:element name="title" type="xs:string"/>
29
+ <xs:element name="note" type="xs:string" minOccurs="0"/>
30
+ <xs:element name="quantity" type="xs:positiveInteger"/>
31
+ <xs:element name="price" type="xs:decimal"/>
32
+ </xs:seq>
33
+ </xs:complexType>
34
+ </xs:element>
35
+ </xs:sequence>
36
+ <xs:attribute name="orderid" type="xs:string" use="required"/>
37
+ <xs:attribute name="foo" default="1" type="xs:integer" use="optional"/>
38
+ <xs:attribute name="bar" use="prohibited"/>
39
+ </xs:complexType>
40
+ </xs:schema>
@@ -0,0 +1,45 @@
1
+ <?xml version="1.0" encoding="iso-8859-1" ?>
2
+ <xs:schema xmlns="http://xml4r.org/libxml-ruby/test/shiporder"
3
+ xmlns:xs="http://www.w3.org/2001/XMLSchema"
4
+ targetNamespace="http://xml4r.org/libxml-ruby/test/shiporder">
5
+
6
+ <xs:element name="shiporder" type="shiporderFooType"/>
7
+ <xs:complexType name="shiporderFooType">
8
+ <xs:annotation>
9
+ <xs:documentation>Shiporder type documentation for Testing of imported schema</xs:documentation>
10
+ </xs:annotation>
11
+ <xs:sequence>
12
+ <xs:element name="orderperson" type="xs:string">
13
+ <xs:annotation>
14
+ <xs:documentation>orderperson element documentation</xs:documentation>
15
+ </xs:annotation>
16
+ </xs:element>
17
+ <xs:element name="shipto">
18
+ <xs:complexType>
19
+ <xs:sequence>
20
+ <xs:element name="name" type="xs:string"/>
21
+ <xs:element name="address" type="xs:string"/>
22
+ <xs:element name="city" type="xs:string"/>
23
+ <xs:element name="country" type="xs:string"/>
24
+ <xs:element name="phone" type="xs:string"/>
25
+ </xs:sequence>
26
+ </xs:complexType>
27
+ </xs:element>
28
+ <xs:element name="item" maxOccurs="unbounded">
29
+ <xs:complexType>
30
+ <xs:sequence>
31
+ <xs:element name="title" type="xs:string"/>
32
+ <xs:element name="note" type="xs:string" minOccurs="0"/>
33
+ <xs:element name="quantity" type="xs:positiveInteger"/>
34
+ <xs:element name="price" type="xs:decimal"/>
35
+ <xs:element name="discount" type="xs:decimal"/>
36
+ </xs:sequence>
37
+ </xs:complexType>
38
+ </xs:element>
39
+ </xs:sequence>
40
+ <xs:attribute name="orderid" type="xs:string" use="required"/>
41
+ <xs:attribute name="foo" default="1" type="xs:integer" use="optional"/>
42
+ <xs:attribute name="bar" type="xs:string" use="optional"/>
43
+ <xs:attribute name="xyzzy" type="xs:string" use="prohibited"/>
44
+ </xs:complexType>
45
+ </xs:schema>
data/test/test_schema.rb CHANGED
@@ -3,18 +3,19 @@
3
3
  require_relative './test_helper'
4
4
 
5
5
  class TestSchema < Minitest::Test
6
+ Import_NS = 'http://xml4r.org/libxml-ruby/test/shiporder'.freeze
7
+
6
8
  def setup
7
9
  file = File.join(File.dirname(__FILE__), 'model/shiporder.xml')
8
10
  @doc = LibXML::XML::Document.file(file)
11
+ schema_file = File.join(File.dirname(__FILE__), 'model/shiporder.xsd')
12
+ schema_doc = LibXML::XML::Document.file(schema_file)
13
+ @schema = LibXML::XML::Schema.document(schema_doc)
9
14
  end
10
15
 
11
16
  def teardown
12
17
  @doc = nil
13
- end
14
-
15
- def schema
16
- document = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/shiporder.xsd'))
17
- LibXML::XML::Schema.document(document)
18
+ @schema = nil
18
19
  end
19
20
 
20
21
  def check_error(error)
@@ -33,11 +34,50 @@ class TestSchema < Minitest::Test
33
34
  end
34
35
 
35
36
  def test_load_from_doc
36
- assert_instance_of(LibXML::XML::Schema, schema)
37
+ assert_instance_of(LibXML::XML::Schema, @schema)
38
+ end
39
+
40
+ def test_schema_load_from_uri
41
+ xlink_schema = LibXML::XML::Schema.new('http://www.w3.org/1999/xlink.xsd')
42
+ assert_instance_of(LibXML::XML::Schema, xlink_schema)
43
+ assert_instance_of(LibXML::XML::Schema::Element, xlink_schema.elements['title'])
44
+ assert_instance_of(LibXML::XML::Schema::Type, xlink_schema.types['titleEltType'])
45
+ assert_equal('http://www.w3.org/1999/xlink', xlink_schema.target_namespace)
46
+ end
47
+
48
+ def test_schema_from_string
49
+ schema_file = File.join(File.dirname(__FILE__), 'model/shiporder.xsd')
50
+ schema_from_str = LibXML::XML::Schema.from_string(File.read(schema_file))
51
+ assert_instance_of(LibXML::XML::Schema, schema_from_str)
52
+ end
53
+
54
+ def test_invalid_schema_doc
55
+ bad_schema_file = File.join(File.dirname(__FILE__), 'model/shiporder_bad.xsd')
56
+ bad_schema_doc = LibXML::XML::Document.file(bad_schema_file)
57
+ bad_schema = nil
58
+ ## Note: this type of error throws
59
+ begin
60
+ bad_schema = LibXML::XML::Schema.document(bad_schema_doc)
61
+ rescue LibXML::XML::Error => error
62
+ bad_schema = error
63
+ assert(error.message.match(/Error: Element '.*': The content is not valid. Expected is \(/))
64
+ assert_kind_of(LibXML::XML::Error, error)
65
+ assert_equal(LibXML::XML::Error::SCHEMASP, error.domain)
66
+ assert_equal(LibXML::XML::Error::SCHEMAP_S4S_ELEM_NOT_ALLOWED, error.code)
67
+ assert_equal(LibXML::XML::Error::ERROR, error.level)
68
+ assert(error.file.match(/shiporder_bad.xsd/))
69
+ assert_equal("Element '{http://www.w3.org/2001/XMLSchema}complexType'", error.str1)
70
+ refute_nil(error.str2)
71
+ assert_nil(error.str3)
72
+ assert_equal(0, error.int1)
73
+ assert_equal(0, error.int2)
74
+ end
75
+ ## Last but not least, make sure we threw an error
76
+ assert_instance_of(LibXML::XML::Error, bad_schema)
37
77
  end
38
78
 
39
79
  def test_doc_valid
40
- assert(@doc.validate_schema(schema))
80
+ assert(@doc.validate_schema(@schema))
41
81
  end
42
82
 
43
83
  def test_doc_invalid
@@ -45,7 +85,7 @@ class TestSchema < Minitest::Test
45
85
  @doc.root << new_node
46
86
 
47
87
  error = assert_raises(LibXML::XML::Error) do
48
- @doc.validate_schema(schema)
88
+ @doc.validate_schema(@schema)
49
89
  end
50
90
 
51
91
  check_error(error)
@@ -56,7 +96,7 @@ class TestSchema < Minitest::Test
56
96
 
57
97
  def test_reader_valid
58
98
  reader = LibXML::XML::Reader.string(@doc.to_s)
59
- assert(reader.schema_validate(schema))
99
+ assert(reader.schema_validate(@schema))
60
100
 
61
101
  while reader.read
62
102
  end
@@ -74,7 +114,7 @@ class TestSchema < Minitest::Test
74
114
  reader = LibXML::XML::Reader.string(@doc.to_s)
75
115
 
76
116
  # Set a schema
77
- assert(reader.schema_validate(schema))
117
+ assert(reader.schema_validate(@schema))
78
118
 
79
119
  while reader.read
80
120
  end
@@ -91,32 +131,55 @@ class TestSchema < Minitest::Test
91
131
 
92
132
  # Schema meta-data tests
93
133
  def test_elements
94
- assert_instance_of(Hash, schema.elements)
95
- assert_equal(1, schema.elements.length)
96
- assert_instance_of(LibXML::XML::Schema::Element, schema.elements['shiporder'])
134
+ assert_instance_of(Hash, @schema.elements)
135
+ assert_equal(1, @schema.elements.length)
136
+ assert_instance_of(LibXML::XML::Schema::Element, @schema.elements['shiporder'])
97
137
  end
98
138
 
99
139
  def test_types
100
- assert_instance_of(Hash, schema.types)
101
- assert_equal(1, schema.types.length)
102
- assert_instance_of(LibXML::XML::Schema::Type, schema.types['shiporder'])
140
+ assert_instance_of(Hash, @schema.types)
141
+ assert_equal(1, @schema.types.length)
142
+ assert_instance_of(LibXML::XML::Schema::Type, @schema.types['shiporderType'])
103
143
  end
104
144
 
105
145
  def test_imported_types
106
- assert_instance_of(Hash, schema.imported_types)
107
- assert_equal(1, schema.imported_types.length)
108
- assert_instance_of(LibXML::XML::Schema::Type, schema.types['shiporder'])
146
+ assert_instance_of(Hash, @schema.imported_types)
147
+ assert_equal(2, @schema.imported_types.length)
148
+ assert_instance_of(LibXML::XML::Schema::Type, @schema.imported_types['shiporderType'])
149
+ assert_instance_of(LibXML::XML::Schema::Type, @schema.imported_types['shiporderFooType'])
150
+ end
151
+
152
+ def test_imported_ns_types
153
+ assert_instance_of(Hash, @schema.imported_ns_types)
154
+ assert_equal(2, @schema.imported_ns_types.length)
155
+ assert_equal(1, @schema.imported_ns_types[nil].length)
156
+ assert_equal(1, @schema.imported_ns_types[Import_NS].length)
157
+ assert_instance_of(LibXML::XML::Schema::Type, @schema.imported_ns_types[nil]['shiporderType'])
158
+ assert_instance_of(LibXML::XML::Schema::Type, @schema.imported_ns_types[Import_NS]['shiporderFooType'])
159
+ end
160
+
161
+ def test_imported_ns_elements
162
+ assert_instance_of(Hash, @schema.imported_ns_elements)
163
+ assert_equal(2, @schema.imported_ns_elements.length)
164
+ assert_equal(1, @schema.imported_ns_elements[nil].length)
165
+ assert_equal(1, @schema.imported_ns_elements[Import_NS].length)
166
+ assert_instance_of(LibXML::XML::Schema::Element, @schema.imported_ns_elements[nil]['shiporder'])
167
+ assert_instance_of(LibXML::XML::Schema::Element, @schema.imported_ns_elements[Import_NS]['shiporder'])
168
+ assert_equal('shiporderType', @schema.imported_ns_elements[nil]['shiporder'].type.name)
169
+ assert_equal('shiporderFooType', @schema.imported_ns_elements[Import_NS]['shiporder'].type.name)
109
170
  end
110
171
 
111
172
  def test_namespaces
112
- assert_instance_of(Array, schema.namespaces)
113
- assert_equal(1, schema.namespaces.length)
173
+ assert_instance_of(Array, @schema.namespaces)
174
+ ## For some reason, when importing another schema we end up with two xs->http://www.w3.org/2001/XMLSchema namespaces.
175
+ ## So, we expect 3 here (one for Import_NS and then two for the schema namespace.
176
+ assert_equal(3, @schema.namespaces.length)
114
177
  end
115
178
 
116
179
  def test_schema_type
117
- type = schema.types['shiporder']
180
+ type = @schema.types['shiporderType']
118
181
 
119
- assert_equal('shiporder', type.name)
182
+ assert_equal('shiporderType', type.name)
120
183
  assert_nil(type.namespace)
121
184
  assert_equal("Shiporder type documentation", type.annotation)
122
185
  assert_instance_of(LibXML::XML::Node, type.node)
@@ -130,22 +193,22 @@ class TestSchema < Minitest::Test
130
193
  end
131
194
 
132
195
  def test_schema_element
133
- element = schema.types['shiporder'].elements['orderperson']
196
+ element = @schema.types['shiporderType'].elements['orderperson']
134
197
 
135
198
  assert_equal('orderperson', element.name)
136
199
  assert_nil(element.namespace)
137
200
  assert_equal("orderperson element documentation", element.annotation)
138
201
 
139
- element = schema.types['shiporder'].elements['item']
202
+ element = @schema.types['shiporderType'].elements['item']
140
203
  assert_equal('item', element.name)
141
204
 
142
- element = schema.types['shiporder'].elements['item'].type.elements['note']
205
+ element = @schema.types['shiporderType'].elements['item'].type.elements['note']
143
206
  assert_equal('note', element.name)
144
207
  assert_equal('string', element.type.name)
145
208
  end
146
209
 
147
210
  def test_schema_attributes
148
- type = schema.types['shiporder']
211
+ type = @schema.types['shiporderType']
149
212
 
150
213
  assert_instance_of(Array, type.attributes)
151
214
  assert_equal(2, type.attributes.length)
@@ -153,14 +216,14 @@ class TestSchema < Minitest::Test
153
216
  end
154
217
 
155
218
  def test_schema_attribute
156
- attribute = schema.types['shiporder'].attributes.first
219
+ attribute = @schema.types['shiporderType'].attributes.first
157
220
 
158
221
  assert_equal("orderid", attribute.name)
159
222
  assert_nil(attribute.namespace)
160
223
  assert_equal(1, attribute.occurs)
161
224
  assert_equal('string', attribute.type.name)
162
225
 
163
- attribute = schema.types['shiporder'].attributes[1]
226
+ attribute = @schema.types['shiporderType'].attributes[1]
164
227
  assert_equal(2, attribute.occurs)
165
228
  assert_equal('1', attribute.default)
166
229
  assert_equal('integer', attribute.type.name)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libxml-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.2
4
+ version: 3.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ross Bamform
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2022-01-15 00:00:00.000000000 Z
17
+ date: 2022-05-22 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: rake-compiler
@@ -215,6 +215,8 @@ files:
215
215
  - test/model/shiporder.rng
216
216
  - test/model/shiporder.xml
217
217
  - test/model/shiporder.xsd
218
+ - test/model/shiporder_bad.xsd
219
+ - test/model/shiporder_import.xsd
218
220
  - test/model/soap.xml
219
221
  - test/model/xinclude.xml
220
222
  - test/test_attr.rb