KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.clipbuilder.html.web.html.Impl.ExtractorFilter;
2
3 import org.jahia.clipbuilder.html.web.Constant.*;
4 import org.jahia.clipbuilder.html.web.html.*;
5 import org.htmlparser.*;
6 import org.htmlparser.util.*;
7 import org.jahia.clipbuilder.html.util.*;
8 import org.htmlparser.tags.*;
9 import org.jahia.clipbuilder.html.web.html.Impl.ExtractorFilter.util.*;
10 import org.apache.log4j.Logger;
11 import org.htmlparser.filters.*;
12 import org.htmlparser.util.NodeList;
13 import org.htmlparser.scanners.ScriptScanner;
14 import org.htmlparser.visitors.NodeVisitor;
15
16 /**
17  * Description of the Class
18  *
19  *@author Tlili Khaled
20  */

21 public class ChewExtractorFilter extends AbsctractExtractoreFilter {
22
23     /**
24      * Description of the Field
25      */

26     public static final String JavaDoc NAME = "ChewExtractorFilter";
27     /**
28      * Description of the Field
29      */

30     public static final String JavaDoc CHEW_HASH = "chewId";
31     /**
32      * Description of the Field
33      */

34     public static final String JavaDoc CHEW_TAG_NAME = "chewTagName";
35     /**
36      * Description of the Field
37      */

38     public static final String JavaDoc NAME_INPUT_PARAM = "chewId";
39     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ChewExtractorFilter.class);
40
41
42     /**
43      * Constructor for the FormExtractorFilter object
44      */

45     public ChewExtractorFilter() {
46         super("chewTagName");
47         ScriptScanner.STRICT = false;
48     }
49
50
51
52     /**
53      * Sets the HashKey attribute of the ChewExtractorFilter object
54      *
55      *@param hashKey The new HashKey value
56      */

57     public void setHashKey(String JavaDoc hashKey) {
58         addKeyPart(ChewExtractorFilter.CHEW_HASH, hashKey);
59     }
60
61
62     /**
63      * Sets the TagNameKey attribute of the ChewExtractorFilter object
64      *
65      *@param tagNameKey The new TagNameKey value
66      */

67     public void setTagNameKey(String JavaDoc tagNameKey) {
68         addKeyPart(ChewExtractorFilter.CHEW_TAG_NAME, tagNameKey);
69     }
70
71
72
73     /**
74      * Gets the SelectedPart attribute of the ChewExtractorFilter object
75      *
76      *@param doc Description of Parameter
77      *@param action Description of Parameter
78      *@return The SelectedPart value
79      */

80     public String JavaDoc getSelectedPart(HTMLDocument doc, int action) {
81         String JavaDoc selectedPart = "";
82         String JavaDoc html = "";
83         if (action == ACTION_BUILD_KEY_PART) {
84             // don't update parameters values ang get selected part
85
html = doc.getTransformedDocumentAsString();
86         }
87         else if (action == ACTION_RETRIEVE_SELECTED_PART) {
88             // update parameters values ang get selected part
89
html = doc.getUserDocumentAsString();
90         }
91
92         try {
93             Parser parser = ParserUtils.createParserParsingAnInputString(html);
94             switch (getMode()) {
95                 case MODE_CSS:
96                     // add css tag
97
TagNameFilter linkFilter = new TagNameFilter("style");
98                     TagNameFilter styleFilter = new TagNameFilter("link");
99                     OrFilter cssFilter = new OrFilter(linkFilter, styleFilter);
100                     NodeList listCss = parser.parse(cssFilter);
101                     //concat all form tagfound
102
for (int i = 0; i < listCss.size(); i++) {
103                         selectedPart = selectedPart + listCss.elementAt(i).toHtml();
104                     }
105
106                 case MODE_WHITOUT_CSS:
107                 // nothing special
108

109                 default:
110                     //add selected node
111
logger.debug("build selected part");
112                     parser.reset();
113                     ChewBuilderVisitor chewVisitor = new ChewBuilderVisitor();
114                     parser.visitAllNodesWith(chewVisitor);
115                     selectedPart = selectedPart + chewVisitor.getFirstNode().toHtml();
116                     logger.debug(selectedPart);
117                     logger.debug("end build selected part");
118
119             }
120
121         }
122         catch (Exception JavaDoc ex) {
123             ex.printStackTrace();
124             logger.error("buldKeySelectedPart error " + ex.getMessage());
125         }
126
127         return selectedPart;
128     }
129
130
131
132     /**
133      * Gets the HashKey attribute of the ChewExtractorFilter object
134      *
135      *@return The HashKey value
136      */

137     public String JavaDoc getHashKey() {
138         return getKeyPart(CHEW_HASH);
139     }
140
141
142     /**
143      * Gets the TagNameKey attribute of the ChewExtractorFilter object
144      *
145      *@return The TagNameKey value
146      */

147     public String JavaDoc getTagNameKey() {
148         return getKeyPart(CHEW_TAG_NAME);
149     }
150
151
152     /**
153      * Gets the HashParamName attribute of the ChewExtractorFilter class
154      *
155      *@return The HashParamName value
156      */

157     public static String JavaDoc getHashParamName() {
158         return NAME_INPUT_PARAM;
159     }
160
161
162
163     /**
164      * Description of the Class
165      *
166      *@author Tlili Khaled
167      */

168     class ChewBuilderVisitor extends NodeVisitor {
169         private int countChewTag = 0;
170         private Node firstNode;
171
172
173         /**
174          * Constructor for the ChewBuilderVisitor object
175          */

176         public ChewBuilderVisitor() {
177             ScriptScanner.STRICT = false;
178         }
179
180
181         /**
182          * Description of the Method
183          *
184          *@param tag Description of Parameter
185          */

186         public void visitTag(Tag tag) {
187             try {
188                 processViewMode(tag);
189             }
190             catch (Exception JavaDoc ex) {
191                 logger.error("Error during extarction");
192                 ex.printStackTrace();
193             }
194         }
195
196
197         /**
198          * Description of the Method
199          *
200          *@param string Description of Parameter
201          */

202         public void visitStringNode(Text string) {
203             //logger.debug(string);
204
}
205
206
207         /**
208          * Sets the FirstNode attribute of the ChewBuilderVisitor object
209          *
210          *@param node The new FirstNode value
211          */

212         private void setFirstNode(Node node) {
213             this.firstNode = node;
214         }
215
216
217         /**
218          * Gets the FirstNode attribute of the ChewBuilderVisitor object
219          *
220          *@return The FirstNode value
221          */

222         private Node getFirstNode() {
223             return firstNode;
224         }
225
226
227         /**
228          * Description of the Method
229          *
230          *@param tag Description of Parameter
231          *@exception Exception Description of Exception
232          */

233         private void processViewMode(Tag tag) throws Exception JavaDoc {
234             String JavaDoc tagName = getTagNameKey();
235
236             // process chew tag
237
if (tag.getTagName().equalsIgnoreCase(tagName)) {
238                 String JavaDoc idAtt = tag.getAttribute("id");
239                 String JavaDoc hash = HashUtilities.buildTagHash(idAtt, countChewTag);
240                 if (hash.equalsIgnoreCase(getHashKey())) {
241                     setFirstNode(tag);
242                 }
243                 countChewTag++;
244             }
245
246         }
247
248     }
249
250 }
251
Popular Tags