kwalify 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (219) hide show
  1. data/CHANGES.txt +232 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.txt +16 -19
  4. data/bin/kwalify +3 -3
  5. data/contrib/inline-require +6 -4
  6. data/contrib/kwalify +3719 -2427
  7. data/doc-api/classes/CommandOptionError.html +17 -17
  8. data/doc-api/classes/CommandOptionParser.html +63 -63
  9. data/doc-api/classes/Kwalify.html +29 -7
  10. data/doc-api/classes/Kwalify/AssertionError.html +9 -9
  11. data/doc-api/classes/Kwalify/BaseError.html +72 -71
  12. data/doc-api/classes/Kwalify/BaseParser.html +461 -0
  13. data/doc-api/classes/Kwalify/CommandOptionError.html +11 -11
  14. data/doc-api/classes/Kwalify/ErrorHelper.html +51 -46
  15. data/doc-api/classes/Kwalify/HashInterface.html +13 -135
  16. data/doc-api/classes/Kwalify/Json.html +105 -0
  17. data/doc-api/classes/Kwalify/Main.html +129 -126
  18. data/doc-api/classes/Kwalify/MetaValidator.html +248 -232
  19. data/doc-api/classes/Kwalify/Parser.html +12 -12
  20. data/doc-api/classes/Kwalify/PlainYamlParser.html +166 -163
  21. data/doc-api/classes/Kwalify/PlainYamlParser/Alias.html +11 -11
  22. data/doc-api/classes/Kwalify/Rule.html +152 -130
  23. data/doc-api/classes/Kwalify/SchemaError.html +10 -10
  24. data/doc-api/classes/Kwalify/SyntaxError.html +185 -0
  25. data/doc-api/classes/Kwalify/Types.html +26 -25
  26. data/doc-api/classes/Kwalify/Util.html +389 -0
  27. data/doc-api/classes/Kwalify/Util/HashLike.html +246 -0
  28. data/doc-api/classes/Kwalify/Util/OrderedHash.html +330 -0
  29. data/doc-api/classes/Kwalify/ValidationError.html +10 -10
  30. data/doc-api/classes/Kwalify/Validator.html +153 -86
  31. data/doc-api/classes/Kwalify/Yaml.html +181 -0
  32. data/doc-api/classes/Kwalify/Yaml/Parser.html +1538 -0
  33. data/doc-api/classes/Kwalify/YamlParser.html +190 -183
  34. data/doc-api/classes/Kwalify/YamlSyntaxError.html +8 -57
  35. data/doc-api/created.rid +1 -1
  36. data/doc-api/files/__/README_txt.html +17 -22
  37. data/doc-api/files/kwalify/errors_rb.html +2 -2
  38. data/doc-api/files/kwalify/main_rb.html +4 -3
  39. data/doc-api/files/kwalify/messages_rb.html +2 -2
  40. data/doc-api/files/kwalify/meta-validator_rb.html +3 -3
  41. data/doc-api/files/kwalify/{util/yaml-helper_rb.html → parser/base_rb.html} +8 -6
  42. data/doc-api/files/kwalify/parser/yaml_rb.html +117 -0
  43. data/doc-api/files/kwalify/rule_rb.html +2 -2
  44. data/doc-api/files/kwalify/types_rb.html +2 -2
  45. data/doc-api/files/kwalify/util/assert-text-equal_rb.html +2 -2
  46. data/doc-api/files/kwalify/util/hash-interface_rb.html +9 -2
  47. data/doc-api/files/kwalify/util/hashlike_rb.html +107 -0
  48. data/doc-api/files/kwalify/util/option-parser_rb.html +2 -2
  49. data/doc-api/files/kwalify/util/ordered-hash_rb.html +107 -0
  50. data/doc-api/files/kwalify/util/testcase-helper_rb.html +2 -2
  51. data/doc-api/files/kwalify/util_rb.html +107 -0
  52. data/doc-api/files/kwalify/validator_rb.html +2 -2
  53. data/doc-api/files/kwalify/yaml-parser_rb.html +2 -2
  54. data/doc-api/files/kwalify_rb.html +3 -2
  55. data/doc-api/fr_class_index.html +8 -1
  56. data/doc-api/fr_file_index.html +5 -1
  57. data/doc-api/fr_method_index.html +128 -69
  58. data/doc/img/fig01.png +0 -0
  59. data/doc/users-guide.html +882 -717
  60. data/examples/address-book/address-book.schema.yaml +2 -2
  61. data/examples/data-binding/BABEL.data.yaml +63 -0
  62. data/examples/data-binding/BABEL.schema.yaml +31 -0
  63. data/examples/data-binding/Makefile +8 -0
  64. data/examples/data-binding/Rakefile +13 -0
  65. data/examples/data-binding/main.rb +27 -0
  66. data/examples/invoice/invoice.schema.yaml +3 -3
  67. data/examples/tapkit/tapkit.schema.yaml +2 -2
  68. data/lib/kwalify.rb +41 -4
  69. data/lib/kwalify/errors.rb +118 -96
  70. data/lib/kwalify/kwalify.schema.yaml +58 -0
  71. data/lib/kwalify/main.rb +384 -377
  72. data/lib/kwalify/messages.rb +41 -27
  73. data/lib/kwalify/meta-validator.rb +251 -331
  74. data/lib/kwalify/parser/base.rb +127 -0
  75. data/lib/kwalify/parser/yaml.rb +837 -0
  76. data/lib/kwalify/rule.rb +545 -487
  77. data/lib/kwalify/templates/genclass-java.eruby +189 -162
  78. data/lib/kwalify/templates/genclass-php.eruby +104 -0
  79. data/lib/kwalify/templates/genclass-ruby.eruby +95 -66
  80. data/lib/kwalify/types.rb +107 -106
  81. data/lib/kwalify/util.rb +157 -0
  82. data/lib/kwalify/util/assert-text-equal.rb +33 -31
  83. data/lib/kwalify/util/hash-interface.rb +11 -30
  84. data/lib/kwalify/util/hashlike.rb +51 -0
  85. data/lib/kwalify/util/option-parser.rb +144 -144
  86. data/lib/kwalify/util/ordered-hash.rb +57 -0
  87. data/lib/kwalify/util/testcase-helper.rb +3 -3
  88. data/lib/kwalify/validator.rb +267 -212
  89. data/lib/kwalify/yaml-parser.rb +822 -768
  90. data/setup.rb +861 -607
  91. data/test/Rookbook.yaml +10 -0
  92. data/test/{tmp.dir/Context.java → data/users-guide/AddressBook.java.expected} +11 -11
  93. data/test/data/users-guide/BABEL.data.yaml +24 -0
  94. data/test/data/users-guide/BABEL.schema.yaml +30 -0
  95. data/test/data/users-guide/ExampleAddressBook.java +47 -0
  96. data/test/{tmp.dir/Group.java → data/users-guide/Group.java.expected} +2 -11
  97. data/test/data/users-guide/Person.java.expected +44 -0
  98. data/test/data/users-guide/address_book.rb +52 -0
  99. data/test/data/users-guide/address_book.schema.yaml +28 -0
  100. data/test/data/users-guide/address_book.yaml +27 -0
  101. data/test/data/users-guide/answers-schema.yaml +12 -0
  102. data/test/data/users-guide/answers-validator.rb +52 -0
  103. data/test/data/users-guide/babel_genclass.result +26 -0
  104. data/test/data/users-guide/config.schema.yaml +7 -0
  105. data/test/data/users-guide/config.yaml +4 -0
  106. data/test/{tmp.dir/silent1.document → data/users-guide/document01a.yaml} +0 -0
  107. data/test/data/users-guide/document01b.yaml +3 -0
  108. data/test/data/users-guide/document02a.yaml +4 -0
  109. data/test/data/users-guide/document02b.yaml +4 -0
  110. data/test/data/users-guide/document03a.yaml +6 -0
  111. data/test/data/users-guide/document03b.yaml +6 -0
  112. data/test/data/users-guide/document04a.yaml +9 -0
  113. data/test/data/users-guide/document04b.yaml +9 -0
  114. data/test/data/users-guide/document05a.yaml +11 -0
  115. data/test/data/users-guide/document05b.yaml +12 -0
  116. data/test/data/users-guide/document06a.yaml +15 -0
  117. data/test/data/users-guide/document06b.yaml +16 -0
  118. data/test/data/users-guide/document07a.yaml +9 -0
  119. data/test/data/users-guide/document07b.yaml +7 -0
  120. data/test/data/users-guide/document12a.json +10 -0
  121. data/test/data/users-guide/document12b.json +6 -0
  122. data/test/data/users-guide/document13a.yaml +17 -0
  123. data/test/data/users-guide/document14a.yaml +3 -0
  124. data/test/data/users-guide/document14b.yaml +3 -0
  125. data/test/data/users-guide/document15a.yaml +6 -0
  126. data/test/data/users-guide/document15b.yaml +5 -0
  127. data/test/data/users-guide/example_address_book.rb +10 -0
  128. data/test/data/users-guide/example_address_book_java.result +32 -0
  129. data/test/data/users-guide/example_address_book_ruby.result +31 -0
  130. data/test/data/users-guide/genclass_java.result +4 -0
  131. data/test/data/users-guide/howto-validation-with-parsing.rb +28 -0
  132. data/test/data/users-guide/howto-validation.rb +25 -0
  133. data/test/data/users-guide/howto3.rb +6 -0
  134. data/test/data/users-guide/howto3.result +5 -0
  135. data/test/data/users-guide/howto3.yaml +8 -0
  136. data/test/data/users-guide/howto5_databinding.result +111 -0
  137. data/test/data/users-guide/invalid01.result +3 -0
  138. data/test/data/users-guide/invalid02.result +5 -0
  139. data/test/data/users-guide/invalid03.result +5 -0
  140. data/test/data/users-guide/invalid04.result +4 -0
  141. data/test/data/users-guide/invalid05.result +11 -0
  142. data/test/data/users-guide/invalid06.result +4 -0
  143. data/test/data/users-guide/invalid07.result +3 -0
  144. data/test/data/users-guide/invalid08.result +3 -0
  145. data/test/data/users-guide/invalid12.json +8 -0
  146. data/test/data/users-guide/invalid14.result +4 -0
  147. data/test/data/users-guide/invalid15.result +4 -0
  148. data/test/data/users-guide/loadbabel.rb +27 -0
  149. data/test/data/users-guide/loadconfig.rb +15 -0
  150. data/test/data/users-guide/loadconfig.result +2 -0
  151. data/test/data/users-guide/models.rb +22 -0
  152. data/test/data/users-guide/option_ha.result +6 -0
  153. data/test/data/users-guide/option_ha_genclass_java.result +7 -0
  154. data/test/{tmp.dir/meta1.schema → data/users-guide/schema01.yaml} +0 -0
  155. data/test/data/users-guide/schema02.yaml +12 -0
  156. data/test/data/users-guide/schema03.yaml +9 -0
  157. data/test/data/users-guide/schema04.yaml +20 -0
  158. data/test/data/users-guide/schema05.yaml +29 -0
  159. data/test/data/users-guide/schema06.yaml +11 -0
  160. data/test/data/users-guide/schema12.json +12 -0
  161. data/test/data/users-guide/schema13.yaml +13 -0
  162. data/test/data/users-guide/schema14.yaml +5 -0
  163. data/test/data/users-guide/schema15.yaml +21 -0
  164. data/test/data/users-guide/valid01.result +2 -0
  165. data/test/data/users-guide/valid02.result +2 -0
  166. data/test/data/users-guide/valid03.result +2 -0
  167. data/test/data/users-guide/valid04.result +2 -0
  168. data/test/data/users-guide/valid05.result +2 -0
  169. data/test/data/users-guide/valid06.result +2 -0
  170. data/test/data/users-guide/valid07.result +2 -0
  171. data/test/data/users-guide/valid08.result +2 -0
  172. data/test/data/users-guide/valid12.result +2 -0
  173. data/test/data/users-guide/valid13.result +2 -0
  174. data/test/data/users-guide/valid14.result +2 -0
  175. data/test/data/users-guide/valid15.result +2 -0
  176. data/test/data/users-guide/validate08.rb +37 -0
  177. data/test/test-action.rb +78 -0
  178. data/test/test-action.yaml +738 -0
  179. data/test/test-databinding.rb +80 -0
  180. data/test/test-databinding.yaml +301 -0
  181. data/test/test-main.rb +129 -150
  182. data/test/test-main.yaml +126 -321
  183. data/test/test-metavalidator.rb +47 -47
  184. data/test/test-metavalidator.yaml +77 -21
  185. data/test/test-parser-yaml.rb +57 -0
  186. data/test/test-parser-yaml.yaml +1749 -0
  187. data/test/test-rule.rb +14 -15
  188. data/test/test-rule.yaml +6 -3
  189. data/test/test-users-guide.rb +75 -0
  190. data/test/test-validator.rb +77 -52
  191. data/test/test-validator.yaml +168 -6
  192. data/test/test-yaml-parser.rb +47 -0
  193. data/test/{test-yamlparser.yaml → test-yaml-parser.yaml} +159 -52
  194. data/test/test.rb +37 -21
  195. metadata +136 -37
  196. data/COPYING +0 -340
  197. data/ChangeLog +0 -70
  198. data/doc-api/classes/YamlHelper.html +0 -259
  199. data/lib/kwalify/util/yaml-helper.rb +0 -82
  200. data/test/test-yamlparser.rb +0 -58
  201. data/test/tmp.dir/User.java +0 -43
  202. data/test/tmp.dir/action1.document +0 -18
  203. data/test/tmp.dir/action1.schema +0 -32
  204. data/test/tmp.dir/action2.document +0 -18
  205. data/test/tmp.dir/action2.schema +0 -32
  206. data/test/tmp.dir/emacs.document +0 -6
  207. data/test/tmp.dir/emacs.schema +0 -6
  208. data/test/tmp.dir/meta1.document +0 -0
  209. data/test/tmp.dir/meta2.document +0 -0
  210. data/test/tmp.dir/meta2.schema +0 -3
  211. data/test/tmp.dir/silent1.schema +0 -3
  212. data/test/tmp.dir/silent2.document +0 -7
  213. data/test/tmp.dir/silent2.schema +0 -3
  214. data/test/tmp.dir/stream.invalid +0 -8
  215. data/test/tmp.dir/stream.schema +0 -3
  216. data/test/tmp.dir/stream.valid +0 -8
  217. data/test/tmp.dir/untabify.document +0 -5
  218. data/test/tmp.dir/untabify.schema +0 -10
  219. data/todo.txt +0 -34
