portals-portalapps-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From woon...@apache.org
Subject svn commit: r1608145 - in /portals/applications/webcontent/trunk/content-rewriter/src: main/java/org/apache/portals/applications/webcontent2/rewriter2/htmlcleaner/ main/java/org/apache/portals/applications/webcontent2/rewriter2/impl/ test/java/org/apac...
Date Sun, 06 Jul 2014 00:35:19 GMT
Author: woonsan
Date: Sun Jul  6 00:35:19 2014
New Revision: 1608145

URL: http://svn.apache.org/r1608145
Log:
APA-59: polishing htmlcleaner content rewriter

Modified:
    portals/applications/webcontent/trunk/content-rewriter/src/main/java/org/apache/portals/applications/webcontent2/rewriter2/htmlcleaner/HtmlCleanerContentRewriter.java
    portals/applications/webcontent/trunk/content-rewriter/src/main/java/org/apache/portals/applications/webcontent2/rewriter2/impl/SimpleContentRewritingContext.java
    portals/applications/webcontent/trunk/content-rewriter/src/test/java/org/apache/portals/applications/webcontent2/rewriter2/HtmlCleanerContentRewriterTest.java
    portals/applications/webcontent/trunk/content-rewriter/src/test/resources/org/apache/portals/applications/webcontent2/rewriter2/guidelines.html

Modified: portals/applications/webcontent/trunk/content-rewriter/src/main/java/org/apache/portals/applications/webcontent2/rewriter2/htmlcleaner/HtmlCleanerContentRewriter.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/content-rewriter/src/main/java/org/apache/portals/applications/webcontent2/rewriter2/htmlcleaner/HtmlCleanerContentRewriter.java?rev=1608145&r1=1608144&r2=1608145&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/content-rewriter/src/main/java/org/apache/portals/applications/webcontent2/rewriter2/htmlcleaner/HtmlCleanerContentRewriter.java
(original)
+++ portals/applications/webcontent/trunk/content-rewriter/src/main/java/org/apache/portals/applications/webcontent2/rewriter2/htmlcleaner/HtmlCleanerContentRewriter.java
Sun Jul  6 00:35:19 2014
@@ -21,6 +21,8 @@ import java.io.BufferedWriter;
 import java.io.IOException;
 import java.io.Reader;
 import java.io.Writer;
+import java.util.Map;
+import java.util.TreeMap;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.portals.applications.webcontent2.rewriter2.ContentRewriter;
@@ -28,6 +30,7 @@ import org.apache.portals.applications.w
 import org.apache.portals.applications.webcontent2.rewriter2.ContentRewritingException;
 import org.apache.portals.applications.webcontent2.rewriter2.Sink;
 import org.apache.portals.applications.webcontent2.rewriter2.Source;
+import org.htmlcleaner.CleanerTransformations;
 import org.htmlcleaner.HtmlCleaner;
 import org.htmlcleaner.Serializer;
 import org.htmlcleaner.TagNode;
@@ -45,6 +48,24 @@ public class HtmlCleanerContentRewriter 
         this.serializerFactory = serializerFactory;
     }
 
