KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > web > html > Impl > ExtractorFilter > FormExtractorFilter


1 package org.jahia.clipbuilder.html.web.html.Impl.ExtractorFilter;
2
3 import java.io.*;
4
5 import javax.swing.text.html.*;
6
7 import org.jahia.clipbuilder.html.util.*;
8 import org.jahia.clipbuilder.html.web.html.HTMLDocument;
9 import org.htmlparser.Parser;
10 import org.htmlparser.filters.*;
11 import org.htmlparser.util.*;
12
13 /**
14  * Description of the Class
15  *
16  *@author Tlili Khaled
17  */

18 public class FormExtractorFilter extends AbsctractExtractoreFilter {
19     private String JavaDoc key;
20     private final String JavaDoc NAME = "FormExtractorFilter";
21     private static org.apache.log4j.Logger logger =
22             org.apache.log4j.Logger.getLogger(FormExtractorFilter.class);
23
24
25     /**
26      * Constructor for the FormExtractorFilter object
27      */

28     public FormExtractorFilter() {
29         super("FormExtractorFilter");
30     }
31
32
33     /**
34      * Constructor for the FormExtractorFilter object
35      *
36      *@param name Description of Parameter
37      *@param id Description of Parameter
38      *@param position Description of Parameter
39      */

40     public FormExtractorFilter(String JavaDoc name, String JavaDoc id, int position) {
41         super("FormExtractorFilter");
42         String JavaDoc key = buildKey(name, id, position);
43         this.addKeyPart(key);
44     }
45
46
47     /**
48      * Gets the Name attribute of the FormExtractorFilter object
49      *
50      *@return The Name value
51      */

52     public String JavaDoc getName() {
53         return NAME.toString();
54     }
55
56
57     /**
58      * Gets the SelectedPart attribute of the HTMLFilter object
59      *
60      *@param doc Description of Parameter
61      *@param action Description of Parameter
62      *@return The SelectedPart value
63      */

64     public String JavaDoc getSelectedPart(HTMLDocument doc, int action) {
65         String JavaDoc selectedPart = "";
66         String JavaDoc encoded = "";
67         String JavaDoc html = "";
68         if (action == ACTION_BUILD_KEY_PART) {
69             // don't update parameters values ang get selected part
70
html = doc.getTransformedDocumentAsString();
71         }
72         else if (action == ACTION_RETRIEVE_SELECTED_PART) {
73             // update parameters values ang get selected part
74
html = doc.getUserDocumentAsString();
75         }
76
77         Parser parser = Parser.createParser(html, encoded);
78         try {
79             switch (getMode()) {
80                 case MODE_CSS:
81
82                     // add css tag
83
TagNameFilter styleFilter = new TagNameFilter(HTML.Tag.STYLE.toString());
84                     NodeList styleList = parser.parse(styleFilter);
85
86                     //concat all form tagfound
87
for (int i = 0; i < styleList.size(); i++) {
88                         selectedPart = selectedPart + styleList.elementAt(i).toHtml();
89                     }
90
91                 case MODE_WHITOUT_CSS:
92                 // nothing special
93

94                 default:
95
96                     //add selected node
97
logger.debug("build selected part");
98                     parser.reset();
99                     // build a filter that accept only form tag
100
TagNameFilter formFilter = new TagNameFilter(HTML.Tag.FORM.toString());
101                     NodeList formList = parser.parse(formFilter);
102
103                     //concat all form tagfound
104
for (int i = 0; i < formList.size(); i++) {
105                         selectedPart = selectedPart + formList.elementAt(i).toHtml();
106                     }
107                     encoded = selectedPart;
108
109             }
110         }
111
112         catch (ParserException ex) {
113             ex.printStackTrace();
114         }
115
116         return encoded;
117     }
118
119
120     /**
121      * Gets the KeyPart attribute of the HTMLFilter object
122      *
123      *@return The KeyPart value
124      */

125     public String JavaDoc getKeyPart() {
126         return key;
127     }
128
129
130     /**
131      * Sets the Key attribute of the HTMLFilter object
132      *
133      *@param key The new Key value
134      */

135     public void addKeyPart(String JavaDoc key) {
136         this.key = key;
137     }
138
139
140     /**
141      * Description of the Method
142      *
143      *@param name Description of Parameter
144      *@param id Description of Parameter
145      *@param position Description of Parameter
146      *@return Description of the Returned Value
147      */

148     private String JavaDoc buildKey(String JavaDoc name, String JavaDoc id, int position) {
149         return HashUtilities.buildFormHash(name, id, position);
150     }
151
152 }
153
Popular Tags