Skip to content

Commit

Permalink
XSD parser using LutaML-Model (#2)
Browse files Browse the repository at this point in the history
* XSD parsing LutaML-Model classes

* chore: removed zeitwerk default logger

* Added round-trip xml specs, classes, and attributes

* fix: failing specs

* chore: updated dir path string

* chore: changed matcher gem and moved adapter from method to base file

---------

Co-authored-by: suleman-uzair <[email protected]>
  • Loading branch information
suleman-uzair and suleman-uzair authored Nov 20, 2024
1 parent e135689 commit 45c3a93
Show file tree
Hide file tree
Showing 44 changed files with 4,096 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ Style/StringLiterals:

Style/StringLiteralsInInterpolation:
EnforcedStyle: double_quotes

Documentation:
Enabled: false
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ source "https://rubygems.org"
# Specify your gem's dependencies in lutaml-xsd.gemspec
gemspec

gem "equivalent-xml"

gem "rake", "~> 13.0"

gem "rspec", "~> 3.0"

gem "rubocop", "~> 1.21"

gem "nokogiri"

gem "xml-c14n"
16 changes: 9 additions & 7 deletions lib/lutaml/xsd.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# frozen_string_literal: true

require_relative "xsd/version"
require "zeitwerk"
require "lutaml/model"
require_relative "xsd/xsd"

module Lutaml
module Xsd
class Error < StandardError; end
# Your code goes here...
end
end
Lutaml::Model::Config.xml_adapter_type = :nokogiri

loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: true)
loader.push_dir("#{__dir__}/xsd", namespace: Lutaml::Xsd)
loader.ignore("#{__dir__}/lib/lutaml/xsd.rb")
loader.setup
16 changes: 16 additions & 0 deletions lib/lutaml/xsd/annotation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class Annotation < Lutaml::Model::Serializable
attribute :documentation, Documentation, collection: true

xml do
root "annotation", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_element :documentation, to: :documentation
end
end
end
end
18 changes: 18 additions & 0 deletions lib/lutaml/xsd/any.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class Any < Lutaml::Model::Serializable
attribute :namespace, :string
attribute :process_contents, :string

xml do
root "any", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_attribute :namespace, to: :namespace
map_attribute :processContents, to: :process_contents
end
end
end
end
26 changes: 26 additions & 0 deletions lib/lutaml/xsd/attribute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class Attribute < Lutaml::Model::Serializable
attribute :use, :string
attribute :name, :string
attribute :type, :string
attribute :default, :string
attribute :annotation, Annotation, collection: true
attribute :simple_type, SimpleType, collection: true

xml do
root "attribute", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_attribute :use, to: :use
map_attribute :name, to: :name
map_attribute :type, to: :type
map_attribute :default, to: :default
map_element :annotation, to: :annotation
map_element :simpleType, to: :simple_type
end
end
end
end
22 changes: 22 additions & 0 deletions lib/lutaml/xsd/attribute_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class AttributeGroup < Lutaml::Model::Serializable
attribute :name, :string
attribute :ref, :string
attribute :annotation, Annotation, collection: true
attribute :attribute, Attribute, collection: true

xml do
root "attributeGroup", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_attribute :ref, to: :ref
map_attribute :name, to: :name
map_element :annotation, to: :annotation
map_element :attribute, to: :attribute
end
end
end
end
28 changes: 28 additions & 0 deletions lib/lutaml/xsd/choice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class Sequence < Lutaml::Model::Serializable; end

class Choice < Lutaml::Model::Serializable
attribute :any, Any, collection: true
attribute :min_occurs, :string
attribute :max_occurs, :string
attribute :element, Element, collection: true
attribute :sequence, Sequence, collection: true
attribute :group, Group, collection: true

xml do
root "choice", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_attribute :minOccurs, to: :min_occurs
map_attribute :maxOccurs, to: :max_occurs
map_element :element, to: :element
map_element :sequence, to: :sequence
map_element :group, to: :group
map_element :any, to: :any
end
end
end
end
16 changes: 16 additions & 0 deletions lib/lutaml/xsd/complex_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class ComplexContent < Lutaml::Model::Serializable
attribute :extension, Extension, collection: true

