moxml 0.6.2 → 0.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bench/calibrate_520.rb +89 -0
- data/docs/_pages/divergences.adoc +31 -0
- data/lib/moxml/adapter/leptris.rb +444 -34
- data/lib/moxml/config.rb +43 -10
- data/lib/moxml/context.rb +31 -9
- data/lib/moxml/version.rb +1 -1
- data/lib/moxml/xsd.rb +436 -2
- data/spec/moxml/adapter/leptris_attribute_packet_spec.rb +79 -0
- data/spec/moxml/adapter/leptris_children_address_spec.rb +171 -0
- data/spec/moxml/adapter/leptris_composed_schema_spec.rb +126 -0
- data/spec/moxml/adapter/leptris_spec.rb +0 -6
- data/spec/moxml/config_degradation_spec.rb +84 -12
- data/spec/moxml/xsd_composed_spec.rb +183 -0
- metadata +7 -3
- data/spec/moxml/adapter/leptris_children_columnar_spec.rb +0 -146
data/lib/moxml/config.rb
CHANGED
|
@@ -62,30 +62,63 @@ module Moxml
|
|
|
62
62
|
detect_loaded_adapter || FALLBACK_ADAPTER
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
+
# Clears the memoized adapter-selection state (capability-probe
|
|
66
|
+
# verdict, resolved default name, default Config instance).
|
|
67
|
+
# Call after repointing default_adapter= at runtime; tests
|
|
68
|
+
# exercising selection from cold use it too (#523).
|
|
69
|
+
def reset_adapter_selection!
|
|
70
|
+
@leptris_preferred_available = nil
|
|
71
|
+
@default_adapter = nil
|
|
72
|
+
@default = nil
|
|
73
|
+
end
|
|
74
|
+
|
|
65
75
|
# True when the leptris gem is installed AND meets the adapter's
|
|
66
|
-
# binding floor (issue #149
|
|
67
|
-
#
|
|
68
|
-
#
|
|
69
|
-
#
|
|
70
|
-
#
|
|
76
|
+
# binding floor (issue #149) AND answers a one-shot native
|
|
77
|
+
# capability probe (moxml#523). Memoized: the probe runs once.
|
|
78
|
+
# Below the floor, or when the engine's native build fails at
|
|
79
|
+
# runtime (Ruby 4.0's ABI: every plan build answers status=-5,
|
|
80
|
+
# leptris#1966 — the poison-process analog of the yeptris gate,
|
|
81
|
+
# lutaml-model#978), the default falls back to Nokogiri rather
|
|
82
|
+
# than driving a lane that breaks on first serialize.
|
|
71
83
|
def leptris_preferred_available?
|
|
72
|
-
return @leptris_preferred_available
|
|
84
|
+
return @leptris_preferred_available unless @leptris_preferred_available.nil?
|
|
73
85
|
|
|
74
86
|
@leptris_preferred_available =
|
|
75
87
|
begin
|
|
76
88
|
require "leptris"
|
|
77
89
|
Moxml::Adapter.load(:leptris)
|
|
78
|
-
Gem::Version.new(::Leptris::VERSION) >=
|
|
79
|
-
|
|
90
|
+
if Gem::Version.new(::Leptris::VERSION) >=
|
|
91
|
+
Gem::Version.new(Moxml::Adapter::Leptris::MINIMUM_BINDING_VERSION)
|
|
92
|
+
leptris_native_probe
|
|
93
|
+
else
|
|
94
|
+
false
|
|
95
|
+
end
|
|
80
96
|
rescue LoadError
|
|
81
97
|
# Not installed: a normal deployment shape, silent.
|
|
82
98
|
false
|
|
83
|
-
rescue
|
|
84
|
-
# Installed but unusable
|
|
99
|
+
rescue StandardError
|
|
100
|
+
# Installed but unusable — adapter load failure (NameError,
|
|
101
|
+
# AdapterError; #384) or the native probe answering with a
|
|
102
|
+
# failure status (Ruby 4.0 plan builds, leptris#1966): loud.
|
|
85
103
|
:broken
|
|
86
104
|
end
|
|
87
105
|
end
|
|
88
106
|
|
|
107
|
+
# One-shot native capability probe (moxml#523): parse + serialize
|
|
108
|
+
# a tiny document through the leptris lane — the smallest
|
|
109
|
+
# faithful walk of the plan-build path whose status=-5 failure
|
|
110
|
+
# poisons the whole lane under broken-ABI rubies (leptris#1966).
|
|
111
|
+
# The Config carries every knob explicitly so nothing here
|
|
112
|
+
# consults Config.default, which would re-enter selection
|
|
113
|
+
# (probe → default → probe). Runs once per process; the caller
|
|
114
|
+
# memoizes the outcome.
|
|
115
|
+
def leptris_native_probe
|
|
116
|
+
config = new(PREFERRED_ADAPTER, true, "UTF-8")
|
|
117
|
+
Moxml::Context.new(config)
|
|
118
|
+
.parse("<r><a t=\"1\">x</a></r>").to_xml
|
|
119
|
+
true
|
|
120
|
+
end
|
|
121
|
+
|
|
89
122
|
def detect_loaded_adapter
|
|
90
123
|
return :nokogiri if Object.const_defined?(:Nokogiri)
|
|
91
124
|
return :ox if Object.const_defined?(:Ox)
|
data/lib/moxml/context.rb
CHANGED
|
@@ -26,8 +26,11 @@ module Moxml
|
|
|
26
26
|
attr_reader :config
|
|
27
27
|
|
|
28
28
|
def initialize(adapter = nil)
|
|
29
|
-
Adapter.warn_deprecated!(adapter) if adapter
|
|
30
|
-
|
|
29
|
+
Adapter.warn_deprecated!(adapter) if adapter && !adapter.is_a?(Config)
|
|
30
|
+
# A Config instance is accepted as-is (moxml#523): the probe
|
|
31
|
+
# builds a fully-specified Config so selection is never
|
|
32
|
+
# re-entered through Config.default.
|
|
33
|
+
@config = adapter.is_a?(Config) ? adapter : Config.new(adapter)
|
|
31
34
|
# Bumped on any namespace-scope mutation; Element scope caches
|
|
32
35
|
# compare against it so every wrapper sees changes without
|
|
33
36
|
# needing wrapper identity.
|
|
@@ -205,23 +208,42 @@ module Moxml
|
|
|
205
208
|
# Moxml::NotImplementedError; Moxml::XSD.supported?(self)
|
|
206
209
|
# answers without raising.
|
|
207
210
|
#
|
|
211
|
+
# Composing sources — xs:include / xs:redefine / xs:override
|
|
212
|
+
# (moxml#522) — are REFUSED here with actionable guidance: the
|
|
213
|
+
# bare-string face cannot resolve their schemaLocation
|
|
214
|
+
# attributes, so the compile silently misses every included
|
|
215
|
+
# component and validation fails with misleading "not declared"
|
|
216
|
+
# findings. Use #xsd_file (the engine resolves schemaLocation
|
|
217
|
+
# against the file's directory) or #xsd_set (imports resolve
|
|
218
|
+
# across the whole set).
|
|
219
|
+
#
|
|
208
220
|
# @raise [Moxml::NotImplementedError] on adapters without a validator
|
|
221
|
+
# @raise [ArgumentError] on a composing source (#522)
|
|
222
|
+
COMPOSITION_DIRECTIVE = /<(?:[\w.-]+:)?(?:include|redefine|override)[\s>]/
|
|
223
|
+
|
|
209
224
|
def xsd(schema_source)
|
|
210
|
-
|
|
211
|
-
|
|
225
|
+
source = schema_source.to_s
|
|
226
|
+
if source.match?(COMPOSITION_DIRECTIVE)
|
|
227
|
+
raise ArgumentError,
|
|
228
|
+
"the schema composes other documents (include/redefine/" \
|
|
229
|
+
"override); the bare-string face cannot resolve their " \
|
|
230
|
+
"schemaLocation — use Context#xsd_file (per-file include " \
|
|
231
|
+
"resolution) or Context#xsd_set (multi-document compile)"
|
|
232
|
+
end
|
|
233
|
+
handle = config.adapter.xsd_compile(source)
|
|
234
|
+
Moxml::XSD::Schema.new(handle, self, source)
|
|
212
235
|
end
|
|
213
236
|
|
|
214
237
|
# Compile an XSD schema from a file path — <xs:include href>
|
|
215
238
|
# resolves relative to the file's directory (the engine
|
|
216
239
|
# resolves it; reading the file into #xsd would lose that).
|
|
217
|
-
# The
|
|
218
|
-
#
|
|
219
|
-
#
|
|
220
|
-
# engine-validated and the index can only under-report.
|
|
240
|
+
# The path is kept on the Schema (moxml#522): #validate resolves
|
|
241
|
+
# the instance's xsi:schemaLocation hints against it and unions
|
|
242
|
+
# the hinted documents into the compiled set.
|
|
221
243
|
def xsd_file(path)
|
|
222
244
|
handle = config.adapter.xsd_compile_file(path.to_s)
|
|
223
245
|
source = File.read(path.to_s)
|
|
224
|
-
Moxml::XSD::Schema.new(handle, self, source)
|
|
246
|
+
Moxml::XSD::Schema.new(handle, self, source, path: path.to_s)
|
|
225
247
|
end
|
|
226
248
|
|
|
227
249
|
# Compile a SET of schema documents (paths) into one schema:
|
data/lib/moxml/version.rb
CHANGED
data/lib/moxml/xsd.rb
CHANGED
|
@@ -97,9 +97,10 @@ module Moxml
|
|
|
97
97
|
end
|
|
98
98
|
|
|
99
99
|
def initialize(handle, context, schema_source = nil,
|
|
100
|
-
self_contained: nil)
|
|
100
|
+
self_contained: nil, path: nil)
|
|
101
101
|
@handle = handle
|
|
102
102
|
@context = context
|
|
103
|
+
@schema_path = path
|
|
103
104
|
@rules_index =
|
|
104
105
|
if schema_source
|
|
105
106
|
SchemaRules.index(schema_source, context,
|
|
@@ -157,8 +158,9 @@ module Moxml
|
|
|
157
158
|
end
|
|
158
159
|
end
|
|
159
160
|
engine_errors = @context.config.adapter.xsd_validate(
|
|
160
|
-
|
|
161
|
+
validation_handle(doc), doc.native
|
|
161
162
|
)
|
|
163
|
+
engine_errors = dismiss_refuted_not_declared(engine_errors, doc)
|
|
162
164
|
return engine_errors unless @rules_index
|
|
163
165
|
|
|
164
166
|
# The #452 post-validation rules only ever add findings for
|
|
@@ -170,6 +172,438 @@ module Moxml
|
|
|
170
172
|
SchemaRules.findings(doc, @rules_index)).uniq
|
|
171
173
|
end
|
|
172
174
|
|
|
175
|
+
# Validations run against the compile-time handle. The
|
|
176
|
+
# composition fix (#522) is dismissal-side: the facade indexes
|
|
177
|
+
# every unit of the composition (the schema + the instance's
|
|
178
|
+
# xsi:schemaLocation hints) and drops engine strict-admission
|
|
179
|
+
# rows the index refutes — engines whose admission consults
|
|
180
|
+
# only the head unit's declarations never see the aux units,
|
|
181
|
+
# and recompiling a mixed-targetNamespace union would swap one
|
|
182
|
+
# false answer for another.
|
|
183
|
+
def validation_handle(_doc)
|
|
184
|
+
@handle
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
XSI_NS = "http://www.w3.org/2001/XMLSchema-instance"
|
|
188
|
+
|
|
189
|
+
# xsi:schemaLocation (namespace/path pairs) and
|
|
190
|
+
# xsi:noNamespaceSchemaLocation paths from the validated
|
|
191
|
+
# instance's root, resolved against this schema's directory
|
|
192
|
+
# and filtered to files that exist. Empty when this schema was
|
|
193
|
+
# compiled from a string (no base to resolve against).
|
|
194
|
+
def instance_schema_hints(doc)
|
|
195
|
+
return [].freeze unless @schema_path
|
|
196
|
+
return [].freeze unless doc&.root
|
|
197
|
+
|
|
198
|
+
base = File.dirname(@schema_path)
|
|
199
|
+
root = doc.root
|
|
200
|
+
hints = []
|
|
201
|
+
root.attributes.each do |attribute|
|
|
202
|
+
next unless attribute.namespace&.uri == XSI_NS
|
|
203
|
+
|
|
204
|
+
value = attribute.value.to_s
|
|
205
|
+
case attribute.name
|
|
206
|
+
when "noNamespaceSchemaLocation"
|
|
207
|
+
hints << value
|
|
208
|
+
when "schemaLocation"
|
|
209
|
+
hints.concat(value.split(/\s+/).reject(&:empty?)
|
|
210
|
+
.each_slice(2).map(&:last))
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
hints.uniq
|
|
214
|
+
.map { |hint| File.expand_path(hint, base) }
|
|
215
|
+
.select do |p|
|
|
216
|
+
File.file?(p) && p != File.expand_path(@schema_path)
|
|
217
|
+
end.freeze
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# Engine strict-admission rows the composed unit set REFUTES:
|
|
221
|
+
# engines whose wildcard admission consults only the head
|
|
222
|
+
# unit's declaration table report "attribute 'x': not declared
|
|
223
|
+
# (strict)" for attributes declared in another unit of the
|
|
224
|
+
# same compiled composition, and the mixed-tns fresh union
|
|
225
|
+
# compile itself can mis-admit a root element the same way
|
|
226
|
+
# (leptris#1964 family, moxml#522). The facade owns the
|
|
227
|
+
# composition, so it can refute a row when the named item, as
|
|
228
|
+
# it appears in THIS instance, is a global declaration of any
|
|
229
|
+
# unit (namespace + local name). Only exact "not declared
|
|
230
|
+
# (strict)" rows are ever dropped; every other engine finding
|
|
231
|
+
# passes through untouched.
|
|
232
|
+
def dismiss_refuted_not_declared(engine_errors, doc)
|
|
233
|
+
return engine_errors if engine_errors.empty? || !@schema_path
|
|
234
|
+
|
|
235
|
+
hints = instance_schema_hints(doc)
|
|
236
|
+
units = composed_unit_set([@schema_path, *hints])
|
|
237
|
+
declared = composed_declarations(units)
|
|
238
|
+
admissible = element_admissibility(units)
|
|
239
|
+
return engine_errors if declared.empty? && admissible.empty?
|
|
240
|
+
|
|
241
|
+
attr_index = {}
|
|
242
|
+
element_ns = {}
|
|
243
|
+
stack = [doc.root]
|
|
244
|
+
while (element = stack.pop)
|
|
245
|
+
element_ns[(element.namespace&.uri || "")] ||= true
|
|
246
|
+
stack.concat(element.children.grep(Moxml::Element))
|
|
247
|
+
element.attributes.each do |attribute|
|
|
248
|
+
attr_uri = attribute.namespace&.uri || ""
|
|
249
|
+
((attr_index[[attr_uri, attribute.name]] ||= []) <<
|
|
250
|
+
element.name)
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
engine_errors.reject do |finding|
|
|
254
|
+
match = NOT_DECLARED_ROW.match(finding.message)
|
|
255
|
+
next false unless match
|
|
256
|
+
|
|
257
|
+
element_name = match[1]
|
|
258
|
+
attribute_name = match[2]
|
|
259
|
+
if attribute_name
|
|
260
|
+
attr_index.any? do |(uri, attr_name), holders|
|
|
261
|
+
next false unless attr_name == attribute_name &&
|
|
262
|
+
holders.include?(element_name)
|
|
263
|
+
|
|
264
|
+
# A global declaration of some unit, or a group member
|
|
265
|
+
# the element's type admits (moxml#522 attgC class —
|
|
266
|
+
# exact (element, attribute, namespace) match).
|
|
267
|
+
declared.key?([uri, attribute_name]) ||
|
|
268
|
+
admissible.dig(element_name, attribute_name) == uri
|
|
269
|
+
end
|
|
270
|
+
else
|
|
271
|
+
declared.key?(["", element_name]) ||
|
|
272
|
+
(element_ns[element_name] &&
|
|
273
|
+
declared.key?([element_ns[element_name], element_name]))
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# The composition closure of the unit set: every unit's
|
|
279
|
+
# xs:include / xs:import / xs:redefine schemaLocation targets
|
|
280
|
+
# (existing files), cycle-guarded, resolved relative to each
|
|
281
|
+
# unit. The engine compiles these documents as ONE composition;
|
|
282
|
+
# the facade's index must see the same set even when the
|
|
283
|
+
# instance carries no xsi hints (attgC035's redefined groups
|
|
284
|
+
# live in documents only the schema graph knows).
|
|
285
|
+
def composed_unit_set(units)
|
|
286
|
+
@composed_unit_set ||= {}
|
|
287
|
+
cached = @composed_unit_set[units]
|
|
288
|
+
return cached if cached
|
|
289
|
+
|
|
290
|
+
seen = {}
|
|
291
|
+
queue = units.dup
|
|
292
|
+
until queue.empty?
|
|
293
|
+
unit = queue.pop
|
|
294
|
+
next if seen.key?(unit)
|
|
295
|
+
|
|
296
|
+
seen[unit] = true
|
|
297
|
+
base = File.dirname(unit)
|
|
298
|
+
unit_schema_refs(unit).each do |ref|
|
|
299
|
+
target = File.expand_path(ref, base)
|
|
300
|
+
queue << target if File.file?(target) && !seen.key?(target)
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
closure = seen.keys.sort
|
|
304
|
+
@composed_unit_set[units] = closure
|
|
305
|
+
closure
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
# schemaLocation hrefs on the unit's own xs:include, xs:import
|
|
309
|
+
# and xs:redefine (poison-immune REXML read; the engine's
|
|
310
|
+
# component tables are process-global and a later parse can
|
|
311
|
+
# poison compiled handles, moxml#522).
|
|
312
|
+
def unit_schema_refs(unit)
|
|
313
|
+
require "rexml/document"
|
|
314
|
+
root = REXML::Document.new(File.read(unit)).root
|
|
315
|
+
return [].freeze unless root&.name == "schema"
|
|
316
|
+
|
|
317
|
+
refs = %w[include import redefine].map do |tag|
|
|
318
|
+
root.elements.each_with_object([]) do |node, out|
|
|
319
|
+
next unless node.name == tag
|
|
320
|
+
|
|
321
|
+
loc = node.attributes["schemaLocation"]
|
|
322
|
+
out << loc if loc.to_s.match?(/\S/)
|
|
323
|
+
end
|
|
324
|
+
end.flatten
|
|
325
|
+
refs.freeze
|
|
326
|
+
rescue StandardError
|
|
327
|
+
[].freeze
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
# {element name => {attribute name => namespace}}: every
|
|
331
|
+
# attribute the composition admits for each element — the
|
|
332
|
+
# element's type's attributeGroup members resolved transitively
|
|
333
|
+
# across units (import groups; an xs:redefine body's self-ref
|
|
334
|
+
# is the 4.2.2 hook to the ORIGINAL definition, so the
|
|
335
|
+
# effective group is body UNION original). Only exact members
|
|
336
|
+
# of the real composition land here, so a genuinely undeclared
|
|
337
|
+
# attribute never becomes refutable.
|
|
338
|
+
def element_admissibility(units)
|
|
339
|
+
@element_admissibility ||= {}
|
|
340
|
+
cached = @element_admissibility[units]
|
|
341
|
+
return cached if cached
|
|
342
|
+
|
|
343
|
+
facts = {}
|
|
344
|
+
units.each do |unit|
|
|
345
|
+
unit_facts = schema_unit_facts(unit)
|
|
346
|
+
facts[unit] = unit_facts if unit_facts
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
# Effective group definitions: redefining bodies merge the
|
|
350
|
+
# target unit's original when (and only when) the body hooks
|
|
351
|
+
# it with a self-ref. Keys: bare name AND {uri}name (refs
|
|
352
|
+
# resolve with the referencing unit's xmlns, bare-name
|
|
353
|
+
# fallback — same-name groups are composition-unique).
|
|
354
|
+
plain = {}
|
|
355
|
+
bodies = {}
|
|
356
|
+
facts.each_value do |unit|
|
|
357
|
+
unit[:redefines].each do |name, target_path|
|
|
358
|
+
body = unit[:groups][name]
|
|
359
|
+
original = facts[target_path]&.dig(:groups, name)
|
|
360
|
+
bodies[name] = merge_group_def(body, original, name)
|
|
361
|
+
end
|
|
362
|
+
unit[:groups].each do |name, defn|
|
|
363
|
+
plain[name] = defn unless unit[:redefines].key?(name)
|
|
364
|
+
end
|
|
365
|
+
end
|
|
366
|
+
# Redefine bodies are the EFFECTIVE definitions — they win
|
|
367
|
+
# over the plain originals.
|
|
368
|
+
groups = plain.merge(bodies)
|
|
369
|
+
|
|
370
|
+
resolved = {}
|
|
371
|
+
facts.each_value do |unit|
|
|
372
|
+
unit[:elements].each do |element_name, element|
|
|
373
|
+
attrs = {}
|
|
374
|
+
element[:groups].each do |qref|
|
|
375
|
+
resolve_group_ref(qref, unit, groups, attrs, {})
|
|
376
|
+
end
|
|
377
|
+
element[:attrs].each do |name, uri|
|
|
378
|
+
attrs[name] = uri
|
|
379
|
+
end
|
|
380
|
+
resolved[element_name] = attrs unless attrs.empty?
|
|
381
|
+
end
|
|
382
|
+
end
|
|
383
|
+
@element_admissibility[units] = resolved.freeze
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
# body UNION original when the body hooks the original with a
|
|
387
|
+
# self-ref; the body alone otherwise.
|
|
388
|
+
def merge_group_def(body, original, name)
|
|
389
|
+
return body unless body && original.is_a?(Hash) &&
|
|
390
|
+
body[:refs].include?(name)
|
|
391
|
+
|
|
392
|
+
{
|
|
393
|
+
attrs: body[:attrs] + original[:attrs],
|
|
394
|
+
refs: body[:refs] + original[:refs],
|
|
395
|
+
}
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
# Resolve one attributeGroup ref (raw QName string) into attrs
|
|
399
|
+
# {name => uri}, transitively; cycle-guarded by +seen+. Keys
|
|
400
|
+
# are bare names with qualified "{uri}name" fallbacks.
|
|
401
|
+
def resolve_group_ref(qref, from_unit, groups, attrs, seen)
|
|
402
|
+
return if seen.key?(qref)
|
|
403
|
+
|
|
404
|
+
seen[qref] = true
|
|
405
|
+
prefix, _, local = qref.rpartition(":")
|
|
406
|
+
xmlns = from_unit[:xmlns]
|
|
407
|
+
uri = if prefix.empty?
|
|
408
|
+
xmlns["xmlns"].to_s
|
|
409
|
+
else
|
|
410
|
+
xmlns["xmlns:#{prefix}"].to_s
|
|
411
|
+
end
|
|
412
|
+
defn = groups["{#{uri}}#{local}"] || groups[local]
|
|
413
|
+
return unless defn.is_a?(Hash)
|
|
414
|
+
|
|
415
|
+
defn[:attrs].each do |name, attr_uri|
|
|
416
|
+
attrs[name] = attr_uri
|
|
417
|
+
end
|
|
418
|
+
defn[:refs].each do |ref|
|
|
419
|
+
resolve_group_ref(ref, from_unit, groups, attrs, seen)
|
|
420
|
+
end
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
# Per-unit schema facts for the admissibility index:
|
|
424
|
+
# {tns, afd (attributeFormDefault qualified?), xmlns,
|
|
425
|
+
# groups: {name => {attrs, refs}}, redefines: {name => target},
|
|
426
|
+
# elements: {name => {groups, attrs}}} — poison-immune REXML.
|
|
427
|
+
# Attribute member URIs use the DEFINING unit's form rules;
|
|
428
|
+
# group refs stay raw QNames (resolved at chase time).
|
|
429
|
+
def schema_unit_facts(unit)
|
|
430
|
+
require "rexml/document"
|
|
431
|
+
root = REXML::Document.new(File.read(unit)).root
|
|
432
|
+
return nil unless root&.name == "schema"
|
|
433
|
+
|
|
434
|
+
xmlns = {}
|
|
435
|
+
root.attributes.each do |name, value|
|
|
436
|
+
xmlns[name] = value.to_s if name.start_with?("xmlns")
|
|
437
|
+
end
|
|
438
|
+
tns = root.attributes["targetNamespace"].to_s
|
|
439
|
+
afd = root.attributes["attributeFormDefault"] == "qualified"
|
|
440
|
+
|
|
441
|
+
member_uri = lambda do |node|
|
|
442
|
+
qualified = node.attributes["form"] == "qualified" || afd
|
|
443
|
+
qualified ? tns : ""
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
groups = {}
|
|
447
|
+
redefines = {}
|
|
448
|
+
root.elements.each do |node|
|
|
449
|
+
case node.name
|
|
450
|
+
when "attributeGroup"
|
|
451
|
+
name = node.attributes["name"]
|
|
452
|
+
next unless name
|
|
453
|
+
|
|
454
|
+
groups[name] = walk_group_members(node, member_uri)
|
|
455
|
+
when "redefine"
|
|
456
|
+
loc = node.attributes["schemaLocation"]
|
|
457
|
+
node.elements.each do |body|
|
|
458
|
+
next unless body.name == "attributeGroup"
|
|
459
|
+
|
|
460
|
+
name = body.attributes["name"]
|
|
461
|
+
next unless name
|
|
462
|
+
|
|
463
|
+
groups[name] = walk_group_members(body, member_uri)
|
|
464
|
+
redefines[name] = File.expand_path(loc, File.dirname(unit)) if
|
|
465
|
+
loc.to_s.match?(/\S/)
|
|
466
|
+
end
|
|
467
|
+
end
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
elements = {}
|
|
471
|
+
root.elements.each do |node|
|
|
472
|
+
next unless node.name == "element"
|
|
473
|
+
next unless (name = node.attributes["name"])
|
|
474
|
+
|
|
475
|
+
container = node.elements.find { |child| child.name == "complexType" }
|
|
476
|
+
type_name = node.attributes["type"].to_s
|
|
477
|
+
container ||= root.elements.find do |child|
|
|
478
|
+
child.name == "complexType" && child.attributes["name"] == type_name
|
|
479
|
+
end
|
|
480
|
+
next unless container.is_a?(REXML::Element)
|
|
481
|
+
|
|
482
|
+
groups_seen, attrs = walk_type_attributes(container, member_uri)
|
|
483
|
+
elements[name] = { groups: groups_seen, attrs: attrs }
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
{
|
|
487
|
+
tns: tns, afd: afd, xmlns: xmlns, groups: groups,
|
|
488
|
+
redefines: redefines, elements: elements
|
|
489
|
+
}
|
|
490
|
+
rescue StandardError
|
|
491
|
+
nil
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
# Refs and attribute members of one attributeGroup definition.
|
|
495
|
+
# Refs stay raw QNames — the chase resolves prefixes through
|
|
496
|
+
# the referencing unit's xmlns.
|
|
497
|
+
def walk_group_members(node, member_uri)
|
|
498
|
+
refs = []
|
|
499
|
+
attrs = []
|
|
500
|
+
node.elements.each do |child|
|
|
501
|
+
case child.name
|
|
502
|
+
when "attribute"
|
|
503
|
+
name = child.attributes["name"]
|
|
504
|
+
attrs << [name, member_uri.call(child)] if name
|
|
505
|
+
when "attributeGroup"
|
|
506
|
+
ref = child.attributes["ref"]
|
|
507
|
+
refs << ref if ref.to_s.match?(/\S/)
|
|
508
|
+
end
|
|
509
|
+
end
|
|
510
|
+
{ attrs: attrs, refs: refs }
|
|
511
|
+
end
|
|
512
|
+
|
|
513
|
+
# attributeGroup refs + own attribute members of a complexType
|
|
514
|
+
# (one level; nested group-through-group chains resolve at
|
|
515
|
+
# chase time). Returns [raw refs, attrs].
|
|
516
|
+
def walk_type_attributes(container, member_uri)
|
|
517
|
+
refs = []
|
|
518
|
+
attrs = []
|
|
519
|
+
container.elements.each do |child|
|
|
520
|
+
case child.name
|
|
521
|
+
when "attributeGroup"
|
|
522
|
+
ref = child.attributes["ref"]
|
|
523
|
+
refs << ref if ref.to_s.match?(/\S/)
|
|
524
|
+
when "attribute"
|
|
525
|
+
name = child.attributes["name"]
|
|
526
|
+
attrs << [name, member_uri.call(child)] if name
|
|
527
|
+
end
|
|
528
|
+
end
|
|
529
|
+
[refs, attrs]
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
# {uri, local} => true for every global xs:attribute and
|
|
533
|
+
# xs:element declared across the unit set: a global
|
|
534
|
+
# declaration always takes its unit's targetNamespace.
|
|
535
|
+
# Parsed with REXML when available: the engine's component
|
|
536
|
+
# tables are process-global and can be poisoned by later
|
|
537
|
+
# parses between compile and validate (moxml#522) — a
|
|
538
|
+
# declaration index must be poison-immune. Falls back to the
|
|
539
|
+
# context parse when REXML is unavailable.
|
|
540
|
+
def composed_declarations(units)
|
|
541
|
+
@composed_declarations ||= {}
|
|
542
|
+
cached = @composed_declarations[units]
|
|
543
|
+
return cached if cached
|
|
544
|
+
|
|
545
|
+
declared = {}
|
|
546
|
+
parser = begin
|
|
547
|
+
require "rexml"
|
|
548
|
+
:rexml
|
|
549
|
+
rescue LoadError
|
|
550
|
+
:context
|
|
551
|
+
end
|
|
552
|
+
units.each do |unit|
|
|
553
|
+
tns, decls = unit_declarations(unit, parser)
|
|
554
|
+
next unless decls
|
|
555
|
+
|
|
556
|
+
decls.each do |name|
|
|
557
|
+
declared[[tns, name]] = true
|
|
558
|
+
end
|
|
559
|
+
end
|
|
560
|
+
@composed_declarations[units] = declared
|
|
561
|
+
end
|
|
562
|
+
|
|
563
|
+
# [targetNamespace, [global attribute/element names]] for one
|
|
564
|
+
# schema unit, per the parser flavor. REXML's read is
|
|
565
|
+
# poison-immune; the context parse is the fallback.
|
|
566
|
+
def unit_declarations(unit, parser)
|
|
567
|
+
text = File.read(unit)
|
|
568
|
+
if parser == :rexml
|
|
569
|
+
require "rexml/document"
|
|
570
|
+
root = REXML::Document.new(text).root
|
|
571
|
+
return nil unless root&.name == "schema"
|
|
572
|
+
|
|
573
|
+
tns = root.attributes["targetNamespace"].to_s
|
|
574
|
+
names = %w[attribute element].freeze
|
|
575
|
+
decls = root.elements.filter_map do |decl|
|
|
576
|
+
next unless names.include?(decl.name)
|
|
577
|
+
|
|
578
|
+
decl.attributes["name"]
|
|
579
|
+
end
|
|
580
|
+
else
|
|
581
|
+
root = @context.parse(text).root
|
|
582
|
+
return nil unless root&.name == "schema"
|
|
583
|
+
|
|
584
|
+
tns = root.attributes.find { |a| a.name == "targetNamespace" }
|
|
585
|
+
&.value.to_s
|
|
586
|
+
names = %w[attribute element].freeze
|
|
587
|
+
decls = root.children
|
|
588
|
+
.grep(Moxml::Element)
|
|
589
|
+
.filter_map do |decl|
|
|
590
|
+
next unless names.include?(decl.name)
|
|
591
|
+
|
|
592
|
+
decl.attributes.find { |a| a.name == "name" }&.value
|
|
593
|
+
end
|
|
594
|
+
end
|
|
595
|
+
[tns, decls]
|
|
596
|
+
rescue StandardError
|
|
597
|
+
nil
|
|
598
|
+
end
|
|
599
|
+
|
|
600
|
+
# Matches both row shapes the strict admission emits — with and
|
|
601
|
+
# without the attribute segment:
|
|
602
|
+
# element 'doc' @2:144: element 'doc': not declared (strict)
|
|
603
|
+
# element 'elem' @3:46: attribute 'foo': not declared (strict)
|
|
604
|
+
NOT_DECLARED_ROW =
|
|
605
|
+
/\Aelement '([^']+)'(?: @\S+)? (?:(?:element|attribute) '([^']+)': )?not declared \(strict\)\z/
|
|
606
|
+
|
|
173
607
|
def valid?(node_or_xml)
|
|
174
608
|
check_released!
|
|
175
609
|
validate(node_or_xml).empty?
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
# leptris-ruby#586 adoption — the flat packet attribute face
|
|
6
|
+
# (binding 1.9.406.1): one crossing, interned names, fresh values,
|
|
7
|
+
# no per-element memo retention on the binding side. The facade
|
|
8
|
+
# listing face stays bare names (uniform with every adapter),
|
|
9
|
+
# document order. Older bindings keep the snapshot face
|
|
10
|
+
# (ELEMENT_ATTR_SNAPSHOT); the packet path must be stride-identical
|
|
11
|
+
# to it — parity is the contract, allocations are the win.
|
|
12
|
+
RSpec.describe "leptris element-attribute packet hydration" do
|
|
13
|
+
def bare(pairs)
|
|
14
|
+
pairs.map { |(name, value)| [name.split(":", 2).last, value] }
|
|
15
|
+
end
|
|
16
|
+
let(:ctx) { Moxml.new(:leptris) }
|
|
17
|
+
|
|
18
|
+
context "when the packet face is armed (>= 1.9.406.1)" do
|
|
19
|
+
before do
|
|
20
|
+
unless Moxml::Adapter::Leptris::ELEMENT_ATTR_PACKET
|
|
21
|
+
skip "binding #{Leptris::VERSION} has no attribute_packet face"
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "matches the binding per-attribute face stride for stride" do
|
|
26
|
+
doc = ctx.parse(
|
|
27
|
+
'<root a="1" xml:lang="en" xmlns:ns="urn:u" ns:p="v"/>',
|
|
28
|
+
)
|
|
29
|
+
binding_element = Moxml::Adapter::Leptris.to_binding(doc.root)
|
|
30
|
+
expected = bare(binding_element.attribute_pairs)
|
|
31
|
+
|
|
32
|
+
expect(doc.root.attribute_pairs).to eq(expected)
|
|
33
|
+
expect(doc.root.attribute_pairs).to eq(
|
|
34
|
+
[["a", "1"], ["lang", "en"], ["p", "v"]],
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "keeps document order across plain and prefixed attributes" do
|
|
39
|
+
doc = ctx.parse(
|
|
40
|
+
'<root z="9" xmlns:m="urn:m" m:x="8" k="7"/>',
|
|
41
|
+
)
|
|
42
|
+
expect(doc.root.attribute_pairs.map(&:first)).to eq(
|
|
43
|
+
%w[z x k],
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "hydrates child elements the same way" do
|
|
48
|
+
doc = ctx.parse('<r><child b="2" c="3"/></r>')
|
|
49
|
+
expect(doc.at("//child").attribute_pairs).to eq(
|
|
50
|
+
[["b", "2"], ["c", "3"]],
|
|
51
|
+
)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "yields an empty list for attribute-less elements" do
|
|
55
|
+
doc = ctx.parse("<root/>")
|
|
56
|
+
expect(doc.root.attribute_pairs).to eq([])
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it "mints nothing into the binding identity cache" do
|
|
60
|
+
doc = ctx.parse('<root a="1" b="2"/>')
|
|
61
|
+
binding_element = Moxml::Adapter::Leptris.to_binding(doc.root)
|
|
62
|
+
before = binding_element.document.native_cache.size
|
|
63
|
+
doc.root.attribute_pairs
|
|
64
|
+
expect(binding_element.document.native_cache.size).to eq(before)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
context "with any arm state" do
|
|
69
|
+
it "agrees with the binding per-attribute face" do
|
|
70
|
+
doc = ctx.parse(
|
|
71
|
+
'<root a="1" xml:lang="en" xmlns:ns="urn:u" ns:p="v"/>',
|
|
72
|
+
)
|
|
73
|
+
binding_element = Moxml::Adapter::Leptris.to_binding(doc.root)
|
|
74
|
+
expect(doc.root.attribute_pairs).to eq(
|
|
75
|
+
bare(binding_element.attribute_pairs),
|
|
76
|
+
)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|