KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > FlowControllerInfo


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;
19
20 import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration;
21 import org.apache.beehive.netui.compiler.typesystem.declaration.ClassDeclaration;
22 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
23 import org.apache.beehive.netui.compiler.typesystem.declaration.FieldDeclaration;
24 import org.apache.beehive.netui.compiler.typesystem.type.TypeInstance;
25 import org.apache.beehive.netui.compiler.typesystem.type.DeclaredType;
26 import org.apache.beehive.netui.compiler.typesystem.env.AnnotationProcessorEnvironment;
27
28 import java.util.*;
29 import java.io.File JavaDoc;
30
31
32 public class FlowControllerInfo
33         extends SourceFileInfo
34         implements JpfLanguageConstants
35 {
36     private static final ActionInfo[] EMPTY_ACTION_INFO_ARRAY = new ActionInfo[0];
37     
38     private Set _actions = new HashSet();
39     private Set _returnActions = null;
40     private Map _sharedFlowTypes = Collections.EMPTY_MAP;
41     private Map _sharedFlowTypeNames = Collections.EMPTY_MAP;
42     private Map _sharedFlowFiles = Collections.EMPTY_MAP;
43     private List _referencedFiles = new ArrayList();
44     private boolean _isBuilding = false;
45     private Map _messageBundlesByName = new HashMap();
46     private boolean _navigateToActionEnabled = false;
47     private boolean _navigateToPageEnabled = false;
48     private boolean _isNested;
49     private MergedControllerAnnotation _mergedControllerAnnotation;
50
51     
52     public static class ActionInfo
53     {
54         private String JavaDoc _name;
55         private String JavaDoc _beanType = null;
56
57         public ActionInfo( String JavaDoc name )
58         {
59             _name = name;
60         }
61         
62         public ActionInfo( String JavaDoc name, String JavaDoc beanType )
63         {
64             _name = name;
65             _beanType = beanType;
66         }
67         
68         public void setBeanType( String JavaDoc beanType )
69         {
70             _beanType = beanType;
71         }
72
73         public String JavaDoc getName()
74         {
75             return _name;
76         }
77
78         public String JavaDoc getBeanType()
79         {
80             return _beanType;
81         }
82         
83         public boolean equals( Object JavaDoc o )
84         {
85             if ( o == null || ! ( o instanceof ActionInfo ) )
86             {
87                 return false;
88             }
89             
90             ActionInfo other = ( ActionInfo ) o;
91             if ( ! _name.equals( other.getName() ) ) return false;
92             String JavaDoc otherBeanType = other.getBeanType();
93             return ( ( _beanType == null && otherBeanType == null )
94                      || ( _beanType != null && otherBeanType != null && _beanType.equals( otherBeanType ) ) );
95         }
96         
97         public int hashCode()
98         {
99             int nameHash = _name.hashCode();
100             if ( _beanType == null ) return nameHash;
101             return nameHash != 0 ? _beanType.hashCode() % nameHash : _beanType.hashCode();
102         }
103     }
104     
105     
106     public FlowControllerInfo( ClassDeclaration jclass )
107     {
108         super( CompilerUtils.getSourceFile( jclass, true ), jclass.getQualifiedName() );
109     }
110     
111     void startBuild( AnnotationProcessorEnvironment env, ClassDeclaration jclass )
112     {
113         _isBuilding = true;
114         _mergedControllerAnnotation = new MergedControllerAnnotation( jclass );
115         _isNested = _mergedControllerAnnotation.isNested();
116         setSharedFlowInfo( env );
117     }
118     
119     void endBuild()
120     {
121         _isBuilding = false;
122         _sharedFlowTypes = null; // don't hang onto ClassDeclarations
123
_mergedControllerAnnotation = null;
124     }
125     
126     public ActionInfo[] getActions()
127     {
128         return ( ActionInfo[] ) _actions.toArray( new ActionInfo[ _actions.size() ] );
129     }
130
131     public boolean isNested()
132     {
133         return _isNested;
134     }
135
136     public ActionInfo[] getReturnActions()
137     {
138         if ( _returnActions == null )
139         {
140             return EMPTY_ACTION_INFO_ARRAY;
141         }
142         
143         return ( ActionInfo[] ) _returnActions.toArray( new ActionInfo[ _returnActions.size() ] );
144     }
145     
146     public String JavaDoc getFormBeanType( String JavaDoc actionName )
147     {
148         String JavaDoc bestType = null;
149         
150         for ( Iterator ii = _actions.iterator(); ii.hasNext(); )
151         {
152             ActionInfo actionInfo = ( ActionInfo ) ii.next();
153             if ( actionInfo.getName().equals( actionName ) )
154             {
155                 String JavaDoc beanType = actionInfo.getBeanType();
156                 
157                 //
158
// In the case of overloaded actions, the non-form-bean action takes precedence. Otherwise,
159
// we look at the bean type names in alphabetical order.
160
//
161
if ( beanType == null ) return null;
162                 else if ( bestType == null ) bestType = beanType;
163                 else if ( beanType.compareTo( bestType ) < 0 ) bestType = beanType;
164             }
165         }
166         
167         return bestType;
168     }
169
170     int countReturnActions()
171     {
172         return _returnActions != null ? _returnActions.size() : 0;
173     }
174
175     public void addAction( String JavaDoc actionName, String JavaDoc formBeanType )
176     {
177         _actions.add( new ActionInfo( actionName, formBeanType ) );
178     }
179     
180     public void addReturnAction( String JavaDoc returnActionName, String JavaDoc formBeanType )
181     {
182         if ( _returnActions == null ) _returnActions = new HashSet();
183         _returnActions.add( new ActionInfo( returnActionName, formBeanType ) );
184     }
185     
186     /**
187      * Get a list of referenced files (files that appear in Jpf.Forward paths).
188      */

189     public List getReferencedFiles()
190     {
191         return _referencedFiles;
192     }
193     
194     public void addReferencedFile( File JavaDoc file )
195     {
196         if ( ! file.equals( getSourceFile() ) )
197         {
198             _referencedFiles.add( file );
199         }
200     }
201     
202     private void setSharedFlowInfo( AnnotationProcessorEnvironment env )
203     {
204         //
205
// First, find all referenced Shared Flow types.
206
//
207
_sharedFlowTypes = new LinkedHashMap();
208         
209         Collection sharedFlowRefs = _mergedControllerAnnotation.getSharedFlowRefs();
210         
211         if ( sharedFlowRefs != null )
212         {
213             for ( Iterator i = sharedFlowRefs.iterator(); i.hasNext(); )
214             {
215                 AnnotationInstance sharedFlowRef = ( AnnotationInstance ) i.next();
216                 String JavaDoc name = CompilerUtils.getString( sharedFlowRef, NAME_ATTR, true );
217                 TypeInstance type = CompilerUtils.getTypeInstance( sharedFlowRef, TYPE_ATTR, true );
218                 
219                 if ( type instanceof DeclaredType ) // if it's not a DeclaredType, the error will be caught elsewhere.
220
{
221                     TypeDeclaration typeDecl = ( ( DeclaredType ) type ).getDeclaration();
222                     
223                     if ( typeDecl != null ) // If the declaration is null, it's an error type.
224
{
225                         _sharedFlowTypes.put( name, typeDecl );
226                     }
227                 }
228             }
229         }
230         
231         //
232
// If there's no SharedFlowController, fall back to the deprecated Global.app.
233
//
234
if ( _sharedFlowTypes.isEmpty() )
235         {
236             TypeDeclaration type = env.getTypeDeclaration( GLOBALAPP_FULL_CLASSNAME );
237             if ( type != null ) _sharedFlowTypes.put( GLOBALAPP_SHARED_FLOW_NAME, type );
238         }
239   
240         _sharedFlowTypeNames = new LinkedHashMap();
241         _sharedFlowFiles = new LinkedHashMap();
242         
243         for ( Iterator i = _sharedFlowTypes.entrySet().iterator(); i.hasNext(); )
244         {
245             Map.Entry entry = ( Map.Entry ) i.next();
246             TypeDeclaration type = ( TypeDeclaration ) entry.getValue();
247             _sharedFlowTypeNames.put( entry.getKey(), type.getQualifiedName() );
248             File JavaDoc file = CompilerUtils.getSourceFile( type, false );
249             
250             if ( file != null )
251             {
252                 _sharedFlowFiles.put( entry.getKey(), file );
253                 _referencedFiles.add( file );
254             }
255         }
256     }
257     
258     public Map getSharedFlowTypes()
259     {
260         assert _isBuilding : "use getSharedFlowTypeNames after check or generate phases";
261         return _sharedFlowTypes;
262     }
263
264     public Map getSharedFlowTypeNames()
265     {
266         return _sharedFlowTypeNames;
267     }
268
269     public MergedControllerAnnotation getMergedControllerAnnotation()
270     {
271         assert _isBuilding : "only valid during the check or generate phases";
272         return _mergedControllerAnnotation;
273     }
274
275     public Map getMessageBundlesByName()
276     {
277         return _messageBundlesByName;
278     }
279
280     public void addMessageBundle( String JavaDoc bundleName, String JavaDoc bundlePath )
281     {
282         _messageBundlesByName.put( bundleName, bundlePath );
283     }
284     
285     public String JavaDoc getControllerClassName()
286     {
287         return getClassName();
288     }
289
290     public Map getSharedFlowFiles()
291     {
292         return _sharedFlowFiles;
293     }
294     
295     public void enableNavigateToAction()
296     {
297         _navigateToActionEnabled = true;
298     }
299     
300     public void enableNavigateToPage()
301     {
302         _navigateToPageEnabled = true;
303     }
304
305     public boolean isNavigateToActionEnabled()
306     {
307         return _navigateToActionEnabled;
308     }
309
310     public boolean isNavigateToPageEnabled()
311     {
312         return _navigateToPageEnabled;
313     }
314     
315     /**
316      * Add a return-action from an annotation.
317      * @return the form bean type, or null</code> if there is no form bean.
318      */

319     public TypeInstance addReturnAction( String JavaDoc returnActionName, AnnotationInstance annotation, TypeDeclaration outerType )
320     {
321         TypeInstance formBeanType = CompilerUtils.getTypeInstance( annotation, OUTPUT_FORM_BEAN_TYPE_ATTR, true );
322         
323         if ( formBeanType == null )
324         {
325             String JavaDoc memberFieldName = CompilerUtils.getString( annotation, OUTPUT_FORM_BEAN_ATTR, true );
326             
327             if ( memberFieldName != null )
328             {
329                 FieldDeclaration field = CompilerUtils.findField( outerType, memberFieldName );
330                 if ( field != null ) formBeanType = field.getType();
331             }
332         }
333         
334         String JavaDoc formTypeName =
335                 formBeanType != null && formBeanType instanceof DeclaredType
336                 ? CompilerUtils.getDeclaration( ( DeclaredType ) formBeanType ).getQualifiedName()
337                 : null;
338         addReturnAction( returnActionName, formTypeName );
339         return formBeanType;
340     }
341 }
342
Popular Tags