kwalify 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +24 -19
- data/README.txt +51 -51
- data/bin/kwalify +2 -2
- data/contrib/inline-require +151 -0
- data/contrib/kwalify +2850 -0
- data/doc-api/classes/CommandOptionError.html +184 -0
- data/doc-api/classes/CommandOptionParser.html +325 -0
- data/doc-api/classes/Kwalify.html +270 -0
- data/doc-api/classes/Kwalify/AssertionError.html +148 -0
- data/doc-api/classes/Kwalify/BaseError.html +296 -0
- data/doc-api/classes/Kwalify/CommandOptionError.html +168 -0
- data/doc-api/classes/Kwalify/ErrorHelper.html +218 -0
- data/doc-api/classes/Kwalify/HashInterface.html +240 -0
- data/doc-api/classes/Kwalify/KwalifyError.html +111 -0
- data/doc-api/classes/Kwalify/Main.html +336 -0
- data/doc-api/classes/Kwalify/MetaValidator.html +432 -0
- data/doc-api/classes/Kwalify/Parser.html +155 -0
- data/doc-api/classes/Kwalify/PlainYamlParser.html +520 -0
- data/doc-api/classes/Kwalify/PlainYamlParser/Alias.html +165 -0
- data/doc-api/classes/Kwalify/Rule.html +411 -0
- data/doc-api/classes/Kwalify/SchemaError.html +148 -0
- data/doc-api/classes/Kwalify/Types.html +301 -0
- data/doc-api/classes/Kwalify/ValidationError.html +148 -0
- data/doc-api/classes/Kwalify/Validator.html +311 -0
- data/doc-api/classes/Kwalify/YamlParser.html +535 -0
- data/doc-api/classes/Kwalify/YamlSyntaxError.html +168 -0
- data/doc-api/classes/Test.html +107 -0
- data/doc-api/classes/Test/Unit.html +101 -0
- data/doc-api/classes/YamlHelper.html +259 -0
- data/doc-api/created.rid +1 -0
- data/doc-api/files/__/README_txt.html +179 -0
- data/doc-api/files/kwalify/errors_rb.html +114 -0
- data/doc-api/files/kwalify/main_rb.html +117 -0
- data/doc-api/files/kwalify/messages_rb.html +107 -0
- data/doc-api/files/kwalify/meta-validator_rb.html +117 -0
- data/doc-api/files/kwalify/rule_rb.html +116 -0
- data/doc-api/files/kwalify/types_rb.html +114 -0
- data/doc-api/files/kwalify/util/assert-text-equal_rb.html +115 -0
- data/doc-api/files/kwalify/util/hash-interface_rb.html +107 -0
- data/doc-api/files/kwalify/util/option-parser_rb.html +107 -0
- data/doc-api/files/kwalify/util/testcase-helper_rb.html +115 -0
- data/doc-api/files/kwalify/util/yaml-helper_rb.html +114 -0
- data/doc-api/files/kwalify/validator_rb.html +117 -0
- data/doc-api/files/kwalify/yaml-parser_rb.html +117 -0
- data/doc-api/files/kwalify_rb.html +120 -0
- data/doc-api/fr_class_index.html +50 -0
- data/doc-api/fr_file_index.html +41 -0
- data/doc-api/fr_method_index.html +109 -0
- data/doc-api/index.html +24 -0
- data/doc-api/rdoc-style.css +208 -0
- data/doc/users-guide.html +693 -193
- data/examples/address-book/Makefile +5 -0
- data/examples/address-book/address-book.schema.yaml +2 -1
- data/examples/invoice/Makefile +5 -0
- data/examples/invoice/invoice.schema.yaml +3 -2
- data/examples/tapkit/Makefile +5 -0
- data/examples/tapkit/main.rb +7 -0
- data/examples/tapkit/tapkit.schema.yaml +6 -1
- data/lib/kwalify.rb +3 -3
- data/lib/kwalify/errors.rb +2 -2
- data/lib/kwalify/main.rb +161 -84
- data/lib/kwalify/messages.rb +17 -11
- data/lib/kwalify/meta-validator.rb +11 -2
- data/lib/kwalify/rule.rb +13 -3
- data/lib/kwalify/templates/genclass-java.eruby +195 -0
- data/lib/kwalify/templates/genclass-ruby.eruby +84 -0
- data/lib/kwalify/types.rb +18 -18
- data/lib/kwalify/util/assert-text-equal.rb +44 -0
- data/lib/kwalify/util/hash-interface.rb +37 -0
- data/lib/kwalify/util/option-parser.rb +2 -2
- data/lib/kwalify/util/testcase-helper.rb +112 -0
- data/lib/kwalify/util/yaml-helper.rb +2 -2
- data/lib/kwalify/validator.rb +2 -2
- data/lib/kwalify/yaml-parser.rb +12 -9
- data/test/test-main.rb +77 -78
- data/test/test-main.yaml +543 -769
- data/test/test-metavalidator.rb +27 -47
- data/test/test-metavalidator.yaml +21 -2
- data/test/test-rule.rb +6 -39
- data/test/test-rule.yaml +2 -2
- data/test/test-validator.rb +36 -869
- data/test/test-validator.yaml +28 -20
- data/test/test-yamlparser.rb +30 -1248
- data/test/test-yamlparser.yaml +138 -110
- data/test/test.rb +33 -13
- data/test/tmp.dir/Context.java +40 -0
- data/test/tmp.dir/Group.java +33 -0
- data/test/tmp.dir/User.java +43 -0
- data/test/tmp.dir/action1.document +18 -0
- data/test/tmp.dir/action1.schema +32 -0
- data/test/tmp.dir/action2.document +18 -0
- data/test/tmp.dir/action2.schema +32 -0
- data/test/tmp.dir/emacs.document +6 -0
- data/test/tmp.dir/emacs.schema +6 -0
- data/test/tmp.dir/meta1.document +0 -0
- data/test/tmp.dir/meta1.schema +3 -0
- data/test/tmp.dir/meta2.document +0 -0
- data/test/tmp.dir/meta2.schema +3 -0
- data/test/tmp.dir/silent1.document +3 -0
- data/test/tmp.dir/silent1.schema +3 -0
- data/test/tmp.dir/silent2.document +7 -0
- data/test/tmp.dir/silent2.schema +3 -0
- data/test/tmp.dir/stream.invalid +8 -0
- data/test/tmp.dir/stream.schema +3 -0
- data/test/tmp.dir/stream.valid +8 -0
- data/test/tmp.dir/untabify.document +5 -0
- data/test/tmp.dir/untabify.schema +10 -0
- metadata +98 -12
- data/lib/kwalify/util/assert-diff.rb +0 -44
@@ -0,0 +1,208 @@
|
|
1
|
+
|
2
|
+
body {
|
3
|
+
font-family: Verdana,Arial,Helvetica,sans-serif;
|
4
|
+
font-size: 90%;
|
5
|
+
margin: 0;
|
6
|
+
margin-left: 40px;
|
7
|
+
padding: 0;
|
8
|
+
background: white;
|
9
|
+
}
|
10
|
+
|
11
|
+
h1,h2,h3,h4 { margin: 0; color: #efefef; background: transparent; }
|
12
|
+
h1 { font-size: 150%; }
|
13
|
+
h2,h3,h4 { margin-top: 1em; }
|
14
|
+
|
15
|
+
a { background: #eef; color: #039; text-decoration: none; }
|
16
|
+
a:hover { background: #039; color: #eef; }
|
17
|
+
|
18
|
+
/* Override the base stylesheet's Anchor inside a table cell */
|
19
|
+
td > a {
|
20
|
+
background: transparent;
|
21
|
+
color: #039;
|
22
|
+
text-decoration: none;
|
23
|
+
}
|
24
|
+
|
25
|
+
/* and inside a section title */
|
26
|
+
.section-title > a {
|
27
|
+
background: transparent;
|
28
|
+
color: #eee;
|
29
|
+
text-decoration: none;
|
30
|
+
}
|
31
|
+
|
32
|
+
/* === Structural elements =================================== */
|
33
|
+
|
34
|
+
div#index {
|
35
|
+
margin: 0;
|
36
|
+
margin-left: -40px;
|
37
|
+
padding: 0;
|
38
|
+
font-size: 90%;
|
39
|
+
}
|
40
|
+
|
41
|
+
|
42
|
+
div#index a {
|
43
|
+
margin-left: 0.7em;
|
44
|
+
}
|
45
|
+
|
46
|
+
div#index .section-bar {
|
47
|
+
margin-left: 0px;
|
48
|
+
padding-left: 0.7em;
|
49
|
+
background: #ccc;
|
50
|
+
font-size: small;
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
div#classHeader, div#fileHeader {
|
55
|
+
width: auto;
|
56
|
+
color: white;
|
57
|
+
padding: 0.5em 1.5em 0.5em 1.5em;
|
58
|
+
margin: 0;
|
59
|
+
margin-left: -40px;
|
60
|
+
border-bottom: 3px solid #006;
|
61
|
+
}
|
62
|
+
|
63
|
+
div#classHeader a, div#fileHeader a {
|
64
|
+
background: inherit;
|
65
|
+
color: white;
|
66
|
+
}
|
67
|
+
|
68
|
+
div#classHeader td, div#fileHeader td {
|
69
|
+
background: inherit;
|
70
|
+
color: white;
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
div#fileHeader {
|
75
|
+
background: #057;
|
76
|
+
}
|
77
|
+
|
78
|
+
div#classHeader {
|
79
|
+
background: #048;
|
80
|
+
}
|
81
|
+
|
82
|
+
|
83
|
+
.class-name-in-header {
|
84
|
+
font-size: 180%;
|
85
|
+
font-weight: bold;
|
86
|
+
}
|
87
|
+
|
88
|
+
|
89
|
+
div#bodyContent {
|
90
|
+
padding: 0 1.5em 0 1.5em;
|
91
|
+
}
|
92
|
+
|
93
|
+
div#description {
|
94
|
+
padding: 0.5em 1.5em;
|
95
|
+
background: #efefef;
|
96
|
+
border: 1px dotted #999;
|
97
|
+
}
|
98
|
+
|
99
|
+
div#description h1,h2,h3,h4,h5,h6 {
|
100
|
+
color: #125;;
|
101
|
+
background: transparent;
|
102
|
+
}
|
103
|
+
|
104
|
+
div#validator-badges {
|
105
|
+
text-align: center;
|
106
|
+
}
|
107
|
+
div#validator-badges img { border: 0; }
|
108
|
+
|
109
|
+
div#copyright {
|
110
|
+
color: #333;
|
111
|
+
background: #efefef;
|
112
|
+
font: 0.75em sans-serif;
|
113
|
+
margin-top: 5em;
|
114
|
+
margin-bottom: 0;
|
115
|
+
padding: 0.5em 2em;
|
116
|
+
}
|
117
|
+
|
118
|
+
|
119
|
+
/* === Classes =================================== */
|
120
|
+
|
121
|
+
table.header-table {
|
122
|
+
color: white;
|
123
|
+
font-size: small;
|
124
|
+
}
|
125
|
+
|
126
|
+
.type-note {
|
127
|
+
font-size: small;
|
128
|
+
color: #DEDEDE;
|
129
|
+
}
|
130
|
+
|
131
|
+
.xxsection-bar {
|
132
|
+
background: #eee;
|
133
|
+
color: #333;
|
134
|
+
padding: 3px;
|
135
|
+
}
|
136
|
+
|
137
|
+
.section-bar {
|
138
|
+
color: #333;
|
139
|
+
border-bottom: 1px solid #999;
|
140
|
+
margin-left: -20px;
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
.section-title {
|
145
|
+
background: #79a;
|
146
|
+
color: #eee;
|
147
|
+
padding: 3px;
|
148
|
+
margin-top: 2em;
|
149
|
+
margin-left: -30px;
|
150
|
+
border: 1px solid #999;
|
151
|
+
}
|
152
|
+
|
153
|
+
.top-aligned-row { vertical-align: top }
|
154
|
+
.bottom-aligned-row { vertical-align: bottom }
|
155
|
+
|
156
|
+
/* --- Context section classes ----------------------- */
|
157
|
+
|
158
|
+
.context-row { }
|
159
|
+
.context-item-name { font-family: monospace; font-weight: bold; color: black; }
|
160
|
+
.context-item-value { font-size: small; color: #448; }
|
161
|
+
.context-item-desc { color: #333; padding-left: 2em; }
|
162
|
+
|
163
|
+
/* --- Method classes -------------------------- */
|
164
|
+
.method-detail {
|
165
|
+
background: #efefef;
|
166
|
+
padding: 0;
|
167
|
+
margin-top: 0.5em;
|
168
|
+
margin-bottom: 1em;
|
169
|
+
border: 1px dotted #ccc;
|
170
|
+
}
|
171
|
+
.method-heading {
|
172
|
+
color: black;
|
173
|
+
background: #ccc;
|
174
|
+
border-bottom: 1px solid #666;
|
175
|
+
padding: 0.2em 0.5em 0 0.5em;
|
176
|
+
}
|
177
|
+
.method-signature { color: black; background: inherit; }
|
178
|
+
.method-name { font-weight: bold; }
|
179
|
+
.method-args { font-style: italic; }
|
180
|
+
.method-description { padding: 0 0.5em 0 0.5em; }
|
181
|
+
|
182
|
+
/* --- Source code sections -------------------- */
|
183
|
+
|
184
|
+
a.source-toggle { font-size: 90%; }
|
185
|
+
div.method-source-code {
|
186
|
+
background: #262626;
|
187
|
+
color: #ffdead;
|
188
|
+
margin: 1em;
|
189
|
+
padding: 0.5em;
|
190
|
+
border: 1px dashed #999;
|
191
|
+
overflow: hidden;
|
192
|
+
}
|
193
|
+
|
194
|
+
div.method-source-code pre { color: #ffdead; overflow: hidden; }
|
195
|
+
|
196
|
+
/* --- Ruby keyword styles --------------------- */
|
197
|
+
|
198
|
+
.standalone-code { background: #221111; color: #ffdead; overflow: hidden; }
|
199
|
+
|
200
|
+
.ruby-constant { color: #7fffd4; background: transparent; }
|
201
|
+
.ruby-keyword { color: #00ffff; background: transparent; }
|
202
|
+
.ruby-ivar { color: #eedd82; background: transparent; }
|
203
|
+
.ruby-operator { color: #00ffee; background: transparent; }
|
204
|
+
.ruby-identifier { color: #ffdead; background: transparent; }
|
205
|
+
.ruby-node { color: #ffa07a; background: transparent; }
|
206
|
+
.ruby-comment { color: #b22222; font-weight: bold; background: transparent; }
|
207
|
+
.ruby-regexp { color: #ffa07a; background: transparent; }
|
208
|
+
.ruby-value { color: #7fffd4; background: transparent; }
|
data/doc/users-guide.html
CHANGED
@@ -15,7 +15,7 @@
|
|
15
15
|
<div align="left"><h1>Kwalify Users' Guide (for Ruby and Java)</h1></div>
|
16
16
|
<div align="left">
|
17
17
|
makoto kuwata <kwa(at)kuwata-lab.com><br>
|
18
|
-
last update: $Date:
|
18
|
+
last update: $Date: 2006-05-14 13:57:07 +0900 (Sun, 14 May 2006) $<br>
|
19
19
|
</div>
|
20
20
|
|
21
21
|
<a name="preface"></a>
|
@@ -57,16 +57,18 @@ This rule is more aggressive and cost-effective than Pareto Law. The rule is nam
|
|
57
57
|
<p>Kwalify is small and in fact poorer than RelaxNG or XML Schema.
|
58
58
|
I hope you extend/customize Kwalify for your own way.
|
59
59
|
</p>
|
60
|
-
|
61
|
-
<
|
60
|
+
<div class="footnote">
|
61
|
+
<dl compact>
|
62
|
+
<dt>(<a name="fnref:1" href="#fnlink:1">*1</a>)</dt>
|
63
|
+
<dd>Pronounce as 'Qualify'.</dd>
|
64
|
+
</dl>
|
65
|
+
</div>
|
66
|
+
<a name="toc"></a>
|
67
|
+
<h3 class="section2">Table of Contents</h3>
|
62
68
|
<ul>
|
63
69
|
<li><a href="#preface">Preface</a>
|
64
|
-
</li>
|
65
|
-
<li><a href="#usage">Usage of Kwalify</a>
|
66
70
|
<ul>
|
67
|
-
<li><a href="#
|
68
|
-
</li>
|
69
|
-
<li><a href="#usage2">Usage in Ruby Script</a>
|
71
|
+
<li><a href="#toc">Table of Contents</a>
|
70
72
|
</li>
|
71
73
|
</ul>
|
72
74
|
</li>
|
@@ -104,171 +106,29 @@ Table of Contents:
|
|
104
106
|
</li>
|
105
107
|
</ul>
|
106
108
|
</li>
|
109
|
+
<li><a href="#actions">Actions</a>
|
110
|
+
<ul>
|
111
|
+
<li><a href="#action-genclass">Class Definition Generation</a>
|
112
|
+
<ul>
|
113
|
+
<li><a href="#action-genclass-ruby">Ruby Class Definition</a>
|
114
|
+
</li>
|
115
|
+
<li><a href="#action-genclass-java">Java Class Definition</a>
|
116
|
+
</li>
|
117
|
+
</ul>
|
118
|
+
</li>
|
119
|
+
</ul>
|
120
|
+
</li>
|
121
|
+
<li><a href="#usage">Usage of Kwalify</a>
|
122
|
+
<ul>
|
123
|
+
<li><a href="#usage1">Usage in Command-Line</a>
|
124
|
+
</li>
|
125
|
+
<li><a href="#usage-ruby">Usage in Ruby Script</a>
|
126
|
+
</li>
|
127
|
+
<li><a href="#usage-java">Usage in Java Program</a>
|
128
|
+
</li>
|
129
|
+
</ul>
|
130
|
+
</li>
|
107
131
|
</ul>
|
108
|
-
</div>
|
109
|
-
<div class="footnote">
|
110
|
-
<dl compact>
|
111
|
-
<dt>(<a name="fnref:1" href="#fnlink:1">*1</a>)</dt>
|
112
|
-
<dd>Pronounce as 'Qualify'.</dd>
|
113
|
-
</dl>
|
114
|
-
</div>
|
115
|
-
<br>
|
116
|
-
|
117
|
-
|
118
|
-
<a name="usage"></a>
|
119
|
-
<h2 class="section1">Usage of Kwalify</h2>
|
120
|
-
<a name="usage1"></a>
|
121
|
-
<h3 class="section2">Usage in Command-Line</h3>
|
122
|
-
<div class="terminal_caption">
|
123
|
-
usage1: validate YAML document in command-line</div>
|
124
|
-
<pre class="terminal">### kwalify-ruby
|
125
|
-
$ kwalify -f schema.yaml document.yaml [document2.yaml ...]
|
126
|
-
|
127
|
-
### kwalify-java
|
128
|
-
$ java -classpath kwalify.jar kwalify.Main -f schema.yaml document.yaml [document2.yaml ...]
|
129
|
-
</pre>
|
130
|
-
<div class="terminal_caption">
|
131
|
-
usage2: validate schema definition in command-line</div>
|
132
|
-
<pre class="terminal">### kwalify-ruby
|
133
|
-
$ kwalify -m schema.yaml [schema2.yaml ...]
|
134
|
-
|
135
|
-
### kwalify-java
|
136
|
-
$ java -classpath kwalify.jar kwalify.Main -m schema.yaml [schema2.yaml ...]
|
137
|
-
</pre>
|
138
|
-
<p>Command-line options:
|
139
|
-
</p>
|
140
|
-
<dl class="dl3">
|
141
|
-
<dt class="dt3"><strong>
|
142
|
-
<code>-h</code>, <code>--help</code> </strong></dt>
|
143
|
-
<dd class="dd3">
|
144
|
-
Print help message.
|
145
|
-
</dd>
|
146
|
-
<dt class="dt3"><strong>
|
147
|
-
<code>-v</code> </strong></dt>
|
148
|
-
<dd class="dd3">
|
149
|
-
Print version.
|
150
|
-
</dd>
|
151
|
-
<dt class="dt3"><strong>
|
152
|
-
<code>-s</code> </strong></dt>
|
153
|
-
<dd class="dd3">
|
154
|
-
Silent mode.
|
155
|
-
</dd>
|
156
|
-
<dt class="dt3"><strong>
|
157
|
-
<code>-f <em>schema.yaml</em></code> </strong></dt>
|
158
|
-
<dd class="dd3">
|
159
|
-
Specify schema definition file.
|
160
|
-
</dd>
|
161
|
-
<dt class="dt3"><strong>
|
162
|
-
<code>-m</code> </strong></dt>
|
163
|
-
<dd class="dd3">
|
164
|
-
Meta-validation of schema definition.
|
165
|
-
</dd>
|
166
|
-
<dt class="dt3"><strong>
|
167
|
-
<code>-t</code> </strong></dt>
|
168
|
-
<dd class="dd3">
|
169
|
-
Expand tab characters to spaces automatically.
|
170
|
-
</dd>
|
171
|
-
<dt class="dt3"><strong>
|
172
|
-
<code>-l</code> </strong></dt>
|
173
|
-
<dd class="dd3">
|
174
|
-
Show linenumber on which error found.
|
175
|
-
</dd>
|
176
|
-
<dt class="dt3"><strong>
|
177
|
-
<code>-E</code> </strong></dt>
|
178
|
-
<dd class="dd3">
|
179
|
-
Show errors in Emacs-compatible style (implies '-l' option).
|
180
|
-
</dd>
|
181
|
-
</dl>
|
182
|
-
<p>Notice that the command-line option <code>-l</code> is an experimental feature, for kwalify command use original YAML parser instead of Syck parser when this option is specified.
|
183
|
-
</p>
|
184
|
-
<p>If you are an Emacs user, try <code>-E</code> option that show errors in format which Emacs can parse and jump to errors.
|
185
|
-
You can use <code>C-x `</code> (next-error) to jump into errors.
|
186
|
-
</p>
|
187
|
-
<br>
|
188
|
-
|
189
|
-
|
190
|
-
<a name="usage2"></a>
|
191
|
-
<h3 class="section2">Usage in Ruby Script</h3>
|
192
|
-
<p>The followings are example scripts for Ruby.
|
193
|
-
</p>
|
194
|
-
<div class="program_caption">
|
195
|
-
validate YAML document in Ruby script</div>
|
196
|
-
<pre class="program">require 'kwalify'
|
197
|
-
|
198
|
-
## parse schema definition and create validator
|
199
|
-
schema = YAML.load_file('schema.yaml')
|
200
|
-
validator = Kwalify::Validator.new(schema) # raises Kwalify::SchemaError if wrong
|
201
|
-
|
202
|
-
## validate YAML document
|
203
|
-
document = YAML.load_file('document.yaml')
|
204
|
-
error_list = validator.validate(document)
|
205
|
-
unless error_list.empty?
|
206
|
-
error_list.each do |error| # error is instance of Kwalify::ValidationError
|
207
|
-
puts "[#{error.path}] #{error.message}"
|
208
|
-
end
|
209
|
-
end
|
210
|
-
</pre>
|
211
|
-
<div class="program_caption">
|
212
|
-
validate YAML document and show linenumber on where error is found.</div>
|
213
|
-
<pre class="program">require 'kwalify'
|
214
|
-
|
215
|
-
## parse schema definition and create validator
|
216
|
-
schema = YAML.load_file('schema.yaml')
|
217
|
-
validator = Kwalify::Validator.new(schema) # raises Kwalify::SchemaError if wrong
|
218
|
-
|
219
|
-
## parse YAML document with Kwalify's parser
|
220
|
-
str = File.read('document.yaml')
|
221
|
-
parser = Kwalify::Parser.new(str)
|
222
|
-
document = parser.parse()
|
223
|
-
|
224
|
-
## validate document and show errors
|
225
|
-
error_list = validator.validate(document)
|
226
|
-
unless error_list.empty?
|
227
|
-
parser.set_errors_linenum(error_list) # set linenum on error
|
228
|
-
error_list.sort.each do |error|
|
229
|
-
puts "(line %d)[%s] %s" % [error.linenum, error.path, error.message]
|
230
|
-
end
|
231
|
-
end
|
232
|
-
</pre>
|
233
|
-
<p>Kwalify's YAML parser is experimental. You should notice that Kwalify's YAML parser is limited only for basic syntax of YAML.
|
234
|
-
</p>
|
235
|
-
<p>The followings are example programs of Java.
|
236
|
-
</p>
|
237
|
-
<div class="program_caption">
|
238
|
-
validate YAML document and show linenumber on where error is found.</div>
|
239
|
-
<pre class="program">import kwalify.*;
|
240
|
-
|
241
|
-
public class Test {
|
242
|
-
|
243
|
-
public static void main(String[] args) throws Exception {
|
244
|
-
// read schema
|
245
|
-
String schema_str = Util.readFile("schema.yaml");
|
246
|
-
Object schema = new YamlParser(schema_str).parse();
|
247
|
-
|
248
|
-
// read document file
|
249
|
-
String document_str = Util.readFile("document.yaml");
|
250
|
-
YamlParser parser = new YamlParser(document_str);
|
251
|
-
Object document = parser.parse();
|
252
|
-
|
253
|
-
// create validator and validate
|
254
|
-
Validator validator = new Validator(schema);
|
255
|
-
List errors = validator.validate(document);
|
256
|
-
|
257
|
-
// show errors
|
258
|
-
if (errors != null && errors.size() > 0) {
|
259
|
-
parser.setErrorsLineNumber(errors);
|
260
|
-
Collections.sort(errors);
|
261
|
-
for (Iterator it = errors.iterator(); it.hasNext(); ) {
|
262
|
-
ValidationException error = (ValidationException)it.next();
|
263
|
-
int linenum = error.getLineNumber();
|
264
|
-
String path = error.getPath();
|
265
|
-
String mesg = error.getMessage();
|
266
|
-
System.out.println("- " + linenum + ": [" + path + "] " + mesg);
|
267
|
-
}
|
268
|
-
}
|
269
|
-
}
|
270
|
-
}
|
271
|
-
</pre>
|
272
132
|
<br>
|
273
133
|
|
274
134
|
|
@@ -293,6 +153,7 @@ sequence:
|
|
293
153
|
- bar
|
294
154
|
- baz
|
295
155
|
</pre>
|
156
|
+
<a name="valid01.expected"></a>
|
296
157
|
<div class="terminal_caption">
|
297
158
|
validate</div>
|
298
159
|
<pre class="terminal">$ kwalify -lf schema01.yaml document01a.yaml
|
@@ -305,6 +166,7 @@ document01a.yaml#0: valid.
|
|
305
166
|
- 123
|
306
167
|
- baz
|
307
168
|
</pre>
|
169
|
+
<a name="invalid01.expected"></a>
|
308
170
|
<div class="terminal_caption">
|
309
171
|
validate</div>
|
310
172
|
<pre class="terminal">$ kwalify -lf schema01.yaml document01b.yaml
|
@@ -342,6 +204,7 @@ email: foo@mail.com
|
|
342
204
|
age: 20
|
343
205
|
birth: 1985-01-01
|
344
206
|
</pre>
|
207
|
+
<a name="valid02.expected"></a>
|
345
208
|
<div class="terminal_caption">
|
346
209
|
validate</div>
|
347
210
|
<pre class="terminal">$ kwalify -lf schema02.yaml document02a.yaml
|
@@ -355,6 +218,7 @@ email: foo(at)mail.com
|
|
355
218
|
age: twenty
|
356
219
|
birth: Jun 01, 1985
|
357
220
|
</pre>
|
221
|
+
<a name="invalid02.expected"></a>
|
358
222
|
<div class="terminal_caption">
|
359
223
|
validate</div>
|
360
224
|
<pre class="terminal">$ kwalify -lf schema02.yaml document02b.yaml
|
@@ -391,6 +255,7 @@ sequence:
|
|
391
255
|
- name: baz
|
392
256
|
email: baz@mail.org
|
393
257
|
</pre>
|
258
|
+
<a name="valid03.expected"></a>
|
394
259
|
<div class="terminal_caption">
|
395
260
|
validate</div>
|
396
261
|
<pre class="terminal">$ kwalify -lf schema03.yaml document03a.yaml
|
@@ -406,6 +271,7 @@ document03a.yaml#0: valid.
|
|
406
271
|
- name: baz
|
407
272
|
mail: baz@mail.org
|
408
273
|
</pre>
|
274
|
+
<a name="invalid03.expected"></a>
|
409
275
|
<div class="terminal_caption">
|
410
276
|
validate</div>
|
411
277
|
<pre class="terminal">$ kwalify -lf schema03.yaml document03b.yaml
|
@@ -456,6 +322,7 @@ employees:
|
|
456
322
|
name: bar
|
457
323
|
email: bar@kuwata-lab.com
|
458
324
|
</pre>
|
325
|
+
<a name="valid04.expected"></a>
|
459
326
|
<div class="terminal_caption">
|
460
327
|
validate</div>
|
461
328
|
<pre class="terminal">$ kwalify -lf schema04.yaml document04a.yaml
|
@@ -474,6 +341,7 @@ employees:
|
|
474
341
|
name: bar
|
475
342
|
mail: bar@kuwata-lab.com
|
476
343
|
</pre>
|
344
|
+
<a name="invalid04.expected"></a>
|
477
345
|
<div class="terminal_caption">
|
478
346
|
validate</div>
|
479
347
|
<pre class="terminal">$ kwalify -lf schema04.yaml document04b.yaml
|
@@ -585,6 +453,11 @@ document04b.yaml#0: INVALID
|
|
585
453
|
<dd class="dd3">
|
586
454
|
Description. This is not used for validation.
|
587
455
|
</dd>
|
456
|
+
<dt class="dt3"><strong>
|
457
|
+
<code>classname:</code> </strong></dt>
|
458
|
+
<dd class="dd3">
|
459
|
+
Class name. This is available only with type 'map' and used with 'genclass' action. See '<a href="#actions">Actions</a>' section for details.
|
460
|
+
</dd>
|
588
461
|
</dl>
|
589
462
|
<p>Rule contains 'type:' entry. 'sequence:' entry takes a list of rule. 'mapping:' entry takes a hash which values are rules.
|
590
463
|
</p>
|
@@ -637,6 +510,7 @@ sequence:
|
|
637
510
|
blood: AB
|
638
511
|
birth: 1980-01-01
|
639
512
|
</pre>
|
513
|
+
<a name="valid05.expected"></a>
|
640
514
|
<div class="terminal_caption">
|
641
515
|
validate</div>
|
642
516
|
<pre class="terminal">$ kwalify -lf schema05.yaml document05a.yaml
|
@@ -658,6 +532,7 @@ document05a.yaml#0: valid.
|
|
658
532
|
blood: AB
|
659
533
|
birth: 1980/01/01
|
660
534
|
</pre>
|
535
|
+
<a name="invalid05.expected"></a>
|
661
536
|
<div class="terminal_caption">
|
662
537
|
validate</div>
|
663
538
|
<pre class="terminal">$ kwalify -lf schema05.yaml document05b.yaml
|
@@ -722,6 +597,7 @@ sequence:
|
|
722
597
|
groups:
|
723
598
|
- users
|
724
599
|
</pre>
|
600
|
+
<a name="valid06.expected"></a>
|
725
601
|
<div class="terminal_caption">
|
726
602
|
validate</div>
|
727
603
|
<pre class="terminal">$ kwalify -lf schema06.yaml document06a.yaml
|
@@ -747,6 +623,7 @@ document06a.yaml#0: valid.
|
|
747
623
|
groups:
|
748
624
|
- users
|
749
625
|
</pre>
|
626
|
+
<a name="invalid06.expected"></a>
|
750
627
|
<div class="terminal_caption">
|
751
628
|
validate</div>
|
752
629
|
<pre class="terminal">$ kwalify -lf schema06.yaml document06b.yaml
|
@@ -852,6 +729,7 @@ end
|
|
852
729
|
answer: bad
|
853
730
|
reason: I don't like this style.
|
854
731
|
</pre>
|
732
|
+
<a name="valid07.expected"></a>
|
855
733
|
<div class="terminal_caption">
|
856
734
|
validate</div>
|
857
735
|
<pre class="terminal">$ ruby answers-validator.rb document07a.yaml
|
@@ -868,6 +746,7 @@ Valid.
|
|
868
746
|
- name: Baz
|
869
747
|
answer: not bad
|
870
748
|
</pre>
|
749
|
+
<a name="invalid07.expected"></a>
|
871
750
|
<div class="terminal_caption">
|
872
751
|
validate</div>
|
873
752
|
<pre class="terminal">$ ruby answers-validator.rb document07b.yaml
|
@@ -989,6 +868,7 @@ public class AnswersValidator extends Validator {
|
|
989
868
|
}
|
990
869
|
}
|
991
870
|
</pre>
|
871
|
+
<a name="AnswersValidator.expected"></a>
|
992
872
|
<div class="terminal_caption">
|
993
873
|
validate</div>
|
994
874
|
<pre class="terminal">$ java -classpath kwalify.jar AnswersValidator document07a.yaml
|
@@ -1002,7 +882,7 @@ $ java -classpath kwalify.jar AnswersValidator document07b.yaml
|
|
1002
882
|
|
1003
883
|
<a name="schema-block"></a>
|
1004
884
|
<h3 class="section2">Validator with Block</h3>
|
1005
|
-
<p><strong>Notice: This
|
885
|
+
<p><strong>Notice: This feature has been obsolete. Use Kwalify::Validator#validate_hook() method instead.</strong>
|
1006
886
|
</p>
|
1007
887
|
<p><code>Kwalify::Validator.new()</code> method can take a block which is invoked when validation.
|
1008
888
|
</p>
|
@@ -1047,11 +927,13 @@ else
|
|
1047
927
|
end
|
1048
928
|
end
|
1049
929
|
</pre>
|
930
|
+
<a name="valid08.expected"></a>
|
1050
931
|
<div class="terminal_caption">
|
1051
932
|
validate</div>
|
1052
933
|
<pre class="terminal">$ ruby validate08.rb document07a.yaml
|
1053
934
|
Valid.
|
1054
935
|
</pre>
|
936
|
+
<a name="valid08.expected"></a>
|
1055
937
|
<div class="terminal_caption">
|
1056
938
|
validate</div>
|
1057
939
|
<pre class="terminal">$ ruby validate08.rb document07b.yaml
|
@@ -1113,26 +995,15 @@ JSON can be considered as a subset of YAML. It means that YAML parser can parse
|
|
1113
995
|
<pre class="program">{ "type": "map",
|
1114
996
|
"required": true,
|
1115
997
|
"mapping": {
|
1116
|
-
"name":
|
1117
|
-
|
1118
|
-
|
1119
|
-
},
|
1120
|
-
"
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
|
1125
|
-
},
|
1126
|
-
"gender": {
|
1127
|
-
"type": "str",
|
1128
|
-
"enum": ["M", "F"]
|
1129
|
-
},
|
1130
|
-
"favorite": {
|
1131
|
-
"type": "seq",
|
1132
|
-
"sequence": [
|
1133
|
-
{ "type": "str" }
|
1134
|
-
]
|
1135
|
-
}
|
998
|
+
"name": { "type": "str", "required": true },
|
999
|
+
"email": { "type": "str" },
|
1000
|
+
"age": { "type": "int" },
|
1001
|
+
"gender": { "type": "str", "enum": ["M", "F"] },
|
1002
|
+
"favorite": { "type": "seq",
|
1003
|
+
"sequence": [
|
1004
|
+
{ "type": "str" }
|
1005
|
+
]
|
1006
|
+
}
|
1136
1007
|
}
|
1137
1008
|
}
|
1138
1009
|
</pre>
|
@@ -1150,6 +1021,7 @@ JSON can be considered as a subset of YAML. It means that YAML parser can parse
|
|
1150
1021
|
]
|
1151
1022
|
}
|
1152
1023
|
</pre>
|
1024
|
+
<a name="valid12.expected"></a>
|
1153
1025
|
<div class="terminal_caption">
|
1154
1026
|
validate</div>
|
1155
1027
|
<pre class="terminal">$ kwalify -lf schema12.yaml document12a.yaml
|
@@ -1165,6 +1037,7 @@ document12a.yaml#0: valid.
|
|
1165
1037
|
"favorite": [ 123, 456 ]
|
1166
1038
|
}
|
1167
1039
|
</pre>
|
1040
|
+
<a name="invalid12.yaml"></a>
|
1168
1041
|
<div class="terminal_caption">
|
1169
1042
|
validate</div>
|
1170
1043
|
<pre class="terminal">$ kwalify -lf schema12.yaml document12b.yaml
|
@@ -1225,6 +1098,7 @@ sequence:
|
|
1225
1098
|
post: clerk
|
1226
1099
|
supervisor: <strong>*bar</strong>
|
1227
1100
|
</pre>
|
1101
|
+
<a name="valid13.expected"></a>
|
1228
1102
|
<div class="terminal_caption">
|
1229
1103
|
validate</div>
|
1230
1104
|
<pre class="terminal">$ kwalify -lf schema13.yaml document13a.yaml
|
@@ -1268,6 +1142,7 @@ mapping:
|
|
1268
1142
|
value2: 0.5
|
1269
1143
|
value3: -0.9
|
1270
1144
|
</pre>
|
1145
|
+
<a name="valid14.expected"></a>
|
1271
1146
|
<div class="terminal_caption">
|
1272
1147
|
validate</div>
|
1273
1148
|
<pre class="terminal">$ kwalify -lf schema14.yaml document14a.yaml
|
@@ -1280,6 +1155,7 @@ document14a.yaml#0: valid.
|
|
1280
1155
|
value2: 1.1
|
1281
1156
|
value3: -2.0
|
1282
1157
|
</pre>
|
1158
|
+
<a name="invalid14.expected"></a>
|
1283
1159
|
<div class="terminal_caption">
|
1284
1160
|
validate</div>
|
1285
1161
|
<pre class="terminal">$ kwalify -lf schema14.yaml document14b.yaml
|
@@ -1346,6 +1222,7 @@ user:
|
|
1346
1222
|
name: bar
|
1347
1223
|
email: bar@mail.com
|
1348
1224
|
</pre>
|
1225
|
+
<a name="valid15.expected"></a>
|
1349
1226
|
<div class="terminal_caption">
|
1350
1227
|
validate</div>
|
1351
1228
|
<pre class="terminal">$ kwalify -lf schema15.yaml document15a.yaml
|
@@ -1360,6 +1237,7 @@ document15a.yaml#0: valid.
|
|
1360
1237
|
user:
|
1361
1238
|
name: toooooo-looooong-name
|
1362
1239
|
</pre>
|
1240
|
+
<a name="invalid15.expected"></a>
|
1363
1241
|
<div class="terminal_caption">
|
1364
1242
|
validate</div>
|
1365
1243
|
<pre class="terminal">$ kwalify -lf schema15.yaml document15b.yaml
|
@@ -1370,6 +1248,628 @@ document15b.yaml#0: INVALID
|
|
1370
1248
|
<br>
|
1371
1249
|
|
1372
1250
|
|
1251
|
+
<br>
|
1252
|
+
|
1253
|
+
|
1254
|
+
<a name="actions"></a>
|
1255
|
+
<h2 class="section1">Actions</h2>
|
1256
|
+
<p>Kwalify has the command-line '-a <em>action</em>' which perform a certain action to schema definition.
|
1257
|
+
Currently only the following action is supported.
|
1258
|
+
</p>
|
1259
|
+
<dl class="dl3">
|
1260
|
+
<dt class="dt3"><strong>
|
1261
|
+
genclass-ruby </strong></dt>
|
1262
|
+
<dd class="dd3">
|
1263
|
+
Generate class definitions in Ruby.
|
1264
|
+
</dd>
|
1265
|
+
<dt class="dt3"><strong>
|
1266
|
+
genclass-java </strong></dt>
|
1267
|
+
<dd class="dd3">
|
1268
|
+
Generate class definitions in Java.
|
1269
|
+
</dd>
|
1270
|
+
</dl>
|
1271
|
+
<p>In fact action name represents template filename.
|
1272
|
+
For example, action 'genclass-ruby' invokes template file 'kwalify/templates/genclass-ruby.eruby'.
|
1273
|
+
</p>
|
1274
|
+
<p>Each action can accept some command-line properties.
|
1275
|
+
For example, action 'genclass-ruby' can accept the command-line properties '--module=<em>name</em>', '--parent=<em>name</em>', and so on.
|
1276
|
+
Type 'kwalify -h -a <em>action</em>' to show the list of command-line properties the action can accept.
|
1277
|
+
</p>
|
1278
|
+
<a name="action-genclass"></a>
|
1279
|
+
<h3 class="section2">Class Definition Generation</h3>
|
1280
|
+
<p>Command-line option '-a genclass-ruby' or '-a genclass-java' generates class definition
|
1281
|
+
automatically from schema definition in Ruby or Java.
|
1282
|
+
</p>
|
1283
|
+
<p>Assume the following data file and schema definition.
|
1284
|
+
</p>
|
1285
|
+
<a name="address-book.yaml"></a>
|
1286
|
+
<div class="program_caption">
|
1287
|
+
<code>address-book.yaml</code> : data file</div>
|
1288
|
+
<pre class="program">groups:
|
1289
|
+
|
1290
|
+
- name: family
|
1291
|
+
desc: my family
|
1292
|
+
|
1293
|
+
- name: friend
|
1294
|
+
desc: my friends
|
1295
|
+
|
1296
|
+
- name: business
|
1297
|
+
desc: those who works together
|
1298
|
+
|
1299
|
+
persons:
|
1300
|
+
|
1301
|
+
- name: Sumire
|
1302
|
+
group: family
|
1303
|
+
birth: 2000-01-01
|
1304
|
+
blood: A
|
1305
|
+
|
1306
|
+
- name: Shiina
|
1307
|
+
group: friend
|
1308
|
+
birth: 1995-01-01
|
1309
|
+
email: shiina@mail.org
|
1310
|
+
|
1311
|
+
- name: Sakura
|
1312
|
+
group: business
|
1313
|
+
email: cherry@mail.net
|
1314
|
+
phone: 012-345-6789
|
1315
|
+
</pre>
|
1316
|
+
<a name="address-book.schema.yaml"></a>
|
1317
|
+
<div class="program_caption">
|
1318
|
+
<code>address-book.schema.yaml</code> : schema definition file</div>
|
1319
|
+
<pre class="program">type: map
|
1320
|
+
<strong>classname: AddressBook</strong>
|
1321
|
+
desc: address-book class
|
1322
|
+
mapping:
|
1323
|
+
"groups":
|
1324
|
+
type: seq
|
1325
|
+
sequence:
|
1326
|
+
- type: map
|
1327
|
+
<strong>classname: Group</strong>
|
1328
|
+
desc: group class
|
1329
|
+
mapping:
|
1330
|
+
"name": { type: str, required: yes }
|
1331
|
+
"desc": { type: str }
|
1332
|
+
"persons":
|
1333
|
+
type: seq
|
1334
|
+
sequence:
|
1335
|
+
- type: map
|
1336
|
+
<strong>classname: Person</strong>
|
1337
|
+
desc: person class
|
1338
|
+
mapping:
|
1339
|
+
"name": { type: str, required: yes }
|
1340
|
+
"desc": { type: str }
|
1341
|
+
"group": { type: str }
|
1342
|
+
"email": { type: str, pattern: '/@/' }
|
1343
|
+
"phone": { type: str }
|
1344
|
+
"birth": { type: date }
|
1345
|
+
"blood": { type: str, enum: [A, B, O, AB] }
|
1346
|
+
</pre>
|
1347
|
+
<a name="action-genclass-ruby"></a>
|
1348
|
+
<h4 class="section3">Ruby Class Definition</h4>
|
1349
|
+
<div class="terminal_caption">
|
1350
|
+
generate class definition</div>
|
1351
|
+
<pre class="terminal">$ kwalify <strong>-a genclass-ruby</strong> -tf address-book.schema.yaml > address-book.rb
|
1352
|
+
</pre>
|
1353
|
+
<a name="address-book.rb"></a>
|
1354
|
+
<div class="program_caption">
|
1355
|
+
<code>address-book.rb</code> : generated class definition</div>
|
1356
|
+
<pre class="program">## address-book class
|
1357
|
+
class AddressBook
|
1358
|
+
def initialize(hash)
|
1359
|
+
@persons = (v=hash['persons']) ? v.map!{|e| e.is_a?(Person) ? e : Person.new(e)} : v
|
1360
|
+
@groups = (v=hash['groups']) ? v.map!{|e| e.is_a?(Group) ? e : Group.new(e)} : v
|
1361
|
+
end
|
1362
|
+
attr_accessor :persons # seq
|
1363
|
+
attr_accessor :groups # seq
|
1364
|
+
end
|
1365
|
+
|
1366
|
+
## person class
|
1367
|
+
class Person
|
1368
|
+
def initialize(hash)
|
1369
|
+
@name = hash['name']
|
1370
|
+
@desc = hash['desc']
|
1371
|
+
@phone = hash['phone']
|
1372
|
+
@blood = hash['blood']
|
1373
|
+
@group = hash['group']
|
1374
|
+
@birth = hash['birth']
|
1375
|
+
@email = hash['email']
|
1376
|
+
end
|
1377
|
+
attr_accessor :name # str
|
1378
|
+
attr_accessor :desc # str
|
1379
|
+
attr_accessor :phone # str
|
1380
|
+
attr_accessor :blood # str
|
1381
|
+
attr_accessor :group # str
|
1382
|
+
attr_accessor :birth # date
|
1383
|
+
attr_accessor :email # str
|
1384
|
+
end
|
1385
|
+
|
1386
|
+
## group class
|
1387
|
+
class Group
|
1388
|
+
def initialize(hash)
|
1389
|
+
@name = hash['name']
|
1390
|
+
@desc = hash['desc']
|
1391
|
+
end
|
1392
|
+
attr_accessor :name # str
|
1393
|
+
attr_accessor :desc # str
|
1394
|
+
end
|
1395
|
+
|
1396
|
+
</pre>
|
1397
|
+
<a name="example-address-book.rb"></a>
|
1398
|
+
<div class="program_caption">
|
1399
|
+
<code>example-address-book.rb</code> : example code of using address-book.rb</div>
|
1400
|
+
<pre class="program">require 'address-book'
|
1401
|
+
require 'yaml'
|
1402
|
+
require 'pp'
|
1403
|
+
|
1404
|
+
str = File.read('address-book.yaml')
|
1405
|
+
ydoc = YAML.load(str)
|
1406
|
+
<strong>addrbook = AddressBook.new(ydoc)</strong>
|
1407
|
+
|
1408
|
+
pp <strong>addrbook.groups</strong>
|
1409
|
+
pp <strong>addrbook.persons</strong>
|
1410
|
+
</pre>
|
1411
|
+
<a name="example-address-book.expected"></a>
|
1412
|
+
<div class="terminal_caption">
|
1413
|
+
result</div>
|
1414
|
+
<pre class="terminal">$ ruby example-address-book.rb
|
1415
|
+
[#<Group:0xddf24 @desc="my family", @name="family">,
|
1416
|
+
#<Group:0xddf10 @desc="my friends", @name="friend">,
|
1417
|
+
#<Group:0xdde84 @desc="those who works together", @name="business">]
|
1418
|
+
[#<Person:0xdefdc
|
1419
|
+
@birth=#<Date: 4903089/2,0,2299161>,
|
1420
|
+
@blood="A",
|
1421
|
+
@desc=nil,
|
1422
|
+
@email=nil,
|
1423
|
+
@group="family",
|
1424
|
+
@name="Sumire",
|
1425
|
+
@phone=nil>,
|
1426
|
+
#<Person:0xdee9c
|
1427
|
+
@birth=#<Date: 4899437/2,0,2299161>,
|
1428
|
+
@blood=nil,
|
1429
|
+
@desc=nil,
|
1430
|
+
@email="shiina@mail.org",
|
1431
|
+
@group="friend",
|
1432
|
+
@name="Shiina",
|
1433
|
+
@phone=nil>,
|
1434
|
+
#<Person:0xde8e8
|
1435
|
+
@birth=nil,
|
1436
|
+
@blood=nil,
|
1437
|
+
@desc=nil,
|
1438
|
+
@email="cherry@mail.net",
|
1439
|
+
@group="business",
|
1440
|
+
@name="Sakura",
|
1441
|
+
@phone="012-345-6789">]
|
1442
|
+
</pre>
|
1443
|
+
<p>Command-line option '<code>-h -a genclass-ruby</code>' shows the commpand-line properties that template can accept.
|
1444
|
+
</p>
|
1445
|
+
<a name="option_ha.expected"></a>
|
1446
|
+
<div class="terminal_caption">
|
1447
|
+
show command-line properties</div>
|
1448
|
+
<pre class="terminal">$ kwalify -ha genclass-ruby
|
1449
|
+
--module=name : module name in which class defined
|
1450
|
+
--parent=name : parent class name
|
1451
|
+
--include=name : module name which all classes include
|
1452
|
+
</pre>
|
1453
|
+
<div class="terminal_caption">
|
1454
|
+
example of command-line properties</div>
|
1455
|
+
<pre class="terminal">$ kwalify -a genclass-ruby --module=My --include=Kwalify::HashInterface
|
1456
|
+
</pre>
|
1457
|
+
<p>'Kwalify::HashInterface' is a module which make object accessible like as Hash.
|
1458
|
+
It is defined in 'kwalify/util/hash-interface.rb'
|
1459
|
+
</p>
|
1460
|
+
<br>
|
1461
|
+
|
1462
|
+
<a name="action-genclass-java"></a>
|
1463
|
+
<h4 class="section3">Java Class Definition</h4>
|
1464
|
+
<a name="genclass_java.expected"></a>
|
1465
|
+
<div class="terminal_caption">
|
1466
|
+
generate java class definition</div>
|
1467
|
+
<pre class="terminal">$ kwalify <strong>-a genclass-java</strong> -tf address-book.schema.yaml
|
1468
|
+
generating ./AddressBook.java...done.
|
1469
|
+
generating ./Person.java...done.
|
1470
|
+
generating ./Group.java...done.
|
1471
|
+
</pre>
|
1472
|
+
<a name="AddressBook.java.expected"></a>
|
1473
|
+
<div class="program_caption">
|
1474
|
+
<code>AddressBook.java</code> : generated class definition</div>
|
1475
|
+
<pre class="program">// generated by kwalify from address-book.schema.yaml
|
1476
|
+
|
1477
|
+
import java.util.*;
|
1478
|
+
|
1479
|
+
/**
|
1480
|
+
* address-book class
|
1481
|
+
*/
|
1482
|
+
public class AddressBook {
|
1483
|
+
|
1484
|
+
private List _persons;
|
1485
|
+
private List _groups;
|
1486
|
+
|
1487
|
+
public AddressBook() {}
|
1488
|
+
|
1489
|
+
public AddressBook(Map map) {
|
1490
|
+
List seq;
|
1491
|
+
Object obj;
|
1492
|
+
if ((seq = (List)map.get("persons")) != null) {
|
1493
|
+
for (int i = 0; i < seq.size(); i++) {
|
1494
|
+
if ((obj = seq.get(i)) instanceof Map) {
|
1495
|
+
seq.set(i, new Person((Map)obj));
|
1496
|
+
}
|
1497
|
+
}
|
1498
|
+
}
|
1499
|
+
_persons = seq;
|
1500
|
+
if ((seq = (List)map.get("groups")) != null) {
|
1501
|
+
for (int i = 0; i < seq.size(); i++) {
|
1502
|
+
if ((obj = seq.get(i)) instanceof Map) {
|
1503
|
+
seq.set(i, new Group((Map)obj));
|
1504
|
+
}
|
1505
|
+
}
|
1506
|
+
}
|
1507
|
+
_groups = seq;
|
1508
|
+
}
|
1509
|
+
|
1510
|
+
public List getPersons() { return _persons; }
|
1511
|
+
public void setPersons(List persons_) { _persons = persons_; }
|
1512
|
+
public List getGroups() { return _groups; }
|
1513
|
+
public void setGroups(List groups_) { _groups = groups_; }
|
1514
|
+
}
|
1515
|
+
</pre>
|
1516
|
+
<a name="Group.java.expected"></a>
|
1517
|
+
<div class="program_caption">
|
1518
|
+
<code>Group.java</code> : generated class definition</div>
|
1519
|
+
<pre class="program">// generated by kwalify from address-book.schema.yaml
|
1520
|
+
|
1521
|
+
import java.util.*;
|
1522
|
+
|
1523
|
+
/**
|
1524
|
+
* group class
|
1525
|
+
*/
|
1526
|
+
public class Group {
|
1527
|
+
|
1528
|
+
private String _name;
|
1529
|
+
private String _desc;
|
1530
|
+
|
1531
|
+
public Group() {}
|
1532
|
+
|
1533
|
+
public Group(Map map) {
|
1534
|
+
_name = (String)map.get("name");
|
1535
|
+
_desc = (String)map.get("desc");
|
1536
|
+
}
|
1537
|
+
|
1538
|
+
public String getName() { return _name; }
|
1539
|
+
public void setName(String name_) { _name = name_; }
|
1540
|
+
public String getDesc() { return _desc; }
|
1541
|
+
public void setDesc(String desc_) { _desc = desc_; }
|
1542
|
+
}
|
1543
|
+
</pre>
|
1544
|
+
<a name="Person.java.expected"></a>
|
1545
|
+
<div class="program_caption">
|
1546
|
+
<code>Person.java</code> : generated class definition</div>
|
1547
|
+
<pre class="program">// generated by kwalify from address-book.schema.yaml
|
1548
|
+
|
1549
|
+
import java.util.*;
|
1550
|
+
|
1551
|
+
/**
|
1552
|
+
* person class
|
1553
|
+
*/
|
1554
|
+
public class Person {
|
1555
|
+
|
1556
|
+
private String _name;
|
1557
|
+
private String _desc;
|
1558
|
+
private String _phone;
|
1559
|
+
private String _blood;
|
1560
|
+
private String _group;
|
1561
|
+
private Date _birth;
|
1562
|
+
private String _email;
|
1563
|
+
|
1564
|
+
public Person() {}
|
1565
|
+
|
1566
|
+
public Person(Map map) {
|
1567
|
+
_name = (String)map.get("name");
|
1568
|
+
_desc = (String)map.get("desc");
|
1569
|
+
_phone = (String)map.get("phone");
|
1570
|
+
_blood = (String)map.get("blood");
|
1571
|
+
_group = (String)map.get("group");
|
1572
|
+
_birth = (Date)map.get("birth");
|
1573
|
+
_email = (String)map.get("email");
|
1574
|
+
}
|
1575
|
+
|
1576
|
+
public String getName() { return _name; }
|
1577
|
+
public void setName(String name_) { _name = name_; }
|
1578
|
+
public String getDesc() { return _desc; }
|
1579
|
+
public void setDesc(String desc_) { _desc = desc_; }
|
1580
|
+
public String getPhone() { return _phone; }
|
1581
|
+
public void setPhone(String phone_) { _phone = phone_; }
|
1582
|
+
public String getBlood() { return _blood; }
|
1583
|
+
public void setBlood(String blood_) { _blood = blood_; }
|
1584
|
+
public String getGroup() { return _group; }
|
1585
|
+
public void setGroup(String group_) { _group = group_; }
|
1586
|
+
public Date getBirth() { return _birth; }
|
1587
|
+
public void setBirth(Date birth_) { _birth = birth_; }
|
1588
|
+
public String getEmail() { return _email; }
|
1589
|
+
public void setEmail(String email_) { _email = email_; }
|
1590
|
+
}
|
1591
|
+
</pre>
|
1592
|
+
<a name="ExampleAddressBook.java"></a>
|
1593
|
+
<div class="program_caption">
|
1594
|
+
<code>ExampleAddressBook.java</code> : example code of using *.java</div>
|
1595
|
+
<pre class="program">import java.util.*;
|
1596
|
+
import kwalify.*;
|
1597
|
+
|
1598
|
+
public class ExampleAddressBook {
|
1599
|
+
public static void main(String args[]) throws Exception {
|
1600
|
+
// read schema
|
1601
|
+
String schema_str = Util.readFile("address-book.schema.yaml");
|
1602
|
+
schema_str = Util.untabify(schema_str);
|
1603
|
+
Object schema = new YamlParser(schema_str).parse();
|
1604
|
+
|
1605
|
+
// read document file
|
1606
|
+
String document_str = Util.readFile("address-book.yaml");
|
1607
|
+
document_str = Util.untabify(document_str);
|
1608
|
+
YamlParser parser = new YamlParser(document_str);
|
1609
|
+
Object document = parser.parse();
|
1610
|
+
|
1611
|
+
// create address book object
|
1612
|
+
AddressBook addrbook = new AddressBook((Map)document);
|
1613
|
+
|
1614
|
+
// show groups
|
1615
|
+
List groups = addrbook.getGroups();
|
1616
|
+
if (groups != null) {
|
1617
|
+
for (Iterator it = groups.iterator(); it.hasNext(); ) {
|
1618
|
+
Group group = (Group)it.next();
|
1619
|
+
System.out.println("group name: " + group.getName());
|
1620
|
+
System.out.println("group desc: " + group.getDesc());
|
1621
|
+
System.out.println();
|
1622
|
+
}
|
1623
|
+
}
|
1624
|
+
|
1625
|
+
// show persons
|
1626
|
+
List persons = addrbook.getPersons();
|
1627
|
+
if (persons != null) {
|
1628
|
+
for (Iterator it = persons.iterator(); it.hasNext(); ) {
|
1629
|
+
Person person = (Person)it.next();
|
1630
|
+
System.out.println("person name: " + person.getName());
|
1631
|
+
System.out.println("person group: " + person.getGroup());
|
1632
|
+
System.out.println("person email: " + person.getEmail());
|
1633
|
+
System.out.println("person phone: " + person.getPhone());
|
1634
|
+
System.out.println("person blood: " + person.getBlood());
|
1635
|
+
System.out.println("person birth: " + person.getBirth());
|
1636
|
+
System.out.println();
|
1637
|
+
}
|
1638
|
+
}
|
1639
|
+
}
|
1640
|
+
|
1641
|
+
}
|
1642
|
+
</pre>
|
1643
|
+
<a name="example-address-book-java.expected"></a>
|
1644
|
+
<div class="terminal_caption">
|
1645
|
+
result</div>
|
1646
|
+
<pre class="terminal">$ javac -classpath '.:kwalify.jar' *.java
|
1647
|
+
$ java -classpath '.:kwalify.jar' ExampleAddressBook
|
1648
|
+
group name: family
|
1649
|
+
group desc: my family
|
1650
|
+
|
1651
|
+
group name: friend
|
1652
|
+
group desc: my friends
|
1653
|
+
|
1654
|
+
group name: business
|
1655
|
+
group desc: those who works together
|
1656
|
+
|
1657
|
+
person name: Sumire
|
1658
|
+
person group: family
|
1659
|
+
person email: null
|
1660
|
+
person phone: null
|
1661
|
+
person blood: A
|
1662
|
+
person birth: Tue Feb 01 00:00:00 JST 2000
|
1663
|
+
|
1664
|
+
person name: Shiina
|
1665
|
+
person group: friend
|
1666
|
+
person email: shiina@mail.org
|
1667
|
+
person phone: null
|
1668
|
+
person blood: null
|
1669
|
+
person birth: Wed Feb 01 00:00:00 JST 1995
|
1670
|
+
|
1671
|
+
person name: Sakura
|
1672
|
+
person group: business
|
1673
|
+
person email: cherry@mail.net
|
1674
|
+
person phone: 012-345-6789
|
1675
|
+
person blood: null
|
1676
|
+
person birth: null
|
1677
|
+
|
1678
|
+
</pre>
|
1679
|
+
<p>Command-line option '<code>-h -a genclass-java</code>' shows the commpand-line properties that template can accept.
|
1680
|
+
</p>
|
1681
|
+
<a name="option_ha_genclass_java.expected"></a>
|
1682
|
+
<div class="terminal_caption">
|
1683
|
+
show command-line properties</div>
|
1684
|
+
<pre class="terminal">$ kwalify -ha genclass-java
|
1685
|
+
--package=name : package name
|
1686
|
+
--extends=name : class name to extend
|
1687
|
+
--implements=name,... : interface names to implement
|
1688
|
+
--dir=path : directory to locate output file
|
1689
|
+
--basedir=path : base directory to locate output file
|
1690
|
+
</pre>
|
1691
|
+
<div class="terminal_caption">
|
1692
|
+
example of command-line properties</div>
|
1693
|
+
<pre class="terminal">$ kwalify -a genclass-java --package=com.example.my --implements=Serializable --basedir=src
|
1694
|
+
</pre>
|
1695
|
+
<br>
|
1696
|
+
|
1697
|
+
<br>
|
1698
|
+
|
1699
|
+
|
1700
|
+
<br>
|
1701
|
+
|
1702
|
+
|
1703
|
+
<a name="usage"></a>
|
1704
|
+
<h2 class="section1">Usage of Kwalify</h2>
|
1705
|
+
<a name="usage1"></a>
|
1706
|
+
<h3 class="section2">Usage in Command-Line</h3>
|
1707
|
+
<div class="terminal_caption">
|
1708
|
+
usage1: validate YAML document in command-line</div>
|
1709
|
+
<pre class="terminal">### kwalify-ruby
|
1710
|
+
$ kwalify -f schema.yaml document.yaml [document2.yaml ...]
|
1711
|
+
|
1712
|
+
### kwalify-java
|
1713
|
+
$ java -classpath kwalify.jar kwalify.Main -f schema.yaml document.yaml [document2.yaml ...]
|
1714
|
+
</pre>
|
1715
|
+
<div class="terminal_caption">
|
1716
|
+
usage2: validate schema definition in command-line</div>
|
1717
|
+
<pre class="terminal">### kwalify-ruby
|
1718
|
+
$ kwalify -m schema.yaml [schema2.yaml ...]
|
1719
|
+
|
1720
|
+
### kwalify-java
|
1721
|
+
$ java -classpath kwalify.jar kwalify.Main -m schema.yaml [schema2.yaml ...]
|
1722
|
+
</pre>
|
1723
|
+
<p>Command-line options:
|
1724
|
+
</p>
|
1725
|
+
<dl class="dl3">
|
1726
|
+
<dt class="dt3"><strong>
|
1727
|
+
<code>-h</code>, <code>--help</code> </strong></dt>
|
1728
|
+
<dd class="dd3">
|
1729
|
+
Print help message.
|
1730
|
+
</dd>
|
1731
|
+
<dt class="dt3"><strong>
|
1732
|
+
<code>-v</code> </strong></dt>
|
1733
|
+
<dd class="dd3">
|
1734
|
+
Print version.
|
1735
|
+
</dd>
|
1736
|
+
<dt class="dt3"><strong>
|
1737
|
+
<code>-s</code> </strong></dt>
|
1738
|
+
<dd class="dd3">
|
1739
|
+
Silent mode.
|
1740
|
+
</dd>
|
1741
|
+
<dt class="dt3"><strong>
|
1742
|
+
<code>-f <em>schema.yaml</em></code> </strong></dt>
|
1743
|
+
<dd class="dd3">
|
1744
|
+
Specify schema definition file.
|
1745
|
+
</dd>
|
1746
|
+
<dt class="dt3"><strong>
|
1747
|
+
<code>-m</code> </strong></dt>
|
1748
|
+
<dd class="dd3">
|
1749
|
+
Meta-validation of schema definition.
|
1750
|
+
</dd>
|
1751
|
+
<dt class="dt3"><strong>
|
1752
|
+
<code>-t</code> </strong></dt>
|
1753
|
+
<dd class="dd3">
|
1754
|
+
Expand tab characters to spaces automatically.
|
1755
|
+
</dd>
|
1756
|
+
<dt class="dt3"><strong>
|
1757
|
+
<code>-l</code> </strong></dt>
|
1758
|
+
<dd class="dd3">
|
1759
|
+
Show linenumber on which error found.
|
1760
|
+
</dd>
|
1761
|
+
<dt class="dt3"><strong>
|
1762
|
+
<code>-E</code> </strong></dt>
|
1763
|
+
<dd class="dd3">
|
1764
|
+
Show errors in Emacs-compatible style (implies '-l' option).
|
1765
|
+
</dd>
|
1766
|
+
<dt class="dt3"><strong>
|
1767
|
+
<code>-a action</code> </strong></dt>
|
1768
|
+
<dd class="dd3">
|
1769
|
+
Do action. Currently supported action is 'genclass-ruby' and 'genclass-java'.
|
1770
|
+
Try '-ha action' to get help about the action.
|
1771
|
+
</dd>
|
1772
|
+
</dl>
|
1773
|
+
<p>Notice that the command-line option <code>-l</code> is an experimental feature, for kwalify command use original YAML parser instead of Syck parser when this option is specified.
|
1774
|
+
</p>
|
1775
|
+
<p>If you are an Emacs user, try <code>-E</code> option that show errors in format which Emacs can parse and jump to errors.
|
1776
|
+
You can use <code>C-x `</code> (next-error) to jump into errors.
|
1777
|
+
</p>
|
1778
|
+
<br>
|
1779
|
+
|
1780
|
+
|
1781
|
+
<a name="usage-ruby"></a>
|
1782
|
+
<h3 class="section2">Usage in Ruby Script</h3>
|
1783
|
+
<p>The followings are example scripts for Ruby.
|
1784
|
+
</p>
|
1785
|
+
<div class="program_caption">
|
1786
|
+
validate YAML document in Ruby script</div>
|
1787
|
+
<pre class="program">require 'kwalify'
|
1788
|
+
|
1789
|
+
## parse schema definition and create validator
|
1790
|
+
schema = YAML.load_file('schema.yaml')
|
1791
|
+
validator = Kwalify::Validator.new(schema) # raises Kwalify::SchemaError if wrong
|
1792
|
+
|
1793
|
+
## validate YAML document
|
1794
|
+
document = YAML.load_file('document.yaml')
|
1795
|
+
error_list = validator.validate(document)
|
1796
|
+
unless error_list.empty?
|
1797
|
+
error_list.each do |error| # error is instance of Kwalify::ValidationError
|
1798
|
+
puts "[#{error.path}] #{error.message}"
|
1799
|
+
end
|
1800
|
+
end
|
1801
|
+
</pre>
|
1802
|
+
<div class="program_caption">
|
1803
|
+
validate YAML document and show linenumber on where error is found.</div>
|
1804
|
+
<pre class="program">require 'kwalify'
|
1805
|
+
|
1806
|
+
## parse schema definition and create validator
|
1807
|
+
schema = YAML.load_file('schema.yaml')
|
1808
|
+
validator = Kwalify::Validator.new(schema) # raises Kwalify::SchemaError if wrong
|
1809
|
+
|
1810
|
+
## parse YAML document with Kwalify's parser
|
1811
|
+
str = File.read('document.yaml')
|
1812
|
+
parser = Kwalify::Parser.new(str)
|
1813
|
+
document = parser.parse()
|
1814
|
+
|
1815
|
+
## validate document and show errors
|
1816
|
+
error_list = validator.validate(document)
|
1817
|
+
unless error_list.empty?
|
1818
|
+
parser.set_errors_linenum(error_list) # set linenum on error
|
1819
|
+
error_list.sort.each do |error|
|
1820
|
+
puts "(line %d)[%s] %s" % [error.linenum, error.path, error.message]
|
1821
|
+
end
|
1822
|
+
end
|
1823
|
+
</pre>
|
1824
|
+
<p>Kwalify's YAML parser is experimental. You should notice that Kwalify's YAML parser is limited only for basic syntax of YAML.
|
1825
|
+
</p>
|
1826
|
+
<br>
|
1827
|
+
|
1828
|
+
|
1829
|
+
<a name="usage-java"></a>
|
1830
|
+
<h3 class="section2">Usage in Java Program</h3>
|
1831
|
+
<p>The followings are example programs of Java.
|
1832
|
+
</p>
|
1833
|
+
<div class="program_caption">
|
1834
|
+
validate YAML document and show linenumber on where error is found.</div>
|
1835
|
+
<pre class="program">import kwalify.*;
|
1836
|
+
|
1837
|
+
public class Test {
|
1838
|
+
|
1839
|
+
public static void main(String[] args) throws Exception {
|
1840
|
+
// read schema
|
1841
|
+
String schema_str = Util.readFile("schema.yaml");
|
1842
|
+
schema_str = Util.untabify(schema_str);
|
1843
|
+
Object schema = new YamlParser(schema_str).parse();
|
1844
|
+
|
1845
|
+
// read document file
|
1846
|
+
String document_str = Util.readFile("document.yaml");
|
1847
|
+
document_str = Util.untabify(document_str);
|
1848
|
+
YamlParser parser = new YamlParser(document_str);
|
1849
|
+
Object document = parser.parse();
|
1850
|
+
|
1851
|
+
// create validator and validate
|
1852
|
+
Validator validator = new Validator(schema);
|
1853
|
+
List errors = validator.validate(document);
|
1854
|
+
|
1855
|
+
// show errors
|
1856
|
+
if (errors != null && errors.size() > 0) {
|
1857
|
+
parser.setErrorsLineNumber(errors);
|
1858
|
+
Collections.sort(errors);
|
1859
|
+
for (Iterator it = errors.iterator(); it.hasNext(); ) {
|
1860
|
+
ValidationException error = (ValidationException)it.next();
|
1861
|
+
int linenum = error.getLineNumber();
|
1862
|
+
String path = error.getPath();
|
1863
|
+
String mesg = error.getMessage();
|
1864
|
+
System.out.println("- " + linenum + ": [" + path + "] " + mesg);
|
1865
|
+
}
|
1866
|
+
}
|
1867
|
+
}
|
1868
|
+
}
|
1869
|
+
</pre>
|
1870
|
+
<br>
|
1871
|
+
|
1872
|
+
|
1373
1873
|
<br>
|
1374
1874
|
|
1375
1875
|
|