View Javadoc

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