Class: Jing
- Inherits:
-
Object
- Object
- Jing
- Defined in:
- lib/jing.rb
Constant Summary collapse
- VERSION =
"0.0.3"- 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. do on :java, :required => true, :default => "java" on :java_opts, String 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
-
#initialize(schema, options = nil) ⇒ Jing
constructor
Cretae an instance to validate against the given schema.
-
#valid?(xml) ⇒ Boolean
Validate an XML document against the schema.
-
#validate(xml) ⇒ Object
Validate an XML document against the schema.
Constructor Details
#initialize(schema, options = nil) ⇒ Jing
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 to true 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 a Hash.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/jing.rb', line 44 def initialize(schema, = nil) if raise ArgumentError, "options must be a Hash" unless Hash === @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 |
Instance Method Details
#valid?(xml) ⇒ Boolean
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
113 114 115 116 |
# File 'lib/jing.rb', line 113 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
87 88 89 90 91 92 93 94 |
# File 'lib/jing.rb', line 87 def validate(xml) @options[:xmlfile] = xml out = execute(@options) return [] if $?.success? and out.empty? parse_output(out) end |