You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2019/12/20 18:20:28 UTC

[jspwiki] 06/10: JSPWIKI-120: move getDiff from WikiEngine to DifferenceManager

This is an automated email from the ASF dual-hosted git repository.

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git

commit 00bec876ee7ae7b2d76ece26554cca7b257d6975
Author: juanpablo <ju...@apache.org>
AuthorDate: Fri Dec 20 19:09:17 2019 +0100

    JSPWIKI-120: move getDiff from WikiEngine to DifferenceManager
---
 .../apache/wiki/diff/ContextualDiffProvider.java   | 24 ++++----
 .../org/apache/wiki/diff/DifferenceManager.java    | 31 +++++++++-
 .../java/org/apache/wiki/rss/RSSGenerator.java     | 66 +++++++---------------
 .../java/org/apache/wiki/tags/InsertDiffTag.java   | 51 +++++++----------
 .../wiki/diff/ContextualDiffProviderTest.java      | 30 +++++-----
 5 files changed, 95 insertions(+), 107 deletions(-)

diff --git a/jspwiki-main/src/main/java/org/apache/wiki/diff/ContextualDiffProvider.java b/jspwiki-main/src/main/java/org/apache/wiki/diff/ContextualDiffProvider.java
index 4b4ee3b..27077c0 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/diff/ContextualDiffProvider.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/diff/ContextualDiffProvider.java
@@ -63,27 +63,27 @@ public class ContextualDiffProvider implements DiffProvider {
     public boolean m_emitChangeNextPreviousHyperlinks = true;
 
     //Don't use spans here the deletion and insertions are nested in this...
-    public static final String CHANGE_START_HTML = ""; //This could be a image '>' for a start marker
-    public static final String CHANGE_END_HTML = ""; //and an image for an end '<' marker
-    public static final String DIFF_START = "<div class=\"diff-wikitext\">";
-    public static final String DIFF_END = "</div>";
+    public static String CHANGE_START_HTML = ""; //This could be a image '>' for a start marker
+    public static String CHANGE_END_HTML = ""; //and an image for an end '<' marker
+    public static String DIFF_START = "<div class=\"diff-wikitext\">";
+    public static String DIFF_END = "</div>";
 
     // Unfortunately we need to do dumb HTML here for RSS feeds.
 
-    public static final String INSERTION_START_HTML = "<font color=\"#8000FF\"><span class=\"diff-insertion\">";
-    public static final String INSERTION_END_HTML = "</span></font>";
-    public static final String DELETION_START_HTML = "<strike><font color=\"red\"><span class=\"diff-deletion\">";
-    public static final String DELETION_END_HTML = "</span></font></strike>";
+    public static String INSERTION_START_HTML = "<font color=\"#8000FF\"><span class=\"diff-insertion\">";
+    public static String INSERTION_END_HTML = "</span></font>";
+    public static String DELETION_START_HTML = "<strike><font color=\"red\"><span class=\"diff-deletion\">";
+    public static String DELETION_END_HTML = "</span></font></strike>";
     private static final String ANCHOR_PRE_INDEX = "<a name=\"change-";
     private static final String ANCHOR_POST_INDEX = "\" />";
     private static final String BACK_PRE_INDEX = "<a class=\"diff-nextprev\" title=\"Go to previous change\" href=\"#change-";
     private static final String BACK_POST_INDEX = "\">&lt;&lt;</a>";
     private static final String FORWARD_PRE_INDEX = "<a class=\"diff-nextprev\" title=\"Go to next change\" href=\"#change-";
     private static final String FORWARD_POST_INDEX = "\">&gt;&gt;</a>";
-    public static final String ELIDED_HEAD_INDICATOR_HTML = "<br/><br/><b>...</b>";
-    public static final String ELIDED_TAIL_INDICATOR_HTML = "<b>...</b><br/><br/>";
-    public static final String LINE_BREAK_HTML = "<br />";
-    public static final String ALTERNATING_SPACE_HTML = "&nbsp;";
+    public static String ELIDED_HEAD_INDICATOR_HTML = "<br/><br/><b>...</b>";
+    public static String ELIDED_TAIL_INDICATOR_HTML = "<b>...</b><br/><br/>";
+    public static String LINE_BREAK_HTML = "<br />";
+    public static String ALTERNATING_SPACE_HTML = "&nbsp;";
 
     // This one, I will make property file based...
     private static final int LIMIT_MAX_VALUE = (Integer.MAX_VALUE /2) - 1;
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/diff/DifferenceManager.java b/jspwiki-main/src/main/java/org/apache/wiki/diff/DifferenceManager.java
index 6643847..bf46bde 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/diff/DifferenceManager.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/diff/DifferenceManager.java
@@ -23,6 +23,7 @@ import org.apache.log4j.Logger;
 import org.apache.wiki.WikiContext;
 import org.apache.wiki.WikiEngine;
 import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
+import org.apache.wiki.providers.WikiPageProvider;
 import org.apache.wiki.util.ClassUtil;
 
 import java.io.IOException;
@@ -34,11 +35,10 @@ import java.util.Properties;
  * Load, initialize and delegate to the DiffProvider that will actually do the work.
  */
 public class DifferenceManager {
+
     private static final Logger log = Logger.getLogger( DifferenceManager.class );
 
-    /**
-     * Property value for storing a diff provider.  Value is {@value}.
-     */
+    /** Property value for storing a diff provider.  Value is {@value}. */
     public static final String PROP_DIFF_PROVIDER = "jspwiki.diffProvider";
 
     private DiffProvider m_provider;
@@ -102,5 +102,30 @@ public class DifferenceManager {
         }
         return diff;
     }
+
+    /**
+     *  Returns a diff of two versions of a page.
+     *  <p>
+     *  Note that the API was changed in 2.6 to provide a WikiContext object!
+     *
+     *  @param context The WikiContext of the page you wish to get a diff from
+     *  @param version1 Version number of the old page.  If WikiPageProvider.LATEST_VERSION (-1), then uses current page.
+     *  @param version2 Version number of the new page.  If WikiPageProvider.LATEST_VERSION (-1), then uses current page.
+     *
+     *  @return A HTML-ized difference between two pages.  If there is no difference, returns an empty string.
+     */
+    public String getDiff( final WikiContext context, final int version1, final int version2 ) {
+        final String page = context.getPage().getName();
+        String page1 = context.getEngine().getPureText( page, version1 );
+        final String page2 = context.getEngine().getPureText( page, version2 );
+
+        // Kludge to make diffs for new pages to work this way.
+        if( version1 == WikiPageProvider.LATEST_VERSION ) {
+            page1 = "";
+        }
+
+        return makeDiff( context, page1, page2 );
+    }
+
 }
 
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSGenerator.java b/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSGenerator.java
index 058d9e6..1c21260 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSGenerator.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSGenerator.java
@@ -194,9 +194,9 @@ public class RSSGenerator {
         WikiContext ctx = new WikiContext( m_engine, page );
         if( page.getVersion() > 1 )
         {
-            String diff = m_engine.getDiff( ctx,
-                                            page.getVersion()-1, // FIXME: Will fail when non-contiguous versions
-                                            page.getVersion() );
+            String diff = m_engine.getDifferenceManager().getDiff( ctx,
+                                                          page.getVersion() - 1, // FIXME: Will fail when non-contiguous versions
+                                                                   page.getVersion() );
 
             buf.append(author+" changed this page on "+page.getLastModified()+":<br /><hr /><br />" );
             buf.append(diff);
@@ -238,17 +238,11 @@ public class RSSGenerator {
      *  
      *  @return A RSS 1.0 feed in the "full" mode.
      */
-    public String generate()
-    {
-        WikiContext context = new WikiContext( m_engine,new WikiPage( m_engine, "__DUMMY" ) );
+    public String generate() {
+        final WikiContext context = new WikiContext( m_engine,new WikiPage( m_engine, "__DUMMY" ) );
         context.setRequestContext( WikiContext.RSS );
-        Feed feed = new RSS10Feed( context );
-
-        String result = generateFullWikiRSS( context, feed );
-
-        result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + result;
-
-        return result;
+        final Feed feed = new RSS10Feed( context );
+        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + generateFullWikiRSS( context, feed );
     }
 
     /**
@@ -257,14 +251,10 @@ public class RSSGenerator {
      * @param mode the RSS mode: {@link #RSS10}, {@link #RSS20} or {@link #ATOM}.
      * @return the content type
      */
-    public static String getContentType( String mode )
-    {
-        if( mode.equals( RSS10 )||mode.equals(RSS20) )
-        {
+    public static String getContentType( final String mode ) {
+        if( mode.equals( RSS10 ) || mode.equals( RSS20 ) ) {
             return "application/rss+xml";
-        }
-        else if( mode.equals(ATOM) )
-        {
+        } else if( mode.equals( ATOM ) ) {
             return "application/atom+xml";
         }
 
@@ -282,41 +272,27 @@ public class RSSGenerator {
      * @throws ProviderException If the underlying provider failed.
      * @throws IllegalArgumentException If an illegal mode is given.
      */
-    public String generateFeed( WikiContext wikiContext, List< WikiPage > changed, String mode, String type )
-        throws ProviderException, IllegalArgumentException
-    {
-        Feed feed = null;
-        String res = null;
+    public String generateFeed( final WikiContext wikiContext, final List< WikiPage > changed, final String mode, final String type ) throws IllegalArgumentException {
+        final Feed feed;
+        final String res;
 
-        if( ATOM.equals(type) )
-        {
+        if( ATOM.equals(type) ) {
             feed = new AtomFeed( wikiContext );
-        }
-        else if( RSS20.equals( type ) )
-        {
+        } else if( RSS20.equals( type ) ) {
             feed = new RSS20Feed( wikiContext );
-        }
-        else
-        {
+        } else {
             feed = new RSS10Feed( wikiContext );
         }
 
         feed.setMode( mode );
 
-        if( MODE_BLOG.equals( mode ) )
-        {
+        if( MODE_BLOG.equals( mode ) ) {
             res = generateBlogRSS( wikiContext, changed, feed );
-        }
-        else if( MODE_FULL.equals(mode) )
-        {
+        } else if( MODE_FULL.equals(mode) ) {
             res = generateFullWikiRSS( wikiContext, feed );
-        }
-        else if( MODE_WIKI.equals(mode) )
-        {
+        } else if( MODE_WIKI.equals(mode) ) {
             res = generateWikiPageRSS( wikiContext, changed, feed );
-        }
-        else
-        {
+        } else {
             throw new IllegalArgumentException( "Invalid value for feed mode: "+mode );
         }
 
@@ -340,7 +316,7 @@ public class RSSGenerator {
      * methods output anything.
      * @param enabled whether RSS generation is considered enabled.
      */
-    public synchronized void setEnabled( boolean enabled )
+    public synchronized void setEnabled( final boolean enabled )
     {
         m_enabled = enabled;
     }
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/tags/InsertDiffTag.java b/jspwiki-main/src/main/java/org/apache/wiki/tags/InsertDiffTag.java
index 211bc79..761eff0 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/tags/InsertDiffTag.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/tags/InsertDiffTag.java
@@ -18,15 +18,14 @@
  */
 package org.apache.wiki.tags;
 
-import java.io.IOException;
-
-import javax.servlet.jsp.JspWriter;
-import javax.servlet.jsp.PageContext;
-
 import org.apache.log4j.Logger;
 import org.apache.wiki.WikiContext;
 import org.apache.wiki.WikiEngine;
 
+import javax.servlet.jsp.JspWriter;
+import javax.servlet.jsp.PageContext;
+import java.io.IOException;
+
 /**
  *  Writes difference between two pages using a HTML table.  If there is
  *  no difference, includes the body.
@@ -52,8 +51,7 @@ public class InsertDiffTag extends WikiTagBase {
     protected String m_pageName;
 
     /** {@inheritDoc} */
-    public void initTag()
-    {
+    public void initTag() {
         super.initTag();
         m_pageName = null;
     }
@@ -62,7 +60,7 @@ public class InsertDiffTag extends WikiTagBase {
      *  Sets the page name.
      *  @param page Page to get diff from.
      */
-    public void setPage( String page )
+    public void setPage( final String page )
     {
         m_pageName = page;
     }
@@ -77,39 +75,27 @@ public class InsertDiffTag extends WikiTagBase {
     }
 
     /** {@inheritDoc} */
-    public final int doWikiStartTag()
-        throws IOException
-    {
-        WikiEngine engine = m_wikiContext.getEngine();
-        WikiContext ctx;
+    public final int doWikiStartTag() throws IOException {
+        final WikiEngine engine = m_wikiContext.getEngine();
+        final WikiContext ctx;
         
-        if( m_pageName == null )
-        {
+        if( m_pageName == null ) {
             ctx = m_wikiContext;
-        }
-        else
-        {
-            ctx = (WikiContext)m_wikiContext.clone();
+        } else {
+            ctx = ( WikiContext )m_wikiContext.clone();
             ctx.setPage( engine.getPage(m_pageName) );
         }
 
-        Integer vernew = (Integer) pageContext.getAttribute( ATTR_NEWVERSION,
-                                                             PageContext.REQUEST_SCOPE );
-        Integer verold = (Integer) pageContext.getAttribute( ATTR_OLDVERSION,
-                                                             PageContext.REQUEST_SCOPE );
+        final Integer vernew = ( Integer )pageContext.getAttribute( ATTR_NEWVERSION, PageContext.REQUEST_SCOPE );
+        final Integer verold = ( Integer )pageContext.getAttribute( ATTR_OLDVERSION, PageContext.REQUEST_SCOPE );
 
         log.debug("Request diff between version "+verold+" and "+vernew);
 
-        if( ctx.getPage() != null )
-        {
-            JspWriter out = pageContext.getOut();
-
-            String diff = engine.getDiff( ctx, 
-                                          vernew.intValue(), 
-                                          verold.intValue() );
+        if( ctx.getPage() != null ) {
+            final JspWriter out = pageContext.getOut();
+            final String diff = engine.getDifferenceManager().getDiff( ctx, vernew.intValue(), verold.intValue() );
 
-            if( diff.length() == 0 )
-            {
+            if( diff.length() == 0 ) {
                 return EVAL_BODY_INCLUDE;
             }
 
@@ -118,5 +104,6 @@ public class InsertDiffTag extends WikiTagBase {
 
         return SKIP_BODY;
     }
+
 }
 
diff --git a/jspwiki-main/src/test/java/org/apache/wiki/diff/ContextualDiffProviderTest.java b/jspwiki-main/src/test/java/org/apache/wiki/diff/ContextualDiffProviderTest.java
index f2181d1..d2432bb 100644
--- a/jspwiki-main/src/test/java/org/apache/wiki/diff/ContextualDiffProviderTest.java
+++ b/jspwiki-main/src/test/java/org/apache/wiki/diff/ContextualDiffProviderTest.java
@@ -18,9 +18,6 @@
  */
 package org.apache.wiki.diff;
 
-import java.io.IOException;
-import java.util.Properties;
-
 import org.apache.log4j.PropertyConfigurator;
 import org.apache.wiki.TestEngine;
 import org.apache.wiki.WikiContext;
@@ -29,6 +26,9 @@ import org.apache.wiki.api.exceptions.WikiException;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
+import java.io.IOException;
+import java.util.Properties;
+
 public class ContextualDiffProviderTest
 {
     /**
@@ -40,25 +40,25 @@ public class ContextualDiffProviderTest
      */
     private void specializedNotation(ContextualDiffProvider diff)
     {
-        diff.m_changeEndHtml = "|";
-        diff.m_changeStartHtml = "|";
+        diff.CHANGE_END_HTML = "|";
+        diff.CHANGE_START_HTML = "|";
 
-        diff.m_deletionEndHtml = "-";
-        diff.m_deletionStartHtml = "-";
+        diff.DELETION_END_HTML = "-";
+        diff.DELETION_START_HTML = "-";
 
-        diff.m_diffEnd = "";
-        diff.m_diffStart = "";
+        diff.DIFF_END = "";
+        diff.DIFF_START = "";
 
-        diff.m_elidedHeadIndicatorHtml = "...";
-        diff.m_elidedTailIndicatorHtml = "...";
+        diff.ELIDED_HEAD_INDICATOR_HTML = "...";
+        diff.ELIDED_TAIL_INDICATOR_HTML = "...";
 
         diff.m_emitChangeNextPreviousHyperlinks = false;
 
-        diff.m_insertionEndHtml = "^";
-        diff.m_insertionStartHtml = "^";
+        diff.INSERTION_END_HTML = "^";
+        diff.INSERTION_START_HTML = "^";
 
-        diff.m_lineBreakHtml = "";
-        diff.m_alternatingSpaceHtml = "_";
+        diff.LINE_BREAK_HTML = "";
+        diff.ALTERNATING_SPACE_HTML = "_";
     }