KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > web > html > Impl > JDom > JDomHTMLTransformer


1 package org.jahia.clipbuilder.html.web.html.Impl.JDom;
2
3 import java.util.*;
4 import java.util.Iterator JavaDoc;
5
6 import javax.swing.text.html.*;
7
8 import org.jahia.clipbuilder.html.web.*;
9 import org.jahia.clipbuilder.html.bean.*;
10 import org.jahia.clipbuilder.html.util.*;
11 import org.jahia.clipbuilder.html.web.Url.*;
12 import org.jahia.clipbuilder.html.web.html.Impl.*;
13 import org.jahia.clipbuilder.html.web.Constant.*;
14 import org.jahia.clipbuilder.html.web.Url.URLWrapper;
15
16 import org.jdom.*;
17 import org.jahia.clipbuilder.html.struts.Util.Constants;
18 import org.jahia.clipbuilder.html.web.Url.UrlEncoderIF;
19
20 /**
21  * !!! This class is not complete and doesn't work
22  *
23  *@author Tlili Khaled
24  */

25 public class JDomHTMLTransformer extends DefaultHTMLTransformer {
26     private JDomHTMLDocument document;
27     private String JavaDoc formParentId = "";
28     private int formParentPos = 0;
29     private UrlEncoderIF encoder;
30     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(JDomHTMLTransformer.class);
31
32
33     /**
34      * Constructor for the JDomHTMLTransformer object
35      *
36      *@param builder Description of Parameter
37      *@param jdoc Description of Parameter
38      *@param enableCSS Description of Parameter
39      *@param processTransform Description of Parameter
40      */

41     public JDomHTMLTransformer(HTMLDocumentBuilder builder, JDomHTMLDocument jdoc, boolean enableCSS, boolean processTransform) {
42         super(builder, enableCSS);
43         setDocument(jdoc);
44         if (processTransform) {
45             buildTransformedDocument();
46         }
47
48     }
49
50
51
52     /**
53      * Sets the document attribute of the JDomHTMLTransformer object
54      *
55      *@param jdoc The new Jdoc value
56      */

57     public void setDocument(JDomHTMLDocument jdoc) {
58         this.document = jdoc;
59         jdoc.setTransformer(this);
60     }
61
62
63
64     /**
65      * Sets the DocumentWhithParamsLabel attribute of the JDomHTMLTransformer
66      * object
67      *
68      *@param documentWhithParamsLabel The new DocumentWhithParamsLabel value
69      */

70     public void setDocumentWhithParamsLabel(org.jdom.Document documentWhithParamsLabel) {
71         getDocument().setDocumentWhithParamsLabel(documentWhithParamsLabel);
72     }
73
74
75     /**
76      * Sets the TransformedDocument attribute of the JDomHTMLTransformer object
77      *
78      *@param transformedDocument The new TransformedDocument value
79      */

80     public void setTransformedDocument(org.jdom.Document transformedDocument) {
81         getDocument().setTransformedDocument(transformedDocument);
82     }
83
84
85     /**
86      * Sets the UserDocument attribute of the JDomHTMLTransformer object
87      *
88      *@param userDocument The new UserDocument value
89      */

90     public void setUserDocument(org.jdom.Document userDocument) {
91         getDocument().setUserDocument(userDocument);
92     }
93
94
95
96     /**
97      * Sets the ChewDocument attribute of the JDomHTMLTransformer object
98      *
99      *@param doc The new ChewDocument value
100      */

101     public void setChewDocument(org.jdom.Document doc) {
102         this.getDocument().setChewDocument(doc);
103     }
104
105
106     /**
107      * Sets the UrlEncoder attribute of the JDomHTMLTransformer object
108      *
109      *@param encoder The new UrlEncoder value
110      */

111     public void setUrlEncoder(UrlEncoderIF encoder) {
112         this.encoder = encoder;
113     }
114
115
116     /**
117      * Gets the document attribute of the JDomHTMLTransformer object
118      *
119      *@return The Jdoc value
120      */

121     public JDomHTMLDocument getDocument() {
122         return document;
123     }
124
125
126
127     /**
128      * Gets the DocumentWhithParamsLabel attribute of the JDomHTMLTransformer
129      * object
130      *
131      *@return The DocumentWhithParamsLabel value
132      */

133     public org.jdom.Document getDocumentWhithParamsLabel() {
134         return this.getDocument().getDocumentWhithParamsLabel();
135     }
136
137
138     /**
139      * Gets the UrlBean attribute of the JDomHTMLTransformer object
140      *
141      *@return The UrlBean value
142      */

143     public UrlBean getUrlBean() {
144         return getDocument().getUrlBean();
145     }
146
147
148     /**
149      * Gets the UrlEncoder attribute of the JDomHTMLTransformer object
150      *
151      *@return The UrlEncoder value
152      */

153     public UrlEncoderIF getUrlEncoder() {
154         return encoder;
155     }
156
157
158     /**
159      * Description of the Method
160      */

161     public void buildDocumentWhithLabel() {
162         if (hasBeenTransformed()) {
163             // Get a clone of the root
164
Element root = getTransformedDocument().getRootElement();
165             Element rootClone = cloneWithoutChidren(root);
166             // Set the root of the document whit label
167
org.jdom.Document doc = new org.jdom.Document();
168             doc.setRootElement(rootClone);
169             setDocumentWhithParamsLabel(doc);
170             // process only the children whith input name
171
processOnlyInputChildren(root, rootClone);
172
173         }
174         else {
175             logger.error("[ Document not yet transformed ]");
176         }
177
178     }
179
180
181     /**
182      * Create the Transformed Document
183      */

184     public void buildTransformedDocument() {
185
186         logger.debug("[Begin process transform] ");
187
188         //prepare the original document (rewriting url and extract the css)
189
org.jdom.Document originalDoc = getDocument().getOriginalDocument();
190
191         // rewrite url, record input param
192
walkThrougthChildrenForTransform(originalDoc.getRootElement(), "", 0);
193
194         //Extract body content
195
org.jdom.Document transformedDocument = (Document) getDocument().getOriginalDocument().clone();
196
197         //Rename html into div
198
Element html = processHTMLElement(transformedDocument);
199
200         //rename head into div
201
processHeadElement(html);
202
203         // remove tile
204
processTitleElement(html);
205
206         //rename body into div
207
processBodyElement(transformedDocument);
208
209         // set the document
210
setTransformedDocument(transformedDocument);
211
212         formParentPos = 0;
213         logger.debug("[Process transform finish swhith success ");
214
215     }
216
217
218     /**
219      * Description of the Method
220      */

221     public void buildUserDocument() {
222         logger.debug("[Begin process transform] ");
223
224         //prepare the original document (rewriting url and extract the css)
225
org.jdom.Document originalDoc = getDocument().getOriginalDocument();
226
227         // rewrite url, record input param
228
walkThrougthChildrenForUser(originalDoc.getRootElement(), "", 0);
229
230         //Extract body content
231
org.jdom.Document userDocument = (Document) getDocument().getOriginalDocument().clone();
232
233         // set the document
234
setUserDocument(userDocument);
235
236         logger.debug("[Process transform finish swhith success ");
237
238     }
239
240
241
242     /**
243      * Description of the Method
244      *
245      *@param tagName Description of Parameter
246      */

247     public void buildChewDocument(String JavaDoc tagName) {
248         logger.debug("[ Begin chew transform ]");
249
250         //prepare the original document (rewriting url and extract the css)
251
Element chewRoot = walkThrougthChildrenForChew(getDocument().getTransformedDocument().getRootElement(), 0);
252
253         // set the chew transformed document
254
setChewDocument(new org.jdom.Document(chewRoot));
255
256         logger.debug("[Building chew document finish whith success ");
257
258     }
259
260
261     /**
262      * test if the document has been transformed
263      *
264      *@return true if the document has been transformed else false
265      */

266     public boolean hasBeenTransformed() {
267         return (getTransformedDocument() != null);
268     }
269
270
271     /**
272      * Gets the TransformedDocument attribute of the JDomHTMLTransformer object
273      * The transformed document is the document with the p element as root and
274      * whith url ref encoded. The p element has same content than body.
275      *
276      *@return The TransformedDocument value
277      */

278     private org.jdom.Document getTransformedDocument() {
279
280         return this.getDocument().getTransformedDocument();
281     }
282
283
284     /**
285      * Gets the ElementByTag attribute of the JDomHTMLTransformer object
286      *
287      *@param parent Description of Parameter
288      *@param tag Description of Parameter
289      *@return The ElementByTag value
290      */

291     private Element getElementByTag(Element parent, String JavaDoc tag) {
292         List children = parent.getChildren();
293         Iterator JavaDoc i = children.iterator();
294         while (i.hasNext()) {
295             Element ele = (Element) i.next();
296             if (ele.getName().equalsIgnoreCase(tag)) {
297                 logger.debug("[" + tag + " Element found]");
298                 return ele;
299             }
300             logger.debug("[Parent Element is " + ele.getName() + " ]");
301         }
302         logger.error("[" + tag + " Element not found]");
303         return null;
304     }
305
306
307     /**
308      * Description of the Method
309      *
310      *@param transformedDocument Description of Parameter
311      *@return Description of the Returned Value
312      */

313     private Element processHTMLElement(Document transformedDocument) {
314         Element html = null;
315         try {
316             //change tag HTML-->div
317
html = transformedDocument.getRootElement();
318             html.setName(HTML.Tag.DIV.toString());
319             // add selector
320
html.setAttribute("class", CssUtilities.HTML_CLASS);
321             return html;
322         }
323         catch (Exception JavaDoc ex) {
324             addParsingErrors("HTML: " + ex.toString());
325         }
326         finally {
327             return html;
328         }
329
330     }
331
332
333     /**
334      * Description of the Method
335      *
336      *@param html Description of Parameter
337      */

338     private void processHeadElement(Element html) {
339         //remove head
340
try {
341             Element head = html.getChild(HTML.Tag.HEAD.toString());
342             head.setName(HTML.Tag.DIV.toString());
343         }
344         catch (Exception JavaDoc ex) {
345             addParsingErrors("Head: " + ex.toString());
346         }
347
348     }
349
350
351     /**
352      * Description of the Method
353      *
354      *@param html Description of Parameter
355      */

356     private void processTitleElement(Element html) {
357         //remove title
358
try {
359             Element title = html.getChild(HTML.Tag.TITLE.toString());
360             Parent parentTitle = title.getParent();
361             parentTitle.removeContent(title);
362         }
363         catch (Exception JavaDoc ex) {
364             logger.warn("Title not found: " + ex.toString());
365         }
366
367     }
368
369
370     /**
371      * Description of the Method
372      *
373      *@param transformedDocument Description of Parameter
374      */

375     private void processBodyElement(Document transformedDocument) {
376         try {
377             //hange tag body-->div
378
Element body = null;
379             body = getElementByTag(transformedDocument.getRootElement(), HTML.Tag.BODY.toString());
380             body.setName(HTML.Tag.DIV.toString());
381             Attribute classAtt = body.getAttribute(HTML.Attribute.CLASS.toString());
382             if (classAtt == null) {
383                 body.setAttribute("class", CssUtilities.BODY_CLASS);
384             }
385             else {
386                 String JavaDoc classValue = classAtt.getValue();
387                 body.setAttribute("class", CssUtilities.BODY_CLASS + " " + classValue);
388             }
389         }
390         catch (Exception JavaDoc ex) {
391             addParsingErrors("Exception: " + ex.toString());
392         }
393
394     }
395
396
397
398     /**
399      * Description of the Method
400      *
401      *@param ele Description of Parameter
402      *@param position Description of Parameter
403      *@return Description of the Returned Value
404      */

405     private Element createLinkChewEle(Element ele, int position) {
406         logger.debug("Chew Ele tags.");
407
408         //create a link element
409
Element linkEle = new Element("a");
410         String JavaDoc idAtt = ele.getAttributeValue("id");
411         String JavaDoc hash = HashUtilities.buildTagHash(idAtt, position);
412         linkEle.setAttribute(HTML.Attribute.HREF.toString(), getHrefChewLink(hash));
413         // image
414
Element imgEle = new Element("img");
415         imgEle.setAttribute(HTML.Attribute.SRC.toString(), getImageChewLinkSrcValue());
416         //add link and image
417
linkEle.addContent(imgEle);
418
419         //chewEle.setContent(content);
420
Element chewEleRes = (Element) ele.clone();
421         chewEleRes.removeContent();
422         chewEleRes.addContent(linkEle);
423         return chewEleRes;
424     }
425
426
427     /**
428      * clone an element and remove it's children
429      *
430      *@param ele Description of Parameter
431      *@return Description of the Returned Value
432      */

433     private Element cloneWithoutChidren(Element ele) {
434         Element clone = (Element) ele.clone();
435         clone.detach();
436         List children = clone.getChildren();
437         children.removeAll(children);
438         return clone;
439     }
440
441
442     /**
443      * Process onl the input element. Add the label of the input Element
444      *
445      *@param parent Parent Element
446      *@param parentClone Description of Parameter
447      */

448     private void processOnlyInputChildren(Element parent, Element parentClone) {
449         logger.debug("[process " + parent.getQualifiedName() + "]");
450         //go throw all the children
451
List children = parent.getContent();
452         Iterator JavaDoc i = children.iterator();
453         while (i.hasNext()) {
454             Content content = (Content) i.next();
455             if (content instanceof Element) {
456                 Element ele = (Element) content;
457                 //Get a clone
458
Element clone = cloneWithoutChidren(ele);
459                 processOnlyInputChildren(ele, clone);
460                 // set the clone as child of the parent clone at same position
461
parentClone.addContent(clone);
462                 //process input element
463
if (ele.getName().equalsIgnoreCase(HTML.Tag.INPUT.toString())) {
464                     // Test that the type is a "valid" one
465
String JavaDoc type = ele.getAttributeValue(HTML.Attribute.TYPE.toString());
466                     if (editableInputParameter(type)) {
467                         addParameterLabel(clone);
468                     }
469                 }
470             }
471             else {
472                 parentClone.addContent((Content) content.clone());
473             }
474         }
475     }
476
477
478
479     /**
480      * Process the children in order to put the document in the correct context
481      *
482      *@param parent Description of Parameter
483      *@param formParentName Description of Parameter
484      *@param nbLink Description of Parameter
485      */

486     private void walkThrougthChildrenForTransform(Element parent, String JavaDoc formParentName, int nbLink) {
487         //logger.debug("[process " + parent.getQualifiedName() + "]");
488
String JavaDoc parentName = parent.getName();
489
490         //go throw all the children
491
List children = parent.getChildren();
492         Iterator JavaDoc i = children.iterator();
493         while (i.hasNext()) {
494             try {
495                 Element ele = (Element) i.next();
496                 walkThrougthChildrenForTransform(ele, formParentName, nbLink);
497
498                 // process ele whith attribut href
499
if (ele.getName().equalsIgnoreCase(HTML.Tag.META.toString())) {
500                     processMetaElement(ele);
501                 }
502
503                 // process ele whith attribut href
504
if (ele.getAttributeValue("href") != null) {
505                     // don't process Css here
506
if (!(ele.getAttributeValue("rel") != null && ele.getAttributeValue("rel").equalsIgnoreCase("stylesheet"))) {
507                         processTagWhithHrefAttribute(ele, nbLink);
508                         nbLink++;
509                     }
510
511                 }
512
513                 // process ele whith attribut src
514
if (ele.getAttributeValue("src") != null) {
515                     processTagWhithSrcAttribute(ele);
516                 }
517
518                 // process ele whith attribut action
519
if (ele.getAttributeValue("action") != null) {
520                     formParentName = ele.getAttributeValue("name");
521                     if (formParentName == null) {
522                         formParentName = "";
523                     }
524                     formParentId = ele.getAttributeValue("id");
525                     if (formParentId == null) {
526                         formParentId = "";
527                     }
528
529                     processTagFORM(ele);
530                     formParentPos++;
531                 }
532
533                 //process input element
534
if (ele.getName().equalsIgnoreCase(HTML.Tag.INPUT.toString())) {
535                     int paramPosition = 0;
536                     processInputElement(ele, formParentName, formParentId, formParentPos, paramPosition);
537                 }
538
539                 // linked css
540
if (ele.getName().equalsIgnoreCase(HTML.Tag.LINK.toString())) {
541                     String JavaDoc relValue = ele.getAttributeValue(HTML.Attribute.REL.toString());
542                     if (relValue != null && relValue.equalsIgnoreCase("stylesheet")) {
543                         processLinkElement(ele);
544                     }
545                 }
546
547                 // inline css
548
if (ele.getName().equalsIgnoreCase(HTML.Tag.STYLE.toString())) {
549                     processStyleElement(ele);
550                 }
551
552                 // extract javascript
553
if (ele.getName().equalsIgnoreCase(HTML.Tag.SCRIPT.toString())) {
554                     if (ele.getAttributeValue(HTML.Attribute.SRC.toString()) != null) {
555                         processImportedJavascript(ele);
556                     }
557                     else {
558                         processIncludedJavascript(parentName, ele);
559                     }
560
561                 }
562             }
563             catch (WebClippingException ex) {
564                 addParsingErrors(ex.getType() + ": " + ex.toString());
565             }
566             catch (Exception JavaDoc ex) {
567                 addParsingErrors("Exception: " + ex.toString());
568             }
569         }
570
571     }
572
573
574     /**
575      * Description of the Method
576      *
577      *@param parent Description of Parameter
578      *@param formParentName Description of Parameter
579      *@param nbLink Description of Parameter
580      */

581     private void walkThrougthChildrenForUser(Element parent, String JavaDoc formParentName, int nbLink) {
582         //logger.debug("[process " + parent.getQualifiedName() + "]");
583
String JavaDoc parentName = parent.getName();
584
585         //go throw all the children
586
List children = parent.getChildren();
587         Iterator JavaDoc i = children.iterator();
588         while (i.hasNext()) {
589             try {
590                 Element ele = (Element) i.next();
591                 walkThrougthChildrenForTransform(ele, formParentName, nbLink);
592
593                 // process ele whith attribut href
594
if (ele.getName().equalsIgnoreCase(HTML.Tag.META.toString())) {
595                     processMetaElement(ele);
596                 }
597
598                 // process ele whith attribut href
599
if (ele.getAttributeValue("href") != null) {
600                     // don't process Css here
601
if (!(ele.getAttributeValue("rel") != null && ele.getAttributeValue("rel").equalsIgnoreCase("stylesheet"))) {
602                         processTagWhithHrefAttribute(ele, nbLink);
603                         nbLink++;
604                     }
605
606                 }
607
608                 // process ele whith attribut action
609
if (ele.getAttributeValue("action") != null) {
610                     formParentName = ele.getAttributeValue("name");
611                     if (formParentName == null) {
612                         formParentName = "";
613                     }
614                     formParentId = ele.getAttributeValue("id");
615                     if (formParentId == null) {
616                         formParentId = "";
617                     }
618
619                     processTagFORM(ele);
620                     formParentPos++;
621                 }
622
623                 //process input element
624
if (ele.getName().equalsIgnoreCase(HTML.Tag.INPUT.toString())) {
625                     int paramPosition = 0;
626                     processInputElement(ele, formParentName, formParentId, formParentPos, paramPosition);
627                 }
628
629             }
630             catch (WebClippingException ex) {
631                 addParsingErrors(ex.getType() + ": " + ex.toString());
632             }
633             catch (Exception JavaDoc ex) {
634                 addParsingErrors("Exception: " + ex.toString());
635             }
636         }
637
638     }
639
640
641
642     /**
643      * Description of the Method
644      *
645      *@param parent Description of Parameter
646      *@param position Description of Parameter
647      *@return Description of the Returned Value
648      */

649     private Element walkThrougthChildrenForChew(Element parent, int position) {
650         logger.debug("[ Walk througth Children For Chew ]");
651
652         Element root = (Element) parent.clone();
653         //update the root
654
root.removeContent();
655         root.setText(parent.getTextTrim());
656
657         //go throw all the children
658
List children = parent.getContent();
659         Iterator JavaDoc i = children.iterator();
660         while (i.hasNext()) {
661             Content content = (Content) i.next();
662             if (content instanceof Element) {
663                 Element ele = (Element) (content.clone());
664                 if (ele.getName().equalsIgnoreCase(
665                         HTML.Tag.TABLE.toString())) {
666                     //process td element
667
Element chewEle = createLinkChewEle(ele, position);
668                     chewEle.setAttribute(HTML.Attribute.BORDER.toString(), "1");
669                     chewEle.addContent(walkThrougthChildrenForChew(ele, position));
670                     root.addContent(chewEle);
671                     position++;
672                 }
673                 else {
674                     Element clone = walkThrougthChildrenForChew(ele, position);
675                     root.addContent(clone);
676                 }
677             }
678             else {
679                 root.addContent((Content) content.clone());
680             }
681         }
682
683         return root;
684     }
685
686
687     /**
688      * Deal with CSS
689      *
690      *@param ele link element
691      *@exception WebClippingException Description of Exception
692      */

693     private void processLinkElement(Element ele) throws WebClippingException {
694         if (isEnableCSS()) {
695             try {
696                 // link --> Style
697
ele.setName(HTML.Tag.STYLE.toString());
698
699                 //get the content of the css
700
String JavaDoc href = ele.getAttributeValue("href");
701                 String JavaDoc media = ele.getAttributeValue("media");
702
703                 // remove content and attribute
704
List atts = ele.getAttributes();
705                 for (int i = 0; i < atts.size(); i++) {
706                     ele.removeAttribute((Attribute) atts.get(i));
707                 }
708                 ele.removeContent();
709
710                 //add to style content
711
logger.debug("[ Linked CSS --> included Css ]");
712                 String JavaDoc content = getImportCssContent(href, media);
713                 Comment style = new Comment(content);
714                 ele.addContent(style);
715                 ele.setText(content);
716
717             }
718             catch (WebClippingException ex) {
719                 throw ex;
720             }
721             catch (Exception JavaDoc ex) {
722                 throw new WebClippingException("Link", ex);
723             }
724         }
725         // dont't process link
726
else {
727             logger.debug("[ CSS removed ]");
728         }
729
730     }
731
732
733     /**
734      * Description of the Method
735      *
736      *@param ele Description of Parameter
737      *@exception WebClippingException Description of Exception
738      */

739     private void processStyleElement(Element ele) throws WebClippingException {
740
741         try {
742             //get the content of the css
743
logger.debug("[ Process Inline css ]");
744             String JavaDoc content = getInlineCssContent(ele.getTextTrim());
745             ele.setText(content);
746
747         }
748         catch (WebClippingException ex) {
749             throw ex;
750         }
751         catch (Exception JavaDoc ex) {
752             throw new WebClippingException("Style", ex);
753         }
754
755     }
756
757
758
759     /**
760      * Imported javascript --> included javascript
761      *
762      *@param ele Description of Parameter
763      *@exception Exception Description of Exception
764      */

765     private void processImportedJavascript(Element ele) throws Exception JavaDoc {
766         String JavaDoc src = ele.getAttributeValue("src");
767         ele.setText(getImportJavascriptContent(src, Constants.WEB_BROWSER_SHOW_BROWSE));
768         ele.removeAttribute("src");
769     }
770
771
772     /**
773      * Description of the Method
774      *
775      *@param parentName Description of Parameter
776      *@param scriptEle Description of Parameter
777      */

778     private void processIncludedJavascript(String JavaDoc parentName, Element scriptEle) {
779         //Scripts in the head section will be loaded and then executed when CALLED
780
if (parentName.equalsIgnoreCase(HTML.Tag.BODY.toString())) {
781             return;
782         }
783
784     }
785
786
787     /**
788      * Redirect the href path to the current context
789      *
790      *@param ele element whith name = a
791      *@param position Description of Parameter
792      *@exception WebClippingException Description of Exception
793      */

794     private void processTagWhithHrefAttribute(Element ele, int position) throws WebClippingException {
795         try {
796             String JavaDoc href = ele.getAttributeValue("href");
797             String JavaDoc pos = "" + position;
798             String JavaDoc tagName = ele.getName();
799             String JavaDoc tagId = ele.getAttributeValue("id");
800             if (tagId == null) {
801                 ele.setAttribute("id", HashUtilities.buildLinkHash(href, tagId, pos));
802             }
803
804             ele.setAttribute("href", this.getRewritedHrefValue(tagName, href, tagId, pos, Constants.WEB_BROWSER_SHOW_BROWSE));
805
806         }
807         catch (Exception JavaDoc ex) {
808             throw new WebClippingException("Attribute Href", ex);
809         }
810     }
811
812
813     /**
814      * Rewrite the form Element
815      *
816      *@param ele Description of Parameter
817      *@exception WebClippingException Description of Exception
818      */

819     private void processTagFORM(Element ele) throws WebClippingException {
820         try {
821             // encode the url
822
String JavaDoc url = ele.getAttributeValue("action");
823             String JavaDoc method = ele.getAttributeValue("method");
824             String JavaDoc formName = ele.getAttributeValue("name");
825             String JavaDoc formId = ele.getAttributeValue("id");
826             String JavaDoc enctype = ele.getAttributeValue("enctype");
827             ele.setAttribute("action", getRewritedActionValue(url, method, formName, formId, enctype, formParentPos, Constants.WEB_BROWSER_SHOW_BROWSE));
828             ele.setAttribute("method", "post");
829
830             // add action input elements params
831
/*
832              * List listAtt = getNewActionInputAtts();
833              * Attribute[] att = new Attribute[listAtt.size()];
834              * for (int i = 0; i < att.length; i++) {
835              * String[] attArray = (String[]) listAtt.get(i);
836              * att[i] = new Attribute(attArray[0], attArray[1]);
837              * }
838              * ele.addContent(0, buildInputElement(att));
839              * // add sourceUrl input elements params
840              * listAtt = getNewSourceUrlInputAtts(url);
841              * att = new Attribute[listAtt.size()];
842              * for (int i = 0; i < att.length; i++) {
843              * String[] attArray = (String[]) listAtt.get(i);
844              * att[i] = new Attribute(attArray[0], attArray[1]);
845              * }
846              * ele.addContent(0, buildInputElement(att));
847              * // add httpMethod input elements params
848              * String value = ele.getAttributeValue(
849              * HTML.Attribute.METHOD.toString());
850              * if (value == null || value.equalsIgnoreCase("")) {
851              * value = "GET";
852              * }
853              * listAtt = getNewHttpMethosInputAtts(value);
854              * att = new Attribute[listAtt.size()];
855              * for (int i = 0; i < att.length; i++) {
856              * String[] attArray = (String[]) listAtt.get(i);
857              * att[i] = new Attribute(attArray[0], attArray[1]);
858              * }
859              * ele.addContent(0, buildInputElement(att));
860              * //add form hash
861              * String linkHash = ele.getAttributeValue("name");
862              * if (linkHash == null) {
863              * linkHash = "";
864              * }
865              * listAtt = this.getNewLinkHashInputAtts(linkHash);
866              * att = new Attribute[listAtt.size()];
867              * for (int i = 0; i < att.length; i++) {
868              * String[] attArray = (String[]) listAtt.get(i);
869              * att[i] = new Attribute(attArray[0], attArray[1]);
870              * }
871              * ele.addContent(0, buildInputElement(att));
872              */

873         }
874         catch (Exception JavaDoc ex) {
875             throw new WebClippingException("Form tag", ex);
876         }
877
878     }
879
880
881     /**
882      * Description of the Method
883      *
884      *@param ele Description of Parameter
885      */

886     private void processMetaElement(Element ele) {
887         //Get the http-equiv value
888
String JavaDoc httpEquiv = ele.getAttributeValue(HTML.Attribute.HTTPEQUIV.toString());
889         if (httpEquiv != null) {
890             //process the refresh
891
if ("refresh".equals(httpEquiv.toLowerCase())) {
892                 String JavaDoc content = ele.getAttributeValue(HTML.Attribute.CONTENT.toString());
893                 //Get the url of the refresh
894
int urlIndex = content.toLowerCase().indexOf("url");
895                 if (urlIndex != -1) {
896                     //Get the content just before the url
897
String JavaDoc contentBeforeURL = content.substring(0, urlIndex).trim();
898                     logger.debug("[ Content before url " + contentBeforeURL + " ]");
899
900                     //Get the url
901
int equalIndex = urlIndex + 3;
902                     String JavaDoc contentURL = content.substring(equalIndex + 1).trim();
903                     logger.debug("[ BEFORE meta http-equiv=[" + httpEquiv + "] content=[" + content + "] ]");
904                     logger.debug("[ Refresh url is [" + contentURL + " ] ]");
905
906                     //Encode the url
907
String JavaDoc refreshURL = URLUtilities.getHrefAbsoluteValue(this.getDocument().getUrlBean().getBaseURL(), contentURL);
908                     getDocument().getUrlBean().setRedirectUrl(URLUtilities.getURL(refreshURL));
909                     String JavaDoc[] params = {refreshURL, "metaLink"};
910                     String JavaDoc hash = HashUtilities.buildLinkHash(refreshURL, "", "");
911                     URLWrapper urlWrapper = new URLWrapper(refreshURL, WebConstants.FROM_META, "GET", "", hash);
912                     // UrlEncoder encoder = new UrlEncoder(getHttpServletRequest(), getHttpServletResponse(), "browse", urlWrapper);
913
UrlEncoderIF encoder = getUrlEncoder();
914                     encoder.setAction("browse");
915                     encoder.setUrlWrapper(urlWrapper);
916
917                     String JavaDoc encodedUrl = encoder.getEncodedUrl();
918
919                     //Set the new value whith the encoded url
920
ele.setAttribute(HTML.Attribute.CONTENT.toString(), contentBeforeURL + "url=" + encodedUrl);
921                     logger.debug("[ AFTER meta http-equiv=[" + httpEquiv + "] content=[" + contentBeforeURL + "url=" + encodedUrl + "] ]+");
922                 }
923             }
924         }
925
926     }
927
928
929
930     /**
931      * process Input tag. Add "permitted" parameter to clipper.
932      *
933      *@param ele element whith name = "input"
934      *@param formParentName Description of Parameter
935      *@param formParentId Description of Parameter
936      *@param formParentPos Description of Parameter
937      *@param paramPosition Description of Parameter
938      *@exception WebClippingException Description of Exception
939      */

940     private void processInputElement(Element ele, String JavaDoc formParentName, String JavaDoc formParentId, int formParentPos, int paramPosition) throws WebClippingException {
941         try {
942             String JavaDoc type = ele.getAttributeValue(HTML.Attribute.TYPE.toString());
943             String JavaDoc visibility = Boolean.TRUE.toString();
944             type = notNullValueForType(type);
945
946             //Get the properties of the param
947
String JavaDoc name = notNullValueForType(ele.getAttributeValue(HTML.Attribute.NAME.toString()));
948             String JavaDoc possibleValue = notNullValueForType(ele.getAttributeValue(HTML.Attribute.VALUE.toString()));
949
950             //logger.debug("[Input element found whith type: " + type + " ]";
951
recordFormParam(type, formParentName, formParentId, formParentPos, name, possibleValue, visibility, paramPosition);
952         }
953         catch (Exception JavaDoc ex) {
954             throw new WebClippingException("Parser, Input Element", ex);
955         }
956
957     }
958
959
960
961     /**
962      * Adds a feature to the ParameterLabel attribute of the JDomHTMLTransformer
963      * object
964      *
965      *@param ele The feature to be added to the ParameterLabel attribute
966      */

967     private void addParameterLabel(Element ele) {
968         Element parentEle = ele.getParentElement();
969         int pos = parentEle.indexOf(ele);
970         String JavaDoc type = ele.getAttributeValue(HTML.Attribute.TYPE.toString());
971         String JavaDoc name = ele.getAttributeValue(HTML.Attribute.NAME.toString());
972         String JavaDoc value = ele.getAttributeValue(HTML.Attribute.VALUE.toString());
973
974         //create the label
975
// add the label element
976
Element labelEle = new Element(HTML.Tag.I.toString());
977         labelEle.setText(getLabelText(name, value, type));
978         parentEle.addContent(pos, labelEle);
979     }
980
981
982
983     /**
984      * build and input elememt
985      *
986      *@param att attributes of the element Description of the Returned Value
987      *@return element whith name input and whith attributes att
988      */

989     private Element buildInputElement(Attribute[] att) {
990         Element inputElement = new Element(HTML.Tag.INPUT.toString());
991         for (int i = 0; i < att.length; i++) {
992             inputElement.setAttribute(att[i]);
993         }
994         return inputElement;
995     }
996
997
998     /**
999      * replace the src relative path by an absolute path
1000     *
1001     *@param ele element whit a src attribute
1002     *@exception WebClippingException Description of Exception
1003     */

1004    private void processTagWhithSrcAttribute(Element ele) throws WebClippingException {
1005        try {
1006            String JavaDoc src = ele.getAttributeValue("src");
1007            ele.setAttribute("src", relatifToAbsolute(src));
1008        }
1009        catch (Exception JavaDoc ex) {
1010            throw new WebClippingException("Attribut src", ex);
1011        }
1012    }
1013
1014}
1015
Popular Tags