nokogiri_schematron_builder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ require "nokogiri/xml/schematron/base"
2
+
3
+ module Nokogiri
4
+ module XML
5
+ module Schematron
6
+ # The internal representation of the +<sch:p>+ XML element.
7
+ #
8
+ # For example:
9
+ #
10
+ # paragraph = Nokogiri::XML::Schematron::Paragraph.new(nil, message: "Hello, world!")
11
+ # # => #<Nokogiri::XML::Schematron::Paragraph:0x00007f848783e628 @parent=nil, @children=[], @options={:message=>"Hello, world!"}>
12
+ # paragraph.to_builder.to_xml
13
+ # # => "<?xml version=\"1.0\"?>\n<sch:p xmlns:sch=\"http://purl.oclc.org/dsdl/schematron\">Hello, world!</sch:p>\n"
14
+ #
15
+ class Paragraph < Nokogiri::XML::Schematron::Base
16
+ # @!attribute [rw] message
17
+ # @return [String] the text content of the +<sch:p>+ XML element.
18
+ attribute :message
19
+
20
+ protected
21
+
22
+ def build!(xml)
23
+ xml["sch"].send(:p, xmlns) do
24
+ unless (s = send(:message)).nil?
25
+ xml.text(s)
26
+ end
27
+
28
+ super(xml)
29
+ end
30
+
31
+ return
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,150 @@
1
+ require "nokogiri/xml/schematron/base"
2
+ require "nokogiri/xml/schematron/nodes/context"
3
+ require "nokogiri/xml/schematron/paragraph"
4
+ require "nokogiri/xml/schematron/rule"
5
+
6
+ module Nokogiri
7
+ module XML
8
+ module Schematron
9
+ # The internal representation of the +<sch:pattern>+ XML element.
10
+ #
11
+ # For example:
12
+ #
13
+ # pattern = Nokogiri::XML::Schematron::Pattern.new(nil, id: "pattern1", name: "Example pattern")
14
+ # # => #<Nokogiri::XML::Schematron::Pattern:0x00007f8486a71f18 @parent=nil, @children=[], @options={:id=>"pattern1", :name=>"Example pattern"}>
15
+ # pattern.to_builder.to_xml
16
+ # # => "<?xml version=\"1.0\"?>\n<sch:pattern xmlns:sch=\"http://purl.oclc.org/dsdl/schematron\" id=\"pattern1\" name=\"Example pattern\"/>\n"
17
+ #
18
+ class Pattern < Nokogiri::XML::Schematron::Base
19
+ # @!attribute [rw] id
20
+ # @return [String] the value of the +@id+ XML attribute.
21
+ attribute :id
22
+
23
+ # @!attribute [rw] name
24
+ # @return [String] the value of the +@name+ XML attribute.
25
+ attribute :name
26
+
27
+ # @!method context(context, **options, &block)
28
+ # Create a new +Node::Context+ object.
29
+ # @example Denest an abstract syntax tree into a sequence of zero or more +<sch:rule>+ XML elements.
30
+ # # For example, the following...
31
+ # context("/") do
32
+ # require("ex:A") do
33
+ # permit("ex:B") do
34
+ # reject("ex:C")
35
+ # end
36
+ # end
37
+ # end
38
+ #
39
+ # # ...is equivalent to:
40
+ # rule(context: "/") do
41
+ # assert(test: "count(ex:A) &gt;= 1", message: "element \"ex:A\" is REQUIRED")
42
+ # end
43
+ # rule(context: "/ex:A") do
44
+ # assert(test: "count(ex:B) &gt;= 0", message: "element \"ex:B\" is OPTIONAL")
45
+ # end
46
+ # rule(context: "/ex:A/ex:B") do
47
+ # assert(test: "not(ex:C)", message: "element \"ex:C\" is NOT RECOMMENDED")
48
+ # end
49
+ # @example Create business rules for the XML representation of a set of values.
50
+ # context("/") do
51
+ # require("ex:Example") do
52
+ # permit("ex:ExampleType") do
53
+ # validates_inclusion_of "text()", in: ["A", "B", "C"]
54
+ # end
55
+ # end
56
+ # end
57
+ # @example Create business rules for the XML representation of a set of values that is augmented with an "Other" value.
58
+ # context("/") do
59
+ # require("ex:Example") do
60
+ # permit("ex:ExampleType") do
61
+ # validates_inclusion_of "text()", in: ["A", "B", "C", "Other"]
62
+ # end
63
+ # end
64
+ # permit("ex:Example[ex:ExampleType and (ex:ExampleType/text() != 'Other')]") do
65
+ # reject("ex:OtherExampleType")
66
+ # end
67
+ # permit("ex:Example[ex:ExampleType and (ex:ExampleType/text() = 'Other')]") do
68
+ # require("ex:OtherExampleType")
69
+ # end
70
+ # permit("ex:Example[ex:OtherExampleType]") do
71
+ # require("ex:ExampleType") do
72
+ # validates_inclusion_of "text()", in: ["Other"]
73
+ # end
74
+ # end
75
+ # end
76
+ # @example Create business rules for the XML representation of a set of zero or more key-value pairs.
77
+ # context("/") do
78
+ # require("ex:KeyValuePairs") do
79
+ # permit("ex:KeyValuePair") do
80
+ # require("ex:Key") do
81
+ # validates_inclusion_of "text()", in: ["A", "B", "C"]
82
+ # end
83
+ # require("ex:Value")
84
+ # end
85
+ # permit("ex:KeyValuePair[ex:Key/text() = 'A']/ex:Value") do
86
+ # validates_inclusion_of "text()", in: ["a"]
87
+ # end
88
+ # permit("ex:KeyValuePair[ex:Key/text() = 'B']/ex:Value") do
89
+ # validates_inclusion_of "text()", in: ["b"]
90
+ # end
91
+ # permit("ex:KeyValuePair[ex:Key/text() = 'C']/ex:Value") do
92
+ # validates_inclusion_of "text()", in: ["c"]
93
+ # end
94
+ # end
95
+ # end
96
+ # @example Create business rules for an entity-relationship model.
97
+ # context("//ex:UserAccount") do
98
+ # require("@ID") do
99
+ # validates_numericality_of "text()", greater_than_or_equal_to: 1
100
+ # end
101
+ # permit("ex:Username") do
102
+ # validates_exclusion_of "text()", in: ["admin"]
103
+ # end
104
+ # reject("ex:Password")
105
+ # end
106
+ # @param context [String] the XPath.
107
+ # @param options [Hash<Symbol, Object>] the options.
108
+ # @yieldparam node [Nokogiri::XML::Schematron::Node::Context] the +Node::Context+ object.
109
+ # @yieldreturn [void]
110
+ # @return [Nokogiri::XML::Schematron::Node::Context] the +Node::Context+ object.
111
+ element :context, Nokogiri::XML::Schematron::Nodes::Context
112
+
113
+ # @!method p(**options, &block)
114
+ # Create a new +Paragraph+ object.
115
+ # @param options [Hash<Symbol, Object>] the options.
116
+ # @option options [String] :message the text content of the +<sch:p>+ XML element.
117
+ # @yieldparam p [Nokogiri::XML::Schematron::Paragraph] the internal representation of the +<sch:p>+ XML element.
118
+ # @yieldreturn [void]
119
+ # @return [Nokogiri::XML::Schematron::Paragraph] the +Paragraph+ object.
120
+ element :p, Nokogiri::XML::Schematron::Paragraph
121
+
122
+ # @!method rule(**options, &block)
123
+ # Create a new +Rule+ object.
124
+ # @param options [Hash<Symbol, Object>] the options.
125
+ # @option options [String] :context the value of the +@context+ XML attribute.
126
+ # @option options [String] :id the value of the +@id+ XML attribute.
127
+ # @yieldparam rule [Nokogiri::XML::Schematron::Rule] the internal representation of the +<sch:rule>+ XML element.
128
+ # @yieldreturn [void]
129
+ # @return [Nokogiri::XML::Schematron::Rule] the +Rule+ object.
130
+ element :rule, Nokogiri::XML::Schematron::Rule
131
+
132
+ protected
133
+
134
+ def build!(xml)
135
+ xml["sch"].send(:pattern, %w(id name).inject(xmlns) { |acc, method_name|
136
+ unless (s = send(method_name.to_sym)).nil?
137
+ acc[method_name.to_s] = s
138
+ end
139
+
140
+ acc
141
+ }) do
142
+ super(xml)
143
+ end
144
+
145
+ return
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,53 @@
1
+ require "nokogiri/xml/schematron/assertion"
2
+ require "nokogiri/xml/schematron/base"
3
+
4
+ module Nokogiri
5
+ module XML
6
+ module Schematron
7
+ # The internal representation of the +<sch:rule>+ XML element.
8
+ #
9
+ # For example:
10
+ #
11
+ # rule = Nokogiri::XML::Schematron::Rule.new(nil, id: "rule1", context: "//ex:Example")
12
+ # # => #<Nokogiri::XML::Schematron::Rule:0x00007fa1fb9e8410 @parent=nil, @children=[], @options={:id=>"rule1", :context=>"//ex:Example"}>
13
+ # rule.to_builder.to_xml
14
+ # # => "<?xml version=\"1.0\"?>\n<sch:rule xmlns:sch=\"http://purl.oclc.org/dsdl/schematron\" id=\"rule1\" context=\"//ex:Example\"/>\n"
15
+ #
16
+ class Rule < Nokogiri::XML::Schematron::Base
17
+ # @!attribute [rw] context
18
+ # @return [String] the value of the +@context+ XML attribute.
19
+ attribute :context
20
+
21
+ # @!attribute [rw] id
22
+ # @return [String] the value of the +@id+ XML attribute.
23
+ attribute :id
24
+
25
+ # @!method assert(**options, &block)
26
+ # Create a new +Assertion+ object.
27
+ # @param options [Hash<Symbol, Object>] the options.
28
+ # @option options [String] :message the text content of the +<sch:assert>+ XML element.
29
+ # @option options [String] :test the value of the +@test+ XML attribute.
30
+ # @yieldparam assert [Nokogiri::XML::Schematron::Assertion] the internal representation of the +<sch:assert>+ XML element.
31
+ # @yieldreturn [void]
32
+ # @return [Nokogiri::XML::Schematron::Assertion] the +Assertion+ object.
33
+ element :assert, Nokogiri::XML::Schematron::Assertion
34
+
35
+ protected
36
+
37
+ def build!(xml)
38
+ xml["sch"].send(:rule, %w(id context).inject(xmlns) { |acc, method_name|
39
+ unless (s = send(method_name.to_sym)).nil?
40
+ acc[method_name.to_s] = s
41
+ end
42
+
43
+ acc
44
+ }) do
45
+ super(xml)
46
+ end
47
+
48
+ return
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,85 @@
1
+ require "nokogiri/xml/schematron/base"
2
+ require "nokogiri/xml/schematron/namespace"
3
+ require "nokogiri/xml/schematron/paragraph"
4
+ require "nokogiri/xml/schematron/pattern"
5
+
6
+ module Nokogiri
7
+ module XML
8
+ module Schematron
9
+ # The internal representation of the +<sch:schema>+ XML element.
10
+ #
11
+ # For example:
12
+ #
13
+ # schema = Nokogiri::XML::Schematron::Schema.new(id: "schema1", title: "Example schema")
14
+ # # => #<Nokogiri::XML::Schematron::Schema:0x00007fa1fb9e3b68 @parent=nil, @children=[], @options={:id=>"schema1", :title=>"Example schema"}>
15
+ # schema.to_builder.to_xml
16
+ # # => "<?xml version=\"1.0\"?>\n<sch:schema xmlns:sch=\"http://purl.oclc.org/dsdl/schematron\" id=\"schema1\" title=\"Example schema\"/>\n"
17
+ #
18
+ class Schema < Nokogiri::XML::Schematron::Base
19
+ # @!attribute [rw] id
20
+ # @return [String] the value of the +@id+ XML attribute.
21
+ attribute :id
22
+
23
+ # @!attribute [rw] title
24
+ # @return [String] the value of the +@title+ XML attribute.
25
+ attribute :title
26
+
27
+ # @!method ns(**options, &block)
28
+ # Create a new +Namespace+ object.
29
+ # @param options [Hash<Symbol, Object>] the options.
30
+ # @option options [String] :prefix the value of the +@prefix+ XML attribute.
31
+ # @option options [String] :uri the value of the +@uri+ XML attribute.
32
+ # @yieldparam ns [Nokogiri::XML::Schematron::Namespace] the internal representation of the +<sch:ns>+ XML element.
33
+ # @yieldreturn [void]
34
+ # @return [Nokogiri::XML::Schematron::Namespace] the +Namespace+ object.
35
+ element :ns, Nokogiri::XML::Schematron::Namespace
36
+
37
+ # @!method p(**options, &block)
38
+ # Create a new +Paragraph+ object.
39
+ # @param options [Hash<Symbol, Object>] the options.
40
+ # @option options [String] :message the text content of the +<sch:p>+ XML element.
41
+ # @yieldparam p [Nokogiri::XML::Schematron::Paragraph] the internal representation of the +<sch:p>+ XML element.
42
+ # @yieldreturn [void]
43
+ # @return [Nokogiri::XML::Schematron::Paragraph] the +Paragraph+ object.
44
+ element :p, Nokogiri::XML::Schematron::Paragraph
45
+
46
+ # @!method pattern(**options, &block)
47
+ # Create a new +Pattern+ object.
48
+ # @param options [Hash<Symbol, Object>] the options.
49
+ # @option options [String] :id the value of the +@id+ XML attribute.
50
+ # @option options [String] :name the value of the +@name+ XML attribute.
51
+ # @yieldparam pattern [Nokogiri::XML::Schematron::Pattern] the internal representation of the +<sch:pattern>+ XML element.
52
+ # @yieldreturn [void]
53
+ # @return [Nokogiri::XML::Schematron::Pattern] the +Pattern+ object.
54
+ element :pattern, Nokogiri::XML::Schematron::Pattern
55
+
56
+ # Create a new +Schema+ object.
57
+ #
58
+ # @param options [Hash<Symbol, Object>] the options.
59
+ # @option options [#to_s] :id the value of the +@id+ XML attribute.
60
+ # @option options [#to_s] :title the value of the +@title+ XML attribute.
61
+ # @yieldparam schema [Nokogiri::XML::Schematron::Schema] the internal representation of the +<sch:schema>+ XML element.
62
+ # @yieldreturn [void]
63
+ def initialize(**options, &block)
64
+ super(nil, **options, &block)
65
+ end
66
+
67
+ protected
68
+
69
+ def build!(xml)
70
+ xml["sch"].send(:schema, %w(id title).inject(xmlns) { |acc, method_name|
71
+ unless (s = send(method_name.to_sym)).nil?
72
+ acc[method_name.to_s] = s
73
+ end
74
+
75
+ acc
76
+ }) do
77
+ super(xml)
78
+ end
79
+
80
+ return
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,7 @@
1
+ module Nokogiri
2
+ module XML
3
+ module Schematron
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,38 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "nokogiri/xml/schematron/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nokogiri_schematron_builder"
8
+ spec.version = Nokogiri::XML::Schematron::VERSION
9
+ spec.authors = ["Mark Borkum"]
10
+ spec.email = ["mark.borkum@pnnl.gov"]
11
+ spec.license = "BSD-2-Clause"
12
+ spec.summary = %q{Build Schematron XML documents using Nokogiri.}
13
+ spec.description = %q{NokogiriSchematronBuilder is a Ruby library for building Schematron XML documents using Nokogiri.}
14
+ spec.homepage = "https://github.com/pnnl/nokogiri_schematron_builder"
15
+
16
+ # # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against " \
22
+ # "public gem pushes."
23
+ # end
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_dependency "nokogiri"
35
+
36
+ spec.add_development_dependency "bundler", "~> 1.16"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nokogiri_schematron_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mark Borkum
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: NokogiriSchematronBuilder is a Ruby library for building Schematron XML
56
+ documents using Nokogiri.
57
+ email:
58
+ - mark.borkum@pnnl.gov
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - WARRANTY.txt
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/nokogiri/xml/schematron.rb
72
+ - lib/nokogiri/xml/schematron/assertion.rb
73
+ - lib/nokogiri/xml/schematron/base.rb
74
+ - lib/nokogiri/xml/schematron/internal/core_ext/array.rb
75
+ - lib/nokogiri/xml/schematron/namespace.rb
76
+ - lib/nokogiri/xml/schematron/nodes/base.rb
77
+ - lib/nokogiri/xml/schematron/nodes/context.rb
78
+ - lib/nokogiri/xml/schematron/nodes/permit.rb
79
+ - lib/nokogiri/xml/schematron/nodes/reject.rb
80
+ - lib/nokogiri/xml/schematron/nodes/require.rb
81
+ - lib/nokogiri/xml/schematron/nodes/validations/exclusion.rb
82
+ - lib/nokogiri/xml/schematron/nodes/validations/inclusion.rb
83
+ - lib/nokogiri/xml/schematron/nodes/validations/numericality.rb
84
+ - lib/nokogiri/xml/schematron/paragraph.rb
85
+ - lib/nokogiri/xml/schematron/pattern.rb
86
+ - lib/nokogiri/xml/schematron/rule.rb
87
+ - lib/nokogiri/xml/schematron/schema.rb
88
+ - lib/nokogiri/xml/schematron/version.rb
89
+ - nokogiri_schematron_builder.gemspec
90
+ homepage: https://github.com/pnnl/nokogiri_schematron_builder
91
+ licenses:
92
+ - BSD-2-Clause
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.7.6
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Build Schematron XML documents using Nokogiri.
114
+ test_files: []