kwalify 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.txt +232 -0
- data/MIT-LICENSE +20 -0
- data/README.txt +16 -19
- data/bin/kwalify +3 -3
- data/contrib/inline-require +6 -4
- data/contrib/kwalify +3719 -2427
- data/doc-api/classes/CommandOptionError.html +17 -17
- data/doc-api/classes/CommandOptionParser.html +63 -63
- data/doc-api/classes/Kwalify.html +29 -7
- data/doc-api/classes/Kwalify/AssertionError.html +9 -9
- data/doc-api/classes/Kwalify/BaseError.html +72 -71
- data/doc-api/classes/Kwalify/BaseParser.html +461 -0
- data/doc-api/classes/Kwalify/CommandOptionError.html +11 -11
- data/doc-api/classes/Kwalify/ErrorHelper.html +51 -46
- data/doc-api/classes/Kwalify/HashInterface.html +13 -135
- data/doc-api/classes/Kwalify/Json.html +105 -0
- data/doc-api/classes/Kwalify/Main.html +129 -126
- data/doc-api/classes/Kwalify/MetaValidator.html +248 -232
- data/doc-api/classes/Kwalify/Parser.html +12 -12
- data/doc-api/classes/Kwalify/PlainYamlParser.html +166 -163
- data/doc-api/classes/Kwalify/PlainYamlParser/Alias.html +11 -11
- data/doc-api/classes/Kwalify/Rule.html +152 -130
- data/doc-api/classes/Kwalify/SchemaError.html +10 -10
- data/doc-api/classes/Kwalify/SyntaxError.html +185 -0
- data/doc-api/classes/Kwalify/Types.html +26 -25
- data/doc-api/classes/Kwalify/Util.html +389 -0
- data/doc-api/classes/Kwalify/Util/HashLike.html +246 -0
- data/doc-api/classes/Kwalify/Util/OrderedHash.html +330 -0
- data/doc-api/classes/Kwalify/ValidationError.html +10 -10
- data/doc-api/classes/Kwalify/Validator.html +153 -86
- data/doc-api/classes/Kwalify/Yaml.html +181 -0
- data/doc-api/classes/Kwalify/Yaml/Parser.html +1538 -0
- data/doc-api/classes/Kwalify/YamlParser.html +190 -183
- data/doc-api/classes/Kwalify/YamlSyntaxError.html +8 -57
- data/doc-api/created.rid +1 -1
- data/doc-api/files/__/README_txt.html +17 -22
- data/doc-api/files/kwalify/errors_rb.html +2 -2
- data/doc-api/files/kwalify/main_rb.html +4 -3
- data/doc-api/files/kwalify/messages_rb.html +2 -2
- data/doc-api/files/kwalify/meta-validator_rb.html +3 -3
- data/doc-api/files/kwalify/{util/yaml-helper_rb.html → parser/base_rb.html} +8 -6
- data/doc-api/files/kwalify/parser/yaml_rb.html +117 -0
- data/doc-api/files/kwalify/rule_rb.html +2 -2
- data/doc-api/files/kwalify/types_rb.html +2 -2
- data/doc-api/files/kwalify/util/assert-text-equal_rb.html +2 -2
- data/doc-api/files/kwalify/util/hash-interface_rb.html +9 -2
- data/doc-api/files/kwalify/util/hashlike_rb.html +107 -0
- data/doc-api/files/kwalify/util/option-parser_rb.html +2 -2
- data/doc-api/files/kwalify/util/ordered-hash_rb.html +107 -0
- data/doc-api/files/kwalify/util/testcase-helper_rb.html +2 -2
- data/doc-api/files/kwalify/util_rb.html +107 -0
- data/doc-api/files/kwalify/validator_rb.html +2 -2
- data/doc-api/files/kwalify/yaml-parser_rb.html +2 -2
- data/doc-api/files/kwalify_rb.html +3 -2
- data/doc-api/fr_class_index.html +8 -1
- data/doc-api/fr_file_index.html +5 -1
- data/doc-api/fr_method_index.html +128 -69
- data/doc/img/fig01.png +0 -0
- data/doc/users-guide.html +882 -717
- data/examples/address-book/address-book.schema.yaml +2 -2
- data/examples/data-binding/BABEL.data.yaml +63 -0
- data/examples/data-binding/BABEL.schema.yaml +31 -0
- data/examples/data-binding/Makefile +8 -0
- data/examples/data-binding/Rakefile +13 -0
- data/examples/data-binding/main.rb +27 -0
- data/examples/invoice/invoice.schema.yaml +3 -3
- data/examples/tapkit/tapkit.schema.yaml +2 -2
- data/lib/kwalify.rb +41 -4
- data/lib/kwalify/errors.rb +118 -96
- data/lib/kwalify/kwalify.schema.yaml +58 -0
- data/lib/kwalify/main.rb +384 -377
- data/lib/kwalify/messages.rb +41 -27
- data/lib/kwalify/meta-validator.rb +251 -331
- data/lib/kwalify/parser/base.rb +127 -0
- data/lib/kwalify/parser/yaml.rb +837 -0
- data/lib/kwalify/rule.rb +545 -487
- data/lib/kwalify/templates/genclass-java.eruby +189 -162
- data/lib/kwalify/templates/genclass-php.eruby +104 -0
- data/lib/kwalify/templates/genclass-ruby.eruby +95 -66
- data/lib/kwalify/types.rb +107 -106
- data/lib/kwalify/util.rb +157 -0
- data/lib/kwalify/util/assert-text-equal.rb +33 -31
- data/lib/kwalify/util/hash-interface.rb +11 -30
- data/lib/kwalify/util/hashlike.rb +51 -0
- data/lib/kwalify/util/option-parser.rb +144 -144
- data/lib/kwalify/util/ordered-hash.rb +57 -0
- data/lib/kwalify/util/testcase-helper.rb +3 -3
- data/lib/kwalify/validator.rb +267 -212
- data/lib/kwalify/yaml-parser.rb +822 -768
- data/setup.rb +861 -607
- data/test/Rookbook.yaml +10 -0
- data/test/{tmp.dir/Context.java → data/users-guide/AddressBook.java.expected} +11 -11
- data/test/data/users-guide/BABEL.data.yaml +24 -0
- data/test/data/users-guide/BABEL.schema.yaml +30 -0
- data/test/data/users-guide/ExampleAddressBook.java +47 -0
- data/test/{tmp.dir/Group.java → data/users-guide/Group.java.expected} +2 -11
- data/test/data/users-guide/Person.java.expected +44 -0
- data/test/data/users-guide/address_book.rb +52 -0
- data/test/data/users-guide/address_book.schema.yaml +28 -0
- data/test/data/users-guide/address_book.yaml +27 -0
- data/test/data/users-guide/answers-schema.yaml +12 -0
- data/test/data/users-guide/answers-validator.rb +52 -0
- data/test/data/users-guide/babel_genclass.result +26 -0
- data/test/data/users-guide/config.schema.yaml +7 -0
- data/test/data/users-guide/config.yaml +4 -0
- data/test/{tmp.dir/silent1.document → data/users-guide/document01a.yaml} +0 -0
- data/test/data/users-guide/document01b.yaml +3 -0
- data/test/data/users-guide/document02a.yaml +4 -0
- data/test/data/users-guide/document02b.yaml +4 -0
- data/test/data/users-guide/document03a.yaml +6 -0
- data/test/data/users-guide/document03b.yaml +6 -0
- data/test/data/users-guide/document04a.yaml +9 -0
- data/test/data/users-guide/document04b.yaml +9 -0
- data/test/data/users-guide/document05a.yaml +11 -0
- data/test/data/users-guide/document05b.yaml +12 -0
- data/test/data/users-guide/document06a.yaml +15 -0
- data/test/data/users-guide/document06b.yaml +16 -0
- data/test/data/users-guide/document07a.yaml +9 -0
- data/test/data/users-guide/document07b.yaml +7 -0
- data/test/data/users-guide/document12a.json +10 -0
- data/test/data/users-guide/document12b.json +6 -0
- data/test/data/users-guide/document13a.yaml +17 -0
- data/test/data/users-guide/document14a.yaml +3 -0
- data/test/data/users-guide/document14b.yaml +3 -0
- data/test/data/users-guide/document15a.yaml +6 -0
- data/test/data/users-guide/document15b.yaml +5 -0
- data/test/data/users-guide/example_address_book.rb +10 -0
- data/test/data/users-guide/example_address_book_java.result +32 -0
- data/test/data/users-guide/example_address_book_ruby.result +31 -0
- data/test/data/users-guide/genclass_java.result +4 -0
- data/test/data/users-guide/howto-validation-with-parsing.rb +28 -0
- data/test/data/users-guide/howto-validation.rb +25 -0
- data/test/data/users-guide/howto3.rb +6 -0
- data/test/data/users-guide/howto3.result +5 -0
- data/test/data/users-guide/howto3.yaml +8 -0
- data/test/data/users-guide/howto5_databinding.result +111 -0
- data/test/data/users-guide/invalid01.result +3 -0
- data/test/data/users-guide/invalid02.result +5 -0
- data/test/data/users-guide/invalid03.result +5 -0
- data/test/data/users-guide/invalid04.result +4 -0
- data/test/data/users-guide/invalid05.result +11 -0
- data/test/data/users-guide/invalid06.result +4 -0
- data/test/data/users-guide/invalid07.result +3 -0
- data/test/data/users-guide/invalid08.result +3 -0
- data/test/data/users-guide/invalid12.json +8 -0
- data/test/data/users-guide/invalid14.result +4 -0
- data/test/data/users-guide/invalid15.result +4 -0
- data/test/data/users-guide/loadbabel.rb +27 -0
- data/test/data/users-guide/loadconfig.rb +15 -0
- data/test/data/users-guide/loadconfig.result +2 -0
- data/test/data/users-guide/models.rb +22 -0
- data/test/data/users-guide/option_ha.result +6 -0
- data/test/data/users-guide/option_ha_genclass_java.result +7 -0
- data/test/{tmp.dir/meta1.schema → data/users-guide/schema01.yaml} +0 -0
- data/test/data/users-guide/schema02.yaml +12 -0
- data/test/data/users-guide/schema03.yaml +9 -0
- data/test/data/users-guide/schema04.yaml +20 -0
- data/test/data/users-guide/schema05.yaml +29 -0
- data/test/data/users-guide/schema06.yaml +11 -0
- data/test/data/users-guide/schema12.json +12 -0
- data/test/data/users-guide/schema13.yaml +13 -0
- data/test/data/users-guide/schema14.yaml +5 -0
- data/test/data/users-guide/schema15.yaml +21 -0
- data/test/data/users-guide/valid01.result +2 -0
- data/test/data/users-guide/valid02.result +2 -0
- data/test/data/users-guide/valid03.result +2 -0
- data/test/data/users-guide/valid04.result +2 -0
- data/test/data/users-guide/valid05.result +2 -0
- data/test/data/users-guide/valid06.result +2 -0
- data/test/data/users-guide/valid07.result +2 -0
- data/test/data/users-guide/valid08.result +2 -0
- data/test/data/users-guide/valid12.result +2 -0
- data/test/data/users-guide/valid13.result +2 -0
- data/test/data/users-guide/valid14.result +2 -0
- data/test/data/users-guide/valid15.result +2 -0
- data/test/data/users-guide/validate08.rb +37 -0
- data/test/test-action.rb +78 -0
- data/test/test-action.yaml +738 -0
- data/test/test-databinding.rb +80 -0
- data/test/test-databinding.yaml +301 -0
- data/test/test-main.rb +129 -150
- data/test/test-main.yaml +126 -321
- data/test/test-metavalidator.rb +47 -47
- data/test/test-metavalidator.yaml +77 -21
- data/test/test-parser-yaml.rb +57 -0
- data/test/test-parser-yaml.yaml +1749 -0
- data/test/test-rule.rb +14 -15
- data/test/test-rule.yaml +6 -3
- data/test/test-users-guide.rb +75 -0
- data/test/test-validator.rb +77 -52
- data/test/test-validator.yaml +168 -6
- data/test/test-yaml-parser.rb +47 -0
- data/test/{test-yamlparser.yaml → test-yaml-parser.yaml} +159 -52
- data/test/test.rb +37 -21
- metadata +136 -37
- data/COPYING +0 -340
- data/ChangeLog +0 -70
- data/doc-api/classes/YamlHelper.html +0 -259
- data/lib/kwalify/util/yaml-helper.rb +0 -82
- data/test/test-yamlparser.rb +0 -58
- data/test/tmp.dir/User.java +0 -43
- data/test/tmp.dir/action1.document +0 -18
- data/test/tmp.dir/action1.schema +0 -32
- data/test/tmp.dir/action2.document +0 -18
- data/test/tmp.dir/action2.schema +0 -32
- data/test/tmp.dir/emacs.document +0 -6
- data/test/tmp.dir/emacs.schema +0 -6
- data/test/tmp.dir/meta1.document +0 -0
- data/test/tmp.dir/meta2.document +0 -0
- data/test/tmp.dir/meta2.schema +0 -3
- data/test/tmp.dir/silent1.schema +0 -3
- data/test/tmp.dir/silent2.document +0 -7
- data/test/tmp.dir/silent2.schema +0 -3
- data/test/tmp.dir/stream.invalid +0 -8
- data/test/tmp.dir/stream.schema +0 -3
- data/test/tmp.dir/stream.valid +0 -8
- data/test/tmp.dir/untabify.document +0 -5
- data/test/tmp.dir/untabify.schema +0 -10
- data/todo.txt +0 -34
@@ -1,32 +1,44 @@
|
|
1
1
|
<%
|
2
2
|
|
3
|
+
###
|
4
|
+
### $Rev: 95 $
|
5
|
+
### $Release: 0.7.0 $
|
6
|
+
### copyright(c) 2005-2008 kuwata-lab all rights reserved.
|
7
|
+
###
|
8
|
+
|
3
9
|
require 'fileutils'
|
4
10
|
|
5
11
|
if @describe
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
12
|
+
sb = []
|
13
|
+
sb << " --package=name : package name\n"
|
14
|
+
sb << " --extends=name : class name to extend\n"
|
15
|
+
sb << " --implements=name,... : interface names to implement\n"
|
16
|
+
sb << " --dir=path : directory to locate output file\n"
|
17
|
+
sb << " --basedir=path : base directory to locate output file\n"
|
18
|
+
sb << " --constructor=false : not print initialize() method\n"
|
19
|
+
#sb << " --ggap : use generation gap pattern\n"
|
20
|
+
return sb.join
|
13
21
|
end
|
14
22
|
|
15
23
|
|
16
|
-
def java_type(
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
24
|
+
def java_type(rulehash)
|
25
|
+
return rulehash['class'] if rulehash['class']
|
26
|
+
imports = []
|
27
|
+
case rulehash['type']
|
28
|
+
when nil ; return 'String'
|
29
|
+
when 'str', 'text' ; return 'String'
|
30
|
+
when 'char' ; return 'char'
|
31
|
+
when 'int' ; return 'int'
|
32
|
+
when 'float' ; return 'float'
|
33
|
+
when 'bool' ; return 'boolean'
|
34
|
+
when 'date' ; return 'Date'
|
35
|
+
when 'timestamp' ; return 'Date'
|
36
|
+
when 'seq' ; return 'List'
|
37
|
+
when 'map' ; return 'Map'
|
38
|
+
when 'any' ; return 'Object'
|
39
|
+
else
|
40
|
+
raise "*** genclass-java.eruby: '#{rulehash['type']}': unknown type.\n"
|
41
|
+
end
|
30
42
|
end
|
31
43
|
|
32
44
|
|
@@ -37,159 +49,174 @@
|
|
37
49
|
|
38
50
|
|
39
51
|
def camelize(str, all=true)
|
40
|
-
|
41
|
-
|
42
|
-
|
52
|
+
s = str.split(/[^\w]/).collect { |w| w.capitalize }.join()
|
53
|
+
s[0,1] = s[0].chr.lower unless all
|
54
|
+
return s
|
43
55
|
end
|
44
56
|
|
45
57
|
|
46
|
-
def write_file(
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
$stdout.print "done.\n"
|
58
|
+
def write_file(classname, classdef)
|
59
|
+
if @properties[:dir]
|
60
|
+
dir = @properties[:dir]
|
61
|
+
else
|
62
|
+
basedir = @properties[:basedir] || '.'
|
63
|
+
dir = basedir.dup
|
64
|
+
dir << '/' << @properties[:package].gsub(/\./, '/') if @properties[:package]
|
65
|
+
end
|
66
|
+
FileUtils.mkdir_p(dir) unless test(?d, dir)
|
67
|
+
filename = "#{dir}/#{classname}.java"
|
68
|
+
print "generating #{filename}..."
|
69
|
+
File.open(filename, 'w') { |f| f.write(classdef) }
|
70
|
+
print "done.\n"
|
60
71
|
end
|
61
72
|
|
62
73
|
|
63
|
-
def generate_classdef(
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
74
|
+
def generate_classdef(schema)
|
75
|
+
hash = Kwalify::Util::OrderedHash.new
|
76
|
+
Kwalify::Util.traverse_schema(schema) do |rulehash|
|
77
|
+
if rulehash['class']
|
78
|
+
classdef = _generate(rulehash)
|
79
|
+
classname = rulehash['class']
|
80
|
+
hash[classname] = classdef if !hash[classname] || classdef.length > hash[classname].length
|
81
|
+
end
|
82
|
+
end
|
83
|
+
hash.each do |classname, classdef|
|
84
|
+
write_file(classname, classdef)
|
85
|
+
end
|
86
|
+
print ""
|
71
87
|
end
|
72
88
|
|
73
89
|
|
74
|
-
def _generate(
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
sb << " }\n"
|
143
|
-
sb << " }\n"
|
144
|
-
sb << " _%-12s = seq;\n" % name
|
145
|
-
else
|
146
|
-
type = java_type(map_rule)
|
147
|
-
@wrapper_classes ||= {
|
148
|
-
'int' => 'Number', 'short' => 'Number',
|
149
|
-
'float' => 'Number', 'double' => 'Number',
|
150
|
-
'long' => 'Number', 'byte' => 'Number',
|
151
|
-
'char' => 'Character',
|
152
|
-
'boolean' => 'Boolean',
|
153
|
-
}
|
154
|
-
if wrapper = @wrapper_classes[type]
|
155
|
-
sb << " if (map.get(\"#{name}\") != null) {\n"
|
156
|
-
sb << " _%-8s = ((#{wrapper})map.get(\"#{name}\")).#{type}Value();\n" % name
|
157
|
-
sb << " }\n"
|
158
|
-
else
|
159
|
-
sb << " _%-12s = (#{type})map.get(\"#{name}\");\n" % name
|
160
|
-
end
|
161
|
-
end #if
|
162
|
-
end #each
|
163
|
-
sb << " }\n"
|
164
|
-
sb << "\n"
|
165
|
-
|
166
|
-
## setter/getter
|
167
|
-
rule.mapping.each do |name, map_rule|
|
168
|
-
next unless name =~ /\A[a-zA-Z_]\w*\z/
|
169
|
-
type = java_type(map_rule)
|
170
|
-
sb << " public #{type} get#{camelize(name)}() { return _#{name}; }\n"
|
171
|
-
sb << " public void set#{camelize(name)}(#{type} #{name}_) { _#{name} = #{name}_; }\n"
|
172
|
-
end
|
90
|
+
def _generate(rulehash)
|
91
|
+
classname = rulehash['class']
|
92
|
+
return unless classname
|
93
|
+
assert unless rulehash['mapping']
|
94
|
+
|
95
|
+
## preamble
|
96
|
+
schema_filename = @properties[:schema_filename]
|
97
|
+
sb = ''
|
98
|
+
sb << "// generated by kwalify from #{schema_filename}\n"
|
99
|
+
sb << "\n"
|
100
|
+
|
101
|
+
## package and import classes
|
102
|
+
package = @properties[:package]
|
103
|
+
sb << "package #{package};\n" if package
|
104
|
+
sb << "import java.util.*;\n"
|
105
|
+
sb << "\n"
|
106
|
+
|
107
|
+
## start class declaration
|
108
|
+
extends = @properties[:extends] ? " extends #{@properties[:extends] % classname}" : nil
|
109
|
+
implements = @properties[:implements] ? " implements #{@properties[:implements]}" : nil
|
110
|
+
classname = classname + '_' if @properties[:ggap]
|
111
|
+
sb << "/**\n"
|
112
|
+
sb << " * #{rulehash['desc']}\n"
|
113
|
+
sb << " */\n"
|
114
|
+
sb << "public class #{classname}#{extends}#{implements} {\n"
|
115
|
+
sb << "\n"
|
116
|
+
|
117
|
+
## instance variables
|
118
|
+
rulehash['mapping'].each do |name, map_rulehash|
|
119
|
+
next unless name =~ /\A[a-zA-Z_][-\w]*\z/
|
120
|
+
name2 = name.gsub(/-/, '_')
|
121
|
+
type = java_type(map_rulehash)
|
122
|
+
if map_rulehash['default'].nil?
|
123
|
+
sb << " private #{type} _#{name2};\n"
|
124
|
+
else
|
125
|
+
sb << " private #{type} _#{name2} = #{map_rulehash['default'].inspect};\n"
|
126
|
+
end
|
127
|
+
end
|
128
|
+
sb << "\n"
|
129
|
+
|
130
|
+
## constructors
|
131
|
+
if @properties[:constructor] != false
|
132
|
+
sb << _generate_constructor(rulehash)
|
133
|
+
sb << "\n"
|
134
|
+
end #if
|
135
|
+
|
136
|
+
## setter/getter
|
137
|
+
rulehash['mapping'].each do |name, map_rulehash|
|
138
|
+
next unless name =~ /\A[a-zA-Z_][-\w]*\z/
|
139
|
+
name2 = name.gsub(/-/, '_')
|
140
|
+
type = java_type(map_rulehash)
|
141
|
+
get = type == 'boolean' ? 'is' : 'get'
|
142
|
+
sb << " public #{type} #{get}#{camelize(name)}() { return _#{name2}; }\n"
|
143
|
+
sb << " public void set#{camelize(name)}(#{type} #{name2}_) { _#{name2} = #{name2}_; }\n"
|
144
|
+
end
|
145
|
+
|
146
|
+
## end of class declaration
|
147
|
+
if @properties[:ggap]
|
148
|
+
sb << "}\n"
|
149
|
+
sb << "public class #{rulehash['class']} < #{classname}\n"
|
150
|
+
end
|
151
|
+
sb << "}\n"
|
152
|
+
|
153
|
+
##
|
154
|
+
classdef = sb
|
155
|
+
return classdef
|
156
|
+
end
|
157
|
+
|
173
158
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
159
|
+
def _generate_constructor(rulehash)
|
160
|
+
classname = rulehash['class']
|
161
|
+
sb = ''
|
162
|
+
## default constructor
|
163
|
+
sb << " public #{classname}() {}\n"
|
164
|
+
sb << "\n"
|
165
|
+
## constructor
|
166
|
+
sb << " public #{classname}(Map map) {\n"
|
167
|
+
objdecl = " Object obj;\n"
|
168
|
+
seqdecl = " List seq;\n"
|
169
|
+
rulehash['mapping'].each do |name, map_rulehash|
|
170
|
+
next unless name =~ /\A[a-zA-Z_][-\w]*\z/
|
171
|
+
name2 = name.gsub(/-/, '_')
|
172
|
+
if map_rulehash['class']
|
173
|
+
cname = map_rulehash['class']
|
174
|
+
type = java_type(map_rulehash)
|
175
|
+
sb << objdecl; objdecl = ''
|
176
|
+
sb << " if ((obj = map.get(\"#{name}\")) != null && obj instanceof Map) {\n"
|
177
|
+
sb << " _%-8s = new #{cname}((Map)obj);\n" % name2
|
178
|
+
sb << " } else {\n"
|
179
|
+
sb << " _%-8s = (#{type})obj;\n" % name2
|
180
|
+
sb << " }\n"
|
181
|
+
elsif map_rulehash['sequence'] && map_rulehash['sequence'][0]['class']
|
182
|
+
item_rulehash = map_rulehash['sequence'][0]
|
183
|
+
cname = item_rulehash['class']
|
184
|
+
type = java_type(item_rulehash)
|
185
|
+
sb << seqdecl; seqdecl = ''
|
186
|
+
sb << objdecl; objdecl = ''
|
187
|
+
sb << " if ((seq = (List)map.get(\"#{name}\")) != null) {\n"
|
188
|
+
sb << " for (int i = 0; i < seq.size(); i++) {\n"
|
189
|
+
sb << " if ((obj = seq.get(i)) instanceof Map) {\n"
|
190
|
+
sb << " seq.set(i, new #{cname}((Map)obj));\n"
|
191
|
+
sb << " }\n"
|
192
|
+
sb << " }\n"
|
193
|
+
sb << " }\n"
|
194
|
+
sb << " _%-12s = seq;\n" % name2
|
195
|
+
else
|
196
|
+
type = java_type(map_rulehash)
|
197
|
+
@wrapper_classes ||= {
|
198
|
+
'int' => 'Number', 'short' => 'Number',
|
199
|
+
'float' => 'Number', 'double' => 'Number',
|
200
|
+
'long' => 'Number', 'byte' => 'Number',
|
201
|
+
'char' => 'Character',
|
202
|
+
'boolean' => 'Boolean',
|
203
|
+
}
|
204
|
+
if (wrapper = @wrapper_classes[type])
|
205
|
+
sb << " if (map.get(\"#{name}\") != null) {\n"
|
206
|
+
sb << " _%-8s = ((#{wrapper})map.get(\"#{name}\")).#{type}Value();\n" % name2
|
207
|
+
sb << " }\n"
|
208
|
+
elsif map_rulehash.key?('default')
|
209
|
+
sb << " if (map.get(\"#{name}\") != null)\n"
|
210
|
+
sb << " _%-8s = (#{type})map.get(\"#{name}\");\n" % name2
|
211
|
+
else
|
212
|
+
sb << " _%-12s = (#{type})map.get(\"#{name}\");\n" % name2
|
178
213
|
end
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
end #if
|
184
|
-
#
|
185
|
-
rule.sequence.each do |seq_rule|
|
186
|
-
_generate(seq_rule, classdef_list, done)
|
187
|
-
end if rule.sequence
|
188
|
-
rule.mapping.each do |name, map_rule|
|
189
|
-
_generate(map_rule, classdef_list, done)
|
190
|
-
end if rule.mapping
|
214
|
+
end #if
|
215
|
+
end #each
|
216
|
+
sb << " }\n"
|
217
|
+
return sb
|
191
218
|
end
|
192
219
|
|
193
220
|
|
194
221
|
%>
|
195
|
-
<%= generate_classdef(@
|
222
|
+
<%= generate_classdef(@schema) %>
|
@@ -0,0 +1,104 @@
|
|
1
|
+
<%
|
2
|
+
|
3
|
+
###
|
4
|
+
### $Rev$
|
5
|
+
### $Release: 0.7.0 $
|
6
|
+
### copyright(c) 2005-2008 kuwata-lab all rights reserved.
|
7
|
+
###
|
8
|
+
|
9
|
+
if @describe
|
10
|
+
sb = []
|
11
|
+
sb << " --namespace=name : prefix name of class\n"
|
12
|
+
sb << " --extends=name : parent class name\n"
|
13
|
+
sb << " --inplements=name: interface name\n"
|
14
|
+
sb << " --constructor=false : not print constructor\n"
|
15
|
+
#sb << " --ggap : use generation gap pattern\n"
|
16
|
+
return sb.join
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
require 'kwalify/util/ordered-hash'
|
21
|
+
|
22
|
+
def generate_classdef(schema)
|
23
|
+
hash = Kwalify::Util::OrderedHash.new
|
24
|
+
#hash.instance_eval { alias []= add }
|
25
|
+
Kwalify::Util.traverse_schema(schema) do |rulehash|
|
26
|
+
if rulehash['class']
|
27
|
+
arr = _generate_classdef(rulehash)
|
28
|
+
key = rulehash['class']
|
29
|
+
classdef = arr.join()
|
30
|
+
#hash[key] = classdef if !hash.key?(key) || classdef != hash[key]
|
31
|
+
hash[key] = classdef if !hash.key?(key) || classdef.length > hash[key].length
|
32
|
+
end
|
33
|
+
end
|
34
|
+
sb = "<?php\n"
|
35
|
+
sb << "\n"
|
36
|
+
hash.each do |classname, classdef|
|
37
|
+
sb << classdef
|
38
|
+
sb << "\n"
|
39
|
+
end
|
40
|
+
sb << "?>\n"
|
41
|
+
return sb
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
def _generate_classdef(rulehash)
|
46
|
+
return nil unless rulehash['class']
|
47
|
+
assert unless rulehash['mapping']
|
48
|
+
classname = rulehash['class']
|
49
|
+
classname = classname + '_' if @properties[:ggap]
|
50
|
+
ns = @properties[:namespace]
|
51
|
+
ns = ns + '_' if ns
|
52
|
+
flag_init = @properties[:constructor] != false
|
53
|
+
v = nil
|
54
|
+
arr = ['']
|
55
|
+
arr << "/// #{rulehash['desc']}\n"
|
56
|
+
arr << "class #{ns}#{classname}"
|
57
|
+
arr << " extends #{v}" if v = @properties[:extends]
|
58
|
+
arr << " implements #{v}" if v = @properties[:implements]
|
59
|
+
arr << " {\n"
|
60
|
+
arr2 = [] # assign into instance variables
|
61
|
+
rulehash['mapping'].each do |name, map_rulehash|
|
62
|
+
next unless name =~ /\A[a-zA-Z_][-\w]*\z/
|
63
|
+
name2 = name.gsub(/-/, '_')
|
64
|
+
var = "%-10s" % name2
|
65
|
+
len = 10 - name2.length
|
66
|
+
spc = ' ' * (len >= 0 ? len : 0)
|
67
|
+
if map_rulehash.key?('default')
|
68
|
+
arr << " var $#{name2} = #{map_rulehash['default'].inspect}; // #{map_rulehash['type'] || 'str'}\n"
|
69
|
+
else
|
70
|
+
arr << " var $#{name2}; #{spc}// #{map_rulehash['type'] || 'str'}\n"
|
71
|
+
end
|
72
|
+
if !flag_init
|
73
|
+
# pass
|
74
|
+
elsif map_rulehash['class']
|
75
|
+
cname = map_rulehash['class']
|
76
|
+
arr2 << " $this->#{var} = is_array(@$arr['#{name}']) ? new #{cname}($arr['#{name}']) : @$arr['#{name}'];\n"
|
77
|
+
elsif map_rulehash['sequence'] && map_rulehash['sequence'][0]['class']
|
78
|
+
item_rulehash = map_rulehash['sequence'][0]
|
79
|
+
cname = item_rulehash['class']
|
80
|
+
arr2 << " if (is_array(@$arr['#{name}']))\n"
|
81
|
+
arr2 << " foreach ($arr['#{name}'] as $k=>$v)\n"
|
82
|
+
arr2 << " $this->#{name2}[$k] = is_array($v) ? new #{cname}($v) : $v;\n"
|
83
|
+
arr2 << " //else\n"
|
84
|
+
arr2 << " // $this->#{var} = @$arr['#{name}'];\n"
|
85
|
+
elsif map_rulehash.key?('default')
|
86
|
+
arr2 << " if (array_key_exists('#{name}', $arr)) $this->#{name2} = $arr['#{name}'];\n"
|
87
|
+
else
|
88
|
+
arr2 << " $this->#{var} = @$arr['#{name}'];\n"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
arr << " function __construct($arr=NULL) {\n" if flag_init
|
92
|
+
arr << " if (! $arr) return;\n" if flag_init
|
93
|
+
arr.concat(arr2)
|
94
|
+
arr << " }\n" if flag_init
|
95
|
+
if @properties[:ggap]
|
96
|
+
arr << "}\n"
|
97
|
+
arr << "class #{ns}#{rulehash['class']} extends #{ns}#{classname}\n"
|
98
|
+
end
|
99
|
+
arr << "}\n"
|
100
|
+
return arr
|
101
|
+
end
|
102
|
+
|
103
|
+
%>
|
104
|
+
<%= generate_classdef(@schema) %>
|