@@ -0,0 +1,58 @@
1
+ ## $Rev: 95 $
2
+ ## $Date: 2008-01-27 22:49:22 +0900 (Sun, 27 Jan 2008) $
3
+ ## copyright(c) 2005-2008 kuwata-lab all rights reserved.
4
+
5
+ name: MAIN
6
+ type: map
7
+ required: yes
8
+ mapping: &main-rule
9
+ "name": { type: str }
10
+ "desc": { type: str }
11
+ "class": { type: str }
12
+ "type":
13
+ type: str
14
+ #required: yes
15
+ enum: [ seq, map, str, int, float, number, bool, text,
16
+ date, time, timestamp, any, scalar ]
17
+ "required": { type: bool } # equivarent to 'not-null'
18
+ "enum":
19
+ type: seq
20
+ sequence:
21
+ - type: scalar
22
+ unique: yes
23
+ "pattern": { type: str }
24
+ "assert": { type: str, pattern: /\bval\b/ }
25
+ "range":
26
+ type: map
27
+ mapping:
28
+ "max": { type: scalar }
29
+ "min": { type: scalar }
30
+ "max-ex": { type: scalar }
31
+ "min-ex": { type: scalar }
32
+ "length":
33
+ type: map
34
+ mapping:
35
+ "max": { type: int }
36
+ "min": { type: int }
37
+ "max-ex": { type: int }
38
+ "min-ex": { type: int }
39
+ "ident": { type: bool } # equivarent to 'primary-key' (undocumented)
40
+ "unique": { type: bool }
41
+ "default": { type: scalar } # used only in action's template
42
+ "sequence":
43
+ name: SEQUENCE
44
+ type: seq
45
+ sequence:
46
+ - type: map
47
+ mapping: *main-rule
48
+ name: MAIN
49
+ #required: yes
50
+ "mapping":
51
+ name: MAPPING
52
+ type: map
53
+ mapping:
54
+ =:
55
+ type: map
56
+ mapping: *main-rule
57
+ name: MAIN
58
+ #required: yes
@@ -1,435 +1,442 @@
1
1
  ###
