File: xsd2ruby.rb

package info (click to toggle)
ruby-soap4r 2.0.5-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,032 kB
  • sloc: ruby: 52,729; xml: 266; sh: 42; javascript: 20; makefile: 13; perl: 10
file content (90 lines) | stat: -rw-r--r-- 1,900 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env ruby

require 'getoptlong'
require 'logger'
require 'wsdl/xmlSchema/xsd2ruby'


class XSD2RubyApp < Logger::Application
private

  OptSet = [
    ['--xsd','-x', GetoptLong::REQUIRED_ARGUMENT],
    ['--module_path','-m', GetoptLong::REQUIRED_ARGUMENT],
    ['--classdef','-e', GetoptLong::OPTIONAL_ARGUMENT],
    ['--mapping_registry','-r', GetoptLong::NO_ARGUMENT],
    ['--mapper','-p', GetoptLong::NO_ARGUMENT],
    ['--force','-f', GetoptLong::NO_ARGUMENT],
    ['--quiet','-q', GetoptLong::NO_ARGUMENT],
  ]

  def initialize
    super('app')
    STDERR.sync = true
    self.level = Logger::FATAL
  end

  def run
    @worker = WSDL::XMLSchema::XSD2Ruby.new
    @worker.logger = @log
    location, opt = parse_opt(GetoptLong.new(*OptSet))
    usage_exit unless location
    @worker.location = location
    if opt['quiet']
      self.level = Logger::FATAL
    else
      self.level = Logger::INFO
    end
    @worker.opt.update(opt)
    @worker.run
    0
  end

  def usage_exit
    puts <<__EOU__
Usage: #{ $0 } --xsd xsd_location [options]
  xsd_location: filename or URL

Example:
  #{ $0 } --xsd myapp.xsd --classdef foo

Options:
  --xsd xsd_location
  --classdef [filenameprefix]
  --mapping_registry
  --mapper
  --module_path [Module::Path::Name]
  --force
  --quiet
__EOU__
    exit 1
  end

  def parse_opt(getoptlong)
    opt = {}
    xsd = nil
    begin
      getoptlong.each do |name, arg|
       	case name
	when "--xsd"
	  xsd = arg
        when "--module_path"
          opt['module_path'] = arg
	when "--classdef", "--mapping_registry", "--mapper"
  	  opt[name.sub(/^--/, '')] = arg.empty? ? nil : arg
	when "--force"
	  opt['force'] = true
        when "--quiet"
          opt['quiet'] = true
   	else
  	  raise ArgumentError.new("Unknown type #{ arg }")
   	end
      end
    rescue
      usage_exit
    end
    return xsd, opt
  end
end

XSD2RubyApp.new.start