Skip to content

Commit

Permalink
[WIP]: Basic schema flattening changes
Browse files Browse the repository at this point in the history
  • Loading branch information
suleman-uzair committed Nov 21, 2024
1 parent 45c3a93 commit bce680f
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 21 deletions.
32 changes: 32 additions & 0 deletions lib/lutaml/xsd/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Lutaml
module Xsd
module Config
module_function

def set_path_or_url(location)
return if location.nil?

@url = location if location.start_with?(/http\w?:\/{2}[^.]+/)
@path = location unless @url
end

def path
@path
end

def url
@url
end

def path?
!@path.nil?
end

def url?
!@url.nil?
end
end
end
end
1 change: 1 addition & 0 deletions lib/lutaml/xsd/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Xsd
class Import < Lutaml::Model::Serializable
attribute :id, :string
attribute :namespace, :string
attribute :schema_location, :string

xml do
root "import", mixed: true
Expand Down
12 changes: 6 additions & 6 deletions lib/lutaml/xsd/restriction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ module Lutaml
module Xsd
class Restriction < Lutaml::Model::Serializable
attribute :base, :string
attribute :min_inclusive, MinInclusive, collection: true
attribute :max_inclusive, MaxInclusive, collection: true
attribute :enumeration, Enumeration, collection: true
attribute :white_space, WhiteSpace, collection: true
attribute :annotation, Annotation, collection: true
attribute :max_length, MaxLength, collection: true
attribute :max_length, MaxLength
attribute :min_inclusive, MinInclusive
attribute :max_inclusive, MaxInclusive
attribute :pattern, Pattern, collection: true
attribute :annotation, Annotation, collection: true
attribute :white_space, WhiteSpace, collection: true
attribute :enumeration, Enumeration, collection: true
xml do
root "restriction", mixed: true
namespace "http://www.w3.org/2001/XMLSchema", "xsd"
Expand Down
15 changes: 14 additions & 1 deletion lib/lutaml/xsd/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Xsd
class Schema < Lutaml::Model::Serializable
attribute :xmlns, :string
attribute :import, Import, collection: true
attribute :schema, Schema, collection: true
attribute :include, Include, collection: true
attribute :element, Element, collection: true
attribute :complex_type, ComplexType, collection: true
Expand All @@ -21,7 +22,7 @@ class Schema < Lutaml::Model::Serializable
namespace "http://www.w3.org/2001/XMLSchema", "xsd"

map_attribute :xmlns, to: :xmlns, namespace: "http://csrc.nist.gov/ns/oscal/metaschema/1.0", prefix: nil
map_element :import, to: :import
map_element :import, to: :import, with: { from: :from_schema, to: :to_schema }
map_element :include, to: :include
map_element :element, to: :element
map_element :complexType, to: :complex_type
Expand All @@ -33,6 +34,18 @@ class Schema < Lutaml::Model::Serializable
map_attribute :blockDefault, to: :block_default
map_attribute :targetNamespace, to: :target_namespace
end

def from_schema(model, value)
# IN-PROGRESS
# value.each do |imported_schema|
# binding.irb
# imported_schema.from_schema(model, value)
# end
end

def to_schema(model, parent, doc)
# binding.irb
end
end
end
end
3 changes: 2 additions & 1 deletion lib/lutaml/xsd/xsd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Error < StandardError; end

module_function

def parse(xsd)
def parse(xsd, location: nil)
Config.set_path_or_url(location)
Schema.from_xml(xsd)
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<!-- <xs:include schemaLocation="metaschema-prose-base.xsd"/> -->
<xs:include schemaLocation="metaschema-prose-base.xsd"/>
<xs:complexType name="MarkupLineDatatype" mixed="true">
<xs:complexContent>
<xs:extension base="inlineMarkupType"/>
Expand Down
2 changes: 1 addition & 1 deletion spec/lutaml/fixtures/metaschema-markup-multiline.xsd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

<!-- <xs:include schemaLocation="metaschema-prose-base.xsd"/> -->
<xs:include schemaLocation="metaschema-prose-base.xsd"/>

<xs:complexType name="MarkupMultilineDatatype">
<xs:choice minOccurs="1" maxOccurs="unbounded">
Expand Down
4 changes: 2 additions & 2 deletions spec/lutaml/fixtures/metaschema-prose-module.xsd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.1" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<!-- <xs:include schemaLocation="metaschema-markup-line.xsd"/>
<xs:include schemaLocation="metaschema-markup-multiline.xsd"/> -->
<xs:include schemaLocation="metaschema-markup-line.xsd"/>
<xs:include schemaLocation="metaschema-markup-multiline.xsd"/>
</xs:schema>
14 changes: 7 additions & 7 deletions spec/lutaml/fixtures/omml_schema.xsd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified" blockDefault="#all" targetNamespace="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<!-- <xsd:import id="w" namespace="http://schemas.openxmlformats.org/wordprocessingml/2006/main" schemaLocation="wml.xsd" />
<xsd:import id="xml" namespace="http://www.w3.org/XML/1998/namespace" /> -->
<xsd:import id="w" namespace="http://schemas.openxmlformats.org/wordprocessingml/2006/main" schemaLocation="/master/wml.xsd" />
<xsd:import id="xml" namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:simpleType name="ST_Integer255">
<xsd:annotation>
<xsd:documentation>Integer value (1 to 255)</xsd:documentation>
Expand Down Expand Up @@ -410,7 +410,7 @@
<xsd:complexType name="CT_Text">
<xsd:simpleContent>
<xsd:extension base="ST_String">
<xsd:attribute use="optional">
<xsd:attribute ref="xml:space" use="optional">
<xsd:annotation>
<xsd:documentation>Content Contains Significant Whitespace</xsd:documentation>
</xsd:annotation>
Expand All @@ -425,9 +425,9 @@
<xsd:documentation>Run Properties</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:group minOccurs="0" />
<xsd:group ref="w:EG_RPr" minOccurs="0" />
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:group />
<xsd:group ref="w:EG_RunInnerContent" />
<xsd:element name="t" type="CT_Text" minOccurs="0">
<xsd:annotation>
<xsd:documentation>Text</xsd:documentation>
Expand All @@ -438,7 +438,7 @@
</xsd:complexType>
<xsd:complexType name="CT_CtrlPr">
<xsd:sequence>
<xsd:group minOccurs="0" />
<xsd:group ref="w:EG_RPrMath" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="CT_AccPr">
Expand Down Expand Up @@ -1264,7 +1264,7 @@
<xsd:group name="EG_OMathElements">
<xsd:choice>
<xsd:group ref="EG_OMathMathElements" />
<xsd:group />
<xsd:group ref="w:EG_RunLevelElts" />
</xsd:choice>
</xsd:group>
<xsd:complexType name="CT_OMathArgPr">
Expand Down
3 changes: 2 additions & 1 deletion spec/lutaml/xsd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
require "spec_helper"

RSpec.describe Lutaml::Xsd do
subject(:parsed_schema) { described_class.parse(schema) }
# TODO: remove dummy location url
subject(:parsed_schema) { described_class.parse(schema, location: "https://www.something.com/") }

it "has a version number" do
expect(Lutaml::Xsd::VERSION).not_to be nil
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@

def schema_to_xml(xml, escape_content_tags: false)
xml = Xml::C14n.format(xml)
xml.gsub!(/&lt;([^&]+)&\gt;/, '<\1>') if escape_content_tags
xml.gsub!(/&lt;([^&]+)&gt;/, '<\1>') if escape_content_tags
xml
end

0 comments on commit bce680f

Please sign in to comment.