2
- ### $Rev: 51 $
3
- ### $Release: 0.6.1 $
4
- ### copyright(c) 2005 kuwata-lab all rights reserved.
2
+ ### $Rev: 95 $
3
+ ### $Release: 0.7.0 $
4
+ ### copyright(c) 2005-2008 kuwata-lab all rights reserved.
5
5
  ###
6
6
 
7
7
  require 'yaml'
8
8
  require 'erb'
9
9
  require 'kwalify'
10
- require 'kwalify/util/yaml-helper'
11
- #require 'kwalify/util/option-parser'
10
+ require 'kwalify/util'
11
+ require 'kwalify/util/ordered-hash'
12
12
 
13
13
 
14
14
  module Kwalify
15
15
 
16
16
 
17
- class CommandOptionError < KwalifyError
18
- def initialize(message, option, error_symbol)
19
- super(message)
20
- @option = option
21
- @error_symbol = error_symbol
22
- end
23
- attr_reader :option, :error_symbol
24
- end
25
-
26
-
27
- ##
28
- ## ex.
29
- ## command = File.basename($0)
30
- ## begin
31
- ## main = Kwalify::Main.new(command)
32
- ## s = main.execute
33
- ## print s if s
34
- ## rescue Kwalify::CommandOptionError => ex
35
- ## $stderr.puts "ERROR: #{ex.message}"
36
- ## exit 1
37
- ## rescue Kwalify::KwalifyError => ex
38
- ## $stderr.puts "ERROR: #{ex.message}"
39
- ## exit 1
40
- ## end
41
- ##
42
- class Main
43
-
44
-
45
- def initialize(command=nil)
46
- @command = command || File.basename($0)
47
- @options = {}
48
- @properties = {}
49
- @template_path = []
50
- $:.each do |path|
51
- tpath = "#{path}/kwalify/templates"
52
- @template_path << tpath if test(?d, tpath)
53
- end
17
+ class CommandOptionError < KwalifyError
18
+ def initialize(message, option, error_symbol)
19
+ super(message)
20
+ @option = option
21
+ @error_symbol = error_symbol
22
+ end
23
+ attr_reader :option, :error_symbol
24
+ end
25
+
26
+
27
+ ##
28
+ ## ex.
29
+ ## command = File.basename($0)
30
+ ## begin
31
+ ## main = Kwalify::Main.new(command)
32
+ ## s = main.execute
33
+ ## print s if s
34
+ ## rescue Kwalify::CommandOptionError => ex
35
+ ## $stderr.puts "ERROR: #{ex.message}"
36
+ ## exit 1
37
+ ## rescue Kwalify::KwalifyError => ex
38
+ ## $stderr.puts "ERROR: #{ex.message}"
39
+ ## exit 1
40
+ ## end
41
+ ##
42
+ class Main
43
+
44
+
45
+ def initialize(command=nil)
46
+ @command = command || File.basename($0)
47
+ @options = {}
48
+ @properties = {}
49
+ @template_path = []
50
+ $:.each do |path|
51
+ tpath = "#{path}/kwalify/templates"
52
+ @template_path << tpath if test(?d, tpath)
54
53
  end
