1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
145 }
146 }
147 }
148 }
149 }