/
customconstraints.po
1199 lines (1128 loc) · 39.4 KB
/
customconstraints.po
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Language zh-CN translations for PACKAGE package.
# Automatically generated, 2010.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: http://bugs.kde.org\n"
"POT-Creation-Date: 2011-11-14 10:37+0000\n"
"PO-Revision-Date: 2010-08-21 02:16+0830\n"
"Last-Translator: Strong Liu <stliu@hibernate.org>\n"
"Language-Team: none\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#. Tag: title
#: customconstraints.xml:24
#, no-c-format
msgid "Creating custom constraints"
msgstr "创建自己的约束规则"
#. Tag: para
#: customconstraints.xml:26
#, no-c-format
msgid ""
"Though the Bean Validation API defines a whole set of standard constraint "
"annotations one can easily think of situations in which these standard "
"annotations won't suffice. For these cases you are able to create custom "
"constraints tailored to your specific validation requirements in a simple "
"manner."
msgstr ""
"尽管Bean Validation API定义了一大堆标准的约束条件, 但是肯定还是有这些约束不能"
"满足我们需求的时候, 在这种情况下, 你可以根据你的特定的校验需求来创建自己的约"
"束条件."
#. Tag: title
#: customconstraints.xml:33
#, no-c-format
msgid "Creating a simple constraint"
msgstr "创建一个简单的约束条件"
#. Tag: para
#: customconstraints.xml:35
#, no-c-format
msgid "To create a custom constraint, the following three steps are required:"
msgstr "按照以下三个步骤来创建一个自定义的约束条件"
#. Tag: para
#: customconstraints.xml:40
#, no-c-format
msgid "Create a constraint annotation"
msgstr "创建约束标注"
#. Tag: para
#: customconstraints.xml:44
#, no-c-format
msgid "Implement a validator"
msgstr "实现一个验证器"
#. Tag: para
#: customconstraints.xml:48
#, no-c-format
msgid "Define a default error message"
msgstr "定义默认的验证错误信息"
#. Tag: title
#: customconstraints.xml:53
#, no-c-format
msgid "The constraint annotation"
msgstr "约束标注"
#. Tag: para
#: customconstraints.xml:55
#, no-c-format
msgid ""
"Let's write a constraint annotation, that can be used to express that a "
"given string shall either be upper case or lower case. We'll apply it later "
"on to the <property>licensePlate</property> field of the <classname>Car</"
"classname> class from <xref linkend=\"validator-gettingstarted\"/> to "
"ensure, that the field is always an upper-case string."
msgstr ""
"让我们来创建一个新的用来判断一个给定字符串是否全是大写或者小写字符的约束标"
"注. 我们将稍后把它用在<xref linkend=\"validator-gettingstarted\"/>中的类"
"<classname>Car</classname>的<property>licensePlate</property>字段上来确保这个"
"字段的内容一直都是大写字母."
#. Tag: para
#: customconstraints.xml:62
#, no-c-format
msgid ""
"First we need a way to express the two case modes. We might use "
"<classname>String</classname> constants, but a better way to go is to use a "
"Java 5 enum for that purpose:"
msgstr ""
"首先,我们需要一种方法来表示这两种模式( 译注: 大写或小写), 我们可以使用"
"<classname>String</classname>常量, 但是在Java 5中, 枚举类型是个更好的选择:"
#. Tag: title
#: customconstraints.xml:67
#, no-c-format
msgid "Enum <classname>CaseMode</classname> to express upper vs. lower case"
msgstr "枚举类型<classname>CaseMode</classname>, 来表示大写或小写模式."
#. Tag: programlisting
#: customconstraints.xml:70
#, no-c-format
msgid ""
"package com.mycompany;\n"
"\n"
"public enum CaseMode {\n"
" UPPER, \n"
" LOWER;\n"
"}"
msgstr ""
"package com.mycompany;\n"
"\n"
"public enum CaseMode {\n"
" UPPER, \n"
" LOWER;\n"
"}"
#. Tag: para
#: customconstraints.xml:73
#, no-c-format
msgid ""
"Now we can define the actual constraint annotation. If you've never designed "
"an annotation before, this may look a bit scary, but actually it's not that "
"hard:"
msgstr ""
"现在我们可以来定义真正的约束标注了. 如果你以前没有创建过标注(annotation)的话,"
"那么这个可能看起来有点吓人, 可是其实没有那么难的 :)"
#. Tag: title
#: customconstraints.xml:78
#, no-c-format
msgid "Defining CheckCase constraint annotation"
msgstr "定义一个CheckCase的约束标注"
#. Tag: programlisting
#: customconstraints.xml:80
#, no-c-format
msgid ""
"package com.mycompany;\n"
"\n"
"import static java.lang.annotation.ElementType.*;\n"
"import static java.lang.annotation.RetentionPolicy.*;\n"
"\n"
"import java.lang.annotation.Documented;\n"
"import java.lang.annotation.Retention;\n"
"import java.lang.annotation.Target;\n"
"\n"
"import javax.validation.Constraint;\n"
"import javax.validation.Payload;\n"
"\n"
"@Target( { METHOD, FIELD, ANNOTATION_TYPE })\n"
"@Retention(RUNTIME)\n"
"@Constraint(validatedBy = CheckCaseValidator.class)\n"
"@Documented\n"
"public @interface CheckCase {\n"
"\n"
" String message() default \"{com.mycompany.constraints.checkcase}\";\n"
"\n"
" Class<?>[] groups() default {};\n"
"\n"
" Class<? extends Payload>[] payload() default {};\n"
" \n"
" CaseMode value();\n"
"\n"
"}"
msgstr ""
"package com.mycompany;\n"
"\n"
"import static java.lang.annotation.ElementType.*;\n"
"import static java.lang.annotation.RetentionPolicy.*;\n"
"\n"
"import java.lang.annotation.Documented;\n"
"import java.lang.annotation.Retention;\n"
"import java.lang.annotation.Target;\n"
"\n"
"import javax.validation.Constraint;\n"
"import javax.validation.Payload;\n"
"\n"
"@Target( { METHOD, FIELD, ANNOTATION_TYPE })\n"
"@Retention(RUNTIME)\n"
"@Constraint(validatedBy = CheckCaseValidator.class)\n"
"@Documented\n"
"public @interface CheckCase {\n"
"\n"
" String message() default \"{com.mycompany.constraints.checkcase}\";\n"
"\n"
" Class<?>[] groups() default {};\n"
"\n"
" Class<? extends Payload>[] payload() default {};\n"
" \n"
" CaseMode value();\n"
"\n"
"}"
#. Tag: para
#: customconstraints.xml:83
#, no-c-format
msgid ""
"An annotation type is defined using the <code>@interface</code> keyword. All "
"attributes of an annotation type are declared in a method-like manner. The "
"specification of the Bean Validation API demands, that any constraint "
"annotation defines"
msgstr ""
"一个标注(annotation) 是通过<code>@interface</code>关键字来定义的. 这个标注中"
"的属性是声明成类似方法的样式的. 根据Bean Validation API 规范的要求"
#. Tag: para
#: customconstraints.xml:90
#, no-c-format
msgid ""
"an attribute <property>message</property> that returns the default key for "
"creating error messages in case the constraint is violated"
msgstr ""
"<property>message</property>属性, 这个属性被用来定义默认得消息模版, 当这个约"
"束条件被验证失败的时候,通过此属性来输出错误信息."
#. Tag: para
#: customconstraints.xml:96
#, no-c-format
msgid ""
"an attribute <property>groups</property> that allows the specification of "
"validation groups, to which this constraint belongs (see <xref linkend="
"\"validator-usingvalidator-validationgroups\"/>). This must default to an "
"empty array of type <classname>Class<?></classname>."
msgstr ""
"<property>groups</property> 属性, 用于指定这个约束条件属于哪(些)个校验组(请参"
"考<xref linkend=\"validator-usingvalidator-validationgroups\"/>). 这个的默认"
"值必须是<classname>Class<?></classname>类型到空到数组."
#. Tag: para
#: customconstraints.xml:104
#, no-c-format
msgid ""
"an attribute <classname>payload</classname> that can be used by clients of "
"the Bean Validation API to assign custom payload objects to a constraint. "
"This attribute is not used by the API itself."
msgstr ""
"<classname>payload</classname> 属性, Bean Validation API 的使用者可以通过此属"
"性来给约束条件指定严重级别. 这个属性并不被API自身所使用."
#. Tag: para
#: customconstraints.xml:108
#, no-c-format
msgid "An example for a custom payload could be the definition of a severity."
msgstr "通过payload属性来指定默认错误严重级别的示例"
#. Tag: programlisting
#: customconstraints.xml:111
#, no-c-format
msgid ""
"public class Severity {\n"
" public static class Info extends Payload {};\n"
" public static class Error extends Payload {};\n"
"}\n"
"\n"
"public class ContactDetails {\n"
" @NotNull(message=\"Name is mandatory\", payload=Severity.Error.class)\n"
" private String name;\n"
"\n"
" @NotNull(message=\"Phone number not specified, but not mandatory\", "
"payload=Severity.Info.class)\n"
" private String phoneNumber;\n"
"\n"
" // ...\n"
"}"
msgstr ""
"public class Severity {\n"
" public static class Info extends Payload {};\n"
" public static class Error extends Payload {};\n"
"}\n"
"\n"
"public class ContactDetails {\n"
" @NotNull(message=\"Name is mandatory\", payload=Severity.Error.class)\n"
" private String name;\n"
"\n"
" @NotNull(message=\"Phone number not specified, but not mandatory\", "
"payload=Severity.Info.class)\n"
" private String phoneNumber;\n"
"\n"
" // ...\n"
"}"
#. Tag: para
#: customconstraints.xml:113
#, no-c-format
msgid ""
"Now a client can after the validation of a <classname>ContactDetails</"
"classname> instance access the severity of a constraint using "
"<methodname>ConstraintViolation.getConstraintDescriptor().getPayload()</"
"methodname> and adjust its behaviour depending on the severity."
msgstr ""
"这样, 在校验完一个<classname>ContactDetails</classname> 的示例之后, 你就可以"
"通过调用<methodname>ConstraintViolation.getConstraintDescriptor().getPayload"
"()</methodname>来得到之前指定到错误级别了,并且可以根据这个信息来决定接下来到"
"行为."
#. Tag: para
#: customconstraints.xml:122
#, no-c-format
msgid ""
"Besides those three mandatory attributes (<property>message</property>, "
"<property>groups</property> and <property>payload</property>) we add another "
"one allowing for the required case mode to be specified. The name "
"<property>value</property> is a special one, which can be omitted upon using "
"the annotation, if it is the only attribute specified, as e.g. in "
"<code>@CheckCase(CaseMode.UPPER)</code>."
msgstr ""
"除了这三个强制性要求的属性(<property>message</property>, <property>groups</"
"property> 和 <property>payload</property>) 之外, 我们还添加了一个属性用来指定"
"所要求到字符串模式. 此属性的名称<property>value</property>在annotation的定义"
"中比较特殊, 如果只有这个属性被赋值了的话, 那么, 在使用此annotation到时候可以"
"忽略此属性名称, 即<code>@CheckCase(CaseMode.UPPER)</code>."
#. Tag: para
#: customconstraints.xml:130
#, no-c-format
msgid ""
"In addition we annotate the annotation type with a couple of so-called meta "
"annotations:"
msgstr ""
"另外, 我们还给这个annotation标注了一些(所谓的) 元标注( 译注: 或\"元模型信息"
"\"?, \"meta annotatioins\"): "
#. Tag: para
#: customconstraints.xml:135
#, no-c-format
msgid ""
"<code>@Target({ METHOD, FIELD, ANNOTATION_TYPE })</code>: Says, that "
"methods, fields and annotation declarations may be annotated with @CheckCase "
"(but not type declarations e.g.)"
msgstr ""
"<code>@Target({ METHOD, FIELD, ANNOTATION_TYPE })</code>: 表示@CheckCase 可以"
"被用在方法, 字段或者annotation声明上."
#. Tag: para
#: customconstraints.xml:141
#, no-c-format
msgid ""
"<code>@Retention(RUNTIME)</code>: Specifies, that annotations of this type "
"will be available at runtime by the means of reflection"
msgstr ""
"<code>@Retention(RUNTIME)</code>: 表示这个标注信息是在运行期通过反射被读取的."
#. Tag: para
#: customconstraints.xml:147
#, no-c-format
msgid ""
"<code>@Constraint(validatedBy = CheckCaseValidator.class)</code>: Specifies "
"the validator to be used to validate elements annotated with @CheckCase"
msgstr ""
"<code>@Constraint(validatedBy = CheckCaseValidator.class)</code>: 指明使用那"
"个校验器(类) 去校验使用了此标注的元素."
#. Tag: para
#: customconstraints.xml:153
#, no-c-format
msgid ""
"<code>@Documented</code>: Says, that the use of <code>@CheckCase</code> will "
"be contained in the JavaDoc of elements annotated with it"
msgstr ""
"<code>@Documented</code>: 表示在对使用了<code>@CheckCase</code>的类进行"
"javadoc操作到时候, 这个标注会被添加到javadoc当中."
#. Tag: para
#: customconstraints.xml:160
#, no-c-format
msgid ""
"Hibernate Validator provides support for the validation of method parameters "
"using constraint annotations (see <xref linkend=\"validator-customoptions-"
"methodvalidation\"/>)."
msgstr ""
#. Tag: para
#: customconstraints.xml:164
#, no-c-format
msgid ""
"In order to use a custom constraint for parameter validation the "
"<classname>ElementType.PARAMETER</classname> must be specified within the "
"<classname>@Target</classname> annotation. This is already the case for all "
"constraints defined by the Bean Validation API and also the custom "
"constraints provided by Hibernate Validator."
msgstr ""
#. Tag: title
#: customconstraints.xml:173
#, no-c-format
msgid "The constraint validator"
msgstr "约束校验器"
#. Tag: para
#: customconstraints.xml:176
#, fuzzy, no-c-format
msgid ""
"Next, we need to implement a constraint validator, that's able to validate "
"elements with a <classname>@CheckCase</classname> annotation. To do so, we "
"implement the interface <classname>ConstraintValidator</classname> as shown "
"below:"
msgstr ""
"接下来, 我们需要实现一个约束验证器, 它是被用来校验应用了"
"<classname>@CheckCase</classname>标注到元素到. 如下面例子所示, 这个验证器需要"
"实现ConstraintValidator接口."
#. Tag: title
#: customconstraints.xml:182
#, no-c-format
msgid ""
"Implementing a constraint validator for the constraint <classname>CheckCase</"
"classname>"
msgstr "约束条件<classname>CheckCase</classname>的验证器"
#. Tag: programlisting
#: customconstraints.xml:185
#, no-c-format
msgid ""
"package com.mycompany;\n"
"\n"
"import javax.validation.ConstraintValidator;\n"
"import javax.validation.ConstraintValidatorContext;\n"
"\n"
"public class CheckCaseValidator implements ConstraintValidator<CheckCase, "
"String> {\n"
"\n"
" private CaseMode caseMode;\n"
"\n"
" public void initialize(CheckCase constraintAnnotation) {\n"
" this.caseMode = constraintAnnotation.value();\n"
" }\n"
"\n"
" public boolean isValid(String object, ConstraintValidatorContext "
"constraintContext) {\n"
"\n"
" if (object == null)\n"
" return true;\n"
"\n"
" if (caseMode == CaseMode.UPPER)\n"
" return object.equals(object.toUpperCase());\n"
" else\n"
" return object.equals(object.toLowerCase());\n"
" }\n"
"\n"
"}"
msgstr ""
"package com.mycompany;\n"
"\n"
"import javax.validation.ConstraintValidator;\n"
"import javax.validation.ConstraintValidatorContext;\n"
"\n"
"public class CheckCaseValidator implements ConstraintValidator<CheckCase, "
"String> {\n"
"\n"
" private CaseMode caseMode;\n"
"\n"
" public void initialize(CheckCase constraintAnnotation) {\n"
" this.caseMode = constraintAnnotation.value();\n"
" }\n"
"\n"
" public boolean isValid(String object, ConstraintValidatorContext "
"constraintContext) {\n"
"\n"
" if (object == null)\n"
" return true;\n"
"\n"
" if (caseMode == CaseMode.UPPER)\n"
" return object.equals(object.toUpperCase());\n"
" else\n"
" return object.equals(object.toLowerCase());\n"
" }\n"
"\n"
"}"
#. Tag: para
#: customconstraints.xml:188
#, no-c-format
msgid ""
"The <classname>ConstraintValidator</classname> interface defines two type "
"parameters, which we set in our implementation. The first one specifies the "
"annotation type to be validated (in our example <classname>CheckCase</"
"classname>), the second one the type of elements, which the validator can "
"handle (here <classname>String</classname>)."
msgstr ""
"<classname>ConstraintValidator</classname>定义了两个泛型参数, 第一个是这个校"
"验器所服务到标注类型(在我们的例子中即<classname>CheckCase</classname>), 第二"
"个这个校验器所支持到被校验元素到类型 (即<classname>String</classname>)."
#. Tag: para
#: customconstraints.xml:195
#, no-c-format
msgid ""
"In case a constraint annotation is allowed at elements of different types, a "
"<classname>ConstraintValidator</classname> for each allowed type has to be "
"implemented and registered at the constraint annotation as shown above."
msgstr ""
"如果一个约束标注支持多种类型到被校验元素的话, 那么需要为每个所支持的类型定义"
"一个<classname>ConstraintValidator</classname>,并且注册到约束标注中."
#. Tag: para
#: customconstraints.xml:200
#, no-c-format
msgid ""
"The implementation of the validator is straightforward. The "
"<methodname>initialize()</methodname> method gives us access to the "
"attribute values of the annotation to be validated. In the example we store "
"the <classname>CaseMode</classname> in a field of the validator for further "
"usage."
msgstr ""
"这个验证器的实现就很平常了, <methodname>initialize()</methodname> 方法传进来"
"一个所要验证的标注类型的实例, 在本例中, 我们通过此实例来获取其value属性的值,"
"并将其保存为<classname>CaseMode</classname>类型的成员变量供下一步使用."
#. Tag: para
#: customconstraints.xml:206
#, no-c-format
msgid ""
"In the <methodname>isValid()</methodname> method we implement the logic, "
"that determines, whether a <classname>String</classname> is valid according "
"to a given <classname>@CheckCase</classname> annotation or not. This "
"decision depends on the case mode retrieved in <classname>initialize()</"
"classname>. As the Bean Validation specification recommends, we consider "
"<code>null</code> values as being valid. If <code>null</code> is not a valid "
"value for an element, it should be annotated with <code>@NotNull</code> "
"explicitly."
msgstr ""
"<methodname>isValid()</methodname>是实现真正的校验逻辑的地方, 判断一个给定的"
"<classname>String</classname>对于<classname>@CheckCase</classname>这个约束条"
"件来说是否是合法的, 同时这还要取决于在<classname>initialize()</classname>中获"
"得的大小写模式. 根据Bean Validation中所推荐的做法, 我们认为<code>null</code>"
"是合法的值. 如果<code>null</code>对于这个元素来说是不合法的话,那么它应该使用"
"<code>@NotNull</code>来标注."
#. Tag: title
#: customconstraints.xml:216
#, no-c-format
msgid "The ConstraintValidatorContext"
msgstr "ConstraintValidatorContext"
#. Tag: para
#: customconstraints.xml:218
#, no-c-format
msgid ""
"relies on the default error message generation by just returning "
"<constant>true</constant> or <constant>false</constant> from the "
"<methodname>isValid</methodname> call. Using the passed "
"<classname>ConstraintValidatorContext</classname> object it is possible to "
"either add additional error messages or completely disable the default error "
"message generation and solely define custom error messages. The "
"<classname>ConstraintValidatorContext</classname> API is modeled as fluent "
"interface and is best demonstrated with an example:"
msgstr ""
"中的<methodname>isValid</methodname>使用了约束条件中定义的错误消息模板, 然后"
"返回一个<constant>true</constant> 或者 <constant>false</constant>. 通过使用传"
"入的<classname>ConstraintValidatorContext</classname>对象, 我们还可以给约束条"
"件中定义的错误信息模板来添加额外的信息或者完全创建一个新的错误信息模板. "
#. Tag: title
#: customconstraints.xml:230
#, no-c-format
msgid "Use of ConstraintValidatorContext to define custom error messages"
msgstr "使用ConstraintValidatorContext来自定义错误信息"
#. Tag: programlisting
#: customconstraints.xml:233
#, no-c-format
msgid ""
"package com.mycompany;\n"
"\n"
"import javax.validation.ConstraintValidator;\n"
"import javax.validation.ConstraintValidatorContext;\n"
"\n"
"public class CheckCaseValidator implements ConstraintValidator<CheckCase, "
"String> {\n"
"\n"
" private CaseMode caseMode;\n"
"\n"
" public void initialize(CheckCase constraintAnnotation) {\n"
" this.caseMode = constraintAnnotation.value();\n"
" }\n"
"\n"
" public boolean isValid(String object, ConstraintValidatorContext "
"constraintContext) {\n"
"\n"
" if (object == null)\n"
" return true;\n"
" \n"
" boolean isValid;\n"
" if (caseMode == CaseMode.UPPER) {\n"
" isValid = object.equals(object.toUpperCase());\n"
" }\n"
" else {\n"
" isValid = object.equals(object.toLowerCase());\n"
" }\n"
" \n"
" if(!isValid) {\n"
" constraintContext.disableDefaultConstraintViolation();\n"
" constraintContext.buildConstraintViolationWithTemplate( \"{com."
"mycompany.constraints.CheckCase.message}\" ).addConstraintViolation();\n"
" }\n"
" return result;\n"
" }\n"
"\n"
"}"
msgstr ""
"package com.mycompany;\n"
"\n"
"import javax.validation.ConstraintValidator;\n"
"import javax.validation.ConstraintValidatorContext;\n"
"\n"
"public class CheckCaseValidator implements ConstraintValidator<CheckCase, "
"String> {\n"
"\n"
" private CaseMode caseMode;\n"
"\n"
" public void initialize(CheckCase constraintAnnotation) {\n"
" this.caseMode = constraintAnnotation.value();\n"
" }\n"
"\n"
" public boolean isValid(String object, ConstraintValidatorContext "
"constraintContext) {\n"
"\n"
" if (object == null)\n"
" return true;\n"
" \n"
" boolean isValid;\n"
" if (caseMode == CaseMode.UPPER) {\n"
" isValid = object.equals(object.toUpperCase());\n"
" }\n"
" else {\n"
" isValid = object.equals(object.toLowerCase());\n"
" }\n"
" \n"
" if(!isValid) {\n"
" constraintContext.disableDefaultConstraintViolation();\n"
" constraintContext.buildConstraintViolationWithTemplate( \"{com."
"mycompany.constraints.CheckCase.message}\" ).addConstraintViolation();\n"
" }\n"
" return result;\n"
" }\n"
"\n"
"}"
#. Tag: para
#: customconstraints.xml:236
#, no-c-format
msgid ""
"shows how you can disable the default error message generation and add a "
"custom error message using a specified message template. In this example the "
"use of the <classname>ConstraintValidatorContext</classname> results in the "
"same error message as the default error message generation."
msgstr ""
"演示了如果创建一个新的错误信息模板来替换掉约束条件中定义的默认的. 在本例中, "
"实际上通过调用<classname>ConstraintValidatorContext</classname>达到了一个使用"
"默认消息模板的效果."
#. Tag: para
#: customconstraints.xml:242
#, no-c-format
msgid ""
"It is important to end each new constraint violation with "
"<methodname>addConstraintViolation</methodname>. Only after that the new "
"constraint violation will be created."
msgstr ""
"在创建新的constraint violation的时候一定要记得调用"
"<methodname>addConstraintViolation</methodname>, 只有这样, 这个新的"
"constraint violation才会被真正的创建."
#. Tag: para
#: customconstraints.xml:247
#, no-c-format
msgid ""
"In case you are implementing a <classname>ConstraintValidator</classname> a "
"class level constraint it is also possible to adjust set the property path "
"for the created constraint violations. This is important for the case where "
"you validate multiple properties of the class or even traverse the object "
"graph. A custom property path creation could look like <xref linkend="
"\"example-custom-error\"/>."
msgstr ""
#. Tag: title
#: customconstraints.xml:256
#, no-c-format
msgid ""
"Adding new <classname>ConstraintViolation</classname> with custom property "
"path"
msgstr ""
#. Tag: programlisting
#: customconstraints.xml:259
#, no-c-format
msgid ""
"public boolean isValid(Group group, ConstraintValidatorContext "
"constraintValidatorContext) {\n"
" boolean isValid = false;\n"
" ...\n"
"\n"
" if(!isValid) {\n"
" constraintValidatorContext\n"
" .buildConstraintViolationWithTemplate( \"{my.custom."
"template}\" )\n"
" .addNode( \"myProperty\" ).addConstraintViolation();\n"
" }\n"
" return isValid;\n"
"}"
msgstr ""
"public boolean isValid(Group group, ConstraintValidatorContext "
"constraintValidatorContext) {\n"
" boolean isValid = false;\n"
" ...\n"
"\n"
" if(!isValid) {\n"
" constraintValidatorContext\n"
" .buildConstraintViolationWithTemplate( \"{my.custom."
"template}\" )\n"
" .addNode( \"myProperty\" ).addConstraintViolation();\n"
" }\n"
" return isValid;\n"
"}"
#. Tag: title
#: customconstraints.xml:265
#, no-c-format
msgid "The error message"
msgstr "校验错误信息"
#. Tag: para
#: customconstraints.xml:267
#, no-c-format
msgid ""
"Finally we need to specify the error message, that shall be used, in case a "
"<classname>@CheckCase</classname> constraint is violated. To do so, we add "
"the following to our custom <filename>ValidationMessages.properties</"
"filename> (see also <xref linkend=\"section-message-interpolation\"/>)"
msgstr ""
"最后, 我们还需要指定如果<classname>@CheckCase</classname>这个约束条件验证的时"
"候,没有通过的话的校验错误信息. 我们可以添加下面的内容到我们项目自定义的"
"<filename>ValidationMessages.properties</filename> (参考 <xref linkend="
"\"section-message-interpolation\"/>)文件中."
#. Tag: title
#: customconstraints.xml:274
#, no-c-format
msgid ""
"Defining a custom error message for the <classname>CheckCase</classname> "
"constraint"
msgstr "为<classname>CheckCase</classname>约束定义一个错误信息"
#. Tag: programlisting
#: customconstraints.xml:277
#, no-c-format
msgid "com.mycompany.constraints.CheckCase.message=Case mode must be {value}."
msgstr "com.mycompany.constraints.CheckCase.message=Case mode must be {value}."
#. Tag: para
#: customconstraints.xml:280
#, no-c-format
msgid ""
"If a validation error occurs, the validation runtime will use the default "
"value, that we specified for the message attribute of the "
"<classname>@CheckCase</classname> annotation to look up the error message in "
"this file."
msgstr ""
"如果发现校验错误了的话, 你所使用的Bean Validation的实现会用我们定义在"
"<classname>@CheckCase</classname>中message属性上的值作为键到这个文件中去查找"
"对应的错误信息."
#. Tag: title
#: customconstraints.xml:287
#, no-c-format
msgid "Using the constraint"
msgstr "应用约束条件"
#. Tag: para
#: customconstraints.xml:289
#, no-c-format
msgid ""
"Now that our first custom constraint is completed, we can use it in the "
"<classname>Car</classname> class from the <xref linkend=\"validator-"
"gettingstarted\"/> chapter to specify that the <property>licensePlate</"
"property> field shall only contain upper-case strings:"
msgstr ""
"现在我们已经有了一个自定义的约束条件了, 我们可以把它用在<xref linkend="
"\"validator-gettingstarted\"/>中的<classname>Car</classname>类上, 来校验此类"
"的<property>licensePlate</property>属性的值是否全都是大写字母."
#. Tag: title
#: customconstraints.xml:296
#, no-c-format
msgid "Applying the <classname>CheckCase</classname> constraint"
msgstr "应用<classname>CheckCase</classname>约束条件"
#. Tag: programlisting
#: customconstraints.xml:299
#, no-c-format
msgid ""
"package com.mycompany;\n"
"\n"
"import javax.validation.constraints.Min;\n"
"import javax.validation.constraints.NotNull;\n"
"import javax.validation.constraints.Size;\n"
"\n"
"public class Car {\n"
"\n"
" @NotNull\n"
" private String manufacturer;\n"
"\n"
" @NotNull\n"
" @Size(min = 2, max = 14)\n"
" @CheckCase(CaseMode.UPPER)\n"
" private String licensePlate;\n"
"\n"
" @Min(2)\n"
" private int seatCount;\n"
" \n"
" public Car(String manufacturer, String licencePlate, int seatCount) {\n"
"\n"
" this.manufacturer = manufacturer;\n"
" this.licensePlate = licencePlate;\n"
" this.seatCount = seatCount;\n"
" }\n"
"\n"
" //getters and setters ...\n"
"\n"
"}"
msgstr ""
"package com.mycompany;\n"
"\n"
"import javax.validation.constraints.Min;\n"
"import javax.validation.constraints.NotNull;\n"
"import javax.validation.constraints.Size;\n"
"\n"
"public class Car {\n"
"\n"
" @NotNull\n"
" private String manufacturer;\n"
"\n"
" @NotNull\n"
" @Size(min = 2, max = 14)\n"
" @CheckCase(CaseMode.UPPER)\n"
" private String licensePlate;\n"
"\n"
" @Min(2)\n"
" private int seatCount;\n"
" \n"
" public Car(String manufacturer, String licencePlate, int seatCount) {\n"
"\n"
" this.manufacturer = manufacturer;\n"
" this.licensePlate = licencePlate;\n"
" this.seatCount = seatCount;\n"
" }\n"
"\n"
" //getters and setters ...\n"
"\n"
"}"
#. Tag: para
#: customconstraints.xml:302
#, no-c-format
msgid ""
"Finally let's demonstrate in a little test that the <classname>@CheckCase</"
"classname> constraint is properly validated:"
msgstr ""
"最后,让我们用一个简单的测试来检测<classname>@CheckCase</classname>约束已经被"
"正确的校验了:"
#. Tag: title
#: customconstraints.xml:307
#, no-c-format
msgid "Testcase demonstrating the <classname>CheckCase</classname> validation"
msgstr "演示<classname>CheckCase</classname>的验证过程"
#. Tag: programlisting
#: customconstraints.xml:310
#, no-c-format
msgid ""
"package com.mycompany;\n"
"\n"
"import static org.junit.Assert.*;\n"
"\n"
"import java.util.Set;\n"
"\n"
"import javax.validation.ConstraintViolation;\n"
"import javax.validation.Validation;\n"
"import javax.validation.Validator;\n"
"import javax.validation.ValidatorFactory;\n"
"\n"
"import org.junit.BeforeClass;\n"
"import org.junit.Test;\n"
"\n"
"public class CarTest {\n"
"\n"
" private static Validator validator;\n"
"\n"
" @BeforeClass\n"
" public static void setUp() {\n"
" ValidatorFactory factory = Validation.buildDefaultValidatorFactory"
"();\n"
" validator = factory.getValidator();\n"
" }\n"
"\n"
" @Test\n"
" public void testLicensePlateNotUpperCase() {\n"
"\n"
" Car car = new Car(\"Morris\", \"dd-ab-123\", 4);\n"
"\n"
" Set<ConstraintViolation<Car>> constraintViolations =\n"
" validator.validate(car);\n"
" assertEquals(1, constraintViolations.size());\n"
" assertEquals(\n"
" \"Case mode must be UPPER.\", \n"
" constraintViolations.iterator().next().getMessage());\n"
" }\n"
"\n"
" @Test\n"
" public void carIsValid() {\n"
"\n"
" Car car = new Car(\"Morris\", \"DD-AB-123\", 4);\n"
"\n"
" Set<ConstraintViolation<Car>> constraintViolations =\n"
" validator.validate(car);\n"
"\n"
" assertEquals(0, constraintViolations.size());\n"
" }\n"
"}"
msgstr ""
"package com.mycompany;\n"
"\n"
"import static org.junit.Assert.*;\n"
"\n"
"import java.util.Set;\n"
"\n"
"import javax.validation.ConstraintViolation;\n"
"import javax.validation.Validation;\n"
"import javax.validation.Validator;\n"
"import javax.validation.ValidatorFactory;\n"
"\n"
"import org.junit.BeforeClass;\n"
"import org.junit.Test;\n"
"\n"
"public class CarTest {\n"
"\n"
" private static Validator validator;\n"
"\n"
" @BeforeClass\n"
" public static void setUp() {\n"
" ValidatorFactory factory = Validation.buildDefaultValidatorFactory"
"();\n"
" validator = factory.getValidator();\n"
" }\n"
"\n"
" @Test\n"
" public void testLicensePlateNotUpperCase() {\n"
"\n"
" Car car = new Car(\"Morris\", \"dd-ab-123\", 4);\n"
"\n"
" Set<ConstraintViolation<Car>> constraintViolations =\n"
" validator.validate(car);\n"
" assertEquals(1, constraintViolations.size());\n"
" assertEquals(\n"
" \"Case mode must be UPPER.\", \n"
" constraintViolations.iterator().next().getMessage());\n"
" }\n"
"\n"
" @Test\n"
" public void carIsValid() {\n"
"\n"
" Car car = new Car(\"Morris\", \"DD-AB-123\", 4);\n"
"\n"
" Set<ConstraintViolation<Car>> constraintViolations =\n"
" validator.validate(car);\n"
"\n"
" assertEquals(0, constraintViolations.size());\n"
" }\n"
"}"
#. Tag: title
#: customconstraints.xml:316
#, no-c-format
msgid "Constraint composition"
msgstr "约束条件组合"
#. Tag: para
#: customconstraints.xml:318
#, no-c-format
msgid ""
"Looking at the <property>licensePlate</property> field of the "
"<classname>Car</classname> class in <xref linkend=\"example-car-with-"
"checkcase\"/>, we see three constraint annotations already. In complexer "
"scenarios, where even more constraints could be applied to one element, this "
"might become a bit confusing easily. Furthermore, if we had a "
"<property>licensePlate</property> field in another class, we would have to "
"copy all constraint declarations to the other class as well, violating the "
"DRY principle."
msgstr ""
"在<xref linkend=\"example-car-with-checkcase\"/>中我们可以看到, 类"
"<classname>Car</classname>的<property>licensePlate</property>属性上定义了三个"
"约束条件. 在某些复杂的场景中, 可能还会有更多的约束条件被定义到同一个元素上"
"面, 这可能会让代码看起来有些复杂, 另外, 如果在另外的类里面还有一个"