54
+ end
55
55
 
56
56
 
57
- def debug?
58
- @options[:debug]
59
- end
57
+ def debug?
58
+ @options[:debug]
59
+ end
60
60
 
61
61
 
62
- def _inspect()
63
- sb = []
64
- sb << "command: #{@command}\n"
65
- sb << "options:\n"
66
- @options.keys.sort {|k1,k2| k1.to_s<=>k2.to_s }.each do |key|
67
- sb << " - #{key}: #{@options[key]}\n"
68
- end
69
- sb << "properties:\n"
70
- @properties.keys.sort_by {|k| k.to_s}.each do |key|
71
- sb << " - #{key}: #{@properties[key]}\n"
72
- end
73
- #sb << "template_path:\n"
74
- #@template_path.each do |path|
75
- # sb << " - #{path}\n"
76
- #end
77
- return sb.join
62
+ def _inspect()
63
+ sb = []
64
+ sb << "command: #{@command}\n"
65
+ sb << "options:\n"
66
+ @options.keys.sort {|k1,k2| k1.to_s<=>k2.to_s }.each do |key|
67
+ sb << " - #{key}: #{@options[key]}\n"
78
68
  end
79
-
80
-
81
- def execute(argv=ARGV)
82
- # parse command-line options
83
- filenames = _parse_argv(argv)
84
-
85
- # help or version
86
- if @options[:help] || @options[:version]
87
- action = @options[:action]
88
- s = ''
89
- s << _version() << "\n" if @options[:version]
90
- s << _usage() if @options[:help] && !action
91
- s << _describe_properties(action) if @options[:help] && action
92
- return s
93
- end
94
-
95
- # validation
96
- if @options[:meta2]
97
- s = _quick_meta_validate(filenames)
98
- elsif @options[:meta]
99
- s = _meta_validate(filenames)
100
- elsif @options[:action]
101
- if !@options[:schema]
102
- #* key=:command_option_actionnoschema msg="schema filename is not specified."
103
- raise option_error(:command_option_actionnoschema, @options[:action])
104
- end
105
- s = _perform_action(@options[:action], @options[:schema])
106
- elsif @options[:schema]
107
- if @options[:debug]
108
- s = _inspect_schema(@options[:schema])
109
- else
110
- s = _validate(filenames, @options[:schema])
111
- end
112
- else
113
- #* key=:command_option_noaction msg="command-line option '-f' or '-m' required."
114
- raise option_error(:command_option_noaction, @command)
115
- end
116
- return s # or return (s == nil || s.empty?) ? nil : s
69
+ sb << "properties:\n"
70
+ @properties.keys.sort_by {|k| k.to_s}.each do |key|
71
+ sb << " - #{key}: #{@properties[key]}\n"
117
72
  end
