KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.clipbuilder.html.web.html.Impl.ExtractorFilter;
2
3 import java.io.*;
4
5 import org.jahia.clipbuilder.html.util.*;
6 import org.jahia.clipbuilder.html.web.html.*;
7 import org.jahia.clipbuilder.html.web.html.Impl.*;
8 import org.w3c.dom.*;
9 import org.xml.sax.*;
10
11 /**
12  * Description of the Class
13  *
14  *@author Tlili Khaled
15  */

16 public class XPathExtractorFilter extends AbsctractExtractoreFilter {
17     private int mode;
18     private final String JavaDoc NAME = "XPathExtractorFilter";
19     private static final String JavaDoc XPATH_KEY = "xPathKey";
20     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(XPathExtractorFilter.class);
21     private static final String JavaDoc XPATH_STYLE = "//style";
22     private static final String JavaDoc XPATH_JAVASCRIPT = "//script";
23
24
25     /**
26      * Constructor for the XPathExtractorFilter object
27      */

28     public XPathExtractorFilter() {
29         super("XPathExtractorFilter");
30     }
31
32
33     /**
34      * Sets the Mode attribute of the XPathExtractorFilter object
35      *
36      *@param mode The new Mode value
37      */

38     public void setMode(int mode) {
39         this.mode = mode;
40     }
41
42
43
44     /**
45      * Sets the XPahKey attribute of the XPathExtractorFilter object
46      *
47      *@param xPathKey The new XPahKey value
48      */

49     public void setXPahKey(String JavaDoc xPathKey) {
50         super.addKeyPart(XPATH_KEY, xPathKey);
51     }
52
53
54     /**
55      * Gets the Name attribute of the XPathExtractorFilter object
56      *
57      *@return The Name value
58      */

59     public String JavaDoc getName() {
60         return NAME;
61     }
62
63
64     /**
65      * Gets the Mode attribute of the XPathExtractorFilter object
66      *
67      *@return The Mode value
68      */

69     public int getMode() {
70         return mode;
71     }
72
73
74     /**
75      * Gets the XPahKey attribute of the XPathExtractorFilter object
76      *
77      *@return The XPahKey value
78      */

79     public String JavaDoc getXPahKey() {
80         return super.getKeyPart(XPATH_KEY);
81     }
82
83
84     /**
85      * Gets the SelectedPart attribute of the XPathExtractorFilter object
86      *
87      *@param doc Description of Parameter
88      *@param action Description of Parameter
89      *@return The SelectedPart value
90      */

91     public String JavaDoc getSelectedPart(HTMLDocument doc, int action) {
92         String JavaDoc html = "";
93         if (action == ACTION_BUILD_KEY_PART) {
94             // don't update parameters values ang get selected part
95
html = doc.getTransformedDocumentAsString();
96         }
97         else if (action == ACTION_RETRIEVE_SELECTED_PART) {
98             // update parameters values ang get selected part
99
html = doc.getUserDocumentAsString();
100         }
101
102         try {
103             // Get default parser
104
HTMLParser parser = new DefaultHTMLParser();
105
106             // build the transformed document
107

108             org.w3c.dom.Document JavaDoc w3cDoc = parser.parse(html);
109             if (w3cDoc == null) {
110                 logger.error("[Parsing failed !!!!]");
111             }
112             else {
113                 logger.debug("[Parsing finish whith succes !!!!]");
114             }
115
116             Document selectedPartDoc = new org.apache.xerces.dom.DocumentImpl();
117
118             // put the resutl in a div element
119
Element root = selectedPartDoc.createElement("div");
120
121             switch (getMode()) {
122                 case MODE_CSS:
123                     // add css
124
NodeList cssList = DomUtilities.getNodeListByXPath(w3cDoc, XPATH_STYLE);
125                     for (int i = 0; i < cssList.getLength(); i++) {
126                         Node node = cssList.item(i).cloneNode(true);
127                         Node clone = selectedPartDoc.importNode(node, true);
128                         root.appendChild(clone);
129                     }
130
131                 case MODE_WHITOUT_CSS:
132                 // nothing special
133

134                 default:
135                     //add selected node
136
NodeList list = DomUtilities.getNodeListByXPath(w3cDoc, getXPahKey());
137                     for (int i = 0; i < list.getLength(); i++) {
138                         Node node = list.item(i).cloneNode(true);
139                         Node clone = selectedPartDoc.importNode(node, true);
140                         root.appendChild(clone);
141                     }
142             }
143
144             //build the document as string
145
selectedPartDoc.appendChild(root);
146             String JavaDoc result = DomUtilities.getDocumentAsString(selectedPartDoc);
147             return result;
148         }
149         catch (SAXException ex) {
150
151             ex.printStackTrace();
152             return "Error has occured: " + this.getXPahKey();
153         }
154         catch (IOException ex) {
155             ex.printStackTrace();
156             return "Error has occured: " + this.getXPahKey();
157         }
158     }
159
160 }
161
Popular Tags