KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > model > StrutsApp


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.model;
19
20 import org.apache.beehive.netui.compiler.model.schema.struts11.*;
21 import org.apache.beehive.netui.compiler.model.validation.ValidationModel;
22 import org.apache.beehive.netui.compiler.JpfLanguageConstants;
23 import org.apache.beehive.netui.compiler.FatalCompileTimeException;
24 import org.apache.xmlbeans.XmlObject;
25 import org.apache.xmlbeans.XmlException;
26 import org.apache.xmlbeans.XmlDocumentProperties;
27 import org.apache.xmlbeans.XmlCursor;
28 import org.apache.xmlbeans.XmlOptions;
29
30 import java.io.File JavaDoc;
31 import java.io.PrintStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Collections JavaDoc;
39 import java.util.Comparator JavaDoc;
40
41
42 public class StrutsApp
43         extends AbstractForwardContainer
44         implements ForwardContainer, ExceptionContainer, JpfLanguageConstants
45 {
46     //protected boolean _isRootApp = false;
47
private HashMap JavaDoc _actionMappings = new HashMap JavaDoc();
48     private ArrayList JavaDoc _exceptionCatches = new ArrayList JavaDoc();
49     private ArrayList JavaDoc _messageResources = new ArrayList JavaDoc();
50     private HashMap JavaDoc _formBeans = new HashMap JavaDoc();
51     private ValidationModel _validationModel;
52     private List JavaDoc _additionalValidatorConfigs;
53
54     private boolean _returnToPageDisabled = true;
55     private boolean _returnToActionDisabled = true;
56     private boolean _isNestedPageFlow = false;
57     private boolean _isLongLivedPageFlow = false;
58     private boolean _isSharedFlow = false;
59     /** Map of name to typename */
60     private Map JavaDoc _sharedFlows = null;
61     private String JavaDoc _controllerClassName = null;
62     private String JavaDoc _multipartHandlerClassName = null;
63     private List JavaDoc _tilesDefinitionsConfigs = null;
64
65
66     protected static final String JavaDoc DUPLICATE_ACTION_COMMENT = "Note that there is more than one action with path \"{0}\"."
67                                                            + " Use a form-qualified action path if this is not the "
68                                                            + "one you want.";
69     
70     protected static final String JavaDoc PAGEFLOW_REQUESTPROCESSOR_CLASSNAME
71                                    = PAGEFLOW_PACKAGE + ".PageFlowRequestProcessor";
72
73     protected static final String JavaDoc PAGEFLOW_CONTROLLER_CONFIG_CLASSNAME
74                                    = PAGEFLOW_PACKAGE + ".config.PageFlowControllerConfig";
75
76     protected static final String JavaDoc STRUTS_CONFIG_PREFIX = "jpf-struts-config";
77     protected static final String JavaDoc STRUTS_CONFIG_EXTENSION = ".xml";
78     protected static final char STRUTS_CONFIG_SEPARATOR = '-';
79     protected static final String JavaDoc WEBINF_DIR_NAME = "WEB-INF";
80     protected static final String JavaDoc STRUTSCONFIG_OUTPUT_DIR = '/' + WEBINF_DIR_NAME + "/.pageflow-struts-generated";
81     protected static final String JavaDoc VALIDATOR_PLUG_IN_CLASSNAME = STRUTS_PACKAGE + ".validator.ValidatorPlugIn";
82     protected static final String JavaDoc VALIDATOR_PATHNAMES_PROPERTY = "pathnames";
83     protected static final String JavaDoc TILES_PLUG_IN_CLASSNAME = STRUTS_PACKAGE + ".tiles.TilesPlugin";
84     protected static final String JavaDoc TILES_DEFINITIONS_CONFIG_PROPERTY = "definitions-config";
85     protected static final String JavaDoc TILES_MODULE_AWARE_PROPERTY = "moduleAware";
86     protected static final String JavaDoc NETUI_VALIDATOR_RULES_URI = '/' + WEBINF_DIR_NAME + "/beehive-netui-validator-rules.xml";
87     protected static final String JavaDoc STRUTS_VALIDATOR_RULES_URI = '/' + WEBINF_DIR_NAME + "/validator-rules.xml";
88
89     
90     public StrutsApp( String JavaDoc controllerClassName )
91     {
92         super( null );
93         setParentApp( this );
94         _controllerClassName = controllerClassName;
95         
96         //
97
// Add a reference for the default validation message resources (in beehive-netui-pageflow.jar).
98
//
99
MessageResourcesModel mrm = new MessageResourcesModel( this );
100         mrm.setParameter( DEFAULT_VALIDATION_MESSAGE_BUNDLE );
101         mrm.setKey( DEFAULT_VALIDATION_MESSAGE_BUNDLE_NAME );
102         mrm.setReturnNull( true );
103         addMessageResources( mrm );
104     }
105
106     public void addMessageResources( MessageResourcesModel mr )
107     {
108         _messageResources.add( mr );
109     }
110     
111     private void addDisambiguatedActionMapping( ActionModel mapping )
112     {
113         if ( mapping.getFormBeanName() != null )
114         {
115             String JavaDoc qualifiedPath = getFormQualifiedActionPath( mapping );
116             ActionModel qualifiedMapping = new ActionModel( mapping, qualifiedPath );
117             qualifiedMapping.setUnqualifiedActionPath( mapping.getPath() );
118             _actionMappings.put( qualifiedPath, qualifiedMapping );
119         }
120     }
121     
122     /**
123      * Adds a new ActionMapping to this StrutsApp.
124      */

125     public void addActionMapping( ActionModel mapping )
126     {
127         String JavaDoc mappingPath = mapping.getPath();
128         ActionModel conflictingActionMapping = ( ActionModel ) _actionMappings.get( mappingPath );
129         
130         if ( conflictingActionMapping != null )
131         {
132             ActionModel defaultMappingForThisPath = conflictingActionMapping;
133             
134             //
135
// If the new action mapping takes no form, then it has the highest precedence, and replaces the existing
136
// "natural" mapping for the given path. Otherwise, replace the existing one if the existing one has a
137
// form bean and if the new mapping's form bean type comes alphabetically before the existing one's.
138
//
139
if ( mapping.getFormBeanName() == null
140                  || ( conflictingActionMapping.getFormBeanName() != null
141                       && getBeanType( mapping ).compareTo( getBeanType( conflictingActionMapping ) ) < 0 ) )
142             {
143                 _actionMappings.put( mappingPath, mapping );
144                 defaultMappingForThisPath = mapping;
145                 conflictingActionMapping.setOverloaded( false );
146             }
147             
148             addDisambiguatedActionMapping( mapping );
149             addDisambiguatedActionMapping( conflictingActionMapping );
150             defaultMappingForThisPath.setOverloaded( true );
151             defaultMappingForThisPath.setComment( DUPLICATE_ACTION_COMMENT.replaceAll( "\\{0\\}", mappingPath ) ); // @TODO I18N
152
}
153         else
154         {
155             _actionMappings.put( mappingPath, mapping );
156         }
157     }
158
159     
160     //
161
// Returns either the "form class" (specified for non-ActionForm-derived bean types), or the type of the
162
// associated FormBeanModel.
163
//
164
public String JavaDoc getBeanType( ActionModel actionMapping )
165     {
166         String JavaDoc beanType = actionMapping.getFormClass(); // will be non-null for non-ActionForm-derived types
167

168         if ( beanType == null )
169         {
170             FormBeanModel bean = getFormBean( actionMapping.getFormBeanName() );
171             assert bean != null;
172             beanType = bean.getType();
173         }
174         
175         return beanType;
176     }
177     
178     protected String JavaDoc getFormQualifiedActionPath( ActionModel action )
179     {
180         assert action.getFormBeanName() != null : "action " + action.getPath() + " has no form bean";
181         String JavaDoc beanType = getBeanType( action );
182         return action.getPath() + '_' + makeFullyQualifiedBeanName( beanType );
183     }
184     
185     /**
186      * Implemented for {@link ExceptionContainer}.
187      */

188     public void addException( ExceptionModel c )
189     {
190         _exceptionCatches.add( c );
191     }
192
193     /**
194      * Returns all of the form beans that are defined for this
195      * StrutsApp.
196      */

197     public FormBeanModel[] getFormBeans()
198     {
199         return ( FormBeanModel[] ) getFormBeansAsList().toArray( new FormBeanModel[0] );
200     }
201
202     /**
203      * Returns a list of all the form beans that are defined for this StrutsApp.
204      */

205     public List JavaDoc getFormBeansAsList()
206     {
207         ArrayList JavaDoc retList = new ArrayList JavaDoc();
208         
209         for ( Iterator JavaDoc i = _formBeans.values().iterator(); i.hasNext(); )
210         {
211             FormBeanModel fb = ( FormBeanModel ) i.next();
212             if ( fb != null ) retList.add( fb );
213         }
214         
215         return retList;
216     }
217
218     public FormBeanModel getFormBean( String JavaDoc formBeanName )
219     {
220         return ( FormBeanModel ) _formBeans.get( formBeanName );
221     }
222
223     /**
224      * Returns a list of {@link FormBeanModel}.
225      */

226     public List JavaDoc getFormBeansByActualType( String JavaDoc actualTypeName, Boolean JavaDoc usesPageFlowScopedBean )
227     {
228         ArrayList JavaDoc beans = null;
229         
230         for ( Iterator JavaDoc i = _formBeans.values().iterator(); i.hasNext(); )
231         {
232             FormBeanModel formBean = ( FormBeanModel ) i.next();
233             
234             if ( formBean != null && formBean.getActualType().equals( actualTypeName )
235                  && ( usesPageFlowScopedBean == null || usesPageFlowScopedBean.booleanValue() == formBean.isPageFlowScoped() ) )
236             {
237                 if ( beans == null ) beans = new ArrayList JavaDoc();
238                 beans.add( formBean );
239             }
240         }
241
242         return beans;
243     }
244
245     /**
246      * Adds a new form bean to this StrutsApp.
247      */

248     public void addFormBean( FormBeanModel newFormBean )
249     {
250         _formBeans.put( newFormBean.getName(), newFormBean );
251     }
252
253     /**
254      * Delete the given form-bean.
255      */

256     public void deleteFormBean( FormBeanModel formBean )
257     {
258         _formBeans.remove( formBean.getName() );
259     }
260
261     public String JavaDoc getFormNameForType( String JavaDoc formType, boolean isPageFlowScoped )
262     {
263         //
264
// First try and create a form-bean name that is a camelcased version of the classname without all of its
265
// package/outer-class qualifiers. If one with that name already exists, munge the fully-qualified classname.
266
//
267
int lastQualifier = formType.lastIndexOf( '$' );
268         if ( lastQualifier == -1 ) lastQualifier = formType.lastIndexOf( '.' );
269
270         String JavaDoc formBeanName = formType.substring( lastQualifier + 1 );
271         formBeanName = Character.toLowerCase( formBeanName.charAt( 0 ) ) + formBeanName.substring( 1 );
272
273         //
274
// If there's a name conflict, we need to disambiguate.
275
//
276
if ( _formBeans.containsKey( formBeanName ) )
277         {
278             String JavaDoc conflictingName = formBeanName;
279             FormBeanModel conflictingBean = ( FormBeanModel ) _formBeans.get( conflictingName );
280             
281             //
282
// If the conflicting form bean has a different value for isPageFlowScoped(), we can simply add a suffix
283
// to this bean name.
284
//
285
if ( conflictingBean != null && isPageFlowScoped != conflictingBean.isPageFlowScoped() )
286             {
287                 formBeanName += isPageFlowScoped ? "_flowScoped" : "_nonFlowScoped";
288                 assert ! _formBeans.containsKey( formBeanName ); // we generate this name -- shouldn't conflict
289
return formBeanName;
290             }
291             
292             formBeanName = makeFullyQualifiedBeanName( formType );
293             
294             //
295
// Now look for the one we're conflicting with
296
//
297
if ( conflictingBean != null )
298             {
299                 String JavaDoc nonConflictingName = makeFullyQualifiedBeanName( conflictingBean.getType() );
300                 conflictingBean.setName( nonConflictingName );
301                 _formBeans.put( nonConflictingName, conflictingBean );
302                 
303                 //
304
// Now look for any action mappings that are using the conflicting name...
305
//
306
for ( Iterator JavaDoc i = _actionMappings.values().iterator(); i.hasNext(); )
307                 {
308                     ActionModel mapping = ( ActionModel ) i.next();
309                     
310                     if ( mapping.getFormBeanName() != null && mapping.getFormBeanName().equals( conflictingName ) )
311                     {
312                         mapping.setFormBeanName( nonConflictingName );
313                     }
314                 }
315             }
316             
317             _formBeans.put( conflictingName, null );
318         }
319
320         return formBeanName;
321     }
322
323     protected static String JavaDoc makeFullyQualifiedBeanName( String JavaDoc formType )
324     {
325         return formType.replace( '.', '_' ).replace( '$', '_' );
326     }
327
328     protected static class ActionMappingComparator implements Comparator JavaDoc
329     {
330         public int compare( Object JavaDoc o1, Object JavaDoc o2 )
331         {
332             assert o1 instanceof ActionModel && o2 instanceof ActionModel;
333             
334             ActionModel am1 = ( ActionModel ) o1;
335             ActionModel am2 = ( ActionModel ) o2;
336             
337             assert ! am1.getPath().equals( am2.getPath() ); // there should be no duplicate paths
338
return am1.getPath().compareTo( am2.getPath() );
339         }
340     }
341
342     protected ForwardDocument.Forward addNewForward( XmlObject xmlObject )
343     {
344         return ( ( GlobalForwardsDocument.GlobalForwards ) xmlObject ).addNewForward();
345     }
346
347     protected Map JavaDoc getFormBeansMap()
348     {
349         return _formBeans;
350     }
351     
352     protected List JavaDoc getExceptionCatchesList()
353     {
354         return _exceptionCatches;
355     }
356     
357     protected List JavaDoc getSortedActionMappings()
358     {
359         ArrayList JavaDoc sortedActionMappings = new ArrayList JavaDoc();
360         sortedActionMappings.addAll( _actionMappings.values() );
361         Collections.sort( sortedActionMappings, new ActionMappingComparator() );
362         return sortedActionMappings;
363     }
364     
365     protected List JavaDoc getMessageResourcesList()
366     {
367         return _messageResources;
368     }
369     
370     /**
371      * Get the MessageResourcesModel for which no "key" is set (the default one used at runtime).
372      */

373     public MessageResourcesModel getDefaultMessageResources()
374     {
375         for ( java.util.Iterator JavaDoc ii = _messageResources.iterator(); ii.hasNext(); )
376         {
377             MessageResourcesModel i = ( MessageResourcesModel ) ii.next();
378             if ( i.getKey() == null ) return i;
379         }
380         
381         return null;
382     }
383     
384     public boolean isReturnToPageDisabled()
385     {
386         return _returnToPageDisabled;
387     }
388
389     public boolean isReturnToActionDisabled()
390     {
391         return _returnToActionDisabled;
392     }
393     
394     public void setReturnToPageDisabled( boolean disabled )
395     {
396         _returnToPageDisabled = disabled;
397     }
398     
399     public void setReturnToActionDisabled( boolean disabled )
400     {
401         _returnToActionDisabled = disabled;
402     }
403     
404     public void setAdditionalValidatorConfigs( List JavaDoc additionalValidatorConfigs )
405     {
406         if ( additionalValidatorConfigs != null && ! additionalValidatorConfigs.isEmpty() )
407         {
408             _additionalValidatorConfigs = additionalValidatorConfigs;
409         }
410     }
411     
412     public void setValidationModel( ValidationModel validationModel )
413     {
414         if ( ! validationModel.isEmpty() ) // if there's nothing in the validation model, we don't care about it.
415
{
416             _validationModel = validationModel;
417         }
418     }
419     
420     public void writeXml( PrintStream JavaDoc outputStream, File JavaDoc mergeFile, String JavaDoc webappBuildRoot )
421         throws IOException JavaDoc, XmlException, FatalCompileTimeException
422     {
423         StrutsConfigDocument doc;
424         
425         if ( mergeFile != null && mergeFile.canRead() )
426         {
427             doc = StrutsConfigDocument.Factory.parse( mergeFile );
428         }
429         else
430         {
431             doc = StrutsConfigDocument.Factory.newInstance();
432         }
433
434         
435         //
436
// Write the DOCTYPE and all that good stuff.
437
//
438
XmlDocumentProperties docProps = doc.documentProperties();
439         
440         if ( docProps.getDoctypeName() == null )
441         {
442             docProps.setDoctypeName( "struts-config" ); // NOI18N
443
}
444         
445         if ( docProps.getDoctypePublicId() == null )
446         {
447             docProps.setDoctypePublicId( "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" ); // NOI18N
448
}
449         
450         if ( docProps.getDoctypeSystemId() == null )
451         {
452             docProps.setDoctypeSystemId( "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd" ); // NOI18N
453
}
454         
455         
456         //
457
// struts-config
458
//
459
StrutsConfigDocument.StrutsConfig scElement = doc.getStrutsConfig();
460         
461         if ( scElement == null )
462         {
463             scElement = doc.addNewStrutsConfig();
464         }
465         
466         //
467
// Write the "generated by" comment.
468
//
469
XmlCursor curs = scElement.newCursor();
470         String JavaDoc headerComment = getHeaderComment( mergeFile );
471         if ( headerComment != null ) curs.insertComment( headerComment );
472         
473         //
474
// form-beans
475
//
476
writeFormBeans( scElement );
477         
478         
479         //
480
// global-exceptions
481
//
482
writeExceptions( scElement );
483         
484         
485         //
486
// global-forwards
487
//
488
GlobalForwardsDocument.GlobalForwards globalForwards = scElement.getGlobalForwards();
489         
490         if ( globalForwards == null )
491         {
492             globalForwards = scElement.addNewGlobalForwards();
493         }
494         writeForwards( globalForwards.getForwardArray(), globalForwards );
495         
496         
497         //
498
// action-mappings
499
//
500
writeActionMappings( scElement );
501
502         
503         //
504
// message-resources
505
//
506
writeMessageResources( scElement );
507         
508                 
509         //
510
// request-processor
511
//
512
writeControllerElement( scElement );
513         
514         
515         //
516
// ValidatorPlugIn
517
//
518
writeValidatorInit( scElement );
519         
520         
521         //
522
// TilesPlugin
523
//
524
writeTilesInit( scElement );
525
526
527         //
528
// Write the file.
529
//
530
XmlOptions options = new XmlOptions();
531         options.setSavePrettyPrint();
532         doc.save( outputStream, options );
533     }
534
535     private void writeMessageResources( StrutsConfigDocument.StrutsConfig scElement )
536     {
537         MessageResourcesDocument.MessageResources[] existingMessageResources = scElement.getMessageResourcesArray();
538         
539         for ( Iterator JavaDoc i = getMessageResourcesList().iterator(); i.hasNext(); )
540         {
541             MessageResourcesModel mr = ( MessageResourcesModel ) i.next();
542             
543             if ( mr != null )
544             {
545                 MessageResourcesDocument.MessageResources mrToEdit = null;
546                 
547                 for ( int j = 0; j < existingMessageResources.length; ++j )
548                 {
549                     String JavaDoc existingKey = existingMessageResources[j].getKey();
550                     
551                     if ( ( existingKey == null && mr.getKey() == null )
552                          || ( existingKey != null && mr.getKey() != null && existingKey.equals( mr.getKey() ) ) )
553                     {
554                         mrToEdit = existingMessageResources[j];
555                         break;
556                     }
557                     
558                 }
559
560                 if ( mrToEdit == null )
561                 {
562                     mrToEdit = scElement.addNewMessageResources();
563                 }
564
565                 mr.writeToXMLBean( mrToEdit );
566             }
567         }
568     }
569     
570     private void writeActionMappings( StrutsConfigDocument.StrutsConfig scElement )
571     {
572         ActionMappingsDocument.ActionMappings actionMappings = scElement.getActionMappings();
573         
574         if ( actionMappings == null )
575         {
576             actionMappings = scElement.addNewActionMappings();
577         }
578         
579         ActionDocument.Action[] existingActions = actionMappings.getActionArray();
580         List JavaDoc actionMappingsList = getSortedActionMappings();
581         
582         for ( int i = 0; i < actionMappingsList.size(); ++i )
583         {
584             ActionModel am = ( ActionModel ) actionMappingsList.get( i );
585             ActionDocument.Action actionMappingToEdit = null;
586                 
587             for ( int j = 0; j < existingActions.length; ++j )
588             {
589                 if ( am.getPath().equals( existingActions[j].getPath() ) )
590                 {
591                     actionMappingToEdit = existingActions[j];
592                     break;
593                 }
594             }
595                 
596             if ( actionMappingToEdit == null )
597             {
598                 actionMappingToEdit = actionMappings.addNewAction();
599             }
600                 
601             am.writeToXMLBean( actionMappingToEdit );
602         }
603     }
604     
605     private void writeExceptions( StrutsConfigDocument.StrutsConfig scElement )
606     {
607         GlobalExceptionsDocument.GlobalExceptions globalExceptions = scElement.getGlobalExceptions();
608         
609         if ( globalExceptions == null )
610         {
611             globalExceptions = scElement.addNewGlobalExceptions();
612         }
613         
614         List JavaDoc exceptionCatches = getExceptionCatchesList();
615         if ( exceptionCatches != null && ! exceptionCatches.isEmpty() )
616         {
617             ExceptionDocument.Exception[] existingExceptions = globalExceptions.getExceptionArray();
618             
619             for ( int i = 0; i < exceptionCatches.size(); ++i )
620             {
621                 ExceptionModel ec = ( ExceptionModel ) exceptionCatches.get( i );
622                 ExceptionDocument.Exception exceptionToEdit = null;
623                 
624                 for ( int j = 0; j < existingExceptions.length; ++j )
625                 {
626                     if ( ec.getType().equals( existingExceptions[j].getType() ) )
627                     {
628                         exceptionToEdit = existingExceptions[j];
629                         break;
630                     }
631                 }
632                 
633                 if ( exceptionToEdit == null )
634                 {
635                     exceptionToEdit = globalExceptions.addNewException();
636                 }
637                 
638                 ec.writeToXMLBean( exceptionToEdit );
639             }
640         }
641     }
642     
643     private void writeFormBeans( StrutsConfigDocument.StrutsConfig scElement )
644     {
645         FormBeansDocument.FormBeans formBeans = scElement.getFormBeans();
646         
647         if ( formBeans == null )
648         {
649             formBeans = scElement.addNewFormBeans();
650         }
651         
652         FormBeanDocument.FormBean[] existingBeans = formBeans.getFormBeanArray();
653         
654         for ( Iterator JavaDoc i = getFormBeansMap().values().iterator(); i.hasNext(); )
655         {
656             FormBeanModel fb = ( FormBeanModel ) i.next();
657             
658             if ( fb != null )
659             {
660                 FormBeanDocument.FormBean formBeanToEdit = null;
661                 
662                 for ( int j = 0; j < existingBeans.length; ++j )
663                 {
664                     if ( existingBeans[j].getName().equals( fb.getName() ) )
665                     {
666                         formBeanToEdit = existingBeans[j];
667                         break;
668                     }
669                     
670                 }
671
672                 if ( formBeanToEdit == null )
673                 {
674                     formBeanToEdit = formBeans.addNewFormBean();
675                 }
676
677                 fb.writeToXMLBean( formBeanToEdit );
678             }
679         }
680     }
681     
682     protected void writeControllerElement( StrutsConfigDocument.StrutsConfig scElement )
683     {
684         ControllerDocument.Controller controller = scElement.getController();
685         if ( controller == null ) controller = scElement.addNewController();
686         
687         if ( controller.getProcessorClass() == null )
688         {
689             controller.setProcessorClass( PAGEFLOW_REQUESTPROCESSOR_CLASSNAME );
690         }
691         
692         if ( controller.getInputForward() == null )
693         {
694             controller.setInputForward( ControllerDocument.Controller.InputForward.TRUE );
695         }
696         
697         if ( _multipartHandlerClassName != null && controller.getMultipartClass() == null )
698         {
699             controller.setMultipartClass( _multipartHandlerClassName );
700         }
701         
702         if ( _isNestedPageFlow ) addSetProperty( controller, "isNestedPageFlow", "true" );
703         if ( _isLongLivedPageFlow ) addSetProperty( controller, "isLongLivedPageFlow", "true" );
704         if ( _isSharedFlow ) addSetProperty( controller, "isSharedFlow", "true" );
705         if ( isReturnToPageDisabled() ) addSetProperty( controller, "isReturnToPageDisabled", "true" );
706         if ( isReturnToActionDisabled() ) addSetProperty( controller, "isReturnToActionDisabled", "true" );
707         
708         if ( _sharedFlows != null )
709         {
710             StringBuffer JavaDoc str = new StringBuffer JavaDoc();
711             boolean first = true;
712             
713             for ( java.util.Iterator JavaDoc ii = _sharedFlows.entrySet().iterator(); ii.hasNext(); )
714             {
715                 Map.Entry JavaDoc entry = ( Map.Entry JavaDoc ) ii.next();
716                 if ( ! first ) str.append( ',' );
717                 first = false;
718                 String JavaDoc name = ( String JavaDoc ) entry.getKey();
719                 String JavaDoc type = ( String JavaDoc ) entry.getValue();
720                 str.append( name ).append( '=' ).append( type );
721             }
722             
723            addSetProperty( controller, "sharedFlows", str.toString() );
724         }
725         
726         addSetProperty( controller, "controllerClass", _controllerClassName );
727         
728         //
729
// If there is not a default MessageResources element in the generated XML, add a special set-property
730
// to communicate this to the runtime.
731
//
732
MessageResourcesDocument.MessageResources[] mrArray = scElement.getMessageResourcesArray();
733         for ( int i = 0; i < mrArray.length; i++ )
734         {
735             MessageResourcesDocument.MessageResources messageResources = mrArray[i];
736             if ( messageResources.getKey() == null ) return;
737         }
738         addSetProperty( controller, "isMissingDefaultMessages", "true" );
739     }
740     
741     protected static void addSetProperty( ControllerDocument.Controller controller, String JavaDoc propName, String JavaDoc propValue )
742     {
743         if ( controller.getClassName() == null ) controller.setClassName( PAGEFLOW_CONTROLLER_CONFIG_CLASSNAME );
744         SetPropertyDocument.SetProperty prop = controller.addNewSetProperty();
745         prop.setProperty( propName );
746         prop.setValue( propValue );
747     }
748     
749     protected void writeValidatorInit( StrutsConfigDocument.StrutsConfig scElement )
750     {
751         if ( ( _validationModel != null && ! _validationModel.isEmpty() ) || _additionalValidatorConfigs != null )
752         {
753             PlugInDocument.PlugIn plugInElementToEdit = null;
754             PlugInDocument.PlugIn[] existingPlugIns = scElement.getPlugInArray();
755             
756             for ( int i = 0; i < existingPlugIns.length; i++ )
757             {
758                 PlugInDocument.PlugIn existingPlugIn = existingPlugIns[i];
759                 
760                 if ( VALIDATOR_PLUG_IN_CLASSNAME.equals( existingPlugIn.getClassName() ) )
761                 {
762                     plugInElementToEdit = existingPlugIn;
763                     break;
764                 }
765             }
766             
767             if ( plugInElementToEdit == null )
768             {
769                 plugInElementToEdit = scElement.addNewPlugIn();
770                 plugInElementToEdit.setClassName( VALIDATOR_PLUG_IN_CLASSNAME );
771             }
772             
773             SetPropertyDocument.SetProperty[] existingSetProperties = plugInElementToEdit.getSetPropertyArray();
774             
775             for ( int i = 0; i < existingSetProperties.length; i++ )
776             {
777                 if ( VALIDATOR_PATHNAMES_PROPERTY.equals( existingSetProperties[i].getProperty() ) )
778                 {
779                     //
780
// This means that in the user's struts-merge file, there's already a "pathnames" set-property
781
// element. We don't want to overwrite it.
782
//
783
return;
784                 }
785             }
786             
787             SetPropertyDocument.SetProperty pathnamesProperty = plugInElementToEdit.addNewSetProperty();
788             pathnamesProperty.setProperty( VALIDATOR_PATHNAMES_PROPERTY );
789             StringBuffer JavaDoc pathNames = new StringBuffer JavaDoc();
790             pathNames.append( NETUI_VALIDATOR_RULES_URI );
791             pathNames.append( ',' ).append( STRUTS_VALIDATOR_RULES_URI );
792             
793             if ( _validationModel != null && ! _validationModel.isEmpty() )
794             {
795                 pathNames.append( ',' ).append( _validationModel.getOutputFileURI() );
796             }
797             
798             if ( _additionalValidatorConfigs != null )
799             {
800                 for ( java.util.Iterator JavaDoc ii = _additionalValidatorConfigs.iterator(); ii.hasNext(); )
801                 {
802                     String JavaDoc configFile = ( String JavaDoc ) ii.next();
803                     pathNames.append( ',' ).append( configFile );
804                 }
805             }
806             
807             pathnamesProperty.setValue( pathNames.toString() );
808         }
809     }
810     
811     protected void writeTilesInit( StrutsConfigDocument.StrutsConfig scElement )
812     {
813         if ( _tilesDefinitionsConfigs == null || _tilesDefinitionsConfigs.isEmpty() )
814         {
815             return;
816         }
817
818         PlugInDocument.PlugIn plugInElementToEdit = null;
819         PlugInDocument.PlugIn[] existingPlugIns = scElement.getPlugInArray();
820
821         for ( int i = 0; i < existingPlugIns.length; i++ )
822         {
823             PlugInDocument.PlugIn existingPlugIn = existingPlugIns[i];
824
825             if ( TILES_PLUG_IN_CLASSNAME.equals( existingPlugIn.getClassName() ) )
826             {
827                 plugInElementToEdit = existingPlugIn;
828                 break;
829             }
830         }
831
832         if ( plugInElementToEdit == null )
833         {
834             plugInElementToEdit = scElement.addNewPlugIn();
835             plugInElementToEdit.setClassName( TILES_PLUG_IN_CLASSNAME );
836         }
837
838         boolean definitionsConfigIsSet = false;
839         boolean moduleAwarePropertyIsSet = false;
840         SetPropertyDocument.SetProperty[] existingSetProperties = plugInElementToEdit.getSetPropertyArray();
841
842         for ( int i = 0; i < existingSetProperties.length; i++ )
843         {
844             String JavaDoc name = existingSetProperties[i].getProperty();
845
846             if ( TILES_DEFINITIONS_CONFIG_PROPERTY.equals( name ) )
847             {
848                 //
849
// This means that in the user's struts-merge file, there's already a
850
// "definitions-config" set-property element. We don't want to overwrite it.
851
//
852
definitionsConfigIsSet = true;
853             }
854
855             if ( TILES_MODULE_AWARE_PROPERTY.equals( name ) )
856             {
857                 // Make sure "moduleAware" is true
858
moduleAwarePropertyIsSet = true;
859             }
860         }
861
862         if ( !definitionsConfigIsSet )
863         {
864             SetPropertyDocument.SetProperty pathnamesProperty = plugInElementToEdit.addNewSetProperty();
865             pathnamesProperty.setProperty( TILES_DEFINITIONS_CONFIG_PROPERTY );
866             StringBuffer JavaDoc pathNames = new StringBuffer JavaDoc();
867             boolean firstOne = true;
868
869             for ( java.util.Iterator JavaDoc ii = _tilesDefinitionsConfigs.iterator(); ii.hasNext(); )
870             {
871                 String JavaDoc definitionsConfig = ( String JavaDoc ) ii.next();
872                 if ( ! firstOne ) pathNames.append( ',' );
873                 firstOne = false;
874                 pathNames.append( definitionsConfig );
875             }
876             pathnamesProperty.setValue( pathNames.toString() );
877         }
878
879         if ( !moduleAwarePropertyIsSet )
880         {
881             SetPropertyDocument.SetProperty pathnamesProperty = plugInElementToEdit.addNewSetProperty();
882             pathnamesProperty.setProperty( TILES_MODULE_AWARE_PROPERTY );
883             pathnamesProperty.setValue( "true" );
884         }
885     }
886
887     protected String JavaDoc getHeaderComment( File JavaDoc mergeFile )
888             throws FatalCompileTimeException
889     {
890         return null;
891     }
892        
893     public void setNestedPageFlow( boolean nestedPageFlow )
894     {
895         _isNestedPageFlow = nestedPageFlow;
896     }
897
898     public void setLongLivedPageFlow( boolean longLivedPageFlow )
899     {
900         _isLongLivedPageFlow = longLivedPageFlow;
901     }
902     
903     protected static String JavaDoc getStrutsConfigURI( String JavaDoc containingPackage, boolean isSharedFlow )
904     {
905         return getOutputFileURI( STRUTS_CONFIG_PREFIX, containingPackage, isSharedFlow );
906     }
907     
908     public static String JavaDoc getOutputFileURI( String JavaDoc filePrefix, String JavaDoc containingPackage, boolean isSharedFlow )
909     {
910         StringBuffer JavaDoc fileName = new StringBuffer JavaDoc( STRUTSCONFIG_OUTPUT_DIR );
911         fileName.append( '/' ).append( filePrefix );
912         if ( containingPackage != null && containingPackage.length() > 0 ) fileName.append( STRUTS_CONFIG_SEPARATOR );
913         if ( isSharedFlow ) fileName.append( STRUTS_CONFIG_SEPARATOR );
914         if ( containingPackage != null ) fileName.append( containingPackage.replace( '.', STRUTS_CONFIG_SEPARATOR ) );
915         fileName.append( STRUTS_CONFIG_EXTENSION );
916         return fileName.toString();
917     }
918     
919     protected void setSharedFlow( boolean sharedFlow )
920     {
921         _isSharedFlow = sharedFlow;
922     }
923
924     protected void setSharedFlows( Map JavaDoc sharedFlows )
925     {
926         _sharedFlows = sharedFlows;
927     }
928     
929     public String JavaDoc getMultipartHandlerClassName()
930     {
931         return _multipartHandlerClassName;
932     }
933
934     protected void setMultipartHandlerClassName( String JavaDoc multipartHandlerClassName )
935     {
936         _multipartHandlerClassName = multipartHandlerClassName;
937     }
938
939     public void setTilesDefinitionsConfigs( List JavaDoc tilesDefinitionsConfigs )
940     {
941         _tilesDefinitionsConfigs = tilesDefinitionsConfigs;
942     }
943
944     protected boolean isSharedFlow()
945     {
946         return _isSharedFlow;
947     }
948 }
949
Popular Tags