bin/validate-workflow.rb in openwferu-0.9.1 vs bin/validate-workflow.rb in openwferu-0.9.2

- old
+ new

@@ -2,57 +2,81 @@ # # Program Name: validate-workflow.rb # Purpose: A helper script that validates a workflow document against # a XML Schema # Author: Alain Hoang -# Version: 0.1 # Notes: Uses Ruby's libxml library to handle the parsing # Stdlib require 'net/http' +require 'optparse' require 'uri' # From the gems archive require 'rubygems' require 'xml/libxml' module OpenWFEru + FLOWDEF_URL = 'http://www.openwfe.org/flowdef.xsd' # Validates a workflow definition against the Workflow XML Schema # defined at http://www.openwfe.org/flowdef.xsd class WorkflowValidator - OPENWFE_FLOWDEF_URL='http://www.openwfe.org/flowdef.xsd' + + attr_accessor :schema_url + attr_accessor :args - # Class method for validating a xml file against the - # OpenWFE XML Schema - def WorkflowValidator.validate(file) - # Should only be initialized once during the lifetime - # of the program - if not defined? @@schema - xsd_str = Net::HTTP.get URI.parse(OPENWFE_FLOWDEF_URL) - @@schema = XML::Schema.from_string(xsd_str) - end + # Create a validator object using a WorkflowValidatorArgs object + def initialize(args) + @args = args + @args[:schema_url] ||= FLOWDEF_URL + + xsd_str = Net::HTTP.get URI.parse(@args[:schema_url]) + @schema = XML::Schema.from_string(xsd_str) + end + + # validates a xml file against the OpenWFE XML Schema + def validate(file) xmldoc = XML::Document.file(file) - xmldoc.validate_schema(@@schema) + xmldoc.validate_schema(@schema) end end -end -def usage - print "Usage: #{$0} <workflow def> [workflow def] [workflow def] ...\n" - print " <workflow def> - workflow definition file\n" + class WorkflowValidatorArgs < Hash + # Create an arguments object to send to a WorkflowValidator object + def initialize(args) + super() + opts = OptionParser.new do |opts| + opts.banner = "Usage: #$0 [-u url] <workflow def> [workflow def] ..." + + opts.on('-u', '--url URL', 'Use XML Schema at URL') do |url| + self[:schema_url] = url + end + + opts.on_tail('-h', '--help', 'display this help and exit') do + puts opts + exit + end + end + opts.parse!(args) + # Check for no args and exit if so + if args.empty? + puts opts + exit 1 + end + end + end end + def main - # Make sure we have at least one argument or bail - if ARGV.length == 0 - usage - exit 1 - end + # Create the arguments object and the validator object + args = OpenWFEru::WorkflowValidatorArgs.new(ARGV) + validator = OpenWFEru::WorkflowValidator.new(args) # Loop through all arguments and validate the file ARGV.each do |f| print "Trying to validate #{f}... " - if OpenWFEru::WorkflowValidator.validate(f) + if FileTest.exists?(f) && validator.validate(f) print "PASSED\n" else print "FAILED\n" end end