+    public void setCleanerTransformations(CleanerTransformations cleanerTransformations)
{
+        cleaner.getProperties().setCleanerTransformations(cleanerTransformations);
+    }
+
+    public void setCleanerTransformations(final String [] transformationInfos) {
+        Map<String, String> transInfos = new TreeMap<String, String>();
+
+        for (String transInfo : transformationInfos) 
+        {
+            int index = transInfo.indexOf('=');
+            String key = index <= 0 ? transInfo : transInfo.substring(0, index);
+            String value = index <= 0 ? null : transInfo.substring(index + 1);
+            transInfos.put(key, value);
+        }
+
+        setCleanerTransformations(new CleanerTransformations(transInfos));
+    }
+
     public void setTagNodeVisitor(TagNodeVisitor tagNodeVisitor)
     {
         this.tagNodeVisitor = tagNodeVisitor;
@@ -54,9 +75,12 @@ public class HtmlCleanerContentRewriter 
     {
         Serializer serializer = null;
 
-        try {
+        try 
+        {
             serializer = serializerFactory.createSerializer();
-        } catch (Exception e) {
+        } 
+        catch (Exception e) 
+        {
             throw new ContentRewritingException("Failed to create serializer. " + e, e);
         }
 
@@ -65,7 +89,8 @@ public class HtmlCleanerContentRewriter 
         Writer writer = null;
         BufferedWriter bw = null;
 
-        try {
+        try 
+        {
             reader = source.getReader();
             br = new BufferedReader(reader);
             writer = sink.getWriter();
@@ -73,12 +98,15 @@ public class HtmlCleanerContentRewriter 
 
             TagNode tagNode = cleaner.clean(br);
 
-            if (tagNodeVisitor != null) {
+            if (tagNodeVisitor != null) 
+            {
                 tagNode.traverse(tagNodeVisitor);
             }
 
             serializer.write(tagNode, writer, "UTF-8");
-        } finally {
+        } 
+        finally 
+        {
             IOUtils.closeQuietly(br);
             IOUtils.closeQuietly(reader);
             IOUtils.closeQuietly(bw);

Modified: portals/applications/webcontent/trunk/content-rewriter/src/main/java/org/apache/portals/applications/webcontent2/rewriter2/impl/SimpleContentRewritingContext.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/content-rewriter/src/main/java/org/apache/portals/applications/webcontent2/rewriter2/impl/SimpleContentRewritingContext.java?rev=1608145&r1=1608144&r2=1608145&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/content-rewriter/src/main/java/org/apache/portals/applications/webcontent2/rewriter2/impl/SimpleContentRewritingContext.java
(original)
+++ portals/applications/webcontent/trunk/content-rewriter/src/main/java/org/apache/portals/applications/webcontent2/rewriter2/impl/SimpleContentRewritingContext.java
Sun Jul  6 00:35:19 2014
@@ -29,7 +29,8 @@ public class SimpleContentRewritingConte
 
     public boolean hasAttribute(String name)
     {
-        if (attributes == null) {
+        if (attributes == null) 
+        {
             return false;
         }
 
@@ -38,7 +39,8 @@ public class SimpleContentRewritingConte
 
     public Object getAttribute(String name)
     {
-        if (attributes == null) {
+        if (attributes == null) 
+        {
             return null;
         }
 
@@ -47,7 +49,8 @@ public class SimpleContentRewritingConte
 
     public Map<String, Object> getAttributesMap()
     {
-        if (attributes == null) {
+        if (attributes == null) 
+        {
             return Collections.emptyMap();
         }
 
@@ -56,7 +59,8 @@ public class SimpleContentRewritingConte
 
     public void setAttribute(String name, Object value)
     {
-        if (attributes == null) {
+        if (attributes == null) 
+        {
             attributes = new ConcurrentHashMap<String, Object>();
         }
 
@@ -65,7 +69,8 @@ public class SimpleContentRewritingConte
 
     public void removeAttribute(String name)
     {
-        if (attributes != null) {
+        if (attributes != null) 
+        {
             attributes.remove(name);
         }
     }

Modified: portals/applications/webcontent/trunk/content-rewriter/src/test/java/org/apache/portals/applications/webcontent2/rewriter2/HtmlCleanerContentRewriterTest.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/content-rewriter/src/test/java/org/apache/portals/applications/webcontent2/rewriter2/HtmlCleanerContentRewriterTest.java?rev=1608145&r1=1608144&r2=1608145&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/content-rewriter/src/test/java/org/apache/portals/applications/webcontent2/rewriter2/HtmlCleanerContentRewriterTest.java
(original)
+++ portals/applications/webcontent/trunk/content-rewriter/src/test/java/org/apache/portals/applications/webcontent2/rewriter2/HtmlCleanerContentRewriterTest.java
Sun Jul  6 00:35:19 2014
@@ -54,8 +54,10 @@ public class HtmlCleanerContentRewriterT
     private HtmlCleanerContentRewriter contentRewriter;
 
     @Before
-    public void before() {
-        source = new Source() {
+    public void before() 
+    {
+        source = new Source() 
+        {
             public InputStream getInputStream() throws IOException
             {
                 return HtmlCleanerContentRewriterTest.class.getResourceAsStream("guidelines.html");
@@ -67,7 +69,8 @@ public class HtmlCleanerContentRewriterT
         };
 
         sinkTeeOut = new ByteArrayOutputStream();
-        sink = new Sink() {
+        sink = new Sink() 
+        {
             public OutputStream getOutputStream() throws IOException
             {
                 return new TeeOutputStream(System.out, sinkTeeOut);
@@ -87,22 +90,32 @@ public class HtmlCleanerContentRewriterT
 
         contentRewriter = new HtmlCleanerContentRewriter(cleaner, serializerFactory);
 
+        String [] transInfos = 
+                        StringUtils.split("cfoutput|c:block=div,false|font=span,true|font.size|font.face|font.style=${style};font-family=${face};font-size=${size};",
"|");
+        contentRewriter.setCleanerTransformations(transInfos);
+
         final String siteUrl = "http://www.example.com/";
-        contentRewriter.setTagNodeVisitor(new TagNodeVisitor() {
-            public boolean visit(TagNode tagNode, HtmlNode htmlNode) {
-                if (htmlNode instanceof TagNode) {
+        contentRewriter.setTagNodeVisitor(new TagNodeVisitor() 
+        {
+            public boolean visit(TagNode tagNode, HtmlNode htmlNode) 
+            {
+                if (htmlNode instanceof TagNode) 
+                {
                     TagNode tag = (TagNode) htmlNode;
                     String tagName = tag.getName();
 
-                    if ("a".equals(tagName) || "link".equals(tagName)) {
+                    if ("a".equals(tagName) || "link".equals(tagName)) 
+                    {
                         String href = tag.getAttributeByName("href");
 
-                        if (href != null) {
+                        if (href != null) 
+                        {
                             tag.addAttribute("href", siteUrl + StringUtils.removeStart(href,
"./"));
                         }
                     }
 
-                    if ("a".equals(tagName) && "Project_Guidelines".equals(tag.getAttributeByName("name")))
{
+                    if ("a".equals(tagName) && "Project_Guidelines".equals(tag.getAttributeByName("name")))

+                    {
                         tag.removeFromTree();
                     }
                 }
@@ -124,6 +137,12 @@ public class HtmlCleanerContentRewriterT
         assertFalse(output.contains("name=\"Project_Guidelines\""));
         assertTrue(output.contains("<a href=\"http://www.example.com/roles.html\">Roles
and Responsibilities</a>"));
         assertTrue(output.contains("general@portals <a href=\"http://www.example.com/mail-lists.html#general\">"));
+
+        assertFalse(output.contains("<cfoutput>"));
+        assertTrue(output.contains("Yin and yang describe the polar effects of phenomena."));
+        assertFalse(output.contains("<c:block"));
+        assertFalse(output.contains("<font"));
+        assertTrue(output.contains("<span id=\"f21\" style=\"color: red;font-family=Arial;font-size=12;\">"));
     }
 
 }

Modified: portals/applications/webcontent/trunk/content-rewriter/src/test/resources/org/apache/portals/applications/webcontent2/rewriter2/guidelines.html
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/content-rewriter/src/test/resources/org/apache/portals/applications/webcontent2/rewriter2/guidelines.html?rev=1608145&r1=1608144&r2=1608145&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/content-rewriter/src/test/resources/org/apache/portals/applications/webcontent2/rewriter2/guidelines.html
(original)
+++ portals/applications/webcontent/trunk/content-rewriter/src/test/resources/org/apache/portals/applications/webcontent2/rewriter2/guidelines.html
Sun Jul  6 00:35:19 2014
@@ -3,39 +3,65 @@
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <title>The Apache Portals Site - Project Guidelines</title>
-<link rel="stylesheet" href="./css/print.css" type="text/css"
-	media="print" />
+<link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
 <meta name="author" content="Apache Portals Project" />
 </head>
 <body>
 
-	<h2>
-		<a name="Project_Guidelines"></a>Project Guidelines
-	</h2>
-	<p>This document defines the guidelines of the Portals Project. It
-		includes definitions of the various categories of membership, who is
-		able to vote, how conflicts are resolved by voting, and the procedures
-		to follow for proposing and making changes to the codebase of the
-		Project.</p>
-	<ul>
-		<li><a href="./roles.html">Roles and Responsibilities</a><br />
-			Defines the recognized roles in the project.</li>
-		<li><a href="./communication.html">Communication</a><br />
-			Defines how users and developers communicate.</li>
-		<li><a href="./decisions.html">Decision Making</a><br /> Defines
-			how action items are proposed and voted on.</li>
-		<li><a href="./management.html">Project Management</a><br />
-			Defines the roles and responsibilities of the Project Management
-			Committee (PMC).</li>
-		<li><a href="./newproject.html">New Subproject Proposals</a><br />
-			Defines the methodology for proposing new top level Jakarta
-			Subprojects.</li>
-	</ul>
-	<p>
-		This is a living document. Changes can be made by the Project
-		Management Committee. Suggestions for changes should be discussed on
-		the general@portals <a href="./mail-lists.html#general">mailing
-			list</a>
-	</p>
+  <h2>
+    <a name="Project_Guidelines"></a>Project Guidelines
+  </h2>
+
+  <p>
+    This document defines the guidelines of the Portals Project. It
+    includes definitions of the various categories of membership, who is
+    able to vote, how conflicts are resolved by voting, and the procedures
+    to follow for proposing and making changes to the codebase of the
+    Project.
+  </p>
+
+  <ul>
+    <li>
+      <a href="./roles.html">Roles and Responsibilities</a><br />
+      Defines the recognized roles in the project.
+    </li>
+    <li>
+      <a href="./communication.html">Communication</a><br />
+      Defines how users and developers communicate.
+    </li>
+    <li>
+      <a href="./decisions.html">Decision Making</a><br /> 
+      Defines how action items are proposed and voted on.
+    </li>
+    <li>
+      <a href="./management.html">Project Management</a><br />
+      Defines the roles and responsibilities of the Project Management Committee (PMC).
+    </li>
+    <li>
+      <a href="./newproject.html">New Subproject Proposals</a><br />
+      Defines the methodology for proposing new top level Jakarta Subprojects.
+    </li>
+  </ul>
+
+  <p>
+    This is a living document. Changes can be made by the Project
+    Management Committee. Suggestions for changes should be discussed on
+    the general@portals <a href="./mail-lists.html#general">mailing list</a>
+  </p>
+
+  <hr />
+
+  <cfoutput>
+    Yin and yang describe the polar effects of phenomena.
+  </cfoutput>
+
+  <c:block parent=b1 count=331>
+    Yin-yang are Mutually Rooted
+  </c:block>
+
+  <font id=f21 size=12 face=Arial style="color: red">
+    The Yin and yang aspects are in dynamic equilibrium
+  </font>
+
 </body>
 </html>
\ No newline at end of file



Mime
View raw message