class Jing
Constants
- DEFAULT_JAR
- Error
- ExecutionError
- OptionError
- VERSION
Public Class Methods
Cretae an instance to validate against the given schema. The schema can be in the XML or compact syntax.
jing = Jing.new("schema.rng", options)
Arguments¶ ↑
- schema (String)
-
Path to the schema
- options (Hash)
-
Jing
options, optional
Options¶ ↑
- :java (String)
-
Name and/or location of the java executable. Defaults to
"java"
. - :jar (String)
-
Path to the
Jing
JAR file. Defaults to the bundled JAR. - :compact (Boolean)
-
Set to
true
if the schema uses the RELAX NG compact syntax. Defaults to false, will be set totrue
is the schema has a.rnc
extension. - :encoding (String)
-
Encoding of the XML document.
- :id_check (Boolean)
-
Disable checking of ID/IDREF/IDREFS. Defaults to
false
Errors¶ ↑
- ArgumentError
-
If the options are not
nil
or aHash
.
# File lib/jing.rb, line 44 def initialize(schema, options = nil) if options raise ArgumentError, "options must be a Hash" unless Hash === options @options = options.dup end @options ||= {} @options[:schema] = schema @options[:compact] = true if @options[:compact].nil? and @options[:schema] =~ /\.rnc\Z/i # Don't override an explicit setting # Optout quirk: true will *include* the switch, which means we *don't* want to check @options[:id_check] = !@options[:id_check] if @options.include?(:id_check) if @options[:encoding] @options[:java_opts] = "-Dfile.encoding=#{@options[:encoding]}" end end
Public Instance Methods
Validate an XML document against the schema. To receive a list of validation errors use validate
.
puts "yay!" if jing.valid?("doc.xml")
Arguments¶ ↑
- xml (String)
-
Path to the XML file
Errors¶ ↑
Same as validate
Returns¶ ↑
- Boolean
-
true
if valid,false
if invalid
# File lib/jing.rb, line 114 def valid?(xml) errors = validate(xml) errors.none? end
Validate an XML document against the schema.
errors = jing.validate("doc.xml")
Arguments¶ ↑
- xml (String)
-
Path to the XML file
Errors¶ ↑
Jing::OptionError
-
A
Jing
option was invalid. Note that this does not apply to an invalid:java
option. Jing::ExecutionError
-
Problems were encountered trying to execute
Jing
.
Returns¶ ↑
- Array
-
The errors, each element is a
Hash
. SeeError
Hash for more info.
Error
Hash¶ ↑
The error hash contains the following keys/values
- :source (String)
-
File containing the error, can be the schema or the instance XML.
- :line (Fixnum)
-
Line number
- :column (Fixnum)
-
Column number
- :message (String)
-
The problem
# File lib/jing.rb, line 87 def validate(xml) @options[:xmlfile] = xml out = execute(@options) return [] if $?.success? and out.empty? errors = parse_output(out) raise ExecutionError, out if errors.none? # There must have been a problem that was not schema related errors end
Private Instance Methods
# File lib/jing.rb, line 120 def execute(options) cmd = @@option_builder.shell(options) `#{cmd} 2>&1` rescue SystemCallError => e raise ExecutionError, "jing execution failed: #{e}" rescue Optout::OptionError => e raise OptionError, e.message end
# File lib/jing.rb, line 129 def parse_output(output) errors = [] output.split("\n").each do |line| if line =~ /\A(.+):(\d+):(\d+):\s+\w+:\s+(.+)\Z/ errors << { :source => $1, :line => $2.to_i, :column => $3.to_i, :message => $4 } end end errors rescue ArgumentError => e raise ExecutionError, "potentially wrong encoding of jing output." \ "try setting the file's encoding via the :encoding option: #{e}" end