jxmlvalidator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ Java based XML Validator (for ruby)
2
+ ===================================
3
+
4
+ Why do this when libxml & friends already support validation?
5
+
6
+ Libxml (fast) does not support mixed-namespace validation and as of this writing have no plans to.
7
+ JAXP (slow) is a very complete XML stack and does support mixed-namespace validation.
8
+
9
+ quickstart
10
+ ----------
11
+
12
+ require 'jxml/validator'
13
+
14
+ val = JXML::Validator.new
15
+ results = val.validate some_big_nasty_xml
16
+
17
+ results[:errors].each do |e|
18
+ puts "#{e[:line]}: #{e[:message]}"
19
+ end
20
+
21
+
22
+ TODO
23
+ ----
24
+
25
+ - cache feature (already implemented in java, need ruby to hook into it)
26
+ - xml parsing options
27
+ - command line tool
28
+ - make gem
Binary file
@@ -0,0 +1,21 @@
1
+ require 'semver'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "jxmlvalidator"
5
+ spec.version = SemVer.find.format '%M.%m.%p'
6
+ spec.summary = "JAXP based XML Validation"
7
+ spec.email = "flazzarino@gmail.com"
8
+ spec.homepage = 'http://github.com/flazz/jxmlvalidator'
9
+ spec.authors = ["Francesco Lazzarino"]
10
+
11
+ spec.files = [
12
+ "README.md",
13
+ "ext/xmlvalidator.jar",
14
+ "jxmlvalidator.gemspec",
15
+ "lib/jxml/validator.rb",
16
+ "spec/jxml/validator_spec.rb"
17
+ ]
18
+
19
+ spec.add_dependency 'rjb', '~> 1.2.0'
20
+ spec.requirements << 'a Java environment with JAXP'
21
+ end
@@ -0,0 +1,64 @@
1
+ require 'tempfile'
2
+ require 'rjb'
3
+
4
+ module JXML
5
+
6
+ class Validator
7
+
8
+ JAR_FILE = File.join File.dirname(__FILE__), '..', '..', 'ext', 'xmlvalidator.jar'
9
+
10
+ # setup rjb validator
11
+ ENV['CLASSPATH'] = if ENV['CLASSPATH']
12
+ "#{JAR_FILE}:#{ENV['CLASSPATH']}"
13
+ else
14
+ JAR_FILE
15
+ end
16
+
17
+ J_File = Rjb.import 'java.io.File'
18
+ J_Validator = Rjb.import 'edu.fcla.da.xml.Validator'
19
+
20
+ def initialize
21
+ @jvalidator = J_Validator.new
22
+ end
23
+
24
+ # return a result set hash with the keys
25
+ # :fatals, :errors, :warnings that maps to an array
26
+ # each containing the keys :line, :message, :column
27
+ def validate src
28
+
29
+ tio = Tempfile.open 'jxmlvalidator'
30
+ tio.write src.to_s
31
+ tio.flush
32
+ tio.close
33
+ jfile = J_File.new tio.path
34
+ jchecker = @jvalidator.validate jfile
35
+ tio.unlink
36
+
37
+ results = {
38
+ :fatals => j2r(jchecker.getFatals),
39
+ :errors => j2r(jchecker.getErrors),
40
+ :warnings => j2r(jchecker.getWarnings)
41
+ }
42
+
43
+ end
44
+
45
+ private
46
+
47
+ def j2r ja
48
+
49
+ (0...ja.size).map do |n|
50
+ e = ja.elementAt n
51
+
52
+ {
53
+ :line => e.getLineNumber,
54
+ :message => e.getMessage,
55
+ :column => e.getColumnNumber
56
+ }
57
+
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,72 @@
1
+ require 'jxml/validator'
2
+
3
+ describe JXML::Validator do
4
+
5
+ subject { JXML::Validator.new }
6
+
7
+ it "should return entries for warnings" do
8
+ pending "i don't know how to get a warning"
9
+ end
10
+
11
+ it "should return entries for errors" do
12
+
13
+ xml=<<-XML
14
+ <mods xmlns="http://www.loc.gov/mods/v3"
15
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16
+ xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-3.xsd">
17
+ <titleInfo>
18
+ <TITLE>
19
+ I'm a limousine ridin', jet flyin', kiss stealin', wheelin' dealin' son of a gun. WOOOO!!
20
+ </TITLE>
21
+ </titleInfo>
22
+ </mods>
23
+ XML
24
+
25
+ results = subject.validate xml
26
+ results[:fatals].should be_empty
27
+ results[:errors].should have_exactly(1).items
28
+ results[:warnings].should be_empty
29
+ end
30
+
31
+ it "should return entries for fatals" do
32
+
33
+ xml=<<-XML
34
+ <mods xmlns="http://www.loc.gov/mods/v3"
35
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
36
+ xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-3.xsd">
37
+ <titleInfo>
38
+ <title>
39
+ I'm a limousine ridin', jet flyin', kiss stealin', wheelin' dealin' son of a gun. WOOOO!!
40
+ </TITLE>
41
+ </titleInfo>
42
+ </mods>
43
+ XML
44
+
45
+ results = subject.validate xml
46
+ results[:fatals].should have_exactly(1).items
47
+ results[:errors].should be_empty
48
+ results[:warnings].should be_empty
49
+
50
+ end
51
+
52
+ it "should return an empty result set if nothing to report" do
53
+
54
+ xml=<<-XML
55
+ <mods xmlns="http://www.loc.gov/mods/v3"
56
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
57
+ xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-3.xsd">
58
+ <titleInfo>
59
+ <title>
60
+ I'm a limousine ridin', jet flyin', kiss stealin', wheelin' dealin' son of a gun. WOOOO!!
61
+ </title>
62
+ </titleInfo>
63
+ </mods>
64
+ XML
65
+
66
+ results = subject.validate xml
67
+ results[:fatals].should be_empty
68
+ results[:errors].should be_empty
69
+ results[:warnings].should be_empty
70
+ end
71
+
72
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jxmlvalidator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Francesco Lazzarino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-16 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rjb
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.0
24
+ version:
25
+ description:
26
+ email: flazzarino@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - README.md
35
+ - ext/xmlvalidator.jar
36
+ - jxmlvalidator.gemspec
37
+ - lib/jxml/validator.rb
38
+ - spec/jxml/validator_spec.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/flazz/jxmlvalidator
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements:
61
+ - a Java environment with JAXP
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: JAXP based XML Validation
67
+ test_files: []
68
+