118
-
119
-
120
- def self.main(command, argv=ARGV)
121
- begin
122
- main = Kwalify::Main.new(command)
123
- s = main.execute(argv)
124
- print s if s
125
- rescue Kwalify::CommandOptionError => ex
126
- $stderr.puts ex.message
127
- exit 1
128
- rescue Kwalify::KwalifyError => ex
129
- $stderr.puts "ERROR: #{ex.message}"
130
- exit 1
131
- #rescue => ex
132
- # if main.debug?
133
- # raise ex
134
- # else
135
- # $stderr.puts ex.message
136
- # exit 1
137
- # end
138
- end
73
+ #sb << "template_path:\n"
74
+ #@template_path.each do |path|
75
+ # sb << " - #{path}\n"
76
+ #end
77
+ return sb.join
78
+ end
79
+
80
+
81
+ def execute(argv=ARGV)
82
+ ## parse command-line options
83
+ filenames = _parse_argv(argv)
84
+
85
+ ## help or version
86
+ if @options[:help] || @options[:version]
87
+ action = @options[:action]
88
+ s = ''
89
+ s << _version() << "\n" if @options[:version]
90
+ s << _usage() if @options[:help] && !action
91
+ s << _describe_properties(action) if @options[:help] && action
92
+ puts s
93
+ return
139
94
  end
140
95
 
141
-
142
- private
143
-
144
-
145
- def option_error(error_symbol, arg)
146
- msg = Kwalify.msg(error_symbol) % arg
147
- return CommandOptionError.new(msg, arg, error_symbol)
96
+ # validation
97
+ if @options[:meta2]
98
+ validate_schemafiles2(filenames)
99
+ elsif @options[:meta]
100
+ validate_schemafiles(filenames)
101
+ elsif @options[:action]
102
+ unless @options[:schema]
103
+ #* key=:command_option_actionnoschema msg="schema filename is not specified."
104
+ raise option_error(:command_option_actionnoschema, @options[:action])
105
+ end
106
+ perform_action(@options[:action], @options[:schema])
107
+ elsif @options[:schema]
108
+ if @options[:debug]
109
+ inspect_schema(@options[:schema])
110
+ else
111
+ validate_files(filenames, @options[:schema])
112
+ end
113
+ else
114
+ #* key=:command_option_noaction msg="command-line option '-f' or '-m' required."
115
+ raise option_error(:command_option_noaction, @command)
148
116
  end
149
-
150
-
151
- def _find_template(action)
152
- template_filename = action + '.eruby'
153
- unless test(?f, template_filename)
154
- tpath = @template_path.find { |path| test(?f, "#{path}/#{template_filename}") }
155
- #* key=:command_option_notemplate msg="%s: invalid action (template not found).\n"
156
- raise option_error(:command_option_notemplate, action) unless tpath
157
- template_filename = "#{tpath}/#{action}.eruby"
158
- end
159
- return template_filename
117
+ return
118
+ end
119
+
120
+
121
+ def self.main(command, argv=ARGV)
122
+ begin
123
+ main = Kwalify::Main.new(command)
124
+ s = main.execute(argv)
125
+ print s if s
126
+ rescue Kwalify::CommandOptionError => ex
127
+ raise ex if main.debug?
128
+ $stderr.puts ex.message
129
+ exit 1
130
+ rescue Kwalify::KwalifyError => ex
131
+ raise ex if main.debug?
132
+ $stderr.puts "ERROR: #{ex.to_s}"
133
+ exit 1
134
+ #rescue => ex
135
+ # if main.debug?
136
+ # raise ex
137
+ # else
138
+ # $stderr.puts ex.message
139
+ # exit 1
140
+ # end
160
141
  end
142
+ end
161
143
 
162
144
 
163
- def _apply_template(template_filename, hash)
164
- template = File.read(template_filename)
165
- trim_mode = 1
166
- erb = ERB.new(template, $SAFE, trim_mode)
167
- context = Object.new
168
- hash.each do |key, val|
169
- context.instance_variable_set("@#{key}", val)
170
- end
171
- return context.instance_eval(erb.src, template_filename)
172
- end
145
+ private
173
146
 
174
147
 
175
- def _describe_properties(action)
176
- template_filename = _find_template(action)
177
- s = _apply_template(template_filename, :describe=>true)
178
- return s
179
- end
148
+ def option_error(error_symbol, arg)
149
+ msg = Kwalify.msg(error_symbol) % arg
150
+ return CommandOptionError.new(msg, arg, error_symbol)
151
+ end
180
152
 
181
153
 
182
- def _perform_action(action, schema_filename, describe=false)
183
- template_filename = _find_template(action)
184
- schema = _load_schema_file(schema_filename)
185
- rule = Rule.new(schema)
186
- @properties[:schema_filename] = schema_filename
187
- s = _apply_template(template_filename, :rule=>rule, :properties=>@properties)
188
- return s
154
+ def _find_template(action)
155
+ template_filename = action + '.eruby'
156
+ unless test(?f, template_filename)
157
+ pathlist = []
158
+ pathlist.concat(@options[:tpath].split(/,/)) if @options[:tpath]
159
+ pathlist.concat(@template_path)
160
+ tpath = pathlist.find {|path| test(?f, "#{path}/#{template_filename}") }
161
+ #* key=:command_option_notemplate msg="%s: invalid action (template not found).\n"
162
+ raise option_error(:command_option_notemplate, action) unless tpath
163
+ template_filename = "#{tpath}/#{action}.eruby"
189
164
  end
165
+ return template_filename
166
+ end
190
167
 
191
168
 
192
- def _inspect_schema(schema_filename)
193
- filename = schema_filename
194
- str = File.open(schema_filename) { |f| f.read() }
195
- str = YamlUtil.untabify(str) if @options[:untabify]
196
- schema = YAML.load(str)
197
- return nil if schema == nil
198
- validator = Kwalify::Validator.new(schema) # error raised when schema is wrong
199
- s = validator._inspect()
200
- s << "\n" unless s[-1] == ?\n
201
- return s
169
+ def apply_template(template_filename, hash)
170
+ template = File.read(template_filename)
171
+ trim_mode = 1
172
+ erb = ERB.new(template, nil, trim_mode)
173
+ context = Object.new
174
+ hash.each do |key, val|
175
+ context.instance_variable_set("@#{key}", val)
202
176
  end
