ktlili 2005/06/07 19:44:40 CEST
Modified files:
war/src/java/com/jahia/clipping/web WebBrowserSimulator.java
war/src/java/com/jahia/clipping/web/html/Impl
DefaultHTMLTransformer.java
StringTreeExtractorFilter.java
war/src/java/com/jahia/portlet ClipperPortlet.java
war/src/webapp/jsp editParams.jsp
Log:
Add Feature:
- extract selected.
- show all / show part
- fix bug href rewriting
- fix bug edit parameter
Revision Changes Path
1.2 +37 -5
webclip_portlet/war/src/java/com/jahia/clipping/web/WebBrowserSimulator.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/webclip_portlet/war/src/java/com/jahia/clipping/web/WebBrowserSimulator.java.diff?r1=1.1&r2=1.2&f=h
1.2 +5 -0
webclip_portlet/war/src/java/com/jahia/clipping/web/html/Impl/DefaultHTMLTransformer.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/webclip_portlet/war/src/java/com/jahia/clipping/web/html/Impl/DefaultHTMLTransformer.java.diff?r1=1.1&r2=1.2&f=h
1.2 +554 -455
webclip_portlet/war/src/java/com/jahia/clipping/web/html/Impl/StringTreeExtractorFilter.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/webclip_portlet/war/src/java/com/jahia/clipping/web/html/Impl/StringTreeExtractorFilter.java.diff?r1=1.1&r2=1.2&f=h
1.2 +60 -15
webclip_portlet/war/src/java/com/jahia/portlet/ClipperPortlet.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/webclip_portlet/war/src/java/com/jahia/portlet/ClipperPortlet.java.diff?r1=1.1&r2=1.2&f=h
1.2 +1 -1 webclip_portlet/war/src/webapp/jsp/editParams.jsp
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/webclip_portlet/war/src/webapp/jsp/editParams.jsp.diff?r1=1.1&r2=1.2&f=h
Index: WebBrowserSimulator.java
===================================================================
RCS file:
/home/cvs/repository/webclip_portlet/war/src/java/com/jahia/clipping/web/WebBrowserSimulator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- WebBrowserSimulator.java 3 Jun 2005 17:03:26 -0000 1.1
+++ WebBrowserSimulator.java 7 Jun 2005 17:44:39 -0000 1.2
@@ -6,6 +6,7 @@
import java.util.Map;
import java.util.Hashtable;
import java.util.*;
+import com.jahia.clipping.web.html.ExtractorFilter;
/**
* Description of the Class
@@ -37,12 +38,9 @@
/**
* Sets the UserPreferences attribute of the WebBrowserSimulator object
*
- [EMAIL PROTECTED] request The new UserPreference value
*/
-
-
/**
* Sets the Request attribute of the WebBrowserSimulator object
*
@@ -76,6 +74,8 @@
logger.error("[ ClipperBean is null]");
}
}
+
+
/**
* Gets the HTMLPart attribute of the WebBrowserSimulator object
*
@@ -84,7 +84,40 @@
public String getHTMLPart() {
logger.debug(" Extract html part");
String html = getFinalHtmlResponse(request, response);
- return html;
+ //Get Filter
+ String selectedPart = null;
+ boolean errorHasOccured = false;
+ try {
+ ClipperBean bean =
SessionManager.getClipperBean(getRequest());
+ if (bean == null) {
+ errorHasOccured = true;
+ logger.error("[ClipperBean not found]");
+ }
+ ExtractorFilter filter = bean.getFilter();
+ logger.debug("[Filter class: " +
filter.getClass().toString() + " ]");
+
+ // Get the target document
+ UrlBean uBean = bean.getLastRecordedUrlBean();
+ HTMLDocument doc = uBean.getDocument();
+
+ if (doc == null) {
+ logger.debug("[ Url is: " +
bean.getLastRecordedUrlBean().getURL() + " ]");
+ logger.error("[ Document is null ]");
+ }
+
+ //Get the selected part
+ selectedPart = filter.getSelectedPart(doc);
+ }
+ catch (Exception ex) {
+ ex.printStackTrace();
+ selectedPart = " Error has occured";
+ }
+ finally {
+
+ //logger.debug("[Selected part is] --> " +
selectedPart);
+ return selectedPart;
+ }
+
}
@@ -193,7 +226,6 @@
-
/**
* Description of the Method
*
Index: DefaultHTMLTransformer.java
===================================================================
RCS file:
/home/cvs/repository/webclip_portlet/war/src/java/com/jahia/clipping/web/html/Impl/DefaultHTMLTransformer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultHTMLTransformer.java 3 Jun 2005 17:03:27 -0000 1.1
+++ DefaultHTMLTransformer.java 7 Jun 2005 17:44:40 -0000 1.2
@@ -130,10 +130,15 @@
[EMAIL PROTECTED] Exception Description of Exception
*/
public String getRewritedHrefValue(String href) throws Exception {
+ // test if it's an url
+ if (URLUtilities.isRelatif(href) || (href!=null &&
href.substring(4).equalsIgnoreCase("http"))) {
href = URLUtilities.getHrefAbsoluteValue(getUrlBean().getURL(),
href);
String[] params = {href};
UrlEncoder encoder = new
UrlEncoder(getRenderRequest(),getRenderResponse(),params);
return encoder.getEncodedUrl();
+ }else{
+ return href;
+ }
}
Index: StringTreeExtractorFilter.java
===================================================================
RCS file:
/home/cvs/repository/webclip_portlet/war/src/java/com/jahia/clipping/web/html/Impl/StringTreeExtractorFilter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- StringTreeExtractorFilter.java 3 Jun 2005 17:03:27 -0000 1.1
+++ StringTreeExtractorFilter.java 7 Jun 2005 17:44:40 -0000 1.2
@@ -1,17 +1,17 @@
package com.jahia.clipping.web.html.Impl;
import com.jahia.clipping.web.html.*;
-import org.w3c.dom.Document;
-import org.jdom.*;
-import org.jdom.input.*;
-import java.io.*;
-import org.xml.sax.*;
import java.util.*;
-import java.io.*;
import com.jahia.clipping.util.*;
-import javax.swing.text.html.HTML;
-import com.jahia.clipping.web.html.Impl.*;
import com.jahia.clipping.util.*;
+import org.htmlparser.*;
+import org.htmlparser.*;
+import org.htmlparser.*;
+import org.htmlparser.util.*;
+import org.htmlparser.*;
+import org.htmlparser.tags.*;
+import org.htmlparser.visitors.*;
+import javax.swing.text.html.HTML;
/**
* JDom implementation of HTMLFilter
@@ -20,452 +20,551 @@
*/
public class StringTreeExtractorFilter implements ExtractorFilter {
- /**
- * Description of the Method
- *
- [EMAIL PROTECTED] ele Description of Parameter
- [EMAIL PROTECTED] Description of the Returned Value
- */
- private String concat = "";
- private HTMLDocument doc;
- private String selectedPart = "<p> Can't retrieve the selected part
</p>";
- private String keyPart;
- private Hashtable stringElementHash = new Hashtable();
- private static org.apache.log4j.Logger logger =
org.apache.log4j.Logger.getLogger(StringTreeExtractorFilter.class);
-
-
- /**
- * Constructor for the JDomHTMLFilter object
- */
- public StringTreeExtractorFilter() {
-
- }
-
-
- /**
- * Constructor for the StringTreeFilter object
- *
- [EMAIL PROTECTED] originalSelectedPart Description of Parameter
- */
- public StringTreeExtractorFilter(String originalSelectedPart) {
- buildKeySelectedPart(originalSelectedPart);
- }
-
-
- /**
- * Constructor for the StringTreeHTMLFilter object
- *
- [EMAIL PROTECTED] doc Description of Parameter
- [EMAIL PROTECTED] originalSelectedPart Description of Parameter
- */
- public StringTreeExtractorFilter(HTMLDocument doc, String
originalSelectedPart) {
- setDoc(doc);
- buildKeySelectedPart(originalSelectedPart);
- }
-
-
- /**
- * Sets the KeyPart attribute of the StringTreeFilter object
- *
- [EMAIL PROTECTED] keyPart The new KeyPart value
- */
- public void setKeyPart(String keyPart) {
- this.keyPart = keyPart;
- }
-
-
- /**
- * Sets the Jdoc attribute of the StringTreeHTMLFilter object
- *
- [EMAIL PROTECTED] doc The new Doc value
- */
- public void setDoc(HTMLDocument doc) {
- this.doc = doc;
- }
-
-
- /**
- * Sets the StringElementHash attribute of the StringTreeHTMLFilter
object
- *
- [EMAIL PROTECTED] stringElementHash The new StringElementHash value
- */
- public void setStringElementHash(Hashtable stringElementHash) {
- this.stringElementHash = stringElementHash;
- }
-
-
- /**
- * Sets the SelectedPart attribute of the StringTreeHTMLFilter object
- *
- [EMAIL PROTECTED] selectedPart The new SelectedPart value
- */
- public void setSelectedPart(String selectedPart) {
- this.selectedPart = selectedPart;
- }
-
-
- /**
- * Sets the Key attribute of the StringTreeHTMLFilter object
- *
- [EMAIL PROTECTED] key The new Key value
- */
- public void setKey(String key) {
- this.keyPart = key;
- }
-
-
- /**
- * Gets the SelectedPart attribute of the StringTreeFilter object
- *
- [EMAIL PROTECTED] The SelectedPart value
- */
- public String getSelectedPart() {
- return selectedPart;
- }
-
-
- /**
- * Gets the KeyPart attribute of the StringTreeFilter object
- *
- [EMAIL PROTECTED] The KeyPart value
- */
- public String getKeyPart() {
- return keyPart;
- }
-
-
- /**
- * Gets the Jdoc attribute of the StringTreeHTMLFilter object
- *
- [EMAIL PROTECTED] The Jdoc value
- */
- public HTMLDocument getDoc() {
- return doc;
- }
-
-
- /**
- * Gets the StringElementHash attribute of the StringTreeHTMLFilter
object
- *
- [EMAIL PROTECTED] The StringElementHash value
- */
- public Hashtable getStringElementHash() {
- return stringElementHash;
- }
-
-
- /**
- * Gets the SelectedPart attribute of the JDomHTMLFilter object
- *
- [EMAIL PROTECTED] doc HTMLDocument object that have the instance of a
- * JDomHTMLDocument
- [EMAIL PROTECTED] The SelectedPart value
- */
- public String getSelectedPart(HTMLDocument doc) {
- //filter the document
- processFiltering(doc);
- return selectedPart;
- }
-
-
- /**
- * Gets the BodyDiv attribute of the StringTreeExtractorFilter object
- *
- [EMAIL PROTECTED] jDoc Description of Parameter
- [EMAIL PROTECTED] The BodyDiv value
- */
- public Element getBodyDiv(org.jdom.Document jDoc) {
- logger.debug("[ Looking for body div class]");
- Element root = jDoc.getRootElement();
- List children = root.getChildren();
- for (int i = 0; i < children.size(); i++) {
- Element ele = (Element) children.get(i);
- if
(ele.getName().equalsIgnoreCase(HTML.Tag.DIV.toString())) {
- logger.debug("[ It as a div element ]");
- String classAtt =
ele.getAttributeValue(HTML.Attribute.CLASS.toString());
- logger.debug("[ class attribute value is: " +
classAtt + "]");
- if (classAtt != null) {
- logger.debug("[ class attribute value
is: " + classAtt + ", " +
classAtt.indexOf(com.jahia.clipping.util.CssUtilities.BODY_CLASS) + "]");
- if
(classAtt.indexOf(com.jahia.clipping.util.CssUtilities.BODY_CLASS) > -1) {
- logger.debug("[ bodyDiv element
founded ]");
- return ele;
- }
- }
-
- }
- }
- return null;
- }
-
-
- /**
- * Description of the Method
- *
- [EMAIL PROTECTED] originalSelectedPart Description of Parameter
- */
- public void buildKeySelectedPart(String originalSelectedPart) {
- try {
- //build a jdom document of the selected part
- DefaultHTMLParser parser = new DefaultHTMLParser();
-
- // get the HTMLDocument
- org.jdom.Document jdoc = new
DOMBuilder().build(parser.parse(originalSelectedPart));
- Element body =
jdoc.getRootElement().getChild(HTML.Tag.BODY.toString());
- setKeyPart(buildStringMapFromHTMLDocument(body));
- }
- catch (IOException ex) {
- logger.error("[IOException has occured while building
kef for selected part]");
- ex.printStackTrace();
- }
- catch (SAXException ex) {
- logger.error("[IOException has occured while building
kef for selected part]");
- ex.printStackTrace();
- }
-
- }
-
-
- /**
- * Gets the ElementFromStringKey attribute of the StringTreeHTMLFilter
- * object
- *
- [EMAIL PROTECTED] key Description of Parameter
- [EMAIL PROTECTED] The ElementFromStringKey value
- */
- private Element getElementFromStringKey(String key) {
- return (Element) getStringElementHash().get(key);
- }
-
-
- /**
- * Gets a list whith all the element that are included in the best
match
- *
- [EMAIL PROTECTED] docString Description of Parameter
- [EMAIL PROTECTED] bestMatch Description of Parameter
- [EMAIL PROTECTED] The AllElementsFormBestMatch value
- */
- private ArrayList getAllElementsFromBestMatch(String docString, String
bestMatch) {
- ArrayList eleList = new ArrayList();
-
- //Retrieve the first elemeent
- int indexBestMatch = docString.indexOf(bestMatch);
- logger.debug("[Best match is " + bestMatch + " ]");
- String key = docString.substring(0, indexBestMatch);
- Element firtsEle = getElementFromStringKey(key);
-
- // add the first ele to the list
- if (firtsEle == null) {
- logger.error("[No element whith key " + key + "
founded]");
- }
- else {
- logger.debug("[Element --" + firtsEle.getName() + "--
whith key " + key +
- " added]");
- eleList.add(firtsEle);
- }
-
- //Retreive and add of the rest of element
- String[] nameEleArray = bestMatch.split(":");
- //key = key_of_element_just_before +
":"+name_elemet_just_before;
- //Warning: the first element has been already processed;
- String keyElementJustBefore = key;
- for (int i = 2; i < nameEleArray.length; i++) {
- logger.debug("[Process: " + nameEleArray[i] + " ]");
- //build key
- String nameElementJustBefore = nameEleArray[i - 1];
- String newKey = keyElementJustBefore + ":" +
nameElementJustBefore;
-
- //add element
- Element currentEle = getElementFromStringKey(newKey);
- if (currentEle == null) {
- logger.error("[No element whith key " + newKey
+ " founded]");
- }
- else {
- logger.debug("[Element <" +
currentEle.getName() + "> whith key " +
- newKey + " added]");
- eleList.add(currentEle);
-
- }
-
- //update
- keyElementJustBefore = newKey;
-
- }
- return eleList;
- }
-
-
- /**
- * Best match = v:br: --> :div:br:
- *
- [EMAIL PROTECTED] where Description of Parameter
- [EMAIL PROTECTED] bestMatch Description of Parameter
- [EMAIL PROTECTED] The ValidBestMatch value
- */
- private String getValidBestMatch(String where, String bestMatch) {
- // bestMatch is null or empty
- if (bestMatch == null || bestMatch.length() == 0) {
- return bestMatch;
- }
- if (bestMatch.charAt(0) == ':') {
- return bestMatch;
- }
- int position = where.indexOf(bestMatch);
-
- String toAppend = "";
- while (where.charAt(position) != ':') {
- toAppend = toAppend + where.charAt(position);
- position--;
- }
- logger.debug("[ Append ]" + toAppend);
- return toAppend + bestMatch;
- }
-
-
- /**
- * Description of the Method
- *
- [EMAIL PROTECTED] ele Description of Parameter
- [EMAIL PROTECTED] Description of the Returned Value
- */
- private String buildStringMapFromHTMLDocument(Element ele) {
- // init concat value
- concat = "";
-
- //build the key from the body element
-
- //Element body =
jDoc.getRootElement().getChild(HTML.Tag.BODY.toString());
- /*
- * Element body = getBodyDiv(jDoc);
- * // test that there is body element
- * if (body == null) {
- * String error = "BodyDiv is null: Enable to build String
Map";
- * logger.error(error);
- *
logger.error(com.jahia.clipping.util.JDomUtilities.getDocumentAsString(jDoc));
- * return error;
- * }
- */
- concatAllChildrenName(ele);
- logger.debug("[ End building map for documen ]");
- return concat;
- }
-
-
- /**
- * Description of the Method
- *
- [EMAIL PROTECTED] ele Description of Parameter
- */
- private void concatAllChildrenName(Element ele) {
- // set the key for the hastable
- addElemenWhitKey(concat, ele);
-
- //concat it's name if its not body
- String name = ele.getName();
- if (!name.equalsIgnoreCase(HTML.Tag.BODY.toString())) {
- if (!name.equalsIgnoreCase("tbody")) {
- concat = concat + ":" + ele.getName();
- }
- else if (!name.equalsIgnoreCase("body")) {
- // due to rule transformation (body --> div)
- concat = concat + ":" + "div";
- }
- }
- List children = ele.getChildren();
-
- for (int i = 0; i < children.size(); i++) {
- Element child = (Element) children.get(i);
- concatAllChildrenName(child);
-
- }
- }
-
-
- /**
- * Adds a feature to the ElemenWhitKey attribute of the
StringTreeHTMLFilter
- * object
- *
- [EMAIL PROTECTED] key The feature to be added to the ElemenWhitKey
attribute
- [EMAIL PROTECTED] ele The feature to be added to the ElemenWhitKey
attribute
- */
- private void addElemenWhitKey(String key, Element ele) {
- getStringElementHash().put(key, ele);
- //logger.debug("Element " + ele + "whith key " + key + "
added");
- }
-
-
- /**
- * Extract the selected part of the document
- *
- [EMAIL PROTECTED] htmlDoc Description of Parameter
- */
- private void processFiltering(HTMLDocument htmlDoc) {
- //Compute best match
- org.jdom.Document jDoc = htmlDoc.getJDOMTransformedDocument();
-
- Element bodyDiv = getBodyDiv(jDoc);
- // test that there is body element
- if (bodyDiv == null) {
- String error = "BodyDiv is null: Enable to build String
Map";
- logger.error(error);
-
logger.error(com.jahia.clipping.util.JDomUtilities.getDocumentAsString(jDoc));
- return;
- }
-
-
- String docString = buildStringMapFromHTMLDocument(bodyDiv);
- String bestMatch =
StringUtilities.getBestMatchString(docString, getKeyPart(), ':');
- bestMatch = getValidBestMatch(docString, bestMatch);
-
- logger.debug("[KeyPart is : " + getKeyPart() + " ]");
- logger.debug("[BestPart is : " + bestMatch + " ]");
-
- //Get list of all the element included in the bestMatch
- ArrayList allEle = getAllElementsFromBestMatch(docString,
bestMatch);
-
- if (allEle.isEmpty()) {
- logger.error("[Can't find anyElement ]");
- return;
- }
-
- logger.debug("[At least one element is retrieved ]");
-
- //Rebuild the document
- org.jdom.Document selectedPartDoc = builPartDocument(allEle);
-
setSelectedPart(JDomUtilities.getDocumentAsString(selectedPartDoc));
- }
-
-
-
- /**
- * Description of the Method
- *
- [EMAIL PROTECTED] eleList Description of Parameter
- [EMAIL PROTECTED] Description of the Returned Value
- */
- private org.jdom.Document builPartDocument(ArrayList eleList) {
- //init
- org.jdom.Document doc = new org.jdom.Document();
- Element pEle = new Element(HTML.Tag.P.toString());
- doc.setRootElement(pEle);
- Element firstEle = (Element) (Element) eleList.get(0);
- pEle.addContent((Element) firstEle.clone());
-
- // reconstruct the document
- for (int i = 1; i < eleList.size(); i++) {
- Element currentEle = (Element) eleList.get(i);
- //add only the element at the firts level. The others
will be automatically added
- if (JDomUtilities.isSibling(currentEle, firstEle)) {
- Element clone = (Element) (currentEle.clone());
- pEle.addContent(clone);
- logger.debug("[element " +
currentEle.toString() + " is added ]");
- }
- else {
- logger.debug("[element " +
currentEle.toString() + " is not sibling of " + firstEle.toString() + "]");
- logger.debug("[element " +
currentEle.toString() + " is not added ]");
- }
-
- }
- return doc;
- }
+ /**
+ * Description of the Method
+ *
+ [EMAIL PROTECTED] ele Description of Parameter
+ [EMAIL PROTECTED] Description of the Returned Value
+ */
+ private HTMLDocument doc;
+ private String selectedPart = "<p> Can't retrieve the selected part
</p>";
+ private String keyPart;
+ private Hashtable stringTagHash = new Hashtable();
+ private static org.apache.log4j.Logger logger =
org.apache.log4j.Logger.getLogger(StringTreeExtractorFilter.class);
+
+
+ /**
+ * Constructor for the JDomHTMLFilter object
+ */
+ public StringTreeExtractorFilter() {
+
+ }
+
+
+ /**
+ * Constructor for the StringTreeFilter object
+ *
+ [EMAIL PROTECTED] originalSelectedPart Description of Parameter
+ */
+ public StringTreeExtractorFilter(String originalSelectedPart) {
+ buildKeySelectedPart(originalSelectedPart);
+ }
+
+
+ /**
+ * Constructor for the StringTreeHTMLFilter object
+ *
+ [EMAIL PROTECTED] doc Description of Parameter
+ [EMAIL PROTECTED] originalSelectedPart Description of Parameter
+ */
+ public StringTreeExtractorFilter(HTMLDocument doc, String
originalSelectedPart) {
+ this(originalSelectedPart);
+ setDoc(doc);
+ buildKeySelectedPart(originalSelectedPart);
+ }
+
+
+ /**
+ * Sets the KeyPart attribute of the StringTreeFilter object
+ *
+ [EMAIL PROTECTED] keyPart The new KeyPart value
+ */
+ public void setKeyPart(String keyPart) {
+ this.keyPart = keyPart;
+ }
+
+
+ /**
+ * Sets the Jdoc attribute of the StringTreeHTMLFilter object
+ *
+ [EMAIL PROTECTED] doc The new Doc value
+ */
+ public void setDoc(HTMLDocument doc) {
+ this.doc = doc;
+ }
+
+
+ /**
+ * Sets the StringTagHash attribute of the StringTreeHTMLFilter
object
+ *
+ [EMAIL PROTECTED] stringTagHash The new StringTagHash value
+ */
+ public void setStringTagHash(Hashtable stringTagHash) {
+ this.stringTagHash = stringTagHash;
+ }
+
+
+ /**
+ * Sets the SelectedPart attribute of the StringTreeHTMLFilter
object
+ *
+ [EMAIL PROTECTED] selectedPart The new SelectedPart value
+ */
+ public void setSelectedPart(String selectedPart) {
+ this.selectedPart = selectedPart;
+ }
+
+
+ /**
+ * Sets the Key attribute of the StringTreeHTMLFilter object
+ *
+ [EMAIL PROTECTED] key The new Key value
+ */
+ public void setKey(String key) {
+ this.keyPart = key;
+ }
+
+
+ /**
+ * Gets the SelectedPart attribute of the StringTreeFilter object
+ *
+ [EMAIL PROTECTED] The SelectedPart value
+ */
+ public String getSelectedPart() {
+ return selectedPart;
+ }
+
+
+ /**
+ * Gets the KeyPart attribute of the StringTreeFilter object
+ *
+ [EMAIL PROTECTED] The KeyPart value
+ */
+ public String getKeyPart() {
+ return keyPart;
+ }
+
+
+ /**
+ * Gets the Jdoc attribute of the StringTreeHTMLFilter object
+ *
+ [EMAIL PROTECTED] The Jdoc value
+ */
+ public HTMLDocument getDoc() {
+ return doc;
+ }
+
+
+ /**
+ * Gets the StringTagHash attribute of the StringTreeHTMLFilter
object
+ *
+ [EMAIL PROTECTED] The StringTagHash value
+ */
+ public Hashtable getStringTagHash() {
+ return stringTagHash;
+ }
+
+
+ /**
+ * Gets the SelectedPart attribute of the JDomHTMLFilter object
+ *
+ [EMAIL PROTECTED] doc HTMLDocument object that have the instance
of a
+ * JDomHTMLDocument
+ [EMAIL PROTECTED] The SelectedPart value
+ */
+ public String getSelectedPart(HTMLDocument doc) {
+ //filter the document
+ processFiltering(doc);
+ return selectedPart;
+ }
+
+
+
+ /**
+ * Description of the Method
+ *
+ [EMAIL PROTECTED] originalSelectedPart Description of Parameter
+ */
+ public void buildKeySelectedPart(String originalSelectedPart) {
+
+ //build a jdom document of the selected part
+ String charSet = "";
+ Parser parser = Parser.createParser(originalSelectedPart,
charSet);
+ BuildKeyVisitor visitor = new BuildKeyVisitor();
+ try {
+ parser.visitAllNodesWith(visitor);
+ logger.debug("key part is: " +
visitor.getBuildedKey());
+ }
+ catch (ParserException ex) {
+ ex.printStackTrace();
+ logger.error("buldKeySelectedPart error " +
ex.getMessage());
+ }
+ setKeyPart(visitor.getBuildedKey());
+
+ }
+
+
+ /**
+ * Gets the TagFromStringKey attribute of the StringTreeHTMLFilter
object
+ *
+ [EMAIL PROTECTED] key Description of Parameter
+ [EMAIL PROTECTED] The TagFromStringKey value
+ */
+ private Tag getTagFromStringKey(String key) {
+ return (Tag) getStringTagHash().get(key);
+ }
+
+
+
+ /**
+ * Best match = v:br: --> :div:br:
+ *
+ [EMAIL PROTECTED] where Description of Parameter
+ [EMAIL PROTECTED] bestMatch Description of Parameter
+ [EMAIL PROTECTED] The ValidBestMatch value
+ */
+ private String getValidBestMatch(String where, String bestMatch) {
+ // bestMatch is null or empty
+ if (bestMatch == null || bestMatch.length() == 0) {
+ return bestMatch;
+ }
+ if (bestMatch.charAt(0) == ':') {
+ return bestMatch;
+ }
+ int position = where.indexOf(bestMatch);
+
+ String toAppend = "";
+ while (where.charAt(position) != ':') {
+ toAppend = toAppend + where.charAt(position);
+ position--;
+ }
+ logger.debug("[ Append ]" + toAppend);
+ return toAppend + bestMatch;
+ }
+
+
+ /**
+ * Gets the AllElementsFromBestMatch attribute of the
+ * StringTreeExtractorFilter object
+ *
+ [EMAIL PROTECTED] docString Description of Parameter
+ [EMAIL PROTECTED] bestMatch Description of Parameter
+ [EMAIL PROTECTED] The AllElementsFromBestMatch value
+ */
+ private Hashtable getAllTagsFromBestMatch(String docString, String
bestMatch) {
+ Hashtable tagList = new Hashtable();
+
+ //Retrieve the first elemeent
+ int indexBestMatch = docString.indexOf(bestMatch);
+ logger.debug("[Best match is " + bestMatch + " ]");
+ String key = docString.substring(0, indexBestMatch);
+ Tag firtsTag = getTagFromStringKey(key);
+
+ // add the first ele to the list
+ if (firtsTag == null) {
+ logger.error("[No element whith key " + key + "
founded]");
+ }
+ else {
+ logger.debug("[Element --" + firtsTag.getTagName() +
"-- whith key " + key + " added]");
+ tagList.put(key, firtsTag);
+ }
+
+ //Retreive and add of the rest of element
+ String[] nameEleArray = bestMatch.split(":");
+ //key = key_of_element_just_before +
":"+name_elemet_just_before;
+ //Warning: the first element has been already processed;
+ String keyElementJustBefore = key;
+ for (int i = 2; i < nameEleArray.length; i++) {
+ logger.debug("[Process: " + nameEleArray[i] + " ]");
+ //build key
+ String nameElementJustBefore = nameEleArray[i - 1];
+ String newKey = keyElementJustBefore + ":" +
nameElementJustBefore;
+
+ //add element
+ Tag currentEle = getTagFromStringKey(newKey);
+ if (currentEle == null) {
+ logger.error("[No element whith key " +
newKey + " founded]");
+ }
+ else {
+ logger.debug("[Element <" +
currentEle.getTagName() + "> whith key " + newKey + " added]");
+ tagList.put(newKey, currentEle);
+
+ }
+
+ //update
+ keyElementJustBefore = newKey;
+
+ }
+ return tagList;
+ }
+
+
+ /**
+ * Gets the RealSelectedPart attribute of the
StringTreeExtractorFilter
+ * object
+ *
+ [EMAIL PROTECTED] originalSelectedPart Description of Parameter
+ [EMAIL PROTECTED] The RealSelectedPart value
+ */
+ private String refactorKey(String originalSelectedPart) {
+ String realSelectedPart = originalSelectedPart;
+ return realSelectedPart;
+ }
+
+
+
+ /**
+ * Adds a feature to the ElemenWhitKey attribute of the
StringTreeHTMLFilter
+ * object
+ *
+ [EMAIL PROTECTED] key The feature to be added to the
ElemenWhitKey attribute
+ [EMAIL PROTECTED] ele The feature to be added to the
ElemenWhitKey attribute
+ */
+ private void addElemenWhitKey(String key, Tag ele) {
+ getStringTagHash().put(key, ele);
+ //logger.debug("Tag " + ele + "whith key " + key + " added");
+ }
+
+
+ /**
+ * Extract the selected part of the document
+ *
+ [EMAIL PROTECTED] htmlDoc Description of Parameter
+ */
+ private void processFiltering(HTMLDocument htmlDoc) {
+ //build key of the html document
+ String charSet = "";
+ Parser parser =
Parser.createParser(htmlDoc.getTransformedDocumentAsString(), charSet);
+ BuildKeyVisitor visitor = new BuildKeyVisitor();
+ String docString = "";
+ try {
+ parser.visitAllNodesWith(visitor);
+ docString = visitor.getBuildedKey();
+ logger.debug("pattern of the documnet part is: " +
docString);
+ }
+ catch (ParserException ex) {
+ ex.printStackTrace();
+ logger.error("buldKeySelectedPart error " +
ex.getMessage());
+ }
+
+ //Compute best match
+ String bestMatch =
StringUtilities.getBestMatchString(docString, getKeyPart(), ':');
+ bestMatch = getValidBestMatch(docString, bestMatch);
+
+ logger.debug("[KeyPart is : " + getKeyPart() + " ]");
+ logger.debug("[BestPart is : " + bestMatch + " ]");
+
+ //build hashtable
+
+ //Build the supposed selected part
+
+ BuildSelectedPartVisitor bspv = new
BuildSelectedPartVisitor(docString, bestMatch);
+ try {
+ parser.reset();
+ parser.visitAllNodesWith(bspv);
+ docString = visitor.getBuildedKey();
+ logger.debug("pattern of the documnet part is: " +
docString);
+ }
+ catch (ParserException ex) {
+ ex.printStackTrace();
+ logger.error("Selectet part error " +
ex.getMessage());
+ }
+
+ String selectedPart = bspv.getSelectedPart();
+ //Rebuild the document
+ setSelectedPart("<div class=\""+CssUtilities.HTML_CLASS+"
"+CssUtilities.BODY_CLASS+" \">"+selectedPart+"\n </div>");
+
+ }
+
+
+
+ /**
+ * Description of the Class
+ *
+ [EMAIL PROTECTED] Tlili Khaled
+ */
+ public class BuildKeyVisitor extends NodeVisitor {
+ private String key = "";
+
+
+ /**
+ * Constructor for the MyVisitor object
+ */
+ public BuildKeyVisitor() {
+ key = "";
+ }
+
+
+ /**
+ * Gets the BuildedKey attribute of the BuildKeyVisitor
object
+ *
+ [EMAIL PROTECTED] The BuildedKey value
+ */
+ public String getBuildedKey() {
+ return key;
+ }
+
+
+ /**
+ * Description of the Method
+ *
+ [EMAIL PROTECTED] tag Description of Parameter
+ */
+ public void visitTag(Tag tag) {
+ key = key + ":" + tag.getTagName();
+ addElemenWhitKey(key, tag);
+ //logger.debug("added key: " + key);
+
+ //logger.debug("\n" + tag.getTagName() +
tag.getStartPosition());
+ }
+
+
+ /**
+ * Description of the Method
+ *
+ [EMAIL PROTECTED] string Description of Parameter
+ */
+ public void visitStringNode(Text string) {
+ //logger.debug(string);
+ }
+
+ }
+
+
+ /**
+ * Description of the Class
+ *
+ [EMAIL PROTECTED] Tlili Khaled
+ */
+ public class BuildSelectedPartVisitor extends NodeVisitor {
+ private String key = "";
+ private String selectedPart = "";
+ private Hashtable tagHash;
+ private Set addedTagSet = new HashSet();
+
+
+ /**
+ * Constructor for the MyVisitor object
+ *
+ [EMAIL PROTECTED] docString Description of Parameter
+ [EMAIL PROTECTED] bestMatch Description of Parameter
+ */
+ public BuildSelectedPartVisitor(String docString, String
bestMatch) {
+ key = "";
+ tagHash = getAllTagsFromBestMatch(docString,
bestMatch);
+
+ }
+
+
+ /**
+ * Gets the BuildedKey attribute of the BuildKeyVisitor
object
+ *
+ [EMAIL PROTECTED] The BuildedKey value
+ */
+ public String getBuildedKey() {
+ return key;
+ }
+
+
+ /**
+ * Gets the SelectedPart attribute of the
BuildSelectedPartVisitor object
+ *
+ [EMAIL PROTECTED] The SelectedPart value
+ */
+ public String getSelectedPart() {
+ return selectedPart;
+ }
+
+
+ /**
+ * Description of the Method
+ *
+ [EMAIL PROTECTED] tag Description of Parameter
+ */
+ public void visitTag(Tag tag) {
+ logger.debug(tag.getTagName());
+ //add style and script
+ if (tag instanceof StyleTag || tag instanceof
ScriptTag) {
+ logger.debug("Style or Script tag added");
+ selectedPart = selectedPart + tag.toHtml();
+ }
+ renderSelectedTag(tag);
+
+ }
+
+
+ /**
+ * Description of the Method
+ *
+ [EMAIL PROTECTED] string Description of Parameter
+ */
+ public void visitStringNode(Text string) {
+ // do nothing
+ }
+
+
+ /**
+ * Description of the Method
+ *
+ [EMAIL PROTECTED] tag Description of Parameter
+ */
+ private void renderSelectedTag(Tag tag) {
+ // add the selected tags
+ key = key + ":" + tag.getTagName();
+ Tag currentTag = (Tag) tagHash.get(key);
+ if (currentTag != null) {
+ logger.debug("This tag is selected");
+ addedTagSet.add(currentTag);
+ Node pNode = currentTag.getParent();
+ if (addedTagSet.contains((Tag) pNode)) {
+ logger.debug("This tag is already
rendered");
+ return;
+ }
+ //Tag is selected
+ logger.debug("Tag added");
+ updateCssHeritance(currentTag, pNode);
+ selectedPart = selectedPart +
currentTag.toHtml();
+
+ }
+ }
+
+
+ /**
+ * Description of the Method
+ *
+ [EMAIL PROTECTED] tag Description of Parameter
+ [EMAIL PROTECTED] parentNode Description of Parameter
+ */
+ private void updateCssHeritance(Tag tag, Node parentNode) {
+ if (parentNode instanceof Tag) {
+ Tag pTag = (Tag) parentNode;
+ Node ppNode = parentNode.getParent();
+ if (ppNode != null) {
+ updateCssHeritance(pTag, ppNode);
+ }
+ //Style att
+ String pStyleAtt =
pTag.getAttribute(HTML.Attribute.STYLE.toString());
+ if (pStyleAtt != null &&
!pStyleAtt.equalsIgnoreCase("")) {
+ logger.debug("update style att");
+ String styleAtt =
tag.getAttribute(HTML.Attribute.STYLE.toString());
+ if (styleAtt == null) {
+
tag.setAttribute(HTML.Attribute.STYLE.toString(), pStyleAtt);
+ }
+ else {
+
tag.setAttribute(HTML.Attribute.STYLE.toString(), styleAtt + " " + pStyleAtt);
+ }
+
+ }
+
+ //class att
+ String pClassAtt =
pTag.getAttribute(HTML.Attribute.CLASS.toString());
+ if (pClassAtt != null &&
!pClassAtt.equalsIgnoreCase("")) {
+ logger.debug("update class att");
+ String classAtt =
tag.getAttribute(HTML.Attribute.CLASS.toString());
+ if (classAtt == null) {
+
tag.setAttribute(HTML.Attribute.CLASS.toString(), pClassAtt);
+
+ }
+ else {
+
tag.setAttribute(HTML.Attribute.CLASS.toString(), classAtt + " " + pClassAtt);
+ }
+ }
+
+ }
+ else {
+ logger.debug("parent node is not a tag");
+ }
+ }
+
+ }
}
Index: ClipperPortlet.java
===================================================================
RCS file:
/home/cvs/repository/webclip_portlet/war/src/java/com/jahia/portlet/ClipperPortlet.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ClipperPortlet.java 3 Jun 2005 17:03:30 -0000 1.1
+++ ClipperPortlet.java 7 Jun 2005 17:44:40 -0000 1.2
@@ -112,29 +112,70 @@
public void doView(RenderRequest request, RenderResponse response) {
logger.debug("[ DOVIEW ] new Session");
response.setContentType("text/html");
- PortletSession session = request.getPortletSession();
- if (session.isNew()) {
- logger.debug("New Session");
- loadClipperFromXML(request);
- }
- else {
- logger.debug("Old Session");
- }
-
try {
- String url = request.getParameter("sourceUrl");
- logger.debug("Source url = " + url);
+ Writer writer = response.getWriter();
+ PortletSession session = request.getPortletSession();
+ if (session.isNew()) {
+ logger.debug("New Session");
+ loadClipperFromXML(request);
+ }
+ else {
+ logger.debug("Old Session");
+ }
+ String show = request.getParameter("show");
+ String url = request.getParameter("sourceUrl");
+
+ //init show attribute
+ if (show == null) {
+ show = "part";
+ }
+ PortletURL aUrl = response.createActionURL();
+ A aEle = new A();
+ if (show.equalsIgnoreCase("part")) {
+ //Show all
+ logger.debug("show part");
+ aUrl.setParameter("show", "all");
+ aEle.setHref(aUrl.toString());
+ aEle.setTagText("Show All Document");
+ }
+ else {
+ if (show.equalsIgnoreCase("all")) {
+ logger.debug("show All");
+ aUrl.setParameter("show", "part");
+ aEle.setHref(aUrl.toString());
+ aEle.setTagText("Show Part of the
Document");
+ }
+ }
+ writer.write(aEle.toString());
+
+ //web simulator
+
if (url == null) {
loadUserPreferences(request);
logger.debug("[ Run WebBrowserSimulator ]");
- Writer writer = response.getWriter();
- writer.write("<p> This is the view mode</p>");
+
PortletPreferencesManager.showNamePreferences(request);
// start web browser simulator
WebBrowserSimulator wbs = new
WebBrowserSimulator(request, response, SessionManager.getClipperBean(request));
- String html = wbs.getHTMLPart();
+
+ //Manage show part or All
+ String html = "";
+ if (show.equalsIgnoreCase("part")) {
+ //Show all
+ logger.debug("show part");
+ html = wbs.getHTMLPart();
+ }
+ else {
+ if (show.equalsIgnoreCase("all")) {
+ logger.debug("show All");
+ html =
wbs.getFinalHtmlResponse(request, response);
+ }
+ }
+
// write the result
+ writer.write("<div class=\"webClipping\"
style=\"position: relative;\" >");
writer.write(html);
+ writer.write("</div>");
}
else {
logger.debug("Go to IFrame mode");
@@ -172,8 +213,12 @@
logger.debug("Param. name: " + name + ", value:
" + value);
}
logger.debug("[ End Listing Param ]");
- // forward the parameters
+ // forward the parameters
+ String show = request.getParameter("show");
+ if (show != null) {
+ response.setRenderParameter("show", show);
+ }
}
//Edit Mode
Index: editParams.jsp
===================================================================
RCS file:
/home/cvs/repository/webclip_portlet/war/src/webapp/jsp/editParams.jsp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- editParams.jsp 3 Jun 2005 17:03:43 -0000 1.1
+++ editParams.jsp 7 Jun 2005 17:44:40 -0000 1.2
@@ -25,7 +25,7 @@
String optionUrlHtml = "";
ClipperBean cb =
com.jahia.portlet.SessionManager.getClipperBean(renderRequest);
List urlBeanList = cb.getUrlListBean();
- for (int i = 0; i < urlBeanList.size(); i++) {
+ for (int i = 0; i < urlBeanList.size()-1; i++) {
UrlBean ub = (UrlBean) urlBeanList.get(i);
String uValue = ub.getUrlValue();
optionUrlHtml = optionUrlHtml + "\n<option value=" + i;