xml do
root "complexContent", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_element :extension, to: :extension
end
end
end
end
34 changes: 34 additions & 0 deletions lib/lutaml/xsd/complex_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class ComplexType < Lutaml::Model::Serializable
attribute :name, :string
attribute :mixed, :string
attribute :abstract, :string
attribute :choice, Choice, collection: true
attribute :sequence, Sequence, collection: true
attribute :attribute, Attribute, collection: true
attribute :annotation, Annotation, collection: true
attribute :attribute_group, AttributeGroup, collection: true
attribute :simple_content, SimpleContent, collection: true
attribute :complex_content, ComplexContent, collection: true

xml do
root "complexType", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_attribute :name, to: :name
map_attribute :mixed, to: :mixed
map_attribute :abstract, to: :abstract
map_element :choice, to: :choice
map_element :sequence, to: :sequence
map_element :attribute, to: :attribute
map_element :annotation, to: :annotation
map_element :attributeGroup, to: :attribute_group
map_element :simpleContent, to: :simple_content
map_element :complexContent, to: :complex_content
end
end
end
end
16 changes: 16 additions & 0 deletions lib/lutaml/xsd/documentation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class Documentation < Lutaml::Model::Serializable
attribute :content, :string

xml do
root "documentation"
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_all to: :content
end
end
end
end
34 changes: 34 additions & 0 deletions lib/lutaml/xsd/element.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class Element < Lutaml::Model::Serializable
attribute :name, :string
attribute :type, :string
attribute :default, :string
attribute :min_occurs, :string
attribute :max_occurs, :string
attribute :key, Key, collection: true
attribute :unique, Unique, collection: true
attribute :complex_type, ComplexType, collection: true
attribute :simple_type, SimpleType, collection: true
attribute :annotation, Annotation, collection: true

xml do
root "element", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_attribute :name, to: :name
map_attribute :type, to: :type
map_attribute :default, to: :default
map_attribute :minOccurs, to: :min_occurs
map_attribute :maxOccurs, to: :max_occurs
map_element :complexType, to: :complex_type
map_element :simpleType, to: :simple_type
map_element :annotation, to: :annotation
map_element :unique, to: :unique
map_element :key, to: :key
end
end
end
end
18 changes: 18 additions & 0 deletions lib/lutaml/xsd/enumeration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class Enumeration < Lutaml::Model::Serializable
attribute :value, :string
attribute :annotation, Annotation, collection: true

xml do
root "enumeration", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_attribute :value, to: :value
map_element :annotation, to: :annotation
end
end
end
end
24 changes: 24 additions & 0 deletions lib/lutaml/xsd/extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class Extension < Lutaml::Model::Serializable
attribute :base, :string
attribute :sequence, Sequence, collection: true
attribute :attribute, Attribute, collection: true
attribute :annotation, Annotation, collection: true
attribute :attribute_group, AttributeGroup, collection: true

xml do
root "extension", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_attribute :base, to: :base
map_element :sequence, to: :sequence
map_element :attribute, to: :attribute
map_element :annotation, to: :annotation
map_element :attributeGroup, to: :attribute_group
end
end
end
end
16 changes: 16 additions & 0 deletions lib/lutaml/xsd/field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class Field < Lutaml::Model::Serializable
attribute :xpath, :string

xml do
root "field", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_attribute :xpath, to: :xpath
end
end
end
end
31 changes: 31 additions & 0 deletions lib/lutaml/xsd/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class Sequence < Lutaml::Model::Serializable; end
class Choice < Lutaml::Model::Serializable; end

class Group < Lutaml::Model::Serializable
attribute :ref, :string
attribute :name, :string
attribute :min_occurs, :string
attribute :max_occurs, :string
attribute :choice, Choice, collection: true
attribute :sequence, Sequence, collection: true
attribute :annotation, Annotation, collection: true

xml do
root "group", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_attribute :ref, to: :ref
map_attribute :name, to: :name
map_attribute :minOccurs, to: :min_occurs
map_attribute :maxOccurs, to: :max_occurs
map_element :annotation, to: :annotation
map_element :sequence, to: :sequence
map_element :choice, to: :choice
end
end
end
end
18 changes: 18 additions & 0 deletions lib/lutaml/xsd/import.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class Import < Lutaml::Model::Serializable
attribute :id, :string
attribute :namespace, :string

xml do
root "import", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_attribute :id, to: :id
map_attribute :namespace, to: :namespace
end
end
end
end
16 changes: 16 additions & 0 deletions lib/lutaml/xsd/include.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Lutaml
module Xsd
class Include < Lutaml::Model::Serializable
attribute :annotation, Annotation, collection: true

xml do
root "include", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_element :annotation, to: :annotation
end
end
end
end
Loading

0 comments on commit 45c3a93

Please sign in to comment.