177
+ s = context.instance_eval(erb.src, template_filename)
178
+ return s
179
+ end
180
+
181
+
182
+ def _describe_properties(action)
183
+ template_filename = _find_template(action)
184
+ s = apply_template(template_filename, :describe=>true)
185
+ return s
186
+ end
187
+
188
+
189
+ def perform_action(action, schema_filename, describe=false)
190
+ template_filename = _find_template(action)
191
+ schema = _load_schemafile(schema_filename, ordered=true)
192
+ validator = Kwalify::Validator.new(schema)
193
+ @properties[:schema_filename] = schema_filename
194
+ hash = { :validator=>validator, :schema=>schema, :properties=>@properties }
195
+ s = apply_template(template_filename, hash)
196
+ puts s if s && !s.empty?
197
+ end
198
+
199
+
200
+ def inspect_schema(schema_filename)
201
+ schema = _load_schemafile(schema_filename)
202
+ if schema.nil?
203
+ puts "nil"
204
+ else
205
+ validator = Kwalify::Validator.new(schema) # error raised when schema is wrong
206
+ puts validator._inspect()
207
+ end
208
+ end
203
209
 
204
210
 
205
- #--
206
- #class QuickMetaValidator
207
- # def validate(schema)
208
- # errors = []
209
- # begin
210
- # validator = Kwalify::Validator.new(schema) # error raised when schema is wrong
211
- # rescue Kwalify::SchemaError => ex
212
- # errors << ex
213
- # end
214
- # return errors
215
- # end
216
- #end
217
- #++
211
+ ## -f schemafile datafile
212
+ def validate_files(filenames, schema_filename)
213
+ schema = _load_schemafile(schema_filename)
214
+ validator = Kwalify::Validator.new(schema)
215
+ _validate_files(validator, filenames)
216
+ end
218
217
 
219
218
 
220
- def _quick_meta_validate(filenames)
221
- meta_validator = Object.new
222
- def meta_validator.validate(schema)
223
- errors = []
224
- begin
225
- validator = Kwalify::Validator.new(schema) # error raised when schema is wrong
226
- rescue Kwalify::SchemaError => ex
227
- errors << ex
228
- end
229
- return errors
230
- end
231
- s = _validate_files(meta_validator, filenames)
232
- return s
219
+ def _load_schemafile(schema_filename, ordered=false)
220
+ str = File.read(schema_filename)
221
+ if str.empty?
222
+ #* key=:schema_empty msg="%s: empty schema.\n"
223
+ msg = Kwalify.msg(:schema_emtpy) % filename
224
+ raise CommandOptionError.new(msg)
233
225
  end
234
-
235
-
236
- def _load_schema_file(schema_filename)
237
- filename = schema_filename
238
- str = File.read(filename)
239
- str = YamlHelper.untabify(str) if @options[:untabify]
240
- schema = YAML.load(str)
241
- return schema
226
+ str = Util.untabify(str) if @options[:untabify]
227
+ parser = Kwalify::Yaml::Parser.new()
228
+ parser.preceding_alias = true if @options[:preceding]
229
+ parser.mapping_class = Kwalify::Util::OrderedHash if ordered
230
+ schema = parser.parse(str, :filename=>schema_filename) # or YAML.load(str)
231
+ return schema
232
+ end
233
+
234
+
235
+ ## -m schemafile
236
+ def validate_schemafiles(schema_filenames)
237
+ meta_validator = Kwalify::MetaValidator.instance()
238
+ _validate_files(meta_validator, schema_filenames)
239
+ end
240
+
241
+
242
+ ## -M schemafile
243
+ def validate_schemafiles2(schema_filenames)
244
+ parser = Kwalify::Yaml::Parser.new()
245
+ parser.preceding_alias = true if @options[:preceding]
246
+ for schema_filename in schema_filenames
247
+ str = File.read(schema_filename)
248
+ str = Util.untabify(str) if @options[:untabify]
249
+ schema = parser.parse(str, :filename=>schema_filename)
250
+ Kwalify::Validator.new(schema) # exception raised when schema has errors
242
251
  end
252
+ end
243
253
 
244
254
 
245
- def _meta_validate(filenames)
246
- meta_validator = Kwalify::MetaValidator.instance()
247
- s = _validate_files(meta_validator, filenames)
248
- return s
255
+ def _validate_files(validator, filenames)
256
+ ## parser
257
+ if @options[:linenum] || @options[:preceding]
258
+ parser = Kwalify::Yaml::Parser.new(validator)
259
+ parser.preceding_alias = true if @options[:preceding]
260
+ else
261
+ parser = nil
249
262
  end
250
-
251
-
252
- def _validate(filenames, schema_filename)
253
- schema = _load_schema_file(schema_filename)
254
- if schema
255
- validator = Kwalify::Validator.new(schema)
256
- s = _validate_files(validator, filenames)
257
- else
258
- #* key=:schema_empty msg="%s: empty schema.\n"
259
- s = Kwalify.msg(:schema_emtpy) % filename
260
- end
261
- return s
263
+ ## filenames
264
+ if filenames.empty?
265
+ filenames = [ $stdin ]
262
266
  end
