schematronium 0.0.0-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,55 @@
1
+ require 'saxon-xslt'
2
+ require 'nokogiri' # Used for output parsing - there doesn't seem to be a clean way to manipulate Saxon::XML documents
3
+
4
+ # Library for running Schematron validators over XML documents
5
+ class Schematronium
6
+ # Constructor for schematron checker
7
+ #
8
+ # @param [String, IO, File] schematron A schematron document, as either an IO object responding to #read,
9
+ # a filename, or a [String]
10
+ def initialize(schematron)
11
+ stages = %w|iso_dsdl_include.xsl
12
+ iso_abstract_expand.xsl
13
+ iso_svrl_for_xslt2.xsl|.map{|s| iso_file s}
14
+
15
+ schematron = case schematron
16
+ when IO
17
+ Saxon.XML(schematron.read)
18
+ when String
19
+ if File.file? schematron
20
+ Saxon.XML(File.open(schematron))
21
+ else
22
+ Saxon.XML(schematron)
23
+ end
24
+ end
25
+
26
+ # Run schematron through each stage of the iso_schematron pipeline
27
+ # then stringify the final result because Saxon.XSLT can't take
28
+ # an XML doc as input
29
+ @sch_script = Saxon.XSLT(
30
+ stages.reduce(schematron) do |result, stage|
31
+ stage.transform(result)
32
+ end.to_s
33
+ )
34
+ end
35
+
36
+ # Run schematron over xml document, returning the resulting XML
37
+ #
38
+ # @param [Saxon::XML::Document, IO, File] xml An XML document
39
+ # @return [Nokogiri::XML::Document]
40
+ def check(xml)
41
+ xml = Saxon.XML(xml) unless xml.is_a? Saxon::XML::Document
42
+ xml = Nokogiri::XML(@sch_script.transform(xml).to_s)
43
+ end
44
+
45
+ private
46
+
47
+ # Helper method returning stages of iso_schematron XSLT transform
48
+ #
49
+ # @param [String] fname The filename of a stage in the iso_schematron XSLT
50
+ # @return [Saxon::XSLT::Stylesheet]
51
+ def iso_file(fname)
52
+ Saxon.XSLT(File.open(File.join(File.dirname(File.expand_path(__FILE__)), 'iso-schematron-xslt2', fname)))
53
+ end
54
+
55
+ end
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = 'schematronium'
3
+ gem.version = '0.0.0'
4
+ gem.date = '2015-07-27'
5
+ gem.summary = 'Tool for running schematron against XML strings/files'
6
+ gem.description = 'Wraps the saxon-xslt wrapper for Saxon 9 HE, providing a simple (one function) interface for running a schematron against an XML string or file'
7
+ gem.authors = ['Dave Mayo']
8
+ gem.email = 'dave_mayo@harvard.edu'
9
+ gem.platform = 'java'
10
+ gem.require_paths = ["lib"]
11
+
12
+ gem.files = `git ls-files`.split($/)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test)/})
15
+
16
+ gem.homepage = 'http://github.com/harvard-library/schematronium'
17
+ gem.license = 'GPLv3'
18
+
19
+ # Runtime dependencies
20
+ gem.add_runtime_dependency "saxon-xslt", '~> 0.7'
21
+ gem.add_runtime_dependency "nokogiri", '~> 1.6'
22
+
23
+ gem.add_development_dependency "rake", '~> 10.4'
24
+ gem.add_development_dependency "minitest"
25
+
26
+ end
@@ -0,0 +1,13 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <schema xmlns="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2">
3
+ <pattern>
4
+ <rule context="*:test">
5
+ <report test="*:testable">
6
+ There should be 'testable' elements within 'test's.
7
+ </report>
8
+ </rule>
9
+ <rule context="*:test/*:testable">
10
+ <assert test="@test-attr">Testables must have have the 'test-attr' attribute.</assert>
11
+ </rule>
12
+ </pattern>
13
+ </schema>
@@ -0,0 +1,10 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <tests>
3
+ <test>
4
+ <testable test-attr="pass" />
5
+ </test>
6
+ <test>
7
+ <testable id="test1" />
8
+ </test>
9
+ <test/>
10
+ </tests>
@@ -0,0 +1,22 @@
1
+ require 'schematronium'
2
+ require 'minitest'
3
+ require 'minitest/pride'
4
+ require 'minitest/autorun'
5
+
6
+ class SchematroniumTest < MiniTest::Test
7
+ def td(*path_segments)
8
+ File.join(File.expand_path(File.dirname(__FILE__)), 'test_data', *path_segments)
9
+ end
10
+
11
+ def setup
12
+ @stron = Schematronium.new(td('schematron', 'test.sch'))
13
+ end
14
+
15
+ def test_check_with_file
16
+ results = @stron.check(File.open(td('xml', 'test.xml')))
17
+ results.remove_namespaces!
18
+ assert_equal 1, results.xpath("//failed-assert").count, "Expects one failure"
19
+ assert_equal 2, results.xpath("//successful-report").count, "Expects two reports"
20
+ end
21
+
22
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: schematronium
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: java
6
+ authors:
7
+ - Dave Mayo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ~>
17
+ - !ruby/object:Gem::Version
18
+ version: '0.7'
19
+ name: saxon-xslt
20
+ prerelease: false
21
+ type: :runtime
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '1.6'
33
+ name: nokogiri
34
+ prerelease: false
35
+ type: :runtime
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '10.4'
47
+ name: rake
48
+ prerelease: false
49
+ type: :development
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10.4'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ name: minitest
62
+ prerelease: false
63
+ type: :development
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Wraps the saxon-xslt wrapper for Saxon 9 HE, providing a simple (one function) interface for running a schematron against an XML string or file
70
+ email: dave_mayo@harvard.edu
71
+ executables:
72
+ - schematronium
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .ruby-version
78
+ - .travis.yml
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - bin/schematronium
83
+ - lib/iso-schematron-xslt2/ExtractSchFromRNG-2.xsl
84
+ - lib/iso-schematron-xslt2/ExtractSchFromXSD-2.xsl
85
+ - lib/iso-schematron-xslt2/iso_abstract_expand.xsl
86
+ - lib/iso-schematron-xslt2/iso_dsdl_include.xsl
87
+ - lib/iso-schematron-xslt2/iso_schematron_message_xslt2.xsl
88
+ - lib/iso-schematron-xslt2/iso_schematron_skeleton_for_saxon.xsl
89
+ - lib/iso-schematron-xslt2/iso_svrl_for_xslt2.xsl
90
+ - lib/iso-schematron-xslt2/readme.txt
91
+ - lib/iso-schematron-xslt2/sch-messages-cs.xhtml
92
+ - lib/iso-schematron-xslt2/sch-messages-de.xhtml
93
+ - lib/iso-schematron-xslt2/sch-messages-en.xhtml
94
+ - lib/iso-schematron-xslt2/sch-messages-fr.xhtml
95
+ - lib/iso-schematron-xslt2/sch-messages-nl.xhtml
96
+ - lib/iso-schematron-xslt2/schematron-skeleton-api.htm
97
+ - lib/schematronium.rb
98
+ - schematronium.gemspec
99
+ - test/test_data/schematron/test.sch
100
+ - test/test_data/xml/test.xml
101
+ - test/test_schematronium.rb
102
+ homepage: http://github.com/harvard-library/schematronium
103
+ licenses:
104
+ - GPLv3
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.4.6
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Tool for running schematron against XML strings/files
126
+ test_files:
127
+ - test/test_data/schematron/test.sch
128
+ - test/test_data/xml/test.xml
129
+ - test/test_schematronium.rb