View Javadoc

1   /*
2    * $Id: Struts1Factory.java 478625 2006-11-23 17:31:52Z wsmoak $
3    * Copyright 2004 The Apache Software Foundation.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   */
18  package org.apache.struts2.s1;
19  
20  import com.opensymphony.xwork2.*;
21  import com.opensymphony.xwork2.config.Configuration;
22  import com.opensymphony.xwork2.config.entities.ActionConfig;
23  import com.opensymphony.xwork2.config.entities.ResultConfig;
24  import com.opensymphony.xwork2.config.entities.ExceptionMappingConfig;
25  
26  import org.apache.struts.Globals;
27  import org.apache.struts.action.*;
28  import org.apache.struts.config.*;
29  
30  import java.util.Iterator;
31  import java.util.Arrays;
32  import java.util.Map;
33  
34  import javax.servlet.ServletContext;
35  
36  
37  /***
38   *  Provides conversion methods between the Struts Action 1.x and XWork
39   *  classes.
40   */
41  public class Struts1Factory {
42      
43      private Configuration configuration;
44  
45      public Struts1Factory(Configuration config) {
46          this.configuration = config;
47      }
48      
49      /***
50       * Create a Struts 1.x ModuleConfig based on an XWork package configuration.
51       * 
52       * @param packageName the name of the XWork package configuration to wrap.  This becomes the module prefix for the
53       *     newly-created ModuleConfig.
54       * @return a wrapper Struts 1.x ModuleConfig.
55       */
56      public ModuleConfig createModuleConfig(String packageName) {
57          assert packageName != null;
58          return new WrapperModuleConfig(this, configuration.getPackageConfig(packageName));
59      }
60      
61      /***
62       * Create a Struts 1.x ActionMapping from an XWork ActionConfig.
63       * 
64       * @param cfg the XWork ActionConfig.
65       * @return a wrapper Struts 1.x ActionMapping.
66       */
67      public ActionMapping createActionMapping(ActionConfig cfg) {
68          assert cfg != null;
69          return new WrapperActionMapping(this, cfg);
70      }
71  
72      /***
73       * Create a Struts 1.x ActionMapping from an XWork ActionConfig.  This version provides an existing action path
74       * and ModuleConfig.  Package-protected for now; may not need to be exposed publicly.
75       * 
76       * @param cfg the XWork ActionConfig.
77       * @param actionPath the Struts 1.x-style action path ('/' + action-name).
78       * @param moduleConfig the Struts 1.x ModuleConfig that contains the ActionMapping.
79       * @return a wrapper Struts 1.x ActionMapping.
80       */
81      ActionMapping createActionMapping(ActionConfig cfg, String actionPath, ModuleConfig moduleConfig) {
82          assert cfg != null;
83          assert moduleConfig != null;
84          return new WrapperActionMapping(this, cfg, actionPath, moduleConfig);
85      }
86  
87      /***
88       * Create a Struts 1.x ActionForward from an XWork ResultConfig.
89       * 
90       * @param cfg the XWork ResultConfig.
91       * @return a wrapper Struts 1.x ActionMapping.
92       */
93      public ActionForward createActionForward(ResultConfig cfg) {
94          assert cfg != null;
95          return new WrapperActionForward(cfg);
96      }
97  
98      /***
99       * Create a Struts 1.x ExceptionConfig from an XWork ExceptionMappingConfig.
100      * 
101      * @param cfg the XWork ExceptionMappingConfig.
102      * @return a wrapper Struts 1.x ExceptionConfig.
103      */
104     public ExceptionConfig createExceptionConfig(ExceptionMappingConfig cfg) {
105         assert cfg != null;
106         return new WrapperExceptionConfig(cfg);
107     }
108 
109     public void convertErrors(ActionErrors errors, Object action) {
110         ValidationAware vaction = null;
111         TextProvider text = null;
112 
113         if (action instanceof ValidationAware) {
114             vaction = (ValidationAware)action;
115         }
116         if (action instanceof TextProvider) {
117             text = (TextProvider)action;
118         }
119 
120         ActionMessage error = null;
121         String field = null;
122         String msg = null;
123         Object[] values = null;
124         for (Iterator i = errors.properties(); i.hasNext(); ) {
125             field = (String) i.next();
126             for (Iterator it = errors.get(field); it.hasNext(); ) {
127                 error = (ActionMessage) it.next();
128                 msg = error.getKey();
129                 if (error.isResource() && text != null) {
130                     values = error.getValues();
131                     if (values != null) {
132                         msg = text.getText(error.getKey(), Arrays.asList(values));
133                     } else {
134                         msg = text.getText(error.getKey());
135                     }
136                 }
137                 if (vaction != null) {
138                     if (field == errors.GLOBAL_MESSAGE) {
139                         vaction.addActionError(msg);
140                     } else {
141                         vaction.addFieldError(field, msg);
142                     }
143                 } else {
144                     // should do something here
145                 }
146             }
147         }
148     }
149 }