263
-
264
-
265
- def _validate_files(validator, filenames)
266
- s = ''
267
- filenames = [ nil ] if filenames.empty?
268
- filenames.each do |filename|
269
- if filename
270
- str = File.open(filename) { |f| f.read() } # or File.read(filename) in ruby1.8
271
- else
272
- str = $stdin.read()
273
- filename = '(stdin)'
274
- end
275
- str = YamlHelper.untabify(str) if @options[:untabify]
276
- if @options[:linenum]
277
- parser = Kwalify::YamlParser.new(str)
278
- i = 0
279
- while parser.has_next?
280
- doc = parser.parse()
281
- s << _validate_document(validator, doc, filename, i, parser)
282
- i += 1
283
- end
284
- else
285
- parser = nil
286
- i = 0
287
- YAML.load_documents(str) do |doc|
288
- s << _validate_document(validator, doc, filename, i, nil)
289
- i += 1
290
- end
291
- end
292
- end
293
- return s
267
+ for filename in filenames
268
+ ## read input
269
+ if filename.is_a?(IO)
270
+ input = filename.read()
271
+ filename = '(stdin)'
272
+ else
273
+ input = File.read(filename)
274
+ end
275
+ if input.empty?
276
+ #* key=:validation_empty msg="%s#%d: empty."
277
+ puts kwalify.msg(:validation_empty) % [filename, i]
278
+ #puts "#{filename}##{i}: empty."
279
+ next
280
+ end
281
+ input = Util.untabify(input) if @options[:untabify]
282
+ ## parse input
283
+ if parser
284
+ #i = 0
285
+ #ydoc = parser.parse(input, :filename=>filename)
286
+ #_show_errors(filename, i, ydoc, parser.errors)
287
+ #while parser.has_next?
288
+ # i += 1
289
+ # ydoc = parser.parse_next()
290
+ # _show_errors(filename, i, ydoc, parser.errors)
291
+ #end
292
+ i = 0
293
+ parser.parse_stream(input, :filename=>filename) do |ydoc|
294
+ _show_errors(filename, i, ydoc, parser.errors)
295
+ i += 1
296
+ end
297
+ else
298
+ i = 0
299
+ YAML.load_documents(input) do |ydoc|
300
+ errors = validator.validate(ydoc)
301
+ _show_errors(filename, i, ydoc, errors)
302
+ i += 1
303
+ end
304
+ end
294
305
  end
295
-
296
-
297
- def _validate_document(validator, doc, filename, i, parser=nil)
298
- s = ''
299
- if doc == nil
300
- #* key=:validation_empty msg="%s#%d: empty.\n"
301
- s << kwalify.msg(:validation_empty) % [filename, i]
302
- return s
303
- end
304
- errors = validator.validate(doc)
305
- if errors == nil || errors.empty?
306
- #* key=:validation_valid msg="%s#%d: valid.\n"
307
- s << Kwalify.msg(:validation_valid) % [filename, i] unless @options[:silent]
308
- else
309
- #* key=:validation_invalid msg="%s#%d: INVALID\n"
310
- s << Kwalify.msg(:validation_invalid) % [filename, i]
311
- if @options[:linenum]
312
- #assert parser != nil
313
- raise unless parser != nil
314
- parser.set_errors_linenum(errors)
315
- errors.sort!
316
- end
317
- errors.each do |error|
318
- if @options[:emacs]
319
- #assert @options[:linenum]
320
- raise unless @options[:linenum]
321
- s << "#{filename}:#{error.linenum}: [#{error.path}] #{error.message}\n"
322
- elsif @options[:linenum]
323
- s << " - (line #{error.linenum}) [#{error.path}] #{error.message}\n"
324
- else
325
- s << " - [#{error.path}] #{error.message}\n"
326
- end
327
- end
328
- end
329
- return s
306
+ end
307
+
308
+
309
+ def _show_errors(filename, i, ydoc, errors, ok_label="valid.", ng_label="INVALID")
310
+ if errors && !errors.empty?
311
+ puts "#{filename}##{i}: #{ng_label}"
312
+ errors.sort!
313
+ for error in errors
314
+ e = error
315
+ if @options[:emacs]
316
+ raise unless @options[:linenum]
317
+ puts "#{filename}:#{e.linenum}:#{e.column} [#{e.path}] #{e.message}\n"
318
+ elsif @options[:linenum]
319
+ puts " - (line #{e.linenum}) [#{e.path}] #{e.message}\n"
320
+ else
321
+ puts " - [#{e.path}] #{e.message}\n"
322
+ end
323
+ end
324
+ elsif ydoc.nil?
325
+ #* key=:validation_empty msg="%s#%d: empty.\n"
326
+ puts kwalify.msg(:validation_empty) % [filename, i]
327
+ else
328
+ #* key=:validation_valid msg="%s#%d: valid."
329
+ puts Kwalify.msg(:validation_valid) % [filename, i] unless @options[:quiet]
330
+ #puts "#{filename}##{i} - #{ok_label}" unless @options[:quiet]
330
331
  end
332
+ end
331
333
 
332
334
 
333
- def _usage()
334
- #msg = Kwalify.msg(:command_help) % [@command, @command, @command]
335
- msg = Kwalify.msg(:command_help)
336
- return msg
337
- end
335
+ def _usage()
336
+ #msg = Kwalify.msg(:command_help) % [@command, @command, @command]
337
+ msg = Kwalify.msg(:command_help)
338
+ return msg
339
+ end
338
340
 
339
341
 
340
- def _version()
341
- return RELEASE
342
- end
342
+ def _version()
343
+ return RELEASE
344
+ end
343
345
 
344
346
 
345
- def _to_value(str)
346
- case str
347
- when nil, "null", "nil" ; return nil
348
- when "true", "yes" ; return true
349
- when "false", "no" ; return false
350
- when /\A\d+\z/ ; return str.to_i
351
- when /\A\d+\.\d+\z/ ; return str.to_f
352
- when /\/(.*)\// ; return Regexp.new($1)
353
- when /\A'.*'\z/, /\A".*"\z/ ; return eval(str)
354
- else ; return str
355
- end
347
+ def _to_value(str)
348
+ case str
349
+ when nil, "null", "nil" ; return nil
350
+ when "true", "yes" ; return true
351
+ when "false", "no" ; return false
352
+ when /\A\d+\z/ ; return str.to_i
353
+ when /\A\d+\.\d+\z/ ; return str.to_f
354
+ when /\/(.*)\// ; return Regexp.new($1)
355
+ when /\A'.*'\z/, /\A".*"\z/ ; return eval(str)
356
+ else ; return str
356
357
  end
