KickJava   Java API By Example, From Geeks To Geeks.

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


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.RuntimeVersionChecker;
21 import org.apache.beehive.netui.compiler.Diagnostics;
22 import org.apache.beehive.netui.compiler.AnnotationMemberType;
23 import org.apache.beehive.netui.compiler.typesystem.env.AnnotationProcessorEnvironment;
24 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeElementDeclaration;
25 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
26 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
27 import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
28
29
30 public class ValidateRangeGrammar
31         extends BaseValidationRuleGrammar
32 {
33     private static String JavaDoc[][] MUTUALLY_EXCLUSIVE_ATTRS =
34             { { MIN_INT_ATTR, MIN_FLOAT_ATTR }, { MAX_INT_ATTR, MAX_FLOAT_ATTR } };
35     
36     private static String JavaDoc[][] ATTR_DEPENDENCIES =
37             {
38                 { MIN_INT_ATTR, MAX_INT_ATTR },
39                 { MIN_FLOAT_ATTR, MAX_FLOAT_ATTR },
40                 { MAX_INT_ATTR, MIN_INT_ATTR },
41                 { MAX_FLOAT_ATTR, MIN_FLOAT_ATTR }
42             };
43     
44     public ValidateRangeGrammar( AnnotationProcessorEnvironment env, Diagnostics diags,
45                                  RuntimeVersionChecker runtimeVersionChecker )
46     {
47         super( env, diags, runtimeVersionChecker );
48         
49         // The annotation defines these as doubles to avoid forcing us to type 'f' after every initialization value.
50
addMemberType( MIN_FLOAT_ATTR, new FloatType() );
51         addMemberType( MAX_FLOAT_ATTR, new FloatType() );
52         
53         // no custom types needed for minInt, maxInt
54
}
55
56     public String JavaDoc[][] getMutuallyExclusiveAttrs()
57     {
58         return MUTUALLY_EXCLUSIVE_ATTRS;
59     }
60
61     public String JavaDoc[][] getRequiredAttrs()
62     {
63         return null;
64     }
65     
66     public String JavaDoc[][] getAttrDependencies()
67     {
68         return ATTR_DEPENDENCIES;
69     }
70     
71     private class FloatType
72             extends AnnotationMemberType
73     {
74         public FloatType()
75         {
76             super( ValidateRangeGrammar.this.getRequiredRuntimeVersion(), ValidateRangeGrammar.this );
77         }
78
79         
80         public Object JavaDoc onCheck( AnnotationTypeElementDeclaration valueDecl, AnnotationValue member,
81                                AnnotationInstance[] parentAnnotations, MemberDeclaration classMember,
82                                int annotationArrayIndex )
83         {
84             double d = ( ( Double JavaDoc ) member.getValue() ).doubleValue();
85             
86             if ( d < -Float.MAX_VALUE ) addError( member, "error.min-float", new Double JavaDoc( -Float.MAX_VALUE ) );
87             else if ( d > Float.MAX_VALUE ) addError( member, "error.max-float", new Double JavaDoc( Float.MAX_VALUE ) );
88             
89             return null;
90         }
91     }
92 }
93
Popular Tags