KickJava   Java API By Example, From Geeks To Geeks.

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


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.AnnotationMemberType;
22 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
23 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeElementDeclaration;
24 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
25 import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
26
27 public class JavaIdentifierType
28         extends AnnotationMemberType
29 {
30     private char[] _validChars;
31     
32     public JavaIdentifierType( String JavaDoc requiredRuntimeVersion, AnnotationGrammar parentGrammar, char[] validChars )
33     {
34         super( requiredRuntimeVersion, parentGrammar );
35         _validChars = validChars;
36     }
37
38     
39     public Object JavaDoc onCheck( AnnotationTypeElementDeclaration valueDecl, AnnotationValue value,
40                            AnnotationInstance[] parentAnnotations, MemberDeclaration classMember,
41                            int annotationArrayIndex )
42     {
43         String JavaDoc val = ( String JavaDoc ) value.getValue();
44
45         if ( val.length() > 0 )
46         {
47             char firstChar = val.charAt( 0 );
48             
49             if ( ! Character.isJavaIdentifierStart( firstChar ) )
50             {
51                 Object JavaDoc[] args = new Object JavaDoc[]{ new Character JavaDoc( firstChar ) };
52                 addError( value, "error.invalid-java-identifier-start", args );
53             }
54             
55             for ( int i = 1; i < val.length(); i++ )
56             {
57                 char c = val.charAt( i );
58                 
59                 if ( ! Character.isJavaIdentifierPart( val.charAt( i ) ) && ! isValid( c ) )
60                 {
61                     Object JavaDoc[] args = new Object JavaDoc[]{ new Character JavaDoc( c ) };
62                     addError( value, "error.invalid-java-identifier-part", args );
63                 }
64             }
65         }
66         
67         return null;
68     }
69     
70     private boolean isValid( char c )
71     {
72         if ( _validChars != null )
73         {
74             for ( int i = 0; i < _validChars.length; i++ )
75             {
76                 if ( c == _validChars[i] ) return true;
77             }
78         }
79         
80         return false;
81     }
82 }
83
Popular Tags