KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > grammar > BaseValidationRuleGrammar


1 /*
2 * Copyright 2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * $Header:$
17 */

18 package org.apache.beehive.netui.compiler.grammar;
19
20 import org.apache.beehive.netui.compiler.AnnotationGrammar;
21 import org.apache.beehive.netui.compiler.CompilerUtils;
22 import org.apache.beehive.netui.compiler.Diagnostics;
23 import org.apache.beehive.netui.compiler.RuntimeVersionChecker;
24 import org.apache.beehive.netui.compiler.FatalCompileTimeException;
25 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
26 import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
27 import org.apache.beehive.netui.compiler.typesystem.env.AnnotationProcessorEnvironment;
28
29 import java.util.List JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32
33 public class BaseValidationRuleGrammar
34         extends AnnotationGrammar
35 {
36     private static final String JavaDoc[][] MUTUALLY_EXCLUSIVE_ATTRS =
37             {
38                 { MESSAGE_KEY_ATTR, MESSAGE_ATTR }
39             };
40     
41     private static final String JavaDoc[][] ATTR_DEPENDENCIES =
42             {
43                 { BUNDLE_NAME_ATTR, MESSAGE_KEY_ATTR }
44             };
45
46
47     public BaseValidationRuleGrammar( AnnotationProcessorEnvironment env, Diagnostics diags,
48                                       RuntimeVersionChecker rvc )
49     {
50         super( env, diags, VERSION_9_0_STRING, rvc );
51
52         addMemberType( MESSAGE_KEY_ATTR, new MessageKeyType( null, this ) );
53         addMemberArrayGrammar( MESSAGE_ARGS_ATTR, new ValidationMessageArgsGrammar( env, diags, rvc ) );
54         addMemberType( BUNDLE_NAME_ATTR, new BundleNameType( null, this ) );
55     }
56
57     
58     public String JavaDoc[][] getMutuallyExclusiveAttrs()
59     {
60         return MUTUALLY_EXCLUSIVE_ATTRS;
61     }
62
63     
64     public String JavaDoc[][] getAttrDependencies()
65     {
66         return ATTR_DEPENDENCIES;
67     }
68
69     
70     protected boolean onBeginCheck( AnnotationInstance annotation, AnnotationInstance[] parentAnnotations,
71                                     MemberDeclaration classMember )
72             throws FatalCompileTimeException
73     {
74         //
75
// Check to make sure that either the parent ValidatableProperty annotation has a displayName property,
76
// or this rule specifies a first argument to the default message, or this rule specifies its own message.
77
// If none of these are true, output a warning.
78
//
79
assert parentAnnotations.length > 0;
80         AnnotationInstance immediateParent = parentAnnotations[parentAnnotations.length - 1];
81         
82         if ( CompilerUtils.getString( immediateParent, DISPLAY_NAME_ATTR, true ) == null
83              && CompilerUtils.getString( immediateParent, DISPLAY_NAME_KEY_ATTR, true ) == null
84              && CompilerUtils.getString( annotation, MESSAGE_KEY_ATTR, true ) == null
85              && CompilerUtils.getString( annotation, MESSAGE_ATTR, true ) == null )
86         {
87             boolean useDefaultDisplayName = true;
88             List JavaDoc messageArgs =
89                     CompilerUtils.getAnnotationArray( annotation, MESSAGE_ARGS_ATTR, true );
90
91             if ( messageArgs != null )
92             {
93                 boolean firstArg = true;
94
95                 for ( Iterator JavaDoc ii = messageArgs.iterator(); ii.hasNext(); )
96                 {
97                     AnnotationInstance messageArg = ( AnnotationInstance ) ii.next();
98                     Integer JavaDoc position = CompilerUtils.getInteger( messageArg, POSITION_ATTR, true );
99
100                     if ( ( position == null && firstArg ) || ( position != null && position.intValue() == 0 ) )
101                     {
102                         useDefaultDisplayName = false;
103                         break;
104                     }
105
106                     firstArg = false;
107                 }
108             }
109
110             if ( useDefaultDisplayName )
111             {
112                 addWarning( annotation, "warning.using-default-display-name",
113                             CompilerUtils.getDeclaration( immediateParent.getAnnotationType() ).getSimpleName() );
114             }
115         }
116
117         return super.onBeginCheck( annotation, parentAnnotations, classMember );
118     }
119 }
120
Popular Tags