1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
149 }
150 }
151 }
152 }
153 }