KickJava   Java API By Example, From Geeks To Geeks.

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


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.AnnotationMemberType;
21 import org.apache.beehive.netui.compiler.CompilerUtils;
22 import org.apache.beehive.netui.compiler.Diagnostics;
23 import org.apache.beehive.netui.compiler.FlowControllerInfo;
24 import org.apache.beehive.netui.compiler.RuntimeVersionChecker;
25 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
26 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeElementDeclaration;
27 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
28 import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
29 import org.apache.beehive.netui.compiler.typesystem.declaration.MethodDeclaration;
30 import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration;
31 import org.apache.beehive.netui.compiler.typesystem.env.AnnotationProcessorEnvironment;
32 import org.apache.beehive.netui.compiler.typesystem.type.DeclaredType;
33 import org.apache.beehive.netui.compiler.typesystem.type.TypeInstance;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39
40
41 public class ForwardGrammar
42         extends BaseFlowControllerGrammar
43 {
44     private static final String JavaDoc[][] NAVIGATE_TO_VALS = new String JavaDoc[][]
45             {
46                 { NAVIGATE_TO_CURRENT_PAGE_STR, VERSION_8_SP2_STRING },
47                 { NAVIGATE_TO_PREVIOUS_PAGE_STR, VERSION_8_SP2_STRING },
48                 { NAVIGATE_TO_PAGE_LEGACY_STR, null },
49                 { NAVIGATE_TO_PREVIOUS_ACTION_STR, VERSION_8_SP2_STRING },
50             };
51     
52     private static final String JavaDoc[][] DEPRECATED_NAVIGATE_TO_VALS = new String JavaDoc[][]
53             {
54                 { NAVIGATE_TO_PAGE_LEGACY_STR, "warning.return-to-page-deprecated" },
55             };
56
57     private static String JavaDoc[][] MUTUALLY_EXCLUSIVE_ATTRS =
58             {
59                 { PATH_ATTR, TILES_DEFINITION_ATTR, RETURN_ACTION_ATTR, NAVIGATE_TO_ATTR, ACTION_ATTR },
60                 { OUTPUT_FORM_BEAN_TYPE_ATTR, OUTPUT_FORM_BEAN_ATTR },
61                 { REDIRECT_ATTR, EXTERNAL_REDIRECT_ATTR }
62             };
63
64     private static String JavaDoc[][] REQUIRED_ATTRS =
65             {
66                 { NAME_ATTR },
67                 { PATH_ATTR, TILES_DEFINITION_ATTR, RETURN_ACTION_ATTR, NAVIGATE_TO_ATTR, ACTION_ATTR }
68             };
69
70     private static String JavaDoc[][] ATTR_DEPENDENCIES =
71             {
72                 { REDIRECT_ATTR, PATH_ATTR, NAVIGATE_TO_ATTR },
73                 { EXTERNAL_REDIRECT_ATTR, PATH_ATTR },
74                 { RESTORE_QUERY_STRING_ATTR, NAVIGATE_TO_ATTR }
75             };
76     
77     
78     public ForwardGrammar( AnnotationProcessorEnvironment env, Diagnostics diags, String JavaDoc requiredRuntimeVersion,
79                            RuntimeVersionChecker runtimeVersionChecker, FlowControllerInfo fcInfo )
80     {
81         super( env, diags, requiredRuntimeVersion, runtimeVersionChecker, fcInfo );
82         
83         addMemberType( NAME_ATTR, getNameType() );
84         addMemberType( OUTPUT_FORM_BEAN_TYPE_ATTR, new TypeNameType( null, false, null, this ) );
85         addMemberType( OUTPUT_FORM_BEAN_ATTR, new MemberFieldType( null , null, this ) );
86         addMemberType( RETURN_ACTION_ATTR, new JavaIdentifierType( null, this, new char[]{ '.' } ) );
87         addMemberType( PATH_ATTR, new ExternalPathOrActionType( false, null, this, fcInfo ) );
88         addMemberType( ACTION_ATTR, new ValidActionType( null, this, fcInfo ) );
89         addMemberType( TILES_DEFINITION_ATTR, new AnnotationMemberType( null, this ) );
90         addMemberType( REDIRECT_ATTR, new AnnotationMemberType( null, this ) );
91         addMemberType( EXTERNAL_REDIRECT_ATTR, new AbsolutePathType( null , this ) );
92         addMemberType( NAVIGATE_TO_ATTR, new EnumType( NAVIGATE_TO_VALS, DEPRECATED_NAVIGATE_TO_VALS, null, this ) );
93         addMemberType( RESTORE_QUERY_STRING_ATTR, new AnnotationMemberType( null, this ) );
94         
95         addMemberArrayGrammar( ACTION_OUTPUTS_ATTR, new ActionOutputGrammar( env, diags, runtimeVersionChecker ) );
96     }
97
98     protected AnnotationMemberType getNameType()
99     {
100         return new ForwardNameType();
101     }
102     
103     public String JavaDoc[][] getMutuallyExclusiveAttrs()
104     {
105         return MUTUALLY_EXCLUSIVE_ATTRS;
106     }
107
108     public String JavaDoc[][] getRequiredAttrs()
109     {
110         return REQUIRED_ATTRS;
111     }
112
113     public String JavaDoc[][] getAttrDependencies()
114     {
115         return ATTR_DEPENDENCIES;
116     }
117
118     protected void onCheckMember( AnnotationTypeElementDeclaration memberDecl, AnnotationValue value,
119                                   AnnotationInstance annotation, AnnotationInstance[] parentAnnotations,
120                                   MemberDeclaration classMember )
121     {
122         String JavaDoc valueName = memberDecl.getSimpleName();
123         boolean isReturnAction = valueName.equals( RETURN_ACTION_ATTR );
124         
125         if ( isReturnAction )
126         {
127             if ( ! getFlowControllerInfo().isNested() )
128             {
129                 addError( value, "error.only-valid-in-nested", new Object JavaDoc[]{ valueName } );
130             }
131         }
132         
133         if ( valueName.equals( ACTION_OUTPUTS_ATTR ) && ( ( List JavaDoc ) value.getValue() ).size() > 0 )
134         {
135             if ( CompilerUtils.getBoolean( annotation, REDIRECT_ATTR, false ).booleanValue() )
136             {
137                 addError( value, "error.action-outputs-with-redirect", REDIRECT_ATTR );
138             }
139             
140             String JavaDoc path = CompilerUtils.getString( annotation, PATH_ATTR, true );
141             if ( path != null && CompilerUtils.isAbsoluteURI( path ) )
142             {
143                 addError( value, "error.action-outputs-with-absolute-uri", PATH_ATTR );
144             }
145         }
146         
147         //
148
// If this is a return-action, store its info in the FlowControllerInfo (which is eventually provided to tools).
149
//
150
if ( isReturnAction )
151         {
152             TypeDeclaration outerType = CompilerUtils.getOuterClass( classMember );
153             TypeInstance formBeanType =
154                     getFlowControllerInfo().addReturnAction( ( String JavaDoc ) value.getValue(), annotation, outerType );
155             
156             if ( formBeanType != null && ! ( formBeanType instanceof DeclaredType ) )
157             {
158                 addError( annotation, "error.action-invalid-form-bean-type", formBeanType.toString() );
159             }
160         }
161     }
162     
163     protected class ForwardNameType
164         extends UniqueValueType
165     {
166         public ForwardNameType()
167         {
168             this( FORWARDS_ATTR );
169         }
170         
171         protected ForwardNameType( String JavaDoc memberGroupName )
172         {
173             super( memberGroupName, false, false, null, ForwardGrammar.this );
174         }
175
176         /**
177          * @return a List of AnnotationInstance
178          */

179         protected List JavaDoc getAdditionalAnnotationsToCheck( MemberDeclaration classMember )
180         {
181             //
182
// curEntity will be either the pageflow class or an action method, where we'll look
183
// for @Jpf.Catch annotations that refer to exception-handler methods, which also have
184
// forwards that get rolled onto this entity.
185
//
186
List JavaDoc additionalEntities = new ArrayList JavaDoc();
187             
188             TypeDeclaration outerType = CompilerUtils.getOuterClass( classMember );
189             
190             Collection JavaDoc classLevelCatches =
191                     getFlowControllerInfo().getMergedControllerAnnotation().getCatches();
192             addAdditionalAnnotationsToCheck( classLevelCatches, outerType, additionalEntities );
193             
194             if ( classMember instanceof MethodDeclaration )
195             {
196                 Collection JavaDoc methodLevelCatches =
197                         CompilerUtils.getAnnotationArrayValue( classMember, ACTION_TAG_NAME, CATCHES_ATTR, true );
198                 addAdditionalAnnotationsToCheck( methodLevelCatches, outerType, additionalEntities );
199             }
200             
201             return additionalEntities;
202         }
203         
204         private void addAdditionalAnnotationsToCheck( Collection JavaDoc catches,
205                                                       TypeDeclaration outerType, List JavaDoc additionalEntities )
206         {
207             //
208
// For each of the given @Jpf.Catch annotations, find the matching @Jpf.ExceptionHandler method and
209
// add all of its @Jpf.Forward annotations to the list.
210
//
211
if ( catches != null )
212             {
213                 for ( Iterator JavaDoc ii = catches.iterator(); ii.hasNext(); )
214                 {
215                     AnnotationInstance catchAnnotation = ( AnnotationInstance ) ii.next();
216                     String JavaDoc methodName = CompilerUtils.getString( catchAnnotation, METHOD_ATTR, false );
217                     
218                     if ( methodName.length() > 0 )
219                     {
220                         MethodDeclaration[] allMethods = CompilerUtils.getClassMethods( outerType, null );
221                         
222                         for ( int i = 0; i < allMethods.length; i++ )
223                         {
224                             MethodDeclaration method = allMethods[i];
225                             AnnotationInstance exHandlerAnnotation =
226                                 CompilerUtils.getAnnotation( method, EXCEPTION_HANDLER_TAG_NAME );
227                             
228                             if ( exHandlerAnnotation != null && method.getSimpleName().equals( methodName ) )
229                             {
230                                 Collection JavaDoc forwardAnnotations =
231                                         CompilerUtils.getAnnotationArray( exHandlerAnnotation, FORWARDS_ATTR, false );
232                                 
233                                 for ( Iterator JavaDoc i3 = forwardAnnotations.iterator(); i3.hasNext(); )
234                                 {
235                                     AnnotationInstance forwardAnnotation = ( AnnotationInstance ) i3.next();
236                                     additionalEntities.add( forwardAnnotation );
237                                 }
238                             }
239                         }
240                     }
241                     
242                 }
243             }
244         }
245
246         protected String JavaDoc getErrorMessageExtraInfo()
247         {
248             return CATCH_TAG_NAME;
249         }
250
251         
252         protected boolean allowExactDuplicates()
253         {
254             return true;
255         }
256     }
257 }
258
Popular Tags