Class: Jing

Inherits:
Object
  • Object
show all
Defined in:
lib/jing.rb

Constant Summary collapse

VERSION =
"0.0.1"
DEFAULT_JAR =
File.join(File.dirname(__FILE__), "jing-20091111.jar")
Error =
Class.new(StandardError)
ExecutionError =
Class.new(Error)
OptionError =
Class.new(Error)
@@option_builder =
Optout.options do
  on :java,     :required => true, :default => "java"
  on :jar,      "-jar", Optout::File.exists, :default => DEFAULT_JAR
  on :compact,  "-c",   Optout::Boolean
  on :encoding, "-e",   String
  on :id_check, "-i",   Optout::Boolean,     :default  => false
  on :schema,           Optout::File.exists, :required => true
  on :xmlfile,          Optout::File.exists, :required => true
end

Instance Method Summary collapse

Constructor Details

#initialize(schema, options = nil) ⇒ Jing

Errors

ArgumentError

If the options are not nil or a Hash.


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jing.rb', line 43

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)
end

Instance Method Details

#valid?(xml) ⇒ Boolean

Validate an XML document against the schema. To receive a list of vlidation 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

Returns:

  • (Boolean)

110
111
112
113
# File 'lib/jing.rb', line 110

def valid?(xml)
  errors = validate(xml)
  errors.none?
end

#validate(xml) ⇒ Object

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. See Error 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

Raises:


83
84
85
86
87
88
89
90
91
# File 'lib/jing.rb', line 83

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