KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > struts > SelectPartAction


1 package org.jahia.clipbuilder.html.struts;
2
3 import java.util.*;
4
5 import javax.servlet.http.*;
6
7 import org.jahia.clipbuilder.html.*;
8 import org.jahia.clipbuilder.html.bean.*;
9 import org.jahia.clipbuilder.html.struts.Util.*;
10 import org.jahia.clipbuilder.html.web.html.*;
11 import org.jahia.clipbuilder.html.web.html.Impl.ExtractorFilter.*;
12 import org.apache.struts.action.*;
13
14
15 /**
16  * Description of the Class
17  *
18  *@author Tlili Khaled
19  */

20 public class SelectPartAction extends AbstractWizardAction {
21     private boolean errorHasOccured = false;
22     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(SelectPartAction.class);
23
24
25     /**
26      * Gets the FormId attribute of the SelectPartAction object
27      *
28      *@return The FormId value
29      */

30     public int getFormId() {
31         return Constants.SELECTPART;
32     }
33
34
35     /**
36      * Gets the KeyMethodMap attribute of the BrowseAction object
37      *
38      *@return The KeyMethodMap value
39      */

40     public Map getKeyMethodMap() {
41         Map map = super.getKeyMethodMap();
42         map.put("select.changeClippingMethod", "changeClippingMethod");
43         map.put("wizard.selectPart", "view");
44         map.put("edit.button.extract", "doExtract");
45         map.put("edit.chew.preview", "chewPreview");
46         map.put("select.editParams", "goToEditParams");
47         map.put("edit.button.chew.cut", "chewCut");
48         map.put("edit.button.chew.autoCut", "chewCut");
49
50         map.put("edit.button.rules.config", "rulesConfig");
51
52         return map;
53     }
54
55
56     /**
57      * Description of the Method
58      *
59      *@param actionMapping Description of Parameter
60      *@param actionForm Description of Parameter
61      *@param httpServletRequest Description of Parameter
62      *@param httpServletResponse Description of Parameter
63      *@return Description of the Returned Value
64      */

65     public ActionForward view(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
66         logger.debug("[ View ]");
67         AbstractWizardForm form = (AbstractWizardForm) actionForm;
68         SessionManager.getWebBrowserForm(httpServletRequest).setShow(Constants.WEB_BROWSER_SHOW_LAST_DOCUMENT);
69
70         return actionMapping.getInputForward();
71     }
72
73
74
75     /**
76      * Called when select button is selected
77      *
78      *@param actionMapping Description of Parameter
79      *@param actionForm Description of Parameter
80      *@param httpServletRequest Description of Parameter
81      *@param httpServletResponse Description of Parameter
82      *@return Description of the Returned Value
83      */

84     public ActionForward doExtract(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
85         SelectPartForm selectPartForm = (SelectPartForm) actionForm;
86         //Get the ClipperBean object
87
ClipperBean bean = SessionManager.getClipperBean(request);
88
89         //set the selected part
90
String JavaDoc selectedContent = selectPartForm.getSelectedContent();
91
92         //logger.debug("[Selected Content] --> " + selectedContent);
93

94         //get the document
95
HTMLDocument doc = bean.getLastRecordedUrlBean().getDocument();
96
97         //set the HTMLFilter
98
ExtractorFilter filter = buildFilter(request,selectPartForm, selectedContent, doc);
99
100         logger.debug("[ Selected content is " + selectedContent + " ]");
101         setClipperFilterBean(request, filter);
102
103         return actionMapping.findForward("preview");
104     }
105
106
107
108     /**
109      * Description of the Method
110      *
111      *@param actionMapping Description of Parameter
112      *@param actionForm Description of Parameter
113      *@param request Description of Parameter
114      *@param httpServletResponse Description of Parameter
115      *@return Description of the Returned Value
116      */

117     public ActionForward chewPreview(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse httpServletResponse) {
118         SelectPartForm selectPartForm = (SelectPartForm) actionForm;
119         HttpSession session = request.getSession();
120         // set the default preview form
121
session.setAttribute(Constants.PREVIEW_FORM, new PreviewForm());
122
123         //set the selected part
124
String JavaDoc selectedContent = "view filter not available";
125         selectPartForm.setSelectedContent(selectedContent);
126
127         //set the HTMLFilter
128
ChewExtractorFilter filter = buildChewFilter(request, selectPartForm);
129
130         // set the filter
131
setClipperFilterBean(request, filter);
132
133         return actionMapping.findForward("preview");
134     }
135
136
137     /**
138      * Description of the Method
139      *
140      *@param mapping Description of Parameter
141      *@param actionForm Description of Parameter
142      *@param request Description of Parameter
143      *@param response Description of Parameter
144      *@return Description of the Returned Value
145      */

146     public ActionForward chewCut(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
147         logger.debug("[ Action = chew cut ]");
148         SelectPartForm selectPartForm = (SelectPartForm) actionForm;
149
150         // set filter bean
151
ChewExtractorFilter filter = new ChewExtractorFilter();
152         filter.setTagNameKey(selectPartForm.getWebClippingTagName());
153         setClipperFilterBean(request, filter);
154
155         //init target url
156
boolean success = initTargetUrl(mapping, actionForm, request);
157         if (!success) {
158             return mapping.findForward("description");
159         }
160
161         SessionManager.getWebBrowserForm(request).setShow(Constants.WEB_BROWSER_SHOW_CHEW);
162
163         return mapping.getInputForward();
164     }
165
166
167     /**
168      * Description of the Method
169      *
170      *@param actionMapping Description of Parameter
171      *@param actionForm Description of Parameter
172      *@param httpServletRequest Description of Parameter
173      *@param httpServletResponse Description of Parameter
174      *@return Description of the Returned Value
175      */

176     public ActionForward changeClippingMethod(ActionMapping actionMapping,
177             ActionForm actionForm,
178             HttpServletRequest
179             httpServletRequest,
180             HttpServletResponse
181             httpServletResponse) {
182         logger.debug("[change clippingMethod ]");
183         SelectPartForm selectPartForm = (SelectPartForm) actionForm;
184         selectPartForm.getClippingMethod();
185
186         //set the content
187
// get the document
188
//HTMLDocument htmlDocument = getHTMLDocument(httpServletRequest, httpServletResponse);
189
//selectPartForm.setBodyContent(htmlDocument.getTransformedDocumentAsString());
190
SessionManager.getWebBrowserForm(httpServletRequest).setShow(Constants.WEB_BROWSER_SHOW_LAST_DOCUMENT);
191
192         return actionMapping.getInputForward();
193     }
194
195
196     /**
197      * Called when editParams button is selected
198      *
199      *@param actionMapping Description of Parameter
200      *@param actionForm Description of Parameter
201      *@param httpServletRequest Description of Parameter
202      *@param httpServletResponse Description of Parameter
203      *@return Description of the Returned Value
204      */

205     public ActionForward goToEditParams(ActionMapping actionMapping,
206             ActionForm actionForm,
207             HttpServletRequest httpServletRequest,
208             HttpServletResponse httpServletResponse) {
209         SelectPartForm selectPartForm = (SelectPartForm) actionForm;
210         return actionMapping.findForward("editParams");
211     }
212
213
214     /**
215      * Called when view is selected
216      *
217      *@param actionMapping Description of Parameter
218      *@param actionForm Description of Parameter
219      *@param httpServletRequest Description of Parameter
220      *@param httpServletResponse Description of Parameter
221      *@return Description of the Returned Value
222      */

223     public ActionForward init(ActionMapping actionMapping, ActionForm actionForm,
224             HttpServletRequest httpServletRequest,
225             HttpServletResponse httpServletResponse) {
226         SelectPartForm selectPartForm = (SelectPartForm) actionForm;
227         super.init(actionMapping, actionForm, httpServletRequest, httpServletResponse);
228         SessionManager.getWebBrowserForm(httpServletRequest).setShow(Constants.WEB_BROWSER_SHOW_LAST_DOCUMENT);
229
230         logger.debug("[ Select part - Action = view ]");
231         boolean success = initTargetUrl(actionMapping, actionForm,
232                 httpServletRequest);
233         if (!success) {
234             return actionMapping.findForward("description");
235         }
236
237         // process the source url in and print the document
238
/*
239          * HTMLDocument doc = getHTMLDocument(httpServletRequest, httpServletResponse);
240          * // set the content
241          * String html = doc.getTransformedDocumentAsString();
242          * try {
243          * String encodedHTML = new String(html.getBytes(), httpServletResponse.getCharacterEncoding());
244          * selectPartForm.setBodyContent(encodedHTML);
245          * }
246          * catch (UnsupportedEncodingException ex) {
247          * ex.printStackTrace();
248          * selectPartForm.setBodyContent(html);
249          * }
250          */

251         return actionMapping.getInputForward();
252     }
253
254
255     /**
256      * Sets the Filter attribute of the SelectPartAction object
257      *
258      *@param request The new Filter value
259      *@param filter The new Filter value
260      */

261     private void setClipperFilterBean(HttpServletRequest request, ExtractorFilter filter) {
262
263         // set filter bean
264
FilterBean filterBean = new FilterBean();
265         filterBean.setKeyMap(filter.getKeyMap());
266         filterBean.setMode(filter.getMode());
267         filterBean.setName(filter.getName());
268         SessionManager.getClipperBean(request).setFilterBean(filterBean);
269     }
270
271
272     /**
273      * Description of the Method
274      *
275      *@param selectPartForm Description of Parameter
276      *@param selectedContent Description of Parameter
277      *@param doc Description of Parameter
278      *@return Description of the Returned Value
279      */

280     private ExtractorFilter buildFilter(HttpServletRequest request,SelectPartForm selectPartForm, String JavaDoc selectedContent, HTMLDocument doc) {
281         ExtractorFilter filter = null;
282         logger.debug("[ Selected filter is: " + selectPartForm.getWebClippingTypeContent() + "]");
283         String JavaDoc typeFilter = selectPartForm.getClippingMethod();
284         String JavaDoc typeContent = selectPartForm.getWebClippingTypeContent();
285         String JavaDoc enableCss = selectPartForm.getWebClippingShowCss();
286
287
288         //Manula selection
289
if (typeFilter.equalsIgnoreCase("0")) {
290             if (typeContent.equalsIgnoreCase("0")) {
291                 //Dynamic
292
filter = new StringTreeExtractorFilter(doc, selectedContent);
293             }
294             else if (typeContent.equalsIgnoreCase("1")) {
295                 //static
296
filter = new SimpleExtractorFilter(selectedContent);
297             }
298         }
299         //chew
300
else if (typeFilter.equalsIgnoreCase("1")) {
301             filter =this.buildChewFilter(request,selectPartForm);
302         }
303         //form
304
else if (typeFilter.equalsIgnoreCase("3")) {
305             filter = new FormExtractorFilter();
306         }
307         //xpath
308
else if (typeFilter.equalsIgnoreCase("4")) {
309             filter = new XPathExtractorFilter();
310             String JavaDoc xPath = selectPartForm.getWebClippingXPath();
311             ((XPathExtractorFilter) filter).setXPahKey(xPath);
312         }
313         //full document
314
else if (typeFilter.equalsIgnoreCase("5")) {
315             filter = new FullWebPageExtractorFilter();
316         }
317
318         //link
319
else {
320             logger.error("[Unexpected value " + selectPartForm.getWebClippingTypeContent() + " ]");
321         }
322
323         //set css mode
324
if (enableCss == null) {
325             filter.setMode(ExtractorFilter.MODE_WHITOUT_CSS);
326         }
327         else {
328             filter.setMode(ExtractorFilter.MODE_CSS);
329         }
330
331
332         return filter;
333     }
334
335
336     /**
337      * Description of the Method
338      *
339      *@param request Description of Parameter
340      *@param selectPartForm Description of Parameter
341      *@return Description of the Returned Value
342      */

343     private ChewExtractorFilter buildChewFilter(HttpServletRequest request, SelectPartForm selectPartForm) {
344         ChewExtractorFilter filter = new ChewExtractorFilter();
345         //set the key of the filter
346
String JavaDoc hashKey = request.getParameter(ChewExtractorFilter.NAME_INPUT_PARAM);
347         String JavaDoc tagName = selectPartForm.getWebClippingTagName();
348         String JavaDoc enableCss = selectPartForm.getWebClippingShowCss();
349         if (enableCss == null) {
350             filter.setMode(ExtractorFilter.MODE_WHITOUT_CSS);
351         }
352         else {
353             filter.setMode(ExtractorFilter.MODE_CSS);
354         }
355         filter.setHashKey(hashKey);
356         filter.setTagNameKey(tagName);
357         logger.debug("Selected Element: " + hashKey);
358         return filter;
359     }
360
361
362
363     /**
364      * Description of the Method
365      *
366      *@param actionMapping Description of Parameter
367      *@param actionForm Description of Parameter
368      *@param httpServletRequest Description of Parameter
369      *@return Description of the Returned Value
370      */

371     private boolean initTargetUrl(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest) {
372         SelectPartForm selectPartForm = (SelectPartForm) actionForm;
373
374         //Get the url from the ClipperBean object
375
ClipperBean bean = SessionManager.getClipperBean(httpServletRequest);
376
377         //Handing error
378
if (bean == null) {
379             errorHasOccured = true;
380             logger.error("ClipperBean error: Not found");
381             return false;
382         }
383
384         //Init targetUrl
385
logger.debug("[Init targetUrl property]");
386         String JavaDoc sourceUrl = bean.getLastRecordedUrlValue();
387         logger.debug("[Last recorded url is " + sourceUrl + " ]");
388         selectPartForm.setTargetUrl(sourceUrl);
389         return true;
390     }
391
392 }
393
Popular Tags