KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > web > html > Impl > Dom > DomHTMLTransformer


1 package org.jahia.clipbuilder.html.web.html.Impl.Dom;
2
3 import java.util.*;
4
5 import javax.swing.text.html.*;
6 import javax.xml.parsers.*;
7
8 import org.jahia.clipbuilder.html.bean.*;
9 import org.jahia.clipbuilder.html.web.Constant.*;
10 import org.jahia.clipbuilder.html.struts.Util.Constants;
11 import org.jahia.clipbuilder.html.web.html.Impl.*;
12 import org.apache.xerces.dom.*;
13 import org.w3c.dom.*;
14 import org.jahia.clipbuilder.html.util.*;
15 import org.jahia.clipbuilder.html.util.*;
16 import org.jahia.clipbuilder.html.web.HTMLDocumentBuilder;
17
18 /**
19  * Tranformer factory for Dom implementation
20  *
21  *@author Tlili Khaled
22  */

23 public class DomHTMLTransformer extends DefaultHTMLTransformer {
24     //public final String JAVASCRIPT_EVENT_XPATH = "//@onclick";
25
/**
26      * Description of the Field
27      */

28     public final String JavaDoc FRAME_ELE_XPATH = "//frame";
29
30     private DomHTMLDocument document;
31
32     //Xpath
33
private final String JavaDoc LINK_ELE_XPATH = "//link[@rel='stylesheet']|//link[@type='text/css']";
34     private final String JavaDoc INPUT_ELE_XPATH = "//input";
35     private final String JavaDoc HREF_ATT_XPATH = "//@href";
36     private final String JavaDoc STYLE_ATT_XPATH = "//@style";
37     private final String JavaDoc SRC_ATT_XPATH = "//@src";
38     private final String JavaDoc FORM_ELE_XPATH = "//form";
39     private final String JavaDoc META_ELE_XPATH = "//meta";
40     private final String JavaDoc SYTLE_ELE_XPATH = "//style";
41     private final String JavaDoc SCRIPT_ELE_XPATH = "//script";
42     private final String JavaDoc TARGET_ATT_XPATH = "//@target";
43     private final String JavaDoc BASE_ELE_XPATH = "//base";
44     private final String JavaDoc JAVASCRIPT_EVENT_XPATH = "//@onabort|//@onblur|//@onchange|//@onclick|//@ondblClick|//@ondragdrop|//@onerror|//@onfocus|//@onkeydown|//@onkeypress|//@onkeyup|//@onload|//@onmousedown|//@onmousemove|//@onmouseout|//@onmouseover|//@onmouseup|//@onmove|//@onreset| //@onresize|//@onselect|//@onsubmit|//@onunload";
45     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(DomHTMLTransformer.class);
46
47
48
49     /**
50      * Constructor for the NekoHTMLTransformer object
51      *
52      *@param builder Description of Parameter
53      *@param document Description of Parameter
54      *@param enableCss Description of Parameter
55      */

56     public DomHTMLTransformer(HTMLDocumentBuilder builder, DomHTMLDocument document, boolean enableCss) {
57         super(builder, enableCss);
58         setDocument(document);
59     }
60
61
62     /**
63      * Sets the Document attribute of the NekoHTMLTransformer object
64      *
65      *@param document The new Document value
66      */

67     public void setDocument(DomHTMLDocument document) {
68         this.document = document;
69
70     }
71
72
73
74     /**
75      * Sets the UrlBean attribute of the NekoHTMLTransformer object
76      *
77      *@param urlBean The new UrlBean value
78      */

79     public void setUrlBean(UrlBean urlBean) {
80         this.getDocument().setUrlBean(urlBean);
81     }
82
83
84     /**
85      * Gets the Document attribute of the NekoHTMLTransformer object
86      *
87      *@return The Document value
88      */

89     public DomHTMLDocument getDocument() {
90         return document;
91     }
92
93
94     /**
95      * Gets the UrlBean attribute of the NekoHTMLTransformer object
96      *
97      *@return The UrlBean value
98      */

99     public UrlBean getUrlBean() {
100         return getDocument().getUrlBean();
101     }
102
103
104
105     /**
106      * Description of the Method
107      *
108      *@param tagName Description of Parameter
109      */

110     public void buildChewDocument(String JavaDoc tagName) {
111         logger.debug("[ Build chew document ]");
112         logger.debug("[ Build Transformed document ]");
113         Document transformedDoc = getDocument().getTransformedDocument();
114         Document chewDocument = null;
115
116         try {
117             chewDocument = DomUtilities.copy(transformedDoc);
118
119             //process chew element
120
processChewElement(chewDocument, tagName);
121
122         }
123
124         catch (FactoryConfigurationError ex) {
125             logger.error("[ FactoryConfigurationError " + ex + " ]");
126             ex.printStackTrace();
127         }
128         catch (Exception JavaDoc ex) {
129             logger.error("[ Exception " + ex + " ]");
130             ex.printStackTrace();
131         }
132
133         //set the transformed document
134
getDocument().setChewDocument(chewDocument);
135
136     }
137
138
139     /**
140      * Description of the Method
141      */

142     public void buildUserDocument() {
143         Document originalDoc = getDocument().getW3cOriginalDocument();
144         Document userDocument = DomUtilities.copy(originalDoc);
145
146         try {
147
148             processMetaTag(userDocument, Constants.WEB_BROWSER_SHOW_TEST);
149             processUserBodyElement(userDocument);
150             //remove target
151
processTargetAttribute(userDocument);
152
153             //process base ele
154
processBaseElement(userDocument);
155
156             // add some usefull parameter to each form element
157
processUserFormElement(userDocument, Constants.WEB_BROWSER_SHOW_TEST);
158
159             //fill the used value of the input element
160
processUserInputElement(userDocument);
161
162             // process the body tag by adding onload att
163
//processUserBodyElement(userDocument);
164

165             //Rewrite urls
166
processAllUserHrefAttribute(userDocument, Constants.WEB_BROWSER_SHOW_TEST);
167             processJavascriptEvent(userDocument, Constants.WEB_BROWSER_SHOW_TEST);
168             processSytleElement(userDocument);
169             processSrcAttribute(userDocument);
170
171         }
172
173         catch (FactoryConfigurationError ex) {
174             logger.error("[ FactoryConfigurationError " + ex + " ]");
175             ex.printStackTrace();
176         }
177         catch (Exception JavaDoc ex) {
178             logger.error("[ Exception " + ex + " ]");
179             ex.printStackTrace();
180         }
181
182         finally {
183
184             //set the transformed document
185
getDocument().setUserDocument(userDocument);
186         }
187
188     }
189
190
191
192     /**
193      * Description of the Method
194      */

195     public void buildDocumentWhithLabel() {
196         logger.debug("[ Build Document whith label ]");
197         Document transformedDoc = getDocument().getTransformedDocument();
198         Document doc = null;
199         try {
200             //create a doc whith the same struture as the transformed document
201
doc = DomUtilities.copy(transformedDoc);
202
203             //Get the list of all input node
204
NodeList inputList = DomUtilities.getNodeListByXPath(doc, INPUT_ELE_XPATH);
205
206             //add a label before each node
207
for (int i = 0; i < inputList.getLength(); i++) {
208                 // Current <input> Element
209
Element currentEle = (Element) inputList.item(i);
210                 String JavaDoc type = currentEle.getAttribute("type");
211                 if (editableInputParameter(type)) {
212                     if (!type.equalsIgnoreCase(WebConstants.TYPE_SUBMIT) && !type.equalsIgnoreCase(WebConstants.TYPE_IMAGE)) {
213                         insertLabel(currentEle);
214                     }
215                 }
216
217             }
218         }
219         catch (DOMException ex) {
220             logger.error("[ DOMException " + ex.getMessage() + " ]");
221             ex.printStackTrace();
222         }
223         catch (FactoryConfigurationError ex) {
224             logger.error("[ FactoryConfigurationError " + ex.getMessage() + " ]");
225             ex.printStackTrace();
226         }
227
228         finally {
229             //set the document
230
getDocument().setDocumentWithLabel(doc);
231         }
232     }
233
234
235     /**
236      * Description of the Method
237      */

238     public void buildTransformedDocument() {
239         logger.debug("[ Build Transformed document ]");
240         Document originalDoc = getDocument().getW3cOriginalDocument();
241         //logger.debug(getDocument().getOriginalDocumentAsString());
242
Document transformedDocument = DomUtilities.copy(originalDoc);
243
244         try {
245             // Import css --> include Css
246
//processLinkElement(transformedDocument);
247
processMetaTag(transformedDocument, Constants.WEB_BROWSER_SHOW_BROWSE);
248
249             //remove target
250
processTargetAttribute(transformedDocument);
251
252             //refactor css rule
253
processStyleAttribute(transformedDocument);
254
255             //process base tag
256
processBaseElement(transformedDocument);
257
258             //Record
259
processInputElement(transformedDocument);
260             processSelectElement(transformedDocument);
261
262             // add some usefull paramter to each form element
263
processFormElement(transformedDocument, Constants.WEB_BROWSER_SHOW_BROWSE);
264
265             // HTML --> DIV
266
//processHtmlElement(transformedDocument);
267

268             //Remove
269
//processHeadElement(transformedDocument);
270
//processTitleElement(transformedDocument);
271

272             //BODY --> DIV
273
//processBodyElement(transformedDocument);
274

275             //Rewrite urls
276
//processActionAttribute(transformedDocument);
277
processSrcAttribute(transformedDocument);
278             processSytleElement(transformedDocument);
279             processScriptElement(transformedDocument);
280             processAllHrefAttribute(transformedDocument, Constants.WEB_BROWSER_SHOW_BROWSE);
281             processJavascriptEvent(transformedDocument, Constants.WEB_BROWSER_SHOW_BROWSE);
282             processFrameElement(transformedDocument);
283
284         }
285
286         catch (FactoryConfigurationError ex) {
287             logger.error("[ FactoryConfigurationError " + ex + " ]");
288             ex.printStackTrace();
289         }
290         catch (Exception JavaDoc ex) {
291             logger.error("[ Exception " + ex + " ]");
292             ex.printStackTrace();
293         }
294
295         finally {
296
297             //set the transformed document
298
getDocument().setTransformedDocument(transformedDocument);
299         }
300
301     }
302
303
304
305     /**
306      * Description of the Method
307      *
308      *@param transformedDocument Description of Parameter
309      *@param webBrowseAction Description of Parameter
310      *@exception WebClippingException Description of Exception
311      */

312     private void processAllUserHrefAttribute(Document transformedDocument, String JavaDoc webBrowseAction) throws WebClippingException {
313         logger.debug("[ Process href att ]");
314         NodeList atts = DomUtilities.getNodeListByXPath(transformedDocument, HREF_ATT_XPATH);
315         int pos = 0;
316         for (int i = 0; i < atts.getLength(); i++) {
317             // init var
318
Attr att = (Attr) atts.item(i);
319             Element parent = att.getOwnerElement();
320             String JavaDoc ownerTagName = parent.getTagName();
321             String JavaDoc href = att.getValue();
322             String JavaDoc absoluteHref = null;
323             try {
324                 absoluteHref = this.relatifToAbsolute(href);
325                 pos = processHrefAtt(webBrowseAction, pos, att);
326             }
327             catch (Exception JavaDoc ex) {
328                 ex.printStackTrace();
329                 logger.error("Error has occurred");
330             }
331
332             if (!ownerTagName.equalsIgnoreCase("meta") && !ownerTagName.equalsIgnoreCase("link")) {
333                 String JavaDoc continualClipping = getUrlBean().getClipperBean().getConfigurationBean().getPortletContinualClipping();
334                 // get the type of Continual clipping
335
int ccInt = Integer.parseInt(continualClipping);
336
337                 // refactor
338
switch (ccInt) {
339                     case
340                             org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_FALSE:
341
342                         // remove link
343
//tag.removeAttribute("href");
344
break;
345                     case
346                             org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_POPUP:
347
348                         // open in popup
349
parent.setAttribute("target", "_blank");
350                     //refactor in order to redirect to clipbuilder
351
case
352                             org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_POPUP:
353
354                         // open in popup
355
parent.setAttribute("target", "_blank");
356
357                         // do'nt redirect to clipbuilder
358
parent.setAttribute("href", absoluteHref);
359                     case
360                             org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_IFRAME:
361                         break;
362                     case
363                             org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_IFRAME:
364
365                         // do'nt redirect to clipbuilder
366
parent.setAttribute("href", absoluteHref);
367                         break;
368                 }
369
370             }
371         }
372
373     }
374
375
376
377     /**
378      * Description of the Method
379      *
380      *@param doc Description of Parameter
381      *@param webBrowserAction Description of Parameter
382      *@exception WebClippingException Description of Exception
383      */

384     private void processMetaTag(Document doc, String JavaDoc webBrowserAction) throws WebClippingException {
385         try {
386             NodeList metaList = DomUtilities.getNodeListByXPath(doc, META_ELE_XPATH);
387             for (int i = 0; i < metaList.getLength(); i++) {
388
389                 //Get the http-equiv value
390
Element metaEle = (Element) metaList.item(i);
391                 if (isRemoveMetaTag()) {
392                     metaEle.removeAttribute(HTML.Attribute.HTTPEQUIV.toString());
393                 }
394                 else {
395
396                     String JavaDoc httpEquiv = metaEle.getAttribute(HTML.Attribute.HTTPEQUIV.toString());
397                     if (httpEquiv != null) {
398                         //process the refresh
399
if ("refresh".equals(httpEquiv.toLowerCase())) {
400                             String JavaDoc content = metaEle.getAttribute(HTML.Attribute.CONTENT.toString());
401                             //Get the url of the refresh
402
int urlIndex = content.toLowerCase().indexOf("url");
403                             if (urlIndex != -1) {
404                                 //Get the content just before the url
405
String JavaDoc contentBeforeURL = content.substring(0, urlIndex).trim();
406                                 logger.debug("[ Content before url " + contentBeforeURL + " ]");
407
408                                 //Get the url
409
int equalIndex = urlIndex + 3;
410                                 String JavaDoc contentURL = content.substring(equalIndex + 1).trim();
411                                 logger.debug("[ BEFORE meta http-equiv=[" + httpEquiv + "] content=[" + content + "] ]");
412                                 logger.debug("[ Refresh url is [" + contentURL + " ] ]");
413                                 //Encode the url
414
String JavaDoc encodedUrl = getRewritedHrefValue("meta", contentURL, "metaId", "metaLink", webBrowserAction);
415
416                                 //Set the new value whith the encoded url
417
metaEle.setAttribute(HTML.Attribute.CONTENT.toString(), contentBeforeURL + "url=" + encodedUrl);
418                                 logger.debug("[ AFTER meta http-equiv=[" + httpEquiv + "] content=[" + contentBeforeURL + "url=" + encodedUrl + "] ]+");
419                             }
420                         }
421                     }
422                 }
423
424             }
425         }
426         catch (Exception JavaDoc ex) {
427             throw new WebClippingException("Meta", ex);
428         }
429         finally {
430             return;
431         }
432
433     }
434
435
436     /**
437      * Constructor for the addChewLinkElementChild object
438      *
439      *@param ele Description of Parameter
440      *@param position Description of Parameter
441      *@return Description of the Returned Value
442      */

443     private String JavaDoc addChewLinkElementChild(Element ele, int position) {
444         Document ownerDoc = ele.getOwnerDocument();
445         String JavaDoc idAtt = ele.getAttribute("id");
446         //create a link element
447
Element linkEle = ownerDoc.createElement("a");
448         linkEle.setAttribute(HTML.Attribute.TARGET.toString(), "_parent");
449
450         // get hash and set the haah of the ele as it's id
451
String JavaDoc hash = HashUtilities.buildTagHash(idAtt, position);
452         ele.setAttribute("id", hash);
453         String JavaDoc linkHref = getHrefChewLink(hash);
454         linkEle.setAttribute(HTML.Attribute.HREF.toString(), linkHref);
455
456         // image
457
Element imgEle = ownerDoc.createElement("img");
458         String JavaDoc chewImagePath = getImageChewLinkSrcValue();
459         imgEle.setAttribute(HTML.Attribute.SRC.toString(), chewImagePath);
460
461         //add link and image
462
linkEle.appendChild(imgEle);
463         ele.appendChild(linkEle);
464
465         return linkHref;
466     }
467
468
469     /**
470      * Description of the Method
471      *
472      *@param doc Description of Parameter
473      */

474     private void processUserBodyElement(Document doc) {
475         logger.debug(" body tag found");
476         Element body = (Element) doc.getElementsByTagName("body").item(0);
477         String JavaDoc onload = body.getAttribute("onload");
478         String JavaDoc clickOnload = "";
479         // get the hash of the cliked link or submitted form
480
UrlBean uBean = getUrlBean();
481         ClipperBean cBean = uBean.getClipperBean();
482         UrlBean nextBean = cBean.getNextRecordedUrlBean(uBean);
483
484         // process only if its not the last url
485
if (nextBean != null) {
486             String JavaDoc from = nextBean.getFrom();
487             String JavaDoc fromHash = nextBean.getHash();
488             // case of link
489
if (from.equalsIgnoreCase(WebConstants.FROM_LINK)) {
490                 // id of link = builded hash
491
clickOnload = "document.getElementById('" + fromHash + "').click();";
492             }
493             else {
494                 //case of form
495
FormParamBean fBean = uBean.getSubmittedParamBean(fromHash);
496                 if (fBean != null) {
497                     String JavaDoc name = fBean.getName();
498                     String JavaDoc formParentName = fBean.getFormParentName();
499                     String JavaDoc formParentId = fBean.getFormParentId();
500                     if (formParentName == null || formParentName.equalsIgnoreCase("")) {
501                         clickOnload = "document.getElementById('" + formParentId + "')" + "." + name + ".click();";
502                     }
503                     else {
504                         clickOnload = "document." + formParentName + "." + name + ".click();";
505                     }
506                 }
507                 else {
508                     logger.warn("submit button not found");
509                     //getFormNameFormHash(fromHash);
510
String JavaDoc submittedFormName = HashUtilities.getFormNameFromHash(fromHash);
511                     if (submittedFormName != null) {
512                         clickOnload = "document." + submittedFormName + ".submit();";
513                     }
514                     else {
515                         String JavaDoc submittedFormId = HashUtilities.getFormIdFromHash(fromHash);
516                         clickOnload = "document.getElementById('" + submittedFormId + "')" + ".submit();";
517                     }
518
519                 }
520             }
521             if (onload == null) {
522                 onload = clickOnload;
523             }
524             else {
525                 onload = onload + ";" + clickOnload;
526             }
527             body.setAttribute("onload", onload);
528             //+"alert('coucou c'est nous!!');");
529
logger.debug("[New Onload: " + onload + " ]");
530         }
531
532     }
533
534
535     /**
536      * Rewrite href attribute
537      *
538      *@param transformedDocument Description of Parameter
539      *@param webBrowseAction Description of Parameter
540      */

541     private void processAllHrefAttribute(Document transformedDocument, String JavaDoc webBrowseAction) {
542         logger.debug("[ Process href att ]");
543         NodeList atts = DomUtilities.getNodeListByXPath(transformedDocument, HREF_ATT_XPATH);
544         int pos = 0;
545         for (int i = 0; i < atts.getLength(); i++) {
546             try {
547                 Attr att = (Attr) atts.item(i);
548                 pos = processHrefAtt(webBrowseAction, pos, att);
549             }
550             catch (Exception JavaDoc ex) {
551                 ex.printStackTrace();
552                 addParsingErrors("href attribute rewriting error: " + ex.toString());
553             }
554         }
555
556     }
557
558
559     /**
560      * Description of the Method
561      *
562      *@param webBrowseAction Description of Parameter
563      *@param pos Description of Parameter
564      *@param att Description of Parameter
565      *@return Description of the Returned Value
566      *@exception DOMException Description of Exception
567      *@exception Exception Description of Exception
568      */

569     private int processHrefAtt(String JavaDoc webBrowseAction, int pos, Attr att) throws
570             DOMException, Exception JavaDoc {
571         String JavaDoc ownerTagName = att.getOwnerElement().getTagName();
572         String JavaDoc position = "" + pos;
573         String JavaDoc id = att.getOwnerElement().getAttribute("id");
574         if (ownerTagName.equalsIgnoreCase("a")) {
575             String JavaDoc linkHash = HashUtilities.buildLinkHash(att.getNodeValue(), id, position);
576             if (id == null || id.equalsIgnoreCase("")) {
577                 att.getOwnerElement().setAttribute("id", linkHash);
578             }
579             att.getOwnerElement().removeAttribute("target");
580             pos++;
581         }
582         att.setNodeValue(getRewritedHrefValue(ownerTagName, att.getNodeValue(), id, position, webBrowseAction));
583         return pos;
584     }
585
586
587     /**
588      * Description of the Method
589      *
590      *@param doc Description of Parameter
591      */

592     private void processStyleAttribute(Document doc) {
593         logger.debug("[ Process href att ]");
594         NodeList atts = DomUtilities.getNodeListByXPath(doc, STYLE_ATT_XPATH);
595         for (int i = 0; i < atts.getLength(); i++) {
596             try {
597                 Attr att = (Attr) atts.item(i);
598                 att.setNodeValue(refactorCssStyleRule(att.getNodeValue()));
599             }
600             catch (Exception JavaDoc ex) {
601                 ex.printStackTrace();
602                 addParsingErrors("href attribute rewriting error: " + ex.toString());
603             }
604         }
605
606     }
607
608
609
610     /**
611      * Description of the Method
612      *
613      *@param doc Description of Parameter
614      *@param webBrowseAction Description of Parameter
615      */

616     private void processJavascriptEvent(Document doc, String JavaDoc webBrowseAction) {
617         logger.debug("[ Process href att ]");
618         NodeList atts = DomUtilities.getNodeListByXPath(doc, JAVASCRIPT_EVENT_XPATH);
619         for (int i = 0; i < atts.getLength(); i++) {
620             Attr att = (Attr) atts.item(i);
621             Element ownerElement = att.getOwnerElement();
622             try {
623                 // refactor
624
boolean refactor = isRefactorJavascriptEvent();
625                 if (refactor) {
626                     String JavaDoc url = getDocument().getUrlBean().getRedirectUrl().toExternalForm();
627                     String JavaDoc value = att.getValue();
628                     String JavaDoc newValue = getRefactoredJavascript(url, webBrowseAction, value);
629                     att.setValue(newValue);
630                 }
631                 else {
632                     // remove
633
boolean remove = isRemoveJavascriptEvent();
634                     if (remove) {
635                         ownerElement.removeAttributeNode(att);
636                     }
637                 }
638             }
639             catch (Exception JavaDoc ex) {
640                 ex.printStackTrace();
641                 addParsingErrors("href attribute rewriting error: " + ex.toString());
642             }
643         }
644
645     }
646
647
648
649     /**
650      * Rewrite action attribute
651      */

652     /*
653      * private void processActionAttribute(Document doc) {
654      * logger.debug("[ Process action att ]");
655      * NodeList atts = DomUtilities.getNodeListByXPath(doc, ACTION_ATT_XPATH);
656      * for (int i = 0; i < atts.getLength(); i++) {
657      * try {
658      * Node att = atts.item(i);
659      * att.setNodeValue(getRewritedActionValue(att.getNodeValue()));
660      * }
661      * catch (Exception ex) {
662      * addParsingErrors("Rewriting Action Attribute error: " + ex.toString());
663      * }
664      * }
665      * }
666      */

667     /**
668      * Rewrite src value
669      *
670      *@param doc Description of Parameter
671      */

672     private void processSrcAttribute(Document doc) {
673         logger.debug("[ Process action src ]");
674         NodeList atts = DomUtilities.getNodeListByXPath(doc, SRC_ATT_XPATH);
675         for (int i = 0; i < atts.getLength(); i++) {
676             Node att = atts.item(i);
677             try {
678                 att.setNodeValue(relatifToAbsolute(att.getNodeValue()));
679             }
680             catch (Exception JavaDoc ex) {
681                 ex.printStackTrace();
682                 addParsingErrors("Src rewriting error: " + ex.toString());
683             }
684         }
685
686     }
687
688
689     /**
690      * Description of the Method
691      *
692      *@param doc Description of Parameter
693      */

694     private void processTargetAttribute(Document doc) {
695         NodeList atts = DomUtilities.getNodeListByXPath(doc, TARGET_ATT_XPATH);
696         for (int i = 0; i < atts.getLength(); i++) {
697             Attr att = (Attr) atts.item(i);
698             Element ele = att.getOwnerElement();
699             ele.removeAttributeNode(att);
700         }
701
702     }
703
704
705     /**
706      * Description of the Method
707      *
708      *@param doc Description of Parameter
709      */

710     private void processBaseElement(Document doc) {
711         NodeList eles = DomUtilities.getNodeListByXPath(doc, BASE_ELE_XPATH);
712         for (int i = 0; i < eles.getLength(); i++) {
713             Element ele = (Element) eles.item(i);
714             ele.removeAttribute("href");
715         }
716
717     }
718
719
720     /**
721      * Description of the Method
722      *
723      *@param doc Description of Parameter
724      *@return Description of the Returned Value
725      */

726     private boolean processFrameElement(Document doc) {
727         NodeList eles = DomUtilities.getNodeListByXPath(doc, FRAME_ELE_XPATH);
728         boolean result = false;
729         List framesList = new ArrayList();
730         for (int i = 0; i < eles.getLength(); i++) {
731             Element ele = (Element) eles.item(i);
732             String JavaDoc name = ele.getAttribute("name");
733             String JavaDoc id = ele.getAttribute("id");
734             String JavaDoc src = ele.getAttribute("src");
735             result = true;
736
737             addFrame(framesList, name, id, src);
738         }
739         getDocument().setFramesList(framesList);
740         return result;
741     }
742
743
744
745     /**
746      * Description of the Method
747      *
748      *@param doc Description of Parameter
749      */

750     private void processSytleElement(Document doc) {
751         logger.debug("[ Process StyleTag ]");
752         NodeList eleList = DomUtilities.getNodeListByXPath(doc, this.SYTLE_ELE_XPATH);
753
754         for (int i = 0; i < eleList.getLength(); i++) {
755             Node ele = eleList.item(i);
756             NodeList children = ele.getChildNodes();
757             Node child = null;
758             //look for text node
759
for (int j = 0; j < children.getLength(); j++) {
760                 child = children.item(j);
761
762                 // style is set as text
763
if (child instanceof Text) {
764                     logger.debug("Sytle Text found");
765                     Text tChild = (Text) child;
766                     try {
767                         String JavaDoc content = getInlineCssContent(child.getNodeValue());
768                         Comment comment = doc.createComment(content);
769                         ele.appendChild(comment);
770                         ele.removeChild(tChild);
771                         break;
772                     }
773                     catch (Exception JavaDoc ex) {
774                         ex.printStackTrace();
775                     }
776                 }
777                 else {
778                     // style is set as Comment
779
if (child instanceof Comment) {
780                         logger.debug("Style set as comment");
781                         Comment cChild = (Comment) child;
782                         try {
783                             String JavaDoc comment = getInlineCssContent(cChild.getNodeValue());
784                             cChild.setNodeValue(comment);
785                         }
786                         catch (Exception JavaDoc ex) {
787                             ex.printStackTrace();
788                         }
789                     }
790                     else {
791                         logger.debug("Child is not a Text, it's a: " + child.toString());
792                     }
793                 }
794
795             }
796
797         }
798
799     }
800
801
802     /**
803      * Description of the Method
804      *
805      *@param doc Description of Parameter
806      */

807     private void processScriptElement(Document doc) {
808         logger.debug("[ Process StyleTag ]");
809         NodeList eleList = DomUtilities.getNodeListByXPath(doc, SCRIPT_ELE_XPATH);
810         for (int i = 0; i < eleList.getLength(); i++) {
811             Element ele = (Element) eleList.item(i);
812             if (isRemoveBodyScript() || isRemoveHeadScriptTag()) {
813                 ele.removeAttribute("src");
814             }
815
816             NodeList children = ele.getChildNodes();
817             Node child = null;
818             //Inline script
819
for (int j = 0; j < children.getLength(); j++) {
820                 child = children.item(j);
821                 if (isRemoveBodyScript() || isRemoveHeadScriptTag()) {
822                     ele.removeChild(child);
823                 }
824                 else {
825                     // style is set as text
826
if (child instanceof Text) {
827                         logger.debug("script Text found");
828                         Text tChild = (Text) child;
829                         try {
830                             String JavaDoc baseUrl = getUrlBean().getAbsoluteUrlValue();
831                             String JavaDoc refactoredJavascript = getRefactoredJavascript(baseUrl, Constants.WEB_BROWSER_SHOW_BROWSE, child.getNodeValue());
832                             Comment comment = doc.createComment(refactoredJavascript);
833                             ele.appendChild(comment);
834                             ele.removeChild(tChild);
835                             break;
836                         }
837                         catch (Exception JavaDoc ex) {
838                             ex.printStackTrace();
839                         }
840                     }
841                 }
842             }
843
844             //imported Script
845
String JavaDoc src = ele.getAttribute(HTML.Attribute.SRC.toString());
846             /*
847              * if (src != null && !src.equals("")) {
848              * // get content
849              * try {
850              * String content = getImportJavascriptContent(src, Constants.WEB_BROWSER_SHOW_BROWSE);
851              * // add it as comment
852              * Comment comment = doc.createComment(content);
853              * ele.removeAttribute(HTML.Attribute.SRC.toString());
854              * ele.appendChild(comment);
855              * }
856              * catch (Exception ex) {
857              * ex.printStackTrace();
858              * addParsingErrors("Extraction Javascript: " + ex.toString());
859              * }
860              * }
861              */

862         }
863
864     }
865
866
867
868     /**
869      * Extract css from link element and make it icluded css
870      *
871      *@param doc Description of Parameter
872      */

873     private void processLinkElement(Document doc) {
874         logger.debug("[ Process link element ]");
875         NodeList elements = DomUtilities.getNodeListByXPath(doc, LINK_ELE_XPATH);
876         for (int i = 0; i < elements.getLength(); i++) {
877             try {
878                 // link element --> style
879
Element ele = (Element) elements.item(i);
880                 Document ownerDoc = ele.getOwnerDocument();
881                 ((DocumentImpl) ownerDoc).renameNode(ele, "", "style");
882
883                 if (isEnableCSS()) {
884                     // refactor the content
885
String JavaDoc href = ele.getAttribute(HTML.Attribute.HREF.toString());
886                     String JavaDoc media = ele.getAttribute("media");
887                     ele.removeAttribute(HTML.Attribute.HREF.toString());
888                     ele.removeAttribute("media");
889                     String JavaDoc content = getImportCssContent(href, media);
890                     Comment comment = ownerDoc.createComment(content);
891                     ele.appendChild(comment);
892
893                 }
894                 else {
895                     // remove link tag
896
Node p = ele.getParentNode();
897                     p.removeChild(ele);
898                 }
899             }
900             catch (Exception JavaDoc ex) {
901                 ex.printStackTrace();
902                 addParsingErrors("CSS Parsing error: " + ex.toString());
903             }
904         }
905
906     }
907
908
909     /**
910      * Extract css from link element and make it icluded css
911      *
912      *@param doc Description of Parameter
913      *@param tagName Description of Parameter
914      */

915     private void processChewElement(Document doc, String JavaDoc tagName) {
916         logger.debug("[ Process link element ]");
917         NodeList elements = DomUtilities.getNodeListByXPath(doc, "//" + tagName);
918         for (int i = 0; i < elements.getLength(); i++) {
919             try {
920                 // Current chew element
921
Element ele = (Element) elements.item(i);
922                 ele.setAttribute(HTML.Attribute.BORDER.toString(), "1");
923                 addChewLinkElementChild(ele, i);
924             }
925             catch (Exception JavaDoc ex) {
926                 ex.printStackTrace();
927                 addParsingErrors("CSS Parsing error: " + ex.toString());
928             }
929         }
930
931     }
932
933
934
935     /**
936      * Description of the Method
937      *
938      *@param doc Description of Parameter
939      */

940     private void processInputElement(Document doc) {
941         NodeList formList = DomUtilities.getNodeListByXPath(doc, FORM_ELE_XPATH);
942         for (int formParentPos = 0; formParentPos < formList.getLength(); formParentPos++) {
943             Element form = (Element) formList.item(formParentPos);
944             String JavaDoc formParentName = form.getAttribute(HTML.Attribute.NAME.toString());
945             String JavaDoc formParentId = form.getAttribute(HTML.Attribute.ID.toString());
946
947             NodeList inputList = form.getElementsByTagName(HTML.Tag.INPUT.toString());
948             if (inputList == null) {
949                 logger.warn("Input list is empty for form whith name: " + formParentName);
950
951             }
952             else {
953                 for (int inputPos = 0; inputPos < inputList.getLength(); inputPos++) {
954                     try {
955                         Element ele = (Element) inputList.item(inputPos);
956                         String JavaDoc type = ele.getAttribute(HTML.Attribute.TYPE.toString());
957                         //logger.debug("[Input element found whith type: " + type + " ]");
958
String JavaDoc visibility = Boolean.TRUE.toString();
959                         // Test that the type is a "valid" one
960
//Get the properties of the param
961
String JavaDoc name = notNullValueForType(ele.getAttribute(HTML.Attribute.NAME.toString()));
962                         int pos = getPosAndUpdateInputHash(name);
963                         String JavaDoc possibleValue = notNullValueForType(ele.getAttribute(HTML.Attribute.VALUE.toString()));
964                         type = notNullValueForType(type);
965                         recordFormParam(type, formParentName, formParentId, formParentPos, name, possibleValue, visibility, pos);
966                     }
967                     catch (Exception JavaDoc ex) {
968                         addParsingErrors("Parser, Input Element: " + ex.toString());
969                     }
970
971                 }
972             }
973             // reset the input hash
974
resetInputHash();
975         }
976
977     }
978
979
980     /**
981      * Description of the Method
982      *
983      *@param doc Description of Parameter
984      */

985     private void processSelectElement(Document doc) {
986         NodeList formList = DomUtilities.getNodeListByXPath(doc, FORM_ELE_XPATH);
987
988         // iterate form Element
989
for (int formParentPos = 0; formParentPos < formList.getLength(); formParentPos++) {
990             Element form = (Element) formList.item(formParentPos);
991             String JavaDoc formParentName = form.getAttribute(HTML.Attribute.NAME.toString());
992             String JavaDoc formParentId = form.getAttribute(HTML.Attribute.ID.toString());
993
994             NodeList selectList = form.getElementsByTagName(HTML.Tag.SELECT.toString());
995             if (selectList == null) {
996                 logger.warn("Select list is empty for form whith name: " + formParentName);
997
998             }
999             else {
1000                //iterate select elemt
1001
for (int selectPos = 0; selectPos < selectList.getLength(); selectPos++) {
1002                    try {
1003                        Element ele = (Element) selectList.item(selectPos);
1004                        String JavaDoc type = WebConstants.TYPE_SELECT;
1005                        String JavaDoc visibility = Boolean.TRUE.toString();
1006                        String JavaDoc name = notNullValueForType(ele.getAttribute(HTML.Attribute.NAME.toString()));
1007                        type = notNullValueForType(type);
1008                        int pos = getPosAndUpdateInputHash(name);
1009                        //iterate option ele
1010
NodeList optionList = form.getElementsByTagName(HTML.Tag.OPTION.toString());
1011                        for (int optionPos = 0; optionPos < optionList.getLength(); optionPos++) {
1012                            Element optEle = (Element) optionList.item(optionPos);
1013                            String JavaDoc value = optEle.getAttribute("value");
1014                            String JavaDoc possibleValue = notNullValueForType(value);
1015                            recordFormParam(type, formParentName, formParentId, formParentPos, name, possibleValue, visibility, pos);
1016                        }
1017
1018                    }
1019                    catch (Exception JavaDoc ex) {
1020                        addParsingErrors("Parser, Input Element: " + ex.toString());
1021                    }
1022
1023                }
1024            }
1025
1026            //reset the input hash
1027
resetInputHash();
1028        }
1029
1030    }
1031
1032
1033
1034    /**
1035     * Description of the Method
1036     *
1037     *@param doc Description of Parameter
1038     */

1039    private void processUserInputElement(Document doc) {
1040        NodeList formList = DomUtilities.getNodeListByXPath(doc, FORM_ELE_XPATH);
1041        /*
1042         * String sizeForm = "" + formList.getLength();
1043         * logger.debug(sizeForm);
1044         */

1045        for (int formPos = 0; formPos < formList.getLength(); formPos++) {
1046            Element form = (Element) formList.item(formPos);
1047            String JavaDoc formParentName = form.getAttribute(HTML.Attribute.NAME.toString());
1048            String JavaDoc formParentId = form.getAttribute(HTML.Attribute.ID.toString());
1049
1050            NodeList inputList = form.getElementsByTagName(HTML.Tag.INPUT.toString());
1051            /*
1052             * String size = "" + inputList.getLength();
1053             * logger.debug(size);
1054             */

1055            if (inputList == null) {
1056                logger.warn("Input list is empty for form whith name: " + formParentName);
1057            }
1058            else {
1059
1060                for (int inputPos = 0; inputPos < inputList.getLength(); inputPos++) {
1061                    Element inputEle = (Element) inputList.item(inputPos);
1062                    String JavaDoc name = inputEle.getAttribute(HTML.Attribute.NAME.toString());
1063                    int pos = getPosAndUpdateInputHash(name);
1064                    FormParamBean fBean = getFormParamBean(formParentName, formParentId, formPos, name, pos);
1065                    String JavaDoc formHash = HashUtilities.buildFormHash(formParentName, formParentId, formPos);
1066
1067                    if (fBean == null) {
1068                        logger.warn("FormParamBean not found for hash " + formHash + " and input name: " + name + " whith type: " + inputEle.getAttribute("type"));
1069
1070                    }
1071                    else {
1072                        /*
1073                         * if (fBean.getType().equalsIgnoreCase(WebConstants.TYPE_SUBMIT)) {
1074                         * logger.warn("FormParamBean type = submit. Don't change the default value.");
1075                         * return;
1076                         * }
1077                         */

1078                        String JavaDoc type = fBean.getType();
1079
1080                        // manage automatic update
1081
String JavaDoc fBeanUpdate = fBean.getUpdate();
1082                        logger.debug("Param: " + fBean.getName() + " whith type: " + fBean.getType() + " update: " + fBeanUpdate);
1083                        if (fBeanUpdate.equalsIgnoreCase("true")) {
1084                            String JavaDoc inputValue = inputEle.getAttribute("value");
1085                            updateInputValue(fBean, inputValue);
1086
1087                        }
1088                        else {
1089                            logger.debug("FormParamBean found for hash " + formHash + " and input name " + name + " whith type " + inputEle.getAttribute("type"));
1090                            logger.debug("Used value: " + fBean.getUsedValue());
1091                            // inputEle.setAttribute("value", fBean.getUsedValue());
1092
}
1093                    }
1094                }
1095            }
1096            //reset the input hash
1097
resetInputHash();
1098        }
1099
1100    }
1101
1102
1103
1104    /**
1105     * Description of the Method
1106     *
1107     *@param doc Description of Parameter
1108     */

1109    private void processUserSelectElement(Document doc) {
1110        NodeList formList = DomUtilities.getNodeListByXPath(doc, FORM_ELE_XPATH);
1111        for (int formPos = 0; formPos < formList.getLength(); formPos++) {
1112            Element form = (Element) formList.item(formPos);
1113            String JavaDoc formParentName = form.getAttribute(HTML.Attribute.NAME.toString());
1114            String JavaDoc formParentId = form.getAttribute(HTML.Attribute.ID.toString());
1115            NodeList selectList = form.getElementsByTagName(HTML.Tag.SELECT.toString());
1116            if (selectList == null) {
1117                logger.warn("Input list is empty for form whith name: " + formParentName);
1118
1119            }
1120            else {
1121                //iterate select ele
1122
for (int selectPos = 0; selectPos < selectList.getLength(); selectPos++) {
1123                    Element selectEle = (Element) selectList.item(selectPos);
1124                    String JavaDoc name = selectEle.getAttribute(HTML.Attribute.NAME.toString());
1125                    int pos = getPosAndUpdateInputHash(name);
1126                    FormParamBean fBean = getFormParamBean(formParentName, formParentId, formPos, name, pos);
1127                    String JavaDoc formHash = HashUtilities.buildFormHash(formParentName, formParentId, formPos);
1128
1129                    if (fBean == null) {
1130                        logger.warn("FormParamBean not found for hash " + formHash + " and input name: " + name + " whith type: " + selectEle.getAttribute("type"));
1131                    }
1132                    else {
1133
1134                        logger.debug("FormParamBean found for hash " + formHash + " and input name " + name + " whith type " + selectEle.getAttribute("type"));
1135                        logger.debug("Used value: " + fBean.getUsedValue());
1136                        String JavaDoc usedValue = fBean.getUsedValue();
1137                        //iterate option ele in order to select the good option
1138
NodeList optionList = form.getElementsByTagName(HTML.Tag.OPTION.toString());
1139                        for (int optionPos = 0; optionPos < optionList.getLength(); optionPos++) {
1140                            Element optEle = (Element) optionList.item(optionPos);
1141                            String JavaDoc value = optEle.getAttribute("value");
1142                            if (value.equalsIgnoreCase(usedValue)) {
1143                                optEle.setAttribute("selected", "selected");
1144                            }
1145                            else {
1146
1147                                optEle.removeAttribute("selected");
1148                            }
1149                        }
1150                    }
1151                }
1152            }
1153
1154            //reset the input hash
1155
resetInputHash();
1156        }
1157
1158    }
1159
1160
1161
1162    /**
1163     * Description of the Method
1164     *
1165     *@param doc Description of Parameter
1166     *@exception DOMException Description of Exception
1167     */

1168    private void processTitleElement(DocumentImpl doc) throws DOMException {
1169        //remove title
1170
Element title = (Element) doc.getElementsByTagName(HTML.Tag.TITLE.toString()).item(0);
1171        if (title != null) {
1172            DomUtilities.removeNode(title);
1173            //doc.renameNode(doc, "", HTML.Tag.DIV.toString());
1174
}
1175        else {
1176            //addParsingErrors("Title element no found");
1177
}
1178    }
1179
1180
1181    /**
1182     * Process Form element
1183     *
1184     *@param doc Description of Parameter
1185     *@param webBrowserAction Description of Parameter
1186     */

1187    private void processFormElement(Document doc, String JavaDoc webBrowserAction) {
1188        // add action input elements params
1189

1190        NodeList formList = DomUtilities.getNodeListByXPath(doc, FORM_ELE_XPATH);
1191        for (int formPos = 0; formPos < formList.getLength(); formPos++) {
1192            try {
1193                Element formEle = (Element) formList.item(formPos);
1194                String JavaDoc url = formEle.getAttribute("action");
1195                String JavaDoc method = formEle.getAttribute("method");
1196                String JavaDoc formName = formEle.getAttribute("name");
1197                String JavaDoc formId = formEle.getAttribute("id");
1198                String JavaDoc enctype = formEle.getAttribute("enctype");
1199
1200                formEle.setAttribute("action", getRewritedActionValue(url, method, formName, formId, enctype, formPos, webBrowserAction));
1201
1202                // set method to post
1203
formEle.setAttribute("method", "post");
1204
1205                //remove att that redirects the responde to another window
1206
formEle.removeAttribute("target");
1207
1208                //remove att that is specific to the server that send the data
1209
formEle.removeAttribute("enctype");
1210
1211            }
1212            catch (Exception JavaDoc ex) {
1213                addParsingErrors("href attribute rewriting error: " + ex.toString());
1214            }
1215        }
1216    }
1217
1218
1219    /**
1220     * Description of the Method
1221     *
1222     *@param doc Description of Parameter
1223     *@param webBrowserAction Description of Parameter
1224     */

1225    private void processUserFormElement(Document doc, String JavaDoc webBrowserAction) {
1226        // add action input elements params
1227

1228        NodeList formList = DomUtilities.getNodeListByXPath(doc, FORM_ELE_XPATH);
1229        for (int formPos = 0; formPos < formList.getLength(); formPos++) {
1230            try {
1231                Element formEle = (Element) formList.item(formPos);
1232                String JavaDoc url = formEle.getAttribute("action");
1233                String JavaDoc method = formEle.getAttribute("method");
1234                String JavaDoc formName = formEle.getAttribute("name");
1235                String JavaDoc formId = formEle.getAttribute("id");
1236                String JavaDoc enctype = formEle.getAttribute("enctype");
1237
1238                formEle.setAttribute("action", getRewritedActionValue(url, method, formName, formId, enctype, formPos, webBrowserAction));
1239
1240                // set method to post
1241
formEle.setAttribute("method", "post");
1242
1243                //remove att that redirects the responde to another window
1244
formEle.removeAttribute("target");
1245
1246                //remove att that is specific to the server that send the data
1247
formEle.removeAttribute("enctype");
1248
1249                String JavaDoc continualClipping = getUrlBean().getClipperBean().getConfigurationBean().getPortletContinualClipping();
1250                // get the type of Continual clipping
1251
int ccInt = Integer.parseInt(continualClipping);
1252
1253                // refactor
1254
switch (ccInt) {
1255                    case
1256                            org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_FALSE:
1257
1258                        // remove link
1259
//tag.removeAttribute("href");
1260
break;
1261                    case
1262                            org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_POPUP:
1263
1264                        // open in popup
1265
formEle.setAttribute("target", "_blank");
1266                    //refactor in order to redirect to clipbuilder
1267
case
1268                            org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_POPUP:
1269
1270                        // open in popup
1271
formEle.setAttribute("target", "_blank");
1272
1273                        // do'nt redirect to clipbuilder
1274
if (method != null) {
1275                            formEle.setAttribute("method", method);
1276                        }
1277                        if (enctype != null) {
1278                            formEle.setAttribute("enctype", enctype);
1279                        }
1280                        formEle.setAttribute("action", relatifToAbsolute(url));
1281
1282                    case
1283                            org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_IFRAME:
1284                        break;
1285                    case
1286                            org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_IFRAME:
1287                        // set params value in order to not redirect
1288
if (method != null) {
1289                            formEle.setAttribute("method", method);
1290                        }
1291                        if (enctype != null) {
1292                            formEle.setAttribute("enctype", enctype);
1293                        }
1294                        formEle.setAttribute("action", relatifToAbsolute(url));
1295
1296                        break;
1297                }
1298
1299            }
1300            catch (Exception JavaDoc ex) {
1301                addParsingErrors("href attribute rewriting error: " + ex.toString());
1302            }
1303        }
1304    }
1305
1306
1307
1308    /**
1309     * Description of the Method
1310     *
1311     *@param ele Description of Parameter
1312     */

1313    private void insertLabel(Element ele) {
1314        String JavaDoc type = ele.getAttribute(HTML.Attribute.TYPE.toString());
1315        String JavaDoc name = ele.getAttribute(HTML.Attribute.NAME.toString());
1316        String JavaDoc value = ele.getAttribute(HTML.Attribute.VALUE.toString());
1317
1318        //Create a lebel element
1319
Document doc = ele.getOwnerDocument();
1320        Element labelEle = createInputLabel(doc, type, name, value);
1321
1322        //add label before input
1323
ele.getParentNode().insertBefore(labelEle, ele);
1324    }
1325
1326
1327
1328    /**
1329     * Description of the Method
1330     *
1331     *@param originalDoc Description of Parameter
1332     *@param type Description of Parameter
1333     *@param name Description of Parameter
1334     *@param value Description of Parameter
1335     *@return Description of the Returned Value
1336     */

1337    private Element createInputLabel(Document originalDoc, String JavaDoc type, String JavaDoc name, String JavaDoc value) {
1338        //New <i> Element
1339
Element labelEle = originalDoc.createElement(HTML.Tag.I.toString());
1340        //add text to <i>
1341
Text text = originalDoc.createTextNode("[Param=" + name + "]");
1342        if (type != null && type.equalsIgnoreCase(WebConstants.TYPE_RADIO)) {
1343            text = originalDoc.createTextNode("[Param=" + name + ", value=\'" + value + "\']");
1344        }
1345        labelEle.appendChild(text);
1346        return labelEle;
1347    }
1348
1349}
1350
Popular Tags