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 16 public class XPathExtractorFilter extends AbsctractExtractoreFilter { 17 private int mode; 18 private final String NAME = "XPathExtractorFilter"; 19 private static final String XPATH_KEY = "xPathKey"; 20 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(XPathExtractorFilter.class); 21 private static final String XPATH_STYLE = "//style"; 22 private static final String XPATH_JAVASCRIPT = "//script"; 23 24 25 28 public XPathExtractorFilter() { 29 super("XPathExtractorFilter"); 30 } 31 32 33 38 public void setMode(int mode) { 39 this.mode = mode; 40 } 41 42 43 44 49 public void setXPahKey(String xPathKey) { 50 super.addKeyPart(XPATH_KEY, xPathKey); 51 } 52 53 54 59 public String getName() { 60 return NAME; 61 } 62 63 64 69 public int getMode() { 70 return mode; 71 } 72 73 74 79 public String getXPahKey() { 80 return super.getKeyPart(XPATH_KEY); 81 } 82 83 84 91 public String getSelectedPart(HTMLDocument doc, int action) { 92 String html = ""; 93 if (action == ACTION_BUILD_KEY_PART) { 94 html = doc.getTransformedDocumentAsString(); 96 } 97 else if (action == ACTION_RETRIEVE_SELECTED_PART) { 98 html = doc.getUserDocumentAsString(); 100 } 101 102 try { 103 HTMLParser parser = new DefaultHTMLParser(); 105 106 108 org.w3c.dom.Document 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 Element root = selectedPartDoc.createElement("div"); 120 121 switch (getMode()) { 122 case MODE_CSS: 123 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 134 default: 135 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 selectedPartDoc.appendChild(root); 146 String 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 |