357
-
358
-
359
- def _parse_argv(argv)
360
- option_table = {
361
- ?h => :help,
362
- ?v => :version,
363
- ?s => :silent,
364
- ?t => :untabify,
365
- ?m => :meta,
366
- ?M => :meta2,
367
- ?E => :emacs,
368
- ?l => :linenum,
369
- ?f => :schema,
370
- ?D => :debug,
371
- ?a => :action,
372
- ?I => :tpath,
373
- }
374
-
375
- errcode_table = {
376
- #* key=:command_option_noschema msg="-%s: schema filename required."
377
- ?f => :command_option_noschema,
378
- #* key=:command_option_noaction msg="-%s: action required."
379
- ?a => :command_option_noaction,
380
- #* key=:command_option_notpath msg="-%s: template path required."
381
- ?I => :command_option_notpath,
382
- }
383
-
384
- while argv[0] && argv[0][0] == ?-
385
- optstr = argv.shift
386
- optstr = optstr[1, optstr.length-1]
387
- # property
388
- if optstr[0] == ?-
389
- unless optstr =~ /\A\-([-\w]+)(?:=(.*))?\z/
390
- #* key=:command_property_invalid msg="%s: invalid property."
391
- raise option_error(:command_property_invalid, optstr)
392
- end
393
- prop_name = $1; prop_value = $2
394
- key = prop_name.gsub(/-/, '_').intern
395
- value = prop_value == nil ? true : _to_value(prop_value)
396
- @properties[key] = value
397
- # option
358
+ end
359
+
360
+
361
+ def _parse_argv(argv)
362
+ option_table = {
363
+ ?h => :help,
364
+ ?v => :version,
365
+ ?q => :quiet,
366
+ ?s => :quiet,
367
+ ?t => :untabify,
368
+ #?z => :meta,
369
+ ?m => :meta,
370
+ ?M => :meta2,
371
+ ?E => :emacs,
372
+ ?l => :linenum,
373
+ ?f => :schema,
374
+ ?D => :debug,
375
+ ?a => :action,
376
+ ?I => :tpath,
377
+ ?P => :preceding,
378
+ }
379
+
380
+ errcode_table = {
381
+ #* key=:command_option_schema_required msg="-%s: schema filename required."
382
+ ?f => :command_option_schema_required,
383
+ #* key=:command_option_action_required msg="-%s: action required."
384
+ ?a => :command_option_action_required,
385
+ #* key=:command_option_tpath_required msg="-%s: template path required."
386
+ ?I => :command_option_tpath_required,
387
+ }
388
+
389
+ while argv[0] && argv[0][0] == ?-
390
+ optstr = argv.shift
391
+ optstr = optstr[1, optstr.length-1]
392
+ ## property
393
+ if optstr[0] == ?-
394
+ unless optstr =~ /\A\-([-\w]+)(?:=(.*))?\z/
395
+ #* key=:command_property_invalid msg="%s: invalid property."
396
+ raise option_error(:command_property_invalid, optstr)
397
+ end
398
+ prop_name = $1; prop_value = $2
399
+ key = prop_name.gsub(/-/, '_').intern
400
+ value = prop_value.nil? ? true : _to_value(prop_value)
401
+ @properties[key] = value
402
+ ## option
403
+ else
404
+ while optstr && !optstr.empty?
405
+ optchar = optstr[0]
406
+ optstr[0,1] = ""
407
+ unless option_table.key?(optchar)
408
+ #* key=:command_option_invalid msg="-%s: invalid command option."
409
+ raise option_error(:command_option_invalid, optchar.chr)
410
+ end
411
+ optkey = option_table[optchar]
412
+ case optchar
413
+ when ?f, ?a, ?I
414
+ arg = optstr.empty? ? argv.shift : optstr
415
+ raise option_error(errcode_table[optchar], optchar.chr) unless arg
416
+ @options[optkey] = arg
417
+ optstr = ''
398
418
  else
399
- while optstr && !optstr.empty?
400
- optchar = optstr[0]
401
- optstr[0,1] = ""
402
- unless option_table.key?(optchar)
403
- #* key=:command_option_invalid msg="-%s: invalid command option."
404
- raise option_error(:command_option_invalid, optchar.chr)
405
- end
406
- optkey = option_table[optchar]
407
- case optchar
408
- when ?f, ?a, ?I
409
- arg = optstr.empty? ? argv.shift : optstr
410
- raise option_error(errcode_table[optchar], optchar.chr) unless arg
411
- @options[optkey] = arg
412
- optstr = ''
413
- else
414
- @options[optkey] = true
415
- end
416
- end
419
+ @options[optkey] = true
417
420
  end
418
- end # end of while
419
- #
420
- @options[:linenum] = true if @options[:emacs]
421
- @options[:help] = true if @properties[:help]
422
- @options[:version] = true if @properties[:version]
423
- filenames = argv
424
- return filenames
425
- end
421
+ end
422
+ end
423
+ end # end of while
424
+ #
425
+ @options[:linenum] = true if @options[:emacs]
426
+ @options[:help] = true if @properties[:help]
427
+ @options[:version] = true if @properties[:version]
428
+ filenames = argv
429
+ return filenames
430
+ end
426
431
 
427
432
 
428
- def _domain_type?(doc)
429
- klass = defined?(YAML::DomainType) ? YAML::DomainType : YAML::Syck::DomainType
430
- return doc.is_a?(klass)
431
- end
433
+ def _domain_type?(doc)
434
+ klass = defined?(YAML::DomainType) ? YAML::DomainType : YAML::Syck::DomainType
435
+ return doc.is_a?(klass)
436
+ end
437
+
438
+
439
+ end
432
440
 
433
- end
434
441
 
435
442
  end