lutaml-model 0.7.1 → 0.7.7
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/.envrc +1 -0
- data/.github/workflows/dependent-tests.yml +4 -0
- data/.github/workflows/rake.yml +12 -0
- data/.github/workflows/release.yml +3 -0
- data/.gitignore +6 -1
- data/.irbrc +1 -0
- data/.pryrc +1 -0
- data/.rubocop.yml +1 -1
- data/.rubocop_todo.yml +58 -84
- data/Gemfile +4 -1
- data/README.adoc +3115 -365
- data/RELEASE_NOTES.adoc +346 -0
- data/docs/custom_adapters.adoc +144 -0
- data/docs/custom_registers.adoc +228 -0
- data/docs/schema_generation.adoc +898 -0
- data/docs/schema_import.adoc +364 -0
- data/flake.lock +114 -0
- data/flake.nix +103 -0
- data/lib/lutaml/model/attribute.rb +242 -100
- data/lib/lutaml/model/choice.rb +30 -0
- data/lib/lutaml/model/collection.rb +212 -0
- data/lib/lutaml/model/comparable_model.rb +3 -3
- data/lib/lutaml/model/config.rb +71 -42
- data/lib/lutaml/model/constants.rb +2 -0
- data/lib/lutaml/model/error/element_count_out_of_range_error.rb +29 -0
- data/lib/lutaml/model/error/invalid_attribute_name_error.rb +15 -0
- data/lib/lutaml/model/error/invalid_attribute_options_error.rb +16 -0
- data/lib/lutaml/model/error/invalid_choice_range_error.rb +3 -5
- data/lib/lutaml/model/error/polymorphic_error.rb +7 -2
- data/lib/lutaml/model/error/register/not_registrable_class_error.rb +11 -0
- data/lib/lutaml/model/error/type/invalid_value_error.rb +5 -3
- data/lib/lutaml/model/error/type/max_bound_error.rb +20 -0
- data/lib/lutaml/model/error/type/max_length_error.rb +20 -0
- data/lib/lutaml/model/error/type/min_bound_error.rb +20 -0
- data/lib/lutaml/model/error/type/min_length_error.rb +20 -0
- data/lib/lutaml/model/error/type/pattern_not_matched_error.rb +18 -0
- data/lib/lutaml/model/error/validation_failed_error.rb +9 -0
- data/lib/lutaml/model/error.rb +10 -0
- data/lib/lutaml/model/errors.rb +36 -0
- data/lib/lutaml/model/format_registry.rb +44 -0
- data/lib/lutaml/model/global_register.rb +41 -0
- data/lib/lutaml/model/hash_adapter/document.rb +11 -0
- data/lib/lutaml/model/hash_adapter/mapping.rb +19 -0
- data/lib/lutaml/model/hash_adapter/mapping_rule.rb +9 -0
- data/lib/lutaml/model/hash_adapter/standard_adapter.rb +17 -0
- data/lib/lutaml/model/hash_adapter/transform.rb +8 -0
- data/lib/lutaml/model/hash_adapter.rb +21 -0
- data/lib/lutaml/model/json/document.rb +11 -0
- data/lib/lutaml/model/json/mapping.rb +19 -0
- data/lib/lutaml/model/json/mapping_rule.rb +9 -0
- data/lib/lutaml/model/{json_adapter → json}/multi_json_adapter.rb +4 -5
- data/lib/lutaml/model/{json_adapter/standard_json_adapter.rb → json/standard_adapter.rb} +5 -3
- data/lib/lutaml/model/json/transform.rb +8 -0
- data/lib/lutaml/model/json.rb +21 -0
- data/lib/lutaml/model/jsonl/document.rb +14 -0
- data/lib/lutaml/model/jsonl/mapping.rb +19 -0
- data/lib/lutaml/model/jsonl/mapping_rule.rb +9 -0
- data/lib/lutaml/model/jsonl/standard_adapter.rb +33 -0
- data/lib/lutaml/model/jsonl/transform.rb +19 -0
- data/lib/lutaml/model/jsonl.rb +21 -0
- data/lib/lutaml/model/key_value_document.rb +28 -0
- data/lib/lutaml/model/mapping/key_value_mapping.rb +85 -13
- data/lib/lutaml/model/mapping/key_value_mapping_rule.rb +5 -0
- data/lib/lutaml/model/mapping/mapping.rb +13 -0
- data/lib/lutaml/model/mapping/mapping_rule.rb +15 -9
- data/lib/lutaml/model/register.rb +105 -0
- data/lib/lutaml/model/{toml_adapter.rb → registrable.rb} +1 -1
- data/lib/lutaml/model/schema/base_schema.rb +64 -0
- data/lib/lutaml/model/schema/decorators/attribute.rb +114 -0
- data/lib/lutaml/model/schema/decorators/choices.rb +31 -0
- data/lib/lutaml/model/schema/decorators/class_definition.rb +85 -0
- data/lib/lutaml/model/schema/decorators/definition_collection.rb +97 -0
- data/lib/lutaml/model/schema/generator/definition.rb +53 -0
- data/lib/lutaml/model/schema/generator/definitions_collection.rb +81 -0
- data/lib/lutaml/model/schema/generator/properties_collection.rb +63 -0
- data/lib/lutaml/model/schema/generator/property.rb +110 -0
- data/lib/lutaml/model/schema/generator/ref.rb +24 -0
- data/lib/lutaml/model/schema/helpers/template_helper.rb +49 -0
- data/lib/lutaml/model/schema/json_schema.rb +42 -49
- data/lib/lutaml/model/schema/relaxng_schema.rb +14 -10
- data/lib/lutaml/model/schema/renderer.rb +36 -0
- data/lib/lutaml/model/schema/shared_methods.rb +24 -0
- data/lib/lutaml/model/schema/templates/model.erb +9 -0
- data/lib/lutaml/model/schema/xml_compiler/attribute.rb +85 -0
- data/lib/lutaml/model/schema/xml_compiler/attribute_group.rb +45 -0
- data/lib/lutaml/model/schema/xml_compiler/choice.rb +65 -0
- data/lib/lutaml/model/schema/xml_compiler/complex_content.rb +27 -0
- data/lib/lutaml/model/schema/xml_compiler/complex_content_restriction.rb +34 -0
- data/lib/lutaml/model/schema/xml_compiler/complex_type.rb +136 -0
- data/lib/lutaml/model/schema/xml_compiler/element.rb +104 -0
- data/lib/lutaml/model/schema/xml_compiler/group.rb +97 -0
- data/lib/lutaml/model/schema/xml_compiler/restriction.rb +101 -0
- data/lib/lutaml/model/schema/xml_compiler/sequence.rb +50 -0
- data/lib/lutaml/model/schema/xml_compiler/simple_content.rb +36 -0
- data/lib/lutaml/model/schema/xml_compiler/simple_type.rb +189 -0
- data/lib/lutaml/model/schema/xml_compiler.rb +231 -587
- data/lib/lutaml/model/schema/xsd_schema.rb +12 -8
- data/lib/lutaml/model/schema/yaml_schema.rb +41 -35
- data/lib/lutaml/model/schema.rb +1 -0
- data/lib/lutaml/model/sequence.rb +60 -30
- data/lib/lutaml/model/serialization_adapter.rb +22 -0
- data/lib/lutaml/model/serialize.rb +296 -548
- data/lib/lutaml/model/services/base.rb +11 -0
- data/lib/lutaml/model/services/logger.rb +54 -0
- data/lib/lutaml/model/services/rule_value_extractor.rb +92 -0
- data/lib/lutaml/model/services/transformer.rb +48 -0
- data/lib/lutaml/model/services/type/validator/number.rb +25 -0
- data/lib/lutaml/model/services/type/validator/string.rb +52 -0
- data/lib/lutaml/model/services/type/validator.rb +43 -0
- data/lib/lutaml/model/services/validator.rb +145 -0
- data/lib/lutaml/model/services.rb +5 -0
- data/lib/lutaml/model/toml/document.rb +11 -0
- data/lib/lutaml/model/toml/mapping.rb +27 -0
- data/lib/lutaml/model/toml/mapping_rule.rb +9 -0
- data/lib/lutaml/model/{toml_adapter → toml}/toml_rb_adapter.rb +3 -3
- data/lib/lutaml/model/toml/tomlib_adapter.rb +19 -0
- data/lib/lutaml/model/toml/transform.rb +8 -0
- data/lib/lutaml/model/toml.rb +30 -0
- data/lib/lutaml/model/transform/key_value_transform.rb +297 -0
- data/lib/lutaml/model/transform/xml_transform.rb +228 -0
- data/lib/lutaml/model/transform.rb +93 -0
- data/lib/lutaml/model/type/boolean.rb +1 -1
- data/lib/lutaml/model/type/date.rb +1 -1
- data/lib/lutaml/model/type/date_time.rb +1 -1
- data/lib/lutaml/model/type/decimal.rb +11 -9
- data/lib/lutaml/model/type/float.rb +2 -1
- data/lib/lutaml/model/type/integer.rb +24 -21
- data/lib/lutaml/model/type/string.rb +4 -2
- data/lib/lutaml/model/type/time.rb +1 -1
- data/lib/lutaml/model/type/time_without_date.rb +1 -1
- data/lib/lutaml/model/type/value.rb +11 -10
- data/lib/lutaml/model/type.rb +5 -2
- data/lib/lutaml/model/uninitialized_class.rb +1 -1
- data/lib/lutaml/model/utils.rb +56 -4
- data/lib/lutaml/model/validation.rb +6 -4
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/model/{xml_adapter → xml}/builder/nokogiri.rb +2 -2
- data/lib/lutaml/model/{xml_adapter → xml}/builder/oga.rb +10 -10
- data/lib/lutaml/model/{xml_adapter → xml}/builder/ox.rb +1 -1
- data/lib/lutaml/model/{xml_adapter/xml_document.rb → xml/document.rb} +42 -25
- data/lib/lutaml/model/xml/element.rb +32 -0
- data/lib/lutaml/model/xml/mapping.rb +471 -0
- data/lib/lutaml/model/xml/mapping_rule.rb +149 -0
- data/lib/lutaml/model/xml/nokogiri_adapter.rb +234 -0
- data/lib/lutaml/model/{xml_adapter → xml}/oga/document.rb +1 -1
- data/lib/lutaml/model/{xml_adapter → xml}/oga/element.rb +7 -2
- data/lib/lutaml/model/xml/oga_adapter.rb +172 -0
- data/lib/lutaml/model/xml/ox_adapter.rb +229 -0
- data/lib/lutaml/model/xml/transform.rb +8 -0
- data/lib/lutaml/model/{xml_adapter → xml}/xml_attribute.rb +1 -1
- data/lib/lutaml/model/{xml_adapter → xml}/xml_element.rb +9 -31
- data/lib/lutaml/model/{xml_adapter → xml}/xml_namespace.rb +1 -1
- data/lib/lutaml/model/xml.rb +31 -0
- data/lib/lutaml/model/xml_adapter/element.rb +11 -25
- data/lib/lutaml/model/xml_adapter/nokogiri_adapter.rb +6 -223
- data/lib/lutaml/model/xml_adapter/oga_adapter.rb +13 -163
- data/lib/lutaml/model/xml_adapter/ox_adapter.rb +10 -207
- data/lib/lutaml/model/yaml/document.rb +10 -0
- data/lib/lutaml/model/yaml/mapping.rb +19 -0
- data/lib/lutaml/model/yaml/mapping_rule.rb +9 -0
- data/lib/lutaml/model/{yaml_adapter/standard_yaml_adapter.rb → yaml/standard_adapter.rb} +4 -3
- data/lib/lutaml/model/yaml/transform.rb +8 -0
- data/lib/lutaml/model/yaml.rb +21 -0
- data/lib/lutaml/model/yamls/document.rb +14 -0
- data/lib/lutaml/model/yamls/mapping.rb +19 -0
- data/lib/lutaml/model/yamls/mapping_rule.rb +9 -0
- data/lib/lutaml/model/yamls/standard_adapter.rb +34 -0
- data/lib/lutaml/model/yamls/transform.rb +19 -0
- data/lib/lutaml/model/yamls.rb +21 -0
- data/lib/lutaml/model.rb +15 -4
- data/lutaml-model.gemspec +0 -4
- data/spec/benchmarks/xml_parsing_benchmark_spec.rb +5 -6
- data/spec/fixtures/xml/advanced_test_schema.xsd +134 -0
- data/spec/fixtures/xml/examples/nested_categories.xml +55 -0
- data/spec/fixtures/xml/examples/valid_catalog.xml +43 -0
- data/spec/fixtures/xml/product_catalog.xsd +151 -0
- data/spec/fixtures/xml/specifications_schema.xsd +38 -0
- data/spec/lutaml/model/attribute_collection_spec.rb +101 -0
- data/spec/lutaml/model/attribute_spec.rb +41 -44
- data/spec/lutaml/model/cdata_spec.rb +7 -7
- data/spec/lutaml/model/choice_spec.rb +44 -0
- data/spec/lutaml/model/custom_bibtex_adapter_spec.rb +598 -0
- data/spec/lutaml/model/custom_collection_spec.rb +830 -0
- data/spec/lutaml/model/custom_vobject_adapter_spec.rb +1226 -0
- data/spec/lutaml/model/global_register_spec.rb +108 -0
- data/spec/lutaml/model/group_spec.rb +27 -10
- data/spec/lutaml/model/hash/adapter_spec.rb +255 -0
- data/spec/lutaml/model/json_adapter_spec.rb +6 -6
- data/spec/lutaml/model/jsonl/standard_adapter_spec.rb +91 -0
- data/spec/lutaml/model/jsonl_spec.rb +229 -0
- data/spec/lutaml/model/key_value_mapping_spec.rb +25 -1
- data/spec/lutaml/model/mixed_content_spec.rb +24 -24
- data/spec/lutaml/model/multiple_mapping_spec.rb +6 -6
- data/spec/lutaml/model/ordered_content_spec.rb +6 -6
- data/spec/lutaml/model/polymorphic_spec.rb +178 -0
- data/spec/lutaml/model/register/key_value_spec.rb +275 -0
- data/spec/lutaml/model/register/xml_spec.rb +185 -0
- data/spec/lutaml/model/register_spec.rb +147 -0
- data/spec/lutaml/model/root_mappings_spec.rb +3 -3
- data/spec/lutaml/model/rule_value_extractor_spec.rb +162 -0
- data/spec/lutaml/model/schema/generator/definitions_collection_spec.rb +120 -0
- data/spec/lutaml/model/schema/json_schema_spec.rb +412 -51
- data/spec/lutaml/model/schema/json_schema_to_models_spec.rb +383 -0
- data/spec/lutaml/model/schema/xml_compiler/attribute_group_spec.rb +65 -0
- data/spec/lutaml/model/schema/xml_compiler/attribute_spec.rb +63 -0
- data/spec/lutaml/model/schema/xml_compiler/choice_spec.rb +71 -0
- data/spec/lutaml/model/schema/xml_compiler/complex_content_restriction_spec.rb +55 -0
- data/spec/lutaml/model/schema/xml_compiler/complex_content_spec.rb +37 -0
- data/spec/lutaml/model/schema/xml_compiler/complex_type_spec.rb +173 -0
- data/spec/lutaml/model/schema/xml_compiler/element_spec.rb +63 -0
- data/spec/lutaml/model/schema/xml_compiler/group_spec.rb +86 -0
- data/spec/lutaml/model/schema/xml_compiler/restriction_spec.rb +76 -0
- data/spec/lutaml/model/schema/xml_compiler/sequence_spec.rb +59 -0
- data/spec/lutaml/model/schema/xml_compiler/simple_content_spec.rb +55 -0
- data/spec/lutaml/model/schema/xml_compiler/simple_type_spec.rb +181 -0
- data/spec/lutaml/model/schema/xml_compiler_spec.rb +503 -1804
- data/spec/lutaml/model/schema/yaml_schema_spec.rb +249 -26
- data/spec/lutaml/model/sequence_spec.rb +36 -0
- data/spec/lutaml/model/serializable_spec.rb +201 -94
- data/spec/lutaml/model/toml_adapter_spec.rb +6 -6
- data/spec/lutaml/model/toml_spec.rb +51 -0
- data/spec/lutaml/model/transformation_spec.rb +72 -15
- data/spec/lutaml/model/type_spec.rb +8 -4
- data/spec/lutaml/model/uninitialized_class_spec.rb +96 -0
- data/spec/lutaml/model/utils_spec.rb +3 -3
- data/spec/lutaml/model/xml/derived_attributes_spec.rb +1 -1
- data/spec/lutaml/model/xml/namespace_spec.rb +57 -0
- data/spec/lutaml/model/xml/root_mappings/nested_child_mappings_spec.rb +217 -0
- data/spec/lutaml/model/xml/xml_element_spec.rb +7 -1
- data/spec/lutaml/model/xml_adapter/nokogiri_adapter_spec.rb +2 -2
- data/spec/lutaml/model/xml_adapter/oga_adapter_spec.rb +2 -2
- data/spec/lutaml/model/xml_adapter/ox_adapter_spec.rb +2 -2
- data/spec/lutaml/model/xml_adapter/xml_namespace_spec.rb +12 -12
- data/spec/lutaml/model/xml_adapter_spec.rb +30 -6
- data/spec/lutaml/model/xml_mapping_rule_spec.rb +14 -7
- data/spec/lutaml/model/xml_mapping_spec.rb +27 -15
- data/spec/lutaml/model/xml_spec.rb +63 -0
- data/spec/lutaml/model/yaml_adapter_spec.rb +3 -5
- data/spec/lutaml/model/yamls/standard_adapter_spec.rb +183 -0
- data/spec/lutaml/model/yamls_spec.rb +294 -0
- data/spec/spec_helper.rb +4 -3
- metadata +162 -60
- data/lib/lutaml/model/json_adapter/json_document.rb +0 -20
- data/lib/lutaml/model/json_adapter/json_object.rb +0 -28
- data/lib/lutaml/model/loggable.rb +0 -15
- data/lib/lutaml/model/mapping/json_mapping.rb +0 -17
- data/lib/lutaml/model/mapping/toml_mapping.rb +0 -25
- data/lib/lutaml/model/mapping/xml_mapping.rb +0 -389
- data/lib/lutaml/model/mapping/xml_mapping_rule.rb +0 -139
- data/lib/lutaml/model/mapping/yaml_mapping.rb +0 -17
- data/lib/lutaml/model/mapping.rb +0 -14
- data/lib/lutaml/model/schema/templates/simple_type.rb +0 -247
- data/lib/lutaml/model/toml_adapter/toml_document.rb +0 -20
- data/lib/lutaml/model/toml_adapter/toml_object.rb +0 -28
- data/lib/lutaml/model/toml_adapter/tomlib_adapter.rb +0 -20
- data/lib/lutaml/model/yaml_adapter/yaml_document.rb +0 -20
- data/lib/lutaml/model/yaml_adapter/yaml_object.rb +0 -28
- data/lib/lutaml/model/yaml_adapter.rb +0 -8
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
require_relative "xml_attribute"
|
|
2
|
+
require_relative "document"
|
|
2
3
|
|
|
3
4
|
module Lutaml
|
|
4
5
|
module Model
|
|
5
|
-
module
|
|
6
|
+
module Xml
|
|
6
7
|
class XmlElement
|
|
8
|
+
XML_NAMESPACE_URI = "http://www.w3.org/XML/1998/namespace".freeze
|
|
9
|
+
|
|
7
10
|
attr_reader :attributes,
|
|
8
11
|
:children,
|
|
9
12
|
:namespace_prefix,
|
|
@@ -16,12 +19,13 @@ module Lutaml
|
|
|
16
19
|
attributes = {},
|
|
17
20
|
children = [],
|
|
18
21
|
text = nil,
|
|
22
|
+
name: nil,
|
|
19
23
|
parent_document: nil,
|
|
20
24
|
namespace_prefix: nil,
|
|
21
25
|
default_namespace: nil
|
|
22
26
|
)
|
|
23
|
-
@name =
|
|
24
|
-
@namespace_prefix = namespace_prefix
|
|
27
|
+
@name = name
|
|
28
|
+
@namespace_prefix = namespace_prefix
|
|
25
29
|
@attributes = attributes
|
|
26
30
|
@children = children
|
|
27
31
|
@text = text
|
|
@@ -59,7 +63,7 @@ module Lutaml
|
|
|
59
63
|
end
|
|
60
64
|
|
|
61
65
|
def document
|
|
62
|
-
|
|
66
|
+
Document.new(self)
|
|
63
67
|
end
|
|
64
68
|
|
|
65
69
|
def namespaces
|
|
@@ -89,36 +93,10 @@ module Lutaml
|
|
|
89
93
|
namespaces[nil] || @parent_document&.namespaces&.dig(nil)
|
|
90
94
|
end
|
|
91
95
|
|
|
92
|
-
def extract_name(node)
|
|
93
|
-
name = name_from_node(node)
|
|
94
|
-
|
|
95
|
-
n = name.split(":")
|
|
96
|
-
return name if n.length <= 1
|
|
97
|
-
|
|
98
|
-
n[1..].join(":")
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
def extract_namespace_prefix(node)
|
|
102
|
-
name = name_from_node(node)
|
|
103
|
-
|
|
104
|
-
n = name.to_s.split(":")
|
|
105
|
-
return if n.length <= 1
|
|
106
|
-
|
|
107
|
-
n.first
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
def name_from_node(node)
|
|
111
|
-
if node.is_a?(String)
|
|
112
|
-
node
|
|
113
|
-
else
|
|
114
|
-
node.name.to_s
|
|
115
|
-
end
|
|
116
|
-
end
|
|
117
|
-
|
|
118
96
|
def order
|
|
119
97
|
children.map do |child|
|
|
120
98
|
type = child.text? ? "Text" : "Element"
|
|
121
|
-
Lutaml::Model::
|
|
99
|
+
Lutaml::Model::Xml::Element.new(type, child.unprefixed_name)
|
|
122
100
|
end
|
|
123
101
|
end
|
|
124
102
|
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Model
|
|
5
|
+
module Xml
|
|
6
|
+
def self.detect_xml_adapter
|
|
7
|
+
return :nokogiri if Object.const_defined?(:Nokogiri)
|
|
8
|
+
return :ox if Object.const_defined?(:Ox)
|
|
9
|
+
return :oga if Object.const_defined?(:Oga)
|
|
10
|
+
|
|
11
|
+
nil
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
require_relative "xml/document"
|
|
18
|
+
require_relative "xml/mapping"
|
|
19
|
+
require_relative "xml/mapping_rule"
|
|
20
|
+
require_relative "xml/transform"
|
|
21
|
+
|
|
22
|
+
Lutaml::Model::FormatRegistry.register(
|
|
23
|
+
:xml,
|
|
24
|
+
mapping_class: Lutaml::Model::Xml::Mapping,
|
|
25
|
+
adapter_class: nil,
|
|
26
|
+
transformer: Lutaml::Model::Xml::Transform,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
if (adapter = Lutaml::Model::Xml.detect_xml_adapter)
|
|
30
|
+
Lutaml::Model::Config.xml_adapter_type = adapter
|
|
31
|
+
end
|
|
@@ -1,31 +1,17 @@
|
|
|
1
|
+
# require "ox"
|
|
2
|
+
# require_relative "xml_document"
|
|
3
|
+
# require_relative "builder/ox"
|
|
4
|
+
|
|
5
|
+
require_relative "../xml/element"
|
|
6
|
+
|
|
1
7
|
module Lutaml
|
|
2
8
|
module Model
|
|
3
9
|
module XmlAdapter
|
|
4
|
-
class Element
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
@name = name
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def text?
|
|
13
|
-
@type == "Text" && @name != "#cdata-section"
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def element_tag
|
|
17
|
-
@name unless text?
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def eql?(other)
|
|
21
|
-
return false unless other.is_a?(self.class)
|
|
22
|
-
|
|
23
|
-
instance_variables.all? do |var|
|
|
24
|
-
instance_variable_get(var) == other.instance_variable_get(var)
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
alias == eql?
|
|
10
|
+
class Element < ::Lutaml::Model::Xml::Element
|
|
11
|
+
Logger.warn_future_deprecation(
|
|
12
|
+
old: "Lutaml::Model::XmlAdapter::Element",
|
|
13
|
+
replacement: "Lutaml::Model::Xml::Element",
|
|
14
|
+
)
|
|
29
15
|
end
|
|
30
16
|
end
|
|
31
17
|
end
|
|
@@ -1,231 +1,14 @@
|
|
|
1
1
|
require "nokogiri"
|
|
2
|
-
require_relative "
|
|
3
|
-
require_relative "builder/nokogiri"
|
|
2
|
+
require_relative "../xml/nokogiri_adapter"
|
|
4
3
|
|
|
5
4
|
module Lutaml
|
|
6
5
|
module Model
|
|
7
6
|
module XmlAdapter
|
|
8
|
-
class NokogiriAdapter <
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def to_xml(options = {})
|
|
16
|
-
builder_options = {}
|
|
17
|
-
|
|
18
|
-
if options.key?(:encoding)
|
|
19
|
-
builder_options[:encoding] = options[:encoding] unless options[:encoding].nil?
|
|
20
|
-
elsif options.key?(:parse_encoding)
|
|
21
|
-
builder_options[:encoding] = options[:parse_encoding]
|
|
22
|
-
else
|
|
23
|
-
builder_options[:encoding] = "UTF-8"
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
builder = Builder::Nokogiri.build(builder_options) do |xml|
|
|
27
|
-
if root.is_a?(Lutaml::Model::XmlAdapter::NokogiriElement)
|
|
28
|
-
root.build_xml(xml)
|
|
29
|
-
else
|
|
30
|
-
mapper_class = options[:mapper_class] || @root.class
|
|
31
|
-
options[:xml_attributes] =
|
|
32
|
-
build_namespace_attributes(mapper_class)
|
|
33
|
-
build_element(xml, @root, options)
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
xml_options = {}
|
|
38
|
-
xml_options[:indent] = 2 if options[:pretty]
|
|
39
|
-
|
|
40
|
-
xml_data = builder.doc.root.to_xml(xml_options)
|
|
41
|
-
options[:declaration] ? declaration(options) + xml_data : xml_data
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
private
|
|
45
|
-
|
|
46
|
-
def prefix_xml(xml, mapping, options)
|
|
47
|
-
if options.key?(:namespace_prefix)
|
|
48
|
-
xml[options[:namespace_prefix]] if options[:namespace_prefix]
|
|
49
|
-
elsif mapping.namespace_prefix
|
|
50
|
-
xml[mapping.namespace_prefix]
|
|
51
|
-
end
|
|
52
|
-
xml
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
def build_ordered_element(xml, element, options = {})
|
|
56
|
-
mapper_class = determine_mapper_class(element, options)
|
|
57
|
-
xml_mapping = mapper_class.mappings_for(:xml)
|
|
58
|
-
return xml unless xml_mapping
|
|
59
|
-
|
|
60
|
-
attributes = build_attributes(element, xml_mapping)&.compact
|
|
61
|
-
|
|
62
|
-
prefixed_xml = prefix_xml(xml, xml_mapping, options)
|
|
63
|
-
|
|
64
|
-
tag_name = options[:tag_name] || xml_mapping.root_element
|
|
65
|
-
tag_name = "#{tag_name}_" if prefixed_xml.respond_to?(tag_name)
|
|
66
|
-
prefixed_xml.public_send(tag_name, attributes) do
|
|
67
|
-
if options.key?(:namespace_prefix) && !options[:namespace_prefix]
|
|
68
|
-
xml.parent.namespace = nil
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
index_hash = {}
|
|
72
|
-
content = []
|
|
73
|
-
|
|
74
|
-
element.element_order.each do |object|
|
|
75
|
-
index_hash[object.name] ||= -1
|
|
76
|
-
curr_index = index_hash[object.name] += 1
|
|
77
|
-
|
|
78
|
-
element_rule = xml_mapping.find_by_name(object.name)
|
|
79
|
-
next if element_rule.nil?
|
|
80
|
-
|
|
81
|
-
attribute_def = attribute_definition_for(element, element_rule,
|
|
82
|
-
mapper_class: mapper_class)
|
|
83
|
-
value = attribute_value_for(element, element_rule)
|
|
84
|
-
|
|
85
|
-
if element_rule == xml_mapping.content_mapping
|
|
86
|
-
next if element_rule.cdata && object.text?
|
|
87
|
-
|
|
88
|
-
text = xml_mapping.content_mapping.serialize(element)
|
|
89
|
-
text = text[curr_index] if text.is_a?(Array)
|
|
90
|
-
|
|
91
|
-
next prefixed_xml.add_text(xml, text, cdata: element_rule.cdata) if element.mixed?
|
|
92
|
-
|
|
93
|
-
content << text
|
|
94
|
-
elsif !value.nil? || element_rule.render_nil?
|
|
95
|
-
value = value[curr_index] if attribute_def.collection?
|
|
96
|
-
|
|
97
|
-
add_to_xml(
|
|
98
|
-
xml,
|
|
99
|
-
element,
|
|
100
|
-
element_rule.prefix,
|
|
101
|
-
value,
|
|
102
|
-
options.merge(
|
|
103
|
-
attribute: attribute_def,
|
|
104
|
-
rule: element_rule,
|
|
105
|
-
mapper_class: mapper_class,
|
|
106
|
-
),
|
|
107
|
-
)
|
|
108
|
-
end
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
prefixed_xml.text content.join
|
|
112
|
-
end
|
|
113
|
-
end
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
class NokogiriElement < XmlElement
|
|
117
|
-
def initialize(node, root_node: nil, default_namespace: nil)
|
|
118
|
-
if root_node
|
|
119
|
-
node.namespaces.each do |prefix, name|
|
|
120
|
-
namespace = XmlNamespace.new(name, prefix)
|
|
121
|
-
|
|
122
|
-
root_node.add_namespace(namespace)
|
|
123
|
-
end
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
attributes = {}
|
|
127
|
-
|
|
128
|
-
# Using `attribute_nodes` instead of `attributes` because
|
|
129
|
-
# `attribute_nodes` handles name collisions as well
|
|
130
|
-
# More info: https://devdocs.io/nokogiri/nokogiri/xml/node#method-i-attributes
|
|
131
|
-
node.attribute_nodes.each do |attr|
|
|
132
|
-
name = if attr.namespace
|
|
133
|
-
"#{attr.namespace.prefix}:#{attr.name}"
|
|
134
|
-
else
|
|
135
|
-
attr.name
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
attributes[name] = XmlAttribute.new(
|
|
139
|
-
name,
|
|
140
|
-
attr.value,
|
|
141
|
-
namespace: attr.namespace&.href,
|
|
142
|
-
namespace_prefix: attr.namespace&.prefix,
|
|
143
|
-
)
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
if root_node.nil? && !node.namespace&.prefix
|
|
147
|
-
default_namespace = node.namespace&.href
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
super(
|
|
151
|
-
node,
|
|
152
|
-
attributes,
|
|
153
|
-
parse_all_children(node, root_node: root_node || self,
|
|
154
|
-
default_namespace: default_namespace),
|
|
155
|
-
node.text,
|
|
156
|
-
parent_document: root_node,
|
|
157
|
-
namespace_prefix: node.namespace&.prefix,
|
|
158
|
-
default_namespace: default_namespace
|
|
159
|
-
)
|
|
160
|
-
end
|
|
161
|
-
|
|
162
|
-
def text?
|
|
163
|
-
# false
|
|
164
|
-
children.empty? && text.length.positive?
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
def to_xml
|
|
168
|
-
return text if text?
|
|
169
|
-
|
|
170
|
-
build_xml.doc.root.to_xml
|
|
171
|
-
end
|
|
172
|
-
|
|
173
|
-
def inner_xml
|
|
174
|
-
children.map(&:to_xml).join
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
def build_xml(builder = nil)
|
|
178
|
-
builder ||= Builder::Nokogiri.build
|
|
179
|
-
|
|
180
|
-
if name == "text"
|
|
181
|
-
builder.text(text)
|
|
182
|
-
else
|
|
183
|
-
builder.public_send(name, build_attributes(self)) do |xml|
|
|
184
|
-
children.each do |child|
|
|
185
|
-
child.build_xml(xml)
|
|
186
|
-
end
|
|
187
|
-
end
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
builder
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
private
|
|
194
|
-
|
|
195
|
-
def parse_children(node, root_node: nil)
|
|
196
|
-
node.children.select(&:element?).map do |child|
|
|
197
|
-
NokogiriElement.new(child, root_node: root_node)
|
|
198
|
-
end
|
|
199
|
-
end
|
|
200
|
-
|
|
201
|
-
def parse_all_children(node, root_node: nil, default_namespace: nil)
|
|
202
|
-
node.children.map do |child|
|
|
203
|
-
NokogiriElement.new(child, root_node: root_node,
|
|
204
|
-
default_namespace: default_namespace)
|
|
205
|
-
end
|
|
206
|
-
end
|
|
207
|
-
|
|
208
|
-
def build_attributes(node, _options = {})
|
|
209
|
-
attrs = node.attributes.transform_values(&:value)
|
|
210
|
-
|
|
211
|
-
attrs.merge(build_namespace_attributes(node))
|
|
212
|
-
end
|
|
213
|
-
|
|
214
|
-
def build_namespace_attributes(node)
|
|
215
|
-
namespace_attrs = {}
|
|
216
|
-
|
|
217
|
-
node.own_namespaces.each_value do |namespace|
|
|
218
|
-
namespace_attrs[namespace.attr_name] = namespace.uri
|
|
219
|
-
end
|
|
220
|
-
|
|
221
|
-
node.children.each do |child|
|
|
222
|
-
namespace_attrs = namespace_attrs.merge(
|
|
223
|
-
build_namespace_attributes(child),
|
|
224
|
-
)
|
|
225
|
-
end
|
|
226
|
-
|
|
227
|
-
namespace_attrs
|
|
228
|
-
end
|
|
7
|
+
class NokogiriAdapter < ::Lutaml::Model::Xml::NokogiriAdapter
|
|
8
|
+
Logger.warn_future_deprecation(
|
|
9
|
+
old: "Lutaml::Model::XmlAdapter::NokogiriAdapter",
|
|
10
|
+
replacement: "Lutaml::Model::Xml::NokogiriAdapter",
|
|
11
|
+
)
|
|
229
12
|
end
|
|
230
13
|
end
|
|
231
14
|
end
|
|
@@ -1,170 +1,20 @@
|
|
|
1
|
-
require "oga"
|
|
2
|
-
require "moxml/adapter/oga"
|
|
3
|
-
require_relative "xml_document"
|
|
4
|
-
require_relative "oga/document"
|
|
5
|
-
require_relative "oga/element"
|
|
6
|
-
require_relative "builder/oga"
|
|
1
|
+
# require "oga"
|
|
2
|
+
# require "moxml/adapter/oga"
|
|
3
|
+
# require_relative "xml_document"
|
|
4
|
+
# require_relative "oga/document"
|
|
5
|
+
# require_relative "oga/element"
|
|
6
|
+
# require_relative "builder/oga"
|
|
7
|
+
|
|
8
|
+
require_relative "../xml/oga_adapter"
|
|
7
9
|
|
|
8
10
|
module Lutaml
|
|
9
11
|
module Model
|
|
10
12
|
module XmlAdapter
|
|
11
|
-
class OgaAdapter <
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
@root = Oga::Element.new(parsed.children.first)
|
|
17
|
-
new(@root, encoding(xml, options))
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def to_xml(options = {})
|
|
21
|
-
builder_options = {}
|
|
22
|
-
builder_options[:encoding] = if options.key?(:encoding)
|
|
23
|
-
options[:encoding]
|
|
24
|
-
elsif options.key?(:parse_encoding)
|
|
25
|
-
options[:parse_encoding]
|
|
26
|
-
else
|
|
27
|
-
"UTF-8"
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
builder = Builder::Oga.build(builder_options) do |xml|
|
|
31
|
-
if @root.is_a?(Oga::Element)
|
|
32
|
-
@root.build_xml(xml)
|
|
33
|
-
else
|
|
34
|
-
build_element(xml, @root, options)
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
xml_data = builder.to_xml
|
|
38
|
-
options[:declaration] ? declaration(options) + xml_data : xml_data
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def attributes_hash(element)
|
|
42
|
-
result = Lutaml::Model::MappingHash.new
|
|
43
|
-
|
|
44
|
-
element.attributes.each do |attr|
|
|
45
|
-
if attr.name == "schemaLocation"
|
|
46
|
-
result["__schema_location"] = {
|
|
47
|
-
namespace: attr.namespace,
|
|
48
|
-
prefix: attr.namespace.prefix,
|
|
49
|
-
schema_location: attr.value,
|
|
50
|
-
}
|
|
51
|
-
else
|
|
52
|
-
result[self.class.namespaced_attr_name(attr)] = attr.value
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
result
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
def self.name_of(element)
|
|
60
|
-
case element
|
|
61
|
-
when Moxml::Text
|
|
62
|
-
"text"
|
|
63
|
-
when Moxml::Cdata
|
|
64
|
-
"cdata"
|
|
65
|
-
else
|
|
66
|
-
element.name
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
def self.prefixed_name_of(node)
|
|
71
|
-
return name_of(node) if TEXT_CLASSES.include?(node.class)
|
|
72
|
-
|
|
73
|
-
[node&.namespace&.prefix, node.name].compact.join(":")
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
def self.text_of(element)
|
|
77
|
-
element.content
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
def self.namespaced_attr_name(attribute)
|
|
81
|
-
attr_ns = attribute.namespace
|
|
82
|
-
attr_name = attribute.name
|
|
83
|
-
return attr_name unless attr_ns
|
|
84
|
-
|
|
85
|
-
prefix = attr_name == "lang" ? attr_ns.prefix : attr_ns.uri
|
|
86
|
-
[prefix, attr_name].compact.join(":")
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
def self.namespaced_name_of(node)
|
|
90
|
-
return name_of(node) unless node.respond_to?(:namespace)
|
|
91
|
-
|
|
92
|
-
[node&.namespace&.uri, node.name].compact.join(":")
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
def order
|
|
96
|
-
children.map do |child|
|
|
97
|
-
type = child.text? ? "Text" : "Element"
|
|
98
|
-
Element.new(type, child.unprefixed_name)
|
|
99
|
-
end
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
def self.order_of(element)
|
|
103
|
-
element.children.map do |child|
|
|
104
|
-
instance_args = if TEXT_CLASSES.include?(child.class)
|
|
105
|
-
["Text", "text"]
|
|
106
|
-
else
|
|
107
|
-
["Element", name_of(child)]
|
|
108
|
-
end
|
|
109
|
-
Element.new(*instance_args)
|
|
110
|
-
end
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
private
|
|
114
|
-
|
|
115
|
-
def build_ordered_element(builder, element, options = {})
|
|
116
|
-
mapper_class = determine_mapper_class(element, options)
|
|
117
|
-
xml_mapping = mapper_class.mappings_for(:xml)
|
|
118
|
-
return xml unless xml_mapping
|
|
119
|
-
|
|
120
|
-
attributes = build_attributes(element, xml_mapping).compact
|
|
121
|
-
|
|
122
|
-
tag_name = options[:tag_name] || xml_mapping.root_element
|
|
123
|
-
builder.create_and_add_element(tag_name,
|
|
124
|
-
attributes: attributes) do |el|
|
|
125
|
-
index_hash = {}
|
|
126
|
-
content = []
|
|
127
|
-
|
|
128
|
-
element.element_order.each do |object|
|
|
129
|
-
index_hash[object.name] ||= -1
|
|
130
|
-
curr_index = index_hash[object.name] += 1
|
|
131
|
-
|
|
132
|
-
element_rule = xml_mapping.find_by_name(object.name)
|
|
133
|
-
next if element_rule.nil?
|
|
134
|
-
|
|
135
|
-
attribute_def = attribute_definition_for(element, element_rule,
|
|
136
|
-
mapper_class: mapper_class)
|
|
137
|
-
value = attribute_value_for(element, element_rule)
|
|
138
|
-
|
|
139
|
-
next if element_rule == xml_mapping.content_mapping && element_rule.cdata && object.text?
|
|
140
|
-
|
|
141
|
-
if element_rule == xml_mapping.content_mapping
|
|
142
|
-
text = xml_mapping.content_mapping.serialize(element)
|
|
143
|
-
text = text[curr_index] if text.is_a?(Array)
|
|
144
|
-
|
|
145
|
-
next el.add_text(el, text, cdata: element_rule.cdata) if element.mixed?
|
|
146
|
-
|
|
147
|
-
content << text
|
|
148
|
-
elsif !value.nil? || element_rule.render_nil?
|
|
149
|
-
value = value[curr_index] if attribute_def.collection?
|
|
150
|
-
|
|
151
|
-
add_to_xml(
|
|
152
|
-
el,
|
|
153
|
-
element,
|
|
154
|
-
nil,
|
|
155
|
-
value,
|
|
156
|
-
options.merge(
|
|
157
|
-
attribute: attribute_def,
|
|
158
|
-
rule: element_rule,
|
|
159
|
-
mapper_class: mapper_class,
|
|
160
|
-
),
|
|
161
|
-
)
|
|
162
|
-
end
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
el.add_text(el, content.join)
|
|
166
|
-
end
|
|
167
|
-
end
|
|
13
|
+
class OgaAdapter < ::Lutaml::Model::Xml::OgaAdapter
|
|
14
|
+
Logger.warn_future_deprecation(
|
|
15
|
+
old: "Lutaml::Model::XmlAdapter::OgaAdapter",
|
|
16
|
+
replacement: "Lutaml::Model::Xml::OgaAdapter",
|
|
17
|
+
)
|
|
168
18
|
end
|
|
169
19
|
end
|
|
170
20
|
end
|