1 | |
package net.sf.jolene.struts; |
2 | |
|
3 | |
import net.sf.jolene.util.PrefsReader; |
4 | |
import net.sf.jolene.dom.Document; |
5 | |
import net.sf.jolene.dom.HTMLElement; |
6 | |
import net.sf.jolene.dom.Label; |
7 | |
import net.sf.jolene.dom.Text; |
8 | |
import net.sf.jolene.factories.DocumentFactory; |
9 | |
import org.apache.log4j.LogManager; |
10 | |
import org.apache.log4j.Logger; |
11 | |
import org.apache.struts.action.ActionForm; |
12 | |
import org.apache.struts.action.ActionForward; |
13 | |
import org.apache.struts.action.ActionMapping; |
14 | |
import org.apache.struts.actions.DispatchAction; |
15 | |
import org.apache.struts.util.MessageResources; |
16 | |
|
17 | |
import javax.servlet.http.HttpServletRequest; |
18 | |
import javax.servlet.http.HttpServletResponse; |
19 | |
import java.io.IOException; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public class DomletAction extends DispatchAction { |
30 | |
|
31 | 0 | private static final Logger log = LogManager.getLogger(DomletAction.class); |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
@Override |
45 | |
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { |
46 | |
|
47 | 0 | log.info("DomletAction Initializing PrefsReader..."); |
48 | 0 | PrefsReader.init(); |
49 | |
|
50 | |
|
51 | 0 | log.debug("QUERY_STRING :" + request.getQueryString()); |
52 | |
|
53 | |
|
54 | |
|
55 | 0 | String parameter = mapping.getParameter(); |
56 | |
String name; |
57 | 0 | if (parameter != null) { |
58 | 0 | name = request.getParameter(parameter); |
59 | |
} else { |
60 | 0 | String path = mapping.getPath(); |
61 | 0 | name = path; |
62 | 0 | if (path.lastIndexOf("/") != -1) { |
63 | 0 | name = path.substring(path.lastIndexOf("/") + 1); |
64 | |
} |
65 | |
} |
66 | |
|
67 | |
|
68 | 0 | return dispatchMethod(mapping, form, request, response, name); |
69 | |
|
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public Document getDocument(ActionMapping mapping, HttpServletRequest request) throws IOException { |
101 | 0 | String uri = mapping.getInput(); |
102 | 0 | log.debug("mapping URI: " + uri); |
103 | 0 | if (uri == null) { |
104 | 0 | log.error("NO URI Specified in struts-config.xml for " + mapping.getPath()); |
105 | 0 | return null; |
106 | |
} |
107 | 0 | return getDocument(request, uri, mapping); |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public Document getDocument(ActionMapping mapping, String forward, HttpServletRequest request) throws IOException { |
140 | |
|
141 | 0 | String uri = mapping.findForward(forward).getPath(); |
142 | 0 | log.debug("Mapping URI from forward: " + forward + ": " + uri); |
143 | 0 | return getDocument(request, uri, mapping); |
144 | |
|
145 | |
} |
146 | |
|
147 | |
private Document getDocument(HttpServletRequest request, String uri, ActionMapping mapping) throws IOException { |
148 | |
String file; |
149 | 0 | file = request.getSession().getServletContext().getRealPath(uri); |
150 | |
|
151 | |
Document document; |
152 | 0 | if ("request".equalsIgnoreCase(mapping.getScope())) { |
153 | 0 | if (request.getAttribute(uri) == null) { |
154 | 0 | document = DocumentFactory.getInstance().getDocument(file, request.getContextPath(), uri); |
155 | |
} else { |
156 | 0 | document = (Document) request.getAttribute(uri); |
157 | |
} |
158 | 0 | request.setAttribute(uri, document); |
159 | |
} else { |
160 | 0 | if (request.getSession().getAttribute(uri) == null) { |
161 | 0 | document = DocumentFactory.getInstance().getDocument(file, request.getContextPath(), uri); |
162 | |
} else { |
163 | 0 | document = (Document) request.getSession().getAttribute(uri); |
164 | |
} |
165 | 0 | request.getSession().setAttribute(uri, document); |
166 | |
} |
167 | |
|
168 | 0 | translate(document, request); |
169 | 0 | return document; |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
private void translate(Document document, HttpServletRequest request) { |
179 | 0 | if (document.isTranslated()) { |
180 | 0 | return; |
181 | |
} |
182 | |
|
183 | 0 | MessageResources messages = getResources(request); |
184 | |
|
185 | |
|
186 | 0 | String docKey = document.getUri(); |
187 | 0 | if (docKey.startsWith("/")) { |
188 | 0 | docKey = docKey.substring(1); |
189 | |
} |
190 | 0 | int n = docKey.indexOf("."); |
191 | 0 | if (n > -1) { |
192 | 0 | docKey = docKey.substring(0, n); |
193 | |
} |
194 | 0 | docKey = docKey.replace("/", "."); |
195 | 0 | docKey = docKey.replace("\\", "."); |
196 | 0 | docKey = docKey.toLowerCase(); |
197 | |
|
198 | 0 | log.debug("docKey " + docKey); |
199 | |
|
200 | |
HTMLElement element; |
201 | |
String formKey; |
202 | 0 | for (int j = 0; j < document.getFormCount(); j++) { |
203 | 0 | formKey = document.forms(j).getName(); |
204 | 0 | if (formKey.trim().length() == 0) { |
205 | 0 | formKey = "form" + (j + 1); |
206 | |
} |
207 | |
|
208 | |
String elementName; |
209 | 0 | for (int k = 0; k < document.forms(j).getElementCount(); k++) { |
210 | 0 | element = document.forms(j).elements(k); |
211 | 0 | elementName = docKey + "." + formKey + "." + element.getName(); |
212 | 0 | log.debug("Element name " + elementName); |
213 | |
|
214 | |
|
215 | 0 | if (element instanceof Label && !(element instanceof Text)) { |
216 | |
|
217 | 0 | if (messages.isPresent(elementName)) { |
218 | 0 | element.setValue(messages.getMessage(elementName)); |
219 | |
} |
220 | |
} else { |
221 | |
|
222 | 0 | if (element.hasAttribute("type") && (element.getAttribute("type").equalsIgnoreCase("button") || element.getAttribute("type").equalsIgnoreCase("submit"))) { |
223 | |
|
224 | 0 | if (messages.isPresent(elementName)) { |
225 | 0 | log.debug("setting element " + elementName); |
226 | 0 | element.setValue(messages.getMessage(elementName)); |
227 | |
} |
228 | |
} |
229 | |
} |
230 | |
|
231 | |
|
232 | 0 | if (element.hasAttribute("title")) { |
233 | 0 | log.debug("Element name title: " + elementName + ".title"); |
234 | 0 | if (messages.isPresent(elementName + ".title")) { |
235 | 0 | log.debug("setting title " + elementName); |
236 | 0 | element.setAttribute("title", messages.getMessage(elementName + ".title")); |
237 | |
} |
238 | |
} |
239 | |
} |
240 | |
} |
241 | 0 | document.setTranslated(true); |
242 | |
|
243 | 0 | } |
244 | |
|
245 | |
|
246 | |
} |