KickJava   Java API By Example, From Geeks To Geeks.

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


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.CompilerUtils;
21 import org.apache.beehive.netui.compiler.FlowControllerInfo;
22 import org.apache.beehive.netui.compiler.AnnotationGrammar;
23 import org.apache.beehive.netui.compiler.MergedControllerAnnotation;
24 import org.apache.beehive.netui.compiler.FatalCompileTimeException;
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 import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration;
29 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeElementDeclaration;
30 import org.apache.beehive.netui.compiler.typesystem.declaration.MethodDeclaration;
31 import org.apache.beehive.netui.compiler.typesystem.declaration.ClassDeclaration;
32 import org.apache.beehive.netui.compiler.typesystem.env.AnnotationProcessorEnvironment;
33 import org.apache.beehive.netui.compiler.typesystem.type.ClassType;
34
35 import java.util.Collection JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 public class WebappPathOrActionType
39         extends WebappPathType
40 {
41     public WebappPathOrActionType( boolean pathMustBeRelative, String JavaDoc requiredRuntimeVersion,
42                                    AnnotationGrammar parentGrammar, FlowControllerInfo fcInfo )
43     {
44         super( pathMustBeRelative, requiredRuntimeVersion, parentGrammar, fcInfo );
45     }
46
47     
48     public Object JavaDoc onCheck( AnnotationTypeElementDeclaration valueDecl, AnnotationValue value,
49                            AnnotationInstance[] parentAnnotations, MemberDeclaration classMember,
50                            int annotationArrayIndex )
51             throws FatalCompileTimeException
52     {
53         String JavaDoc stringValue = ( String JavaDoc ) value.getValue();
54         checkAction( stringValue, value, classMember );
55         return super.onCheck( valueDecl, value, parentAnnotations, classMember, annotationArrayIndex );
56     }
57     
58     protected void checkAction( String JavaDoc stringValue, AnnotationValue annValue, MemberDeclaration classMember )
59     {
60         if ( stringValue.endsWith( ACTION_EXTENSION_DOT ) && stringValue.indexOf( '/' ) == -1 )
61         {
62             TypeDeclaration outerType = CompilerUtils.getOuterClass( classMember );
63
64             if ( outerType != null ) // null in some error conditions
65
{
66                 int extensionPos = stringValue.lastIndexOf( ACTION_EXTENSION_DOT );
67                 String JavaDoc actionMethodName = stringValue.substring( 0, extensionPos );
68                 FlowControllerInfo fcInfo = getFlowControllerInfo();
69                 boolean foundIt = actionExists( actionMethodName, outerType, null, getEnv(), fcInfo, false );
70
71                 if ( ! foundIt && actionMethodName.length() > 0 )
72                 {
73                     //
74
// Check for a Shared Flow action reference of the form <shared-flow-name>..
75
//
76
int dot = actionMethodName.indexOf( '.' );
77                     
78                     if ( dot != -1 && dot < actionMethodName.length() - 1 )
79                     {
80                         String JavaDoc sharedFlowName = actionMethodName.substring( 0, dot );
81                         TypeDeclaration sfTypeDecl = ( TypeDeclaration ) getFlowControllerInfo().getSharedFlowTypes().get( sharedFlowName );
82                         
83                         if ( sfTypeDecl != null )
84                         {
85                             actionMethodName = actionMethodName.substring( dot + 1 );
86                             foundIt = actionExists( actionMethodName, sfTypeDecl, null, getEnv(), fcInfo, false );
87                         }
88                     }
89                 }
90                 
91                 //
92
// Look in (legacy) Global.app, unless the class being checked is a shared flow (shared flows are
93
// the successor to Global.app -- they can't raise Global.app actions.
94
//
95
if ( ! foundIt &&
96                      ! CompilerUtils.isAssignableFrom( SHARED_FLOW_BASE_CLASS,
97                                                        CompilerUtils.getOutermostClass( classMember ), getEnv() ) )
98                 {
99                     TypeDeclaration globalAppDecl = getEnv().getTypeDeclaration( GLOBALAPP_FULL_CLASSNAME );
100                     if ( globalAppDecl != null )
101                     {
102                         foundIt = actionExists( actionMethodName, globalAppDecl, null, getEnv(), fcInfo, false );
103                     }
104                 }
105                 
106                 if ( ! foundIt )
107                 {
108                     if ( doFatalError() )
109                     {
110                         addError( annValue, "error.action-not-found", actionMethodName );
111                     }
112                     else
113                     {
114                         addWarning( annValue, "warning.action-not-found", actionMethodName );
115                     }
116                 }
117             }
118         }
119     }
120     
121     public static boolean actionExists( String JavaDoc actionName, TypeDeclaration type, AnnotationInstance annotationToIgnore,
122                                         AnnotationProcessorEnvironment env, FlowControllerInfo fcInfo,
123                                         boolean checkInheritedActions )
124     {
125         if ( ! ( type instanceof ClassDeclaration ) )
126         {
127             return false;
128         }
129         
130         ClassDeclaration classDecl = ( ClassDeclaration ) type;
131         
132         do
133         {
134             //
135
// First look through the action methods.
136
//
137
MethodDeclaration[] methods = classDecl.getMethods();
138             
139             for ( int i = 0; i < methods.length; i++ )
140             {
141                 MethodDeclaration method = methods[i];
142                 if ( method.getSimpleName().equals( actionName )
143                      && CompilerUtils.getAnnotation( method, ACTION_TAG_NAME ) != null )
144                 {
145                     return true;
146                 }
147             }
148             
149             ClassType superType = classDecl.getSuperclass();
150             classDecl = superType != null ? superType.getClassTypeDeclaration() : null;
151         } while ( classDecl != null );
152         
153         //
154
// Next, look through the simple actions (annotations).
155
//
156
MergedControllerAnnotation mca = fcInfo.getMergedControllerAnnotation();
157         Collection JavaDoc simpleActionAnnotations =
158                 checkInheritedActions
159                 ? mca.getSimpleActions()
160                 : CompilerUtils.getAnnotationArrayValue( type, CONTROLLER_TAG_NAME, SIMPLE_ACTIONS_ATTR, true );
161         
162         if ( simpleActionAnnotations != null )
163         {
164             for ( Iterator JavaDoc ii = simpleActionAnnotations.iterator(); ii.hasNext(); )
165             {
166                 AnnotationInstance ann = ( AnnotationInstance ) ii.next();
167                 String JavaDoc name = CompilerUtils.getString( ann, NAME_ATTR, false );
168                 
169                 if ( actionName.equals( name )
170                      && ! CompilerUtils.annotationsAreEqual( ann, annotationToIgnore, false, env ) )
171                 {
172                     return true;
173                 }
174             }
175         }
176         
177         return false;
178     }
179 }
180
Popular Tags