KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > web > html > Impl > HTMLParser > UserBuilderVisitor


1 package org.jahia.clipbuilder.html.web.html.Impl.HTMLParser;
2
3 import org.jahia.clipbuilder.html.bean.*;
4 import javax.swing.text.html.HTML JavaDoc;
5 import org.htmlparser.tags.*;
6 import org.htmlparser.Tag;
7 import org.jahia.clipbuilder.html.util.*;
8 import org.jahia.clipbuilder.html.web.Constant.*;
9 import org.jahia.clipbuilder.html.struts.Util.Constants;
10 import org.htmlparser.Node;
11 import org.htmlparser.util.NodeList;
12 import org.htmlparser.scanners.ScriptScanner;
13
14 /**
15  * Description of the Class
16  *
17  *@author Tlili Khaled
18  */

19 public class UserBuilderVisitor extends TransformBuilderVisitor {
20
21     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(UserBuilderVisitor.class);
22
23
24     /**
25      * Constructor for the UserBuilderVisitor object
26      *
27      *@param transformer Description of Parameter
28      */

29     public UserBuilderVisitor(HTMLParserTransformer transformer) {
30         super(transformer);
31                 ScriptScanner.STRICT = false;
32     }
33
34
35     /**
36      * Description of the Method
37      *
38      *@param t Description of Parameter
39      */

40     public void visitStringNode(String JavaDoc t) {
41         if (t.toLowerCase().indexOf("onload") > -1) {
42             t = "waza";
43         }
44     }
45
46
47     /**
48      * Description of the Method
49      *
50      *@param tag Description of Parameter
51      */

52     public void visitTag(Tag tag) {
53         if (tag instanceof Html) {
54             processHtmlTag(tag);
55
56         }
57
58         processTargetAtt(tag);
59
60         if (tag instanceof BaseHrefTag) {
61             processBaseHrefTag((BaseHrefTag) tag);
62         }
63
64         //refactor javascript that appear in an event attribute
65
for (int i = 0; i < WebConstants.JAVASCRIPT_EVENT_NAMES_ARRAY.length; i++) {
66             String JavaDoc name = WebConstants.JAVASCRIPT_EVENT_NAMES_ARRAY[i];
67             String JavaDoc value = tag.getAttribute(name);
68             if (value != null) {
69                 String JavaDoc url = getTransformer().getDocument().getUrlBean().getRedirectUrl().toExternalForm();
70                 String JavaDoc newValue = getTransformer().getRefactoredJavascript(url, Constants.WEB_BROWSER_SHOW_BROWSE, value);
71                 tag.setAttribute(name, newValue);
72             }
73         }
74
75         //logger.debug("[ Current tag: " + tag.getTagName() + "]");
76
// process ele whith attribut src
77
if (tag.getAttribute("src") != null) {
78             try {
79                 processSrcAttribute(tag);
80             }
81             catch (WebClippingException ex) {
82                 getTransformer().addParsingErrors(ex.getMessage());
83             }
84         }
85
86         if (tag instanceof MetaTag) {
87             try {
88                 processMetaTag((MetaTag) tag, Constants.WEB_BROWSER_SHOW_BROWSE);
89             }
90             catch (WebClippingException ex) {
91                 ex.printStackTrace();
92                 getTransformer().addParsingErrors(ex.getMessage());
93             }
94         }
95
96         // process body tag
97
if (tag instanceof BodyTag) {
98             /*
99              * try {
100              * processBodyTag((BodyTag) tag);
101              * }
102              * catch (WebClippingException ex) {
103              * getTransformer().addParsingErrors(ex.getMessage());
104              * }
105              */

106         }
107         // process ele whith attribut href
108
else if (tag.getAttribute("href") != null) {
109             logger.debug("href att found, value is: " + tag.getAttribute("href"));
110             try {
111
112                 nbLinks = processHrefAttribute(tag, nbLinks, Constants.WEB_BROWSER_SHOW_BROWSE);
113                 logger.debug("Nb link = " + nbLinks);
114
115             }
116             catch (WebClippingException ex) {
117                 getTransformer().addParsingErrors(ex.getMessage());
118             }
119
120         }
121         //process meta tag
122
else if (tag instanceof MetaTag) {
123             try {
124                 processMetaTag((MetaTag) tag, Constants.WEB_BROWSER_SHOW_TEST);
125             }
126             catch (WebClippingException ex) {
127                 getTransformer().addParsingErrors(ex.getMessage());
128             }
129         }
130
131         // process ele whith attribut action
132
else if (tag instanceof FormTag) {
133             try {
134                 processFormTag((FormTag) tag, Constants.WEB_BROWSER_SHOW_TEST);
135             }
136             catch (WebClippingException ex) {
137                 getTransformer().addParsingErrors(ex.getMessage());
138             }
139         }
140
141         //process input element
142
else if (tag instanceof InputTag) {
143             try {
144                 processInputTag((InputTag) tag);
145             }
146             catch (WebClippingException ex) {
147                 getTransformer().addParsingErrors(ex.getMessage());
148             }
149         }
150         else if (tag instanceof SelectTag) {
151             processSelectTag((SelectTag) tag);
152         }
153
154         // inline css
155
else if (tag instanceof StyleTag) {
156             try {
157                 processStyleTag((StyleTag) tag);
158             }
159             catch (WebClippingException ex) {
160                 getTransformer().addParsingErrors(ex.getMessage());
161             }
162         }
163
164         // extract javascript
165
else if (tag instanceof ScriptTag) {
166             logger.debug("[ Process script tag ]");
167             if (getTransformer().isRemoveBodyScript() || getTransformer().isRemoveHeadScriptTag()) {
168                 tag.setChildren(new NodeList());
169                 tag.removeAttribute("src");
170             }
171             else {
172                 try {
173                     processScriptTag((ScriptTag) tag);
174                 }
175                 catch (WebClippingException ex) {
176                     getTransformer().addParsingErrors(ex.getMessage());
177                     logger.error("Rewrite javascript error");
178                     ex.printStackTrace();
179                 }
180             }
181         }
182
183     }
184
185
186     /**
187      * Description of the Method
188      *
189      *@param tag Description of Parameter
190      *@param position Description of Parameter
191      *@param webBrowserAction Description of Parameter
192      *@return Description of the Returned Value
193      *@exception WebClippingException Description of Exception
194      */

195
196     protected int processHrefAttribute(Tag tag, int position, String JavaDoc webBrowserAction) throws WebClippingException {
197         // compute the absolute href
198
String JavaDoc href = tag.getAttribute("href");
199         String JavaDoc absoluteHref = href;
200         if (href == null) {
201             logger.error("there is no value for href attribut");
202         }
203         try {
204             absoluteHref = getTransformer().relatifToAbsolute(href);
205             logger.debug("absolute url is: " + absoluteHref);
206         }
207         catch (Exception JavaDoc ex) {
208             logger.error("error has occured: " + ex.toString());
209             //ex.printStackTrace();
210
}
211         finally {
212             // add id to the href. Becarefull, this method change href value.
213
int newposition = super.processHrefAttribute(tag, position, webBrowserAction);
214             if (!tag.getTagName().equalsIgnoreCase("meta") && !tag.getTagName().equalsIgnoreCase("link")) {
215
216                 String JavaDoc continualClipping = getTransformer().getUrlBean().getClipperBean().getConfigurationBean().getPortletContinualClipping();
217                 // get the type of Continual clipping
218
int ccInt = Integer.parseInt(continualClipping);
219
220                 // refactor
221
switch (ccInt) {
222                     case
223                             org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_FALSE:
224
225                         // remove link
226
tag.removeAttribute("href");
227                         break;
228                     case
229                             org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_POPUP:
230
231                         // open in popup
232
tag.setAttribute("target", "_blank");
233                     //refactor in order to redirect to clipbuilder
234
case
235                             org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_POPUP:
236
237                         // open in popup
238
tag.setAttribute("target", "_blank");
239
240                         // do'nt redirect to clipbuilder
241
tag.setAttribute("href", absoluteHref);
242                     case
243                             org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_IFRAME:
244                         break;
245                     case
246                             org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_IFRAME:
247
248                         // do'nt redirect to clipbuilder
249
tag.setAttribute("href", absoluteHref);
250                         break;
251                 }
252
253             }
254             return newposition;
255         }
256     }
257
258
259     /**
260      * Description of the Method
261      *
262      *@param tag Description of Parameter
263      *@param webBrowserAction Description of Parameter
264      *@exception WebClippingException Description of Exception
265      */

266     protected void processFormTag(FormTag tag, String JavaDoc webBrowserAction) throws WebClippingException {
267         // compute absolute url
268
String JavaDoc actionUrl = tag.getAttribute("action");
269         String JavaDoc method = tag.getFormMethod();
270         String JavaDoc encrypt = tag.getAttribute("enctype");
271         String JavaDoc absoluteActionUrl = actionUrl;
272         try {
273             if (actionUrl == null) {
274                 logger.warn("action att is null !!");
275                 String JavaDoc name = tag.getFormName();
276                 String JavaDoc id = tag.getAttribute("id");
277                 logger.warn("Name Form: " + name + ", Form id: " + id);
278             }
279             else {
280                 absoluteActionUrl = getTransformer().relatifToAbsolute(actionUrl);
281             }
282         }
283         catch (Exception JavaDoc ex) {
284             logger.debug("error has occured: " + ex.toString());
285             ex.printStackTrace();
286         }
287         finally {
288             super.processFormTag(tag, webBrowserAction);
289             String JavaDoc continualClipping = getTransformer().getUrlBean().getClipperBean().getConfigurationBean().getPortletContinualClipping();
290             int ccInt = Integer.parseInt(continualClipping);
291             switch (ccInt) {
292                 case org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_FALSE:
293                     // remove link
294
//tag.removeAttribute("href");
295
break;
296                 case org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_POPUP:
297                     // open in popup
298
tag.setAttribute("target", "_blank");
299                 //refactor in order to redirect to clipbuilder
300
case org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_POPUP:
301                     // open in popup
302
tag.setAttribute("target", "_blank");
303
304                     // set params value in order to not redirect
305
if (method != null) {
306                         tag.setAttribute("method", method);
307                     }
308                     if (encrypt != null) {
309                         tag.setAttribute("enctype", encrypt);
310                     }
311                     tag.setAttribute("action", absoluteActionUrl);
312                 case org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_IFRAME:
313                     break;
314                 case org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_IFRAME:
315                     // do'nt redirect to clipbuilder
316
// set params value in order to not redirect
317
if (method != null) {
318                         tag.setAttribute("method", method);
319                     }
320                     if (encrypt != null) {
321                         tag.setAttribute("enctype", encrypt);
322                     }
323                     tag.setAttribute("action", absoluteActionUrl);
324                     break;
325             }
326         }
327
328     }
329
330
331
332     /**
333      * Description of the Method
334      *
335      *@param tag Description of Parameter
336      */

337     private void processHtmlTag(Tag tag) {
338         setFirstNode(tag);
339     }
340
341
342     /**
343      * Description of the Method
344      */

345     /*
346      * protected void processBodyTag(BodyTag tag) throws WebClippingException {
347      * logger.debug(" body tag found");
348      * String onload = tag.getAttribute("onload");
349      * }
350      */

351     /**
352      * Description of the Method
353      *
354      *@param tag Description of Parameter
355      */

356     private void processSelectTag(SelectTag tag) {
357         String JavaDoc name = tag.getAttribute(HTML.Attribute.NAME.toString());
358         if (name == null) {
359             name = "";
360         }
361         int paramPosition = getTransformer().getPosAndUpdateInputHash(name);
362         FormParamBean fBean = getTransformer().getFormParamBean(formParentName, formParentId, formParentPosition, name, paramPosition);
363         String JavaDoc formHash = HashUtilities.buildFormHash(formParentName, formParentId, formParentPosition);
364
365         if (fBean == null) {
366             logger.warn("FormParamBean not found for hash " + formHash + " and input name " + name + " whith type " + tag.getAttribute("type"));
367             return;
368         }
369
370         logger.debug("FormParamBean found for hash " + formHash + " and input name " + name + " whith type " + tag.getAttribute("type"));
371         logger.debug("Used value: " + fBean.getUsedValue());
372         String JavaDoc usedValue = fBean.getUsedValue();
373
374         // select the good option
375
NodeList it = tag.getChildren();
376         for (int i = 0; i < it.size(); i++) {
377             Node childNode = it.elementAt(i);
378             if (childNode instanceof OptionTag) {
379                 OptionTag op = (OptionTag) childNode;
380                 String JavaDoc possibleValue = op.getValue();
381                 if (possibleValue.equalsIgnoreCase(usedValue)) {
382                     op.setAttribute(HTML.Attribute.SELECTED.toString(), "selected");
383                 }
384                 else {
385                     op.removeAttribute(HTML.Attribute.SELECTED.toString());
386                 }
387             }
388         }
389     }
390
391
392     /**
393      * Gets the FormParamBean attribute of the UserBuilderVisitor object
394      */

395
396     /**
397      * Description of the Method
398      *
399      *@param tag Description of Parameter
400      *@exception WebClippingException Description of Exception
401      */

402     private void processInputTag(InputTag tag) throws WebClippingException {
403         logger.debug(" Input tag found");
404         try {
405             String JavaDoc name = tag.getAttribute(HTML.Attribute.NAME.toString());
406
407             if (name == null) {
408                 name = "";
409             }
410             //compute the relative position of an input element
411
int paramPosition = getTransformer().getPosAndUpdateInputHash(name);
412             FormParamBean fBean = getTransformer().getFormParamBean(formParentName, formParentId, formParentPosition, name, paramPosition);
413             String JavaDoc formHash = HashUtilities.buildFormHash(formParentName, formParentId, formParentPosition);
414
415             if (fBean == null) {
416                 logger.warn("FormParamBean not found for hash " + formHash + " and input name " + name + " whith type " + tag.getAttribute("type"));
417                 return;
418             }
419
420             // manage automatic update
421
String JavaDoc fBeanUpdate = fBean.getUpdate();
422             logger.debug("Param: " + fBean.getName() + " whith type: " + fBean.getType() + " update: " + fBeanUpdate);
423             if (fBeanUpdate.equalsIgnoreCase("true")) {
424                 String JavaDoc inputValue = tag.getAttribute("value");
425                 getTransformer().updateInputValue(fBean, inputValue);
426                 logger.debug("Updated");
427                 return;
428             }
429             logger.debug("Not Updated");
430             logger.debug("FormParamBean found for hash " + formHash + " and input name " + name + " whith type " + tag.getAttribute("type"));
431
432             logger.debug("Used value: " + fBean.getUsedValue());
433             //tag.setAttribute("value", fBean.getUsedValue());
434

435         }
436         catch (Exception JavaDoc ex) {
437             throw new WebClippingException("Parser, Input Element", ex);
438         }
439         finally {
440             return;
441         }
442
443     }
444
445 }
446
Popular Tags