KickJava   Java API By Example, From Geeks To Geeks.

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


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.FatalCompileTimeException;
26 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
27 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeElementDeclaration;
28 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
29 import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
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.TypeInstance;
33
34 import java.util.Collection JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37
38 public class SimpleActionGrammar
39         extends ActionGrammar
40 {
41     private static String JavaDoc[][] REQUIRED_ATTRS =
42             {
43                 { NAME_ATTR },
44                 { PATH_ATTR, TILES_DEFINITION_ATTR, RETURN_ACTION_ATTR, NAVIGATE_TO_ATTR, FORWARD_REF_ATTR, ACTION_ATTR }
45             };
46     
47     private static String JavaDoc[][] MUTUALLY_EXCLUSIVE_ATTRS =
48             {
49                 { USE_FORM_BEAN_ATTR, USE_FORM_BEAN_TYPE_ATTR }
50             };
51     
52     
53     private ForwardGrammar _forwardGrammar;
54     
55     
56     public SimpleActionGrammar( AnnotationProcessorEnvironment env, Diagnostics diags, RuntimeVersionChecker rvc,
57                                 FlowControllerInfo fcInfo )
58     {
59         super( env, diags, rvc, fcInfo );
60         
61         addMemberType( FORWARD_REF_ATTR, new ForwardRefType() );
62         addMemberArrayGrammar( CONDITIONAL_FORWARDS_ATTR,
63                                new SimpleActionForwardGrammar( env, diags, null, rvc, fcInfo ) );
64         
65         //
66
// The rest of the attributes are checked by ForwardGrammar.
67
//
68
_forwardGrammar = new SimpleActionGrammarPart2();
69     }
70
71     public String JavaDoc[][] getMutuallyExclusiveAttrs()
72     {
73         return MUTUALLY_EXCLUSIVE_ATTRS;
74     }
75
76     protected boolean onBeginCheck( AnnotationInstance annotation, AnnotationInstance[] parentAnnotations,
77                                     MemberDeclaration classMember )
78             throws FatalCompileTimeException
79     {
80         String JavaDoc name = CompilerUtils.getString( annotation, NAME_ATTR, false );
81
82         TypeDeclaration outerClass = CompilerUtils.getOuterClass( classMember );
83         if ( WebappPathOrActionType.actionExists( name, outerClass, annotation, getEnv(), getFlowControllerInfo(), false ) )
84         {
85             if ( ! UniqueValueType.alreadyAddedErrorForValue( classMember, annotation, name, getEnv() ) )
86             {
87                 addError( annotation, "error.duplicate-action", new Object JavaDoc[]{ name } );
88             }
89         }
90         
91         //
92
// A simple action is really like a combination of an action and a forward. We extend ActionGrammar and
93
// then delegate to a ForwardGrammar here.
94
//
95
_forwardGrammar.check( annotation, parentAnnotations, classMember );
96         
97         return super.onBeginCheck( annotation, parentAnnotations, classMember );
98     }
99     
100     protected String JavaDoc getActionName( AnnotationInstance annotation, MemberDeclaration classMember )
101     {
102         return CompilerUtils.getString( annotation, NAME_ATTR, false );
103     }
104     
105     protected TypeInstance getFormBeanType( AnnotationInstance annotation, MemberDeclaration classMember )
106     {
107         // for a SimpleAction, the form bean type is wholly defined by the useFormBean attribute.
108
return getUseFormBeanType( annotation, classMember );
109     }
110     
111     private static class SimpleActionForwardGrammar
112             extends ForwardGrammar
113     {
114         private static String JavaDoc[][] REQUIRED_SIMPLEACTION_ATTRS =
115                 {
116                     { PATH_ATTR, TILES_DEFINITION_ATTR, RETURN_ACTION_ATTR, NAVIGATE_TO_ATTR, ACTION_ATTR }
117                 };
118         
119         public SimpleActionForwardGrammar( AnnotationProcessorEnvironment env, Diagnostics diags,
120                                            String JavaDoc requiredRuntimeVersion, RuntimeVersionChecker runtimeVersionChecker,
121                                            FlowControllerInfo fcInfo )
122         {
123             super( env, diags, requiredRuntimeVersion, runtimeVersionChecker, fcInfo );
124         }
125
126         public String JavaDoc[][] getRequiredAttrs()
127         {
128             return REQUIRED_SIMPLEACTION_ATTRS;
129         }
130
131         protected AnnotationMemberType getNameType()
132         {
133             return new SimpleActionForwardNameType();
134         }
135         
136         private class SimpleActionForwardNameType
137             extends ForwardNameType
138         {
139             public SimpleActionForwardNameType()
140             {
141                 super( CONDITIONAL_FORWARDS_ATTR );
142             }
143         }
144     }
145     
146     private class ForwardRefType
147         extends AnnotationMemberType
148     {
149         public ForwardRefType()
150         {
151             super( null, SimpleActionGrammar.this );
152         }
153
154         
155         public Object JavaDoc onCheck( AnnotationTypeElementDeclaration valueDecl, AnnotationValue member,
156                                AnnotationInstance[] parentAnnotations, MemberDeclaration classMember,
157                                int annotationArrayIndex )
158         {
159             Collection JavaDoc forwards =
160                     getFlowControllerInfo().getMergedControllerAnnotation().getForwards();
161             String JavaDoc forwardName = ( String JavaDoc ) member.getValue();
162             
163             for ( Iterator JavaDoc ii = forwards.iterator(); ii.hasNext(); )
164             {
165                 AnnotationInstance forwardAnn = ( AnnotationInstance ) ii.next();
166                 if ( forwardName.equals( CompilerUtils.getString( forwardAnn, NAME_ATTR, true ) ) ) return null;
167             }
168             
169             // TODO: comment
170
if ( forwardName.equals( "_auto" ) ) return null;
171             
172             addError( member, "error.unresolvable-global-forward", forwardName );
173             return null;
174         }
175     }
176     
177     private class SimpleActionGrammarPart2
178             extends ForwardGrammar
179     {
180         public SimpleActionGrammarPart2()
181         {
182             super( SimpleActionGrammar.this.getEnv(), SimpleActionGrammar.this.getDiagnostics(), null,
183                    SimpleActionGrammar.this.getRuntimeVersionChecker(),
184                    SimpleActionGrammar.this.getFlowControllerInfo() );
185         }
186
187         public String JavaDoc[][] getRequiredAttrs()
188         {
189             return REQUIRED_ATTRS;
190         }
191     }
192 }
193
Popular Tags