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 e0f1975e7713ce3d3f221b8a2904ceab4fefe4dc Author: juanpablo <[email protected]> AuthorDate: Fri Dec 20 18:51:14 2019 +0100 intellij format + fixes --- .../apache/wiki/diff/ContextualDiffProvider.java | 311 +++++++++------------ .../java/org/apache/wiki/diff/DiffProvider.java | 22 +- .../org/apache/wiki/diff/DifferenceManager.java | 55 ++-- .../org/apache/wiki/diff/ExternalDiffProvider.java | 80 +++--- .../apache/wiki/diff/TraditionalDiffProvider.java | 135 ++++----- 5 files changed, 250 insertions(+), 353 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 c568b0f..4b4ee3b 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 @@ -19,12 +19,6 @@ package org.apache.wiki.diff; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; -import java.util.StringTokenizer; - import org.apache.log4j.Logger; import org.apache.wiki.WikiContext; import org.apache.wiki.WikiEngine; @@ -41,13 +35,17 @@ import org.suigeneris.jrcs.diff.delta.DeleteDelta; import org.suigeneris.jrcs.diff.delta.Delta; import org.suigeneris.jrcs.diff.myers.MyersDiff; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.StringTokenizer; + /** - * A seriously better diff provider, which highlights changes word-by-word using - * CSS. + * A seriously better diff provider, which highlights changes word-by-word using CSS. * * Suggested by John Volkar. - * */ public class ContextualDiffProvider implements DiffProvider { @@ -65,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 String m_changeStartHtml = ""; //This could be a image '>' for a start marker - public String m_changeEndHtml = ""; //and an image for an end '<' marker - public String m_diffStart = "<div class=\"diff-wikitext\">"; - public String m_diffEnd = "</div>"; + 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>"; // Unfortunately we need to do dumb HTML here for RSS feeds. - public String m_insertionStartHtml = "<font color=\"#8000FF\"><span class=\"diff-insertion\">"; - public String m_insertionEndHtml = "</span></font>"; - public String m_deletionStartHtml = "<strike><font color=\"red\"><span class=\"diff-deletion\">"; - public String m_deletionEndHtml = "</span></font></strike>"; - private String m_anchorPreIndex = "<a name=\"change-"; - private String m_anchorPostIndex = "\" />"; - private String m_backPreIndex = "<a class=\"diff-nextprev\" title=\"Go to previous change\" href=\"#change-"; - private String m_backPostIndex = "\"><<</a>"; - private String m_forwardPreIndex = "<a class=\"diff-nextprev\" title=\"Go to next change\" href=\"#change-"; - private String m_forwardPostIndex = "\">>></a>"; - public String m_elidedHeadIndicatorHtml = "<br/><br/><b>...</b>"; - public String m_elidedTailIndicatorHtml = "<b>...</b><br/><br/>"; - public String m_lineBreakHtml = "<br />"; - public String m_alternatingSpaceHtml = " "; + 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>"; + 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 = "\"><<</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 = "\">>></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 = " "; // This one, I will make property file based... private static final int LIMIT_MAX_VALUE = (Integer.MAX_VALUE /2) - 1; @@ -114,19 +112,13 @@ public class ContextualDiffProvider implements DiffProvider { * * {@inheritDoc} */ - public void initialize(WikiEngine engine, Properties properties) throws NoRequiredPropertyException, IOException - { - String configuredLimit = properties.getProperty(PROP_UNCHANGED_CONTEXT_LIMIT, - Integer.toString(LIMIT_MAX_VALUE)); + public void initialize( final WikiEngine engine, final Properties properties) throws NoRequiredPropertyException, IOException { + final String configuredLimit = properties.getProperty( PROP_UNCHANGED_CONTEXT_LIMIT, Integer.toString( LIMIT_MAX_VALUE ) ); int limit = LIMIT_MAX_VALUE; - try - { - limit = Integer.parseInt(configuredLimit); - } - catch (NumberFormatException e) - { - log.warn("Failed to parseInt " + PROP_UNCHANGED_CONTEXT_LIMIT + "=" + configuredLimit - + " Will use a huge number as limit.", e); + try { + limit = Integer.parseInt( configuredLimit ); + } catch( final NumberFormatException e ) { + log.warn("Failed to parseInt " + PROP_UNCHANGED_CONTEXT_LIMIT + "=" + configuredLimit + " Will use a huge number as limit.", e ); } m_unchangedContextLimit = limit; } @@ -140,43 +132,34 @@ public class ContextualDiffProvider implements DiffProvider { * * {@inheritDoc} */ - public synchronized String makeDiffHtml( WikiContext ctx, String wikiOld, String wikiNew ) - { + public synchronized String makeDiffHtml( final WikiContext ctx, final String wikiOld, final String wikiNew ) { // // Sequencing handles lineterminator to <br /> and every-other consequtive space to a // - String[] alpha = sequence( TextUtil.replaceEntities( wikiOld ) ); - String[] beta = sequence( TextUtil.replaceEntities( wikiNew ) ); + final String[] alpha = sequence( TextUtil.replaceEntities( wikiOld ) ); + final String[] beta = sequence( TextUtil.replaceEntities( wikiNew ) ); - Revision rev = null; - try - { + final Revision rev; + try { rev = Diff.diff( alpha, beta, new MyersDiff() ); - } - catch( DifferentiationFailedException dfe ) - { + } catch( final DifferentiationFailedException dfe ) { log.error( "Diff generation failed", dfe ); return "Error while creating version diff."; } - int revSize = rev.size(); - - StringBuffer sb = new StringBuffer(); + final int revSize = rev.size(); + final StringBuffer sb = new StringBuffer(); - sb.append( m_diffStart ); + sb.append( DIFF_START ); // // The MyersDiff is a bit dumb by converting a single line multi-word diff into a series // of Changes. The ChangeMerger pulls them together again... // - ChangeMerger cm = new ChangeMerger( sb, alpha, revSize ); - + final ChangeMerger cm = new ChangeMerger( sb, alpha, revSize ); rev.accept( cm ); - cm.shutdown(); - - sb.append( m_diffEnd ); - + sb.append( DIFF_END ); return sb.toString(); } @@ -188,34 +171,29 @@ public class ContextualDiffProvider implements DiffProvider { * All this preseving of newlines and spaces is so the wikitext when diffed will have fidelity * to it's original form. As a side affect we see edits of purely whilespace. */ - private String[] sequence( String wikiText ) - { - String[] linesArray = Diff.stringToArray( wikiText ); - - List<String> list = new ArrayList<String>(); - - for( int i = 0; i < linesArray.length; i++ ) - { - String line = linesArray[i]; + private String[] sequence( final String wikiText ) { + final String[] linesArray = Diff.stringToArray( wikiText ); + final List< String > list = new ArrayList<>(); + for( final String line : linesArray ) { String lastToken = null; - String token = null; + String token; // StringTokenizer might be discouraged but it still is perfect here... - for (StringTokenizer st = new StringTokenizer( line, " ", true ); st.hasMoreTokens();) - { + for( final StringTokenizer st = new StringTokenizer( line, " ", true ); st.hasMoreTokens(); ) { token = st.nextToken(); - if(" ".equals( lastToken) && " ".equals( token )) - token = m_alternatingSpaceHtml; + if( " ".equals( lastToken ) && " ".equals( token ) ) { + token = ALTERNATING_SPACE_HTML; + } - list.add(token); + list.add( token ); lastToken = token; } - list.add(m_lineBreakHtml); // Line Break + list.add( LINE_BREAK_HTML ); // Line Break } - return list.toArray( new String[0] ); + return list.toArray( new String[ 0 ] ); } /** @@ -224,12 +202,11 @@ public class ContextualDiffProvider implements DiffProvider { * whole change process is threadsafe by encapsulating * all necessary variables. */ - private final class ChangeMerger implements RevisionVisitor - { - private StringBuffer m_sb = null; + private final class ChangeMerger implements RevisionVisitor { + private StringBuffer m_sb; /** Keeping score of the original lines to process */ - private int m_max = -1; + private int m_max; private int m_index = 0; @@ -243,15 +220,14 @@ public class ContextualDiffProvider implements DiffProvider { private int m_mode = -1; /* -1: Unset, 0: Add, 1: Del, 2: Change mode */ /** Buffer to coalesce the changes together */ - private StringBuffer m_origBuf = null; + private StringBuffer m_origBuf; - private StringBuffer m_newBuf = null; + private StringBuffer m_newBuf; /** Reference to the source string array */ - private String[] m_origStrings = null; + private String[] m_origStrings; - private ChangeMerger( final StringBuffer sb, final String[] origStrings, final int max ) - { + private ChangeMerger( final StringBuffer sb, final String[] origStrings, final int max ) { m_sb = sb; m_origStrings = origStrings != null ? origStrings.clone() : null; m_max = max; @@ -260,85 +236,73 @@ public class ContextualDiffProvider implements DiffProvider { m_newBuf = new StringBuffer(); } - private void updateState( Delta delta ) - { + private void updateState( final Delta delta ) { m_index++; - - Chunk orig = delta.getOriginal(); - - if (orig.first() > m_firstElem) - { + final Chunk orig = delta.getOriginal(); + if( orig.first() > m_firstElem ) { // We "skip" some lines in the output. // So flush out the last Change, if one exists. flushChanges(); // Allow us to "skip" large swaths of unchanged text, show a "limited" amound of // unchanged context so the changes are shown in - if ((orig.first() - m_firstElem) > 2 * m_unchangedContextLimit) - { - if (m_firstElem > 0) - { - int endIndex = Math.min( m_firstElem + m_unchangedContextLimit, m_origStrings.length -1 ); + if( ( orig.first() - m_firstElem ) > 2 * m_unchangedContextLimit ) { + if (m_firstElem > 0) { + final int endIndex = Math.min( m_firstElem + m_unchangedContextLimit, m_origStrings.length -1 ); - for (int j = m_firstElem; j < endIndex; j++) - m_sb.append(m_origStrings[j]); + for( int j = m_firstElem; j < endIndex; j++ ) { + m_sb.append( m_origStrings[ j ] ); + } - m_sb.append(m_elidedTailIndicatorHtml); + m_sb.append( ELIDED_TAIL_INDICATOR_HTML ); } - m_sb.append(m_elidedHeadIndicatorHtml); + m_sb.append( ELIDED_HEAD_INDICATOR_HTML ); - int startIndex = Math.max(orig.first() - m_unchangedContextLimit, 0); - for (int j = startIndex; j < orig.first(); j++) - m_sb.append(m_origStrings[j]); + final int startIndex = Math.max(orig.first() - m_unchangedContextLimit, 0); + for (int j = startIndex; j < orig.first(); j++) { + m_sb.append( m_origStrings[ j ] ); + } - } - else - { + } else { // No need to skip anything, just output the whole range... - for (int j = m_firstElem; j < orig.first(); j++) - m_sb.append( m_origStrings[j] ); + for( int j = m_firstElem; j < orig.first(); j++ ) { + m_sb.append( m_origStrings[ j ] ); + } } } m_firstElem = orig.last() + 1; } - public void visit( Revision rev ) - { + public void visit( final Revision rev ) { // GNDN (Goes nowhere, does nothing) } - public void visit( AddDelta delta ) - { + public void visit( final AddDelta delta ) { updateState( delta ); // We have run Deletes up to now. Flush them out. - if( m_mode == 1 ) - { + if( m_mode == 1 ) { flushChanges(); m_mode = -1; } // We are in "neutral mode". Start a new Change - if( m_mode == -1 ) - { + if( m_mode == -1 ) { m_mode = 0; } // We are in "add mode". - if( m_mode == 0 || m_mode == 2 ) - { + if( m_mode == 0 || m_mode == 2 ) { addNew( delta.getRevised() ); m_mode = 1; } } - public void visit( ChangeDelta delta ) - { + public void visit( final ChangeDelta delta ) { updateState( delta ); // We are in "neutral mode". A Change might be merged with an add or delete. - if( m_mode == -1 ) - { + if( m_mode == -1 ) { m_mode = 2; } @@ -347,123 +311,101 @@ public class ContextualDiffProvider implements DiffProvider { addNew( delta.getRevised() ); } - public void visit( DeleteDelta delta ) - { + public void visit( final DeleteDelta delta ) { updateState( delta ); // We have run Adds up to now. Flush them out. - if( m_mode == 0 ) - { + if( m_mode == 0 ) { flushChanges(); m_mode = -1; } // We are in "neutral mode". Start a new Change - if( m_mode == -1 ) - { + if( m_mode == -1 ) { m_mode = 1; } // We are in "delete mode". - if( m_mode == 1 || m_mode == 2 ) - { + if( m_mode == 1 || m_mode == 2 ) { addOrig( delta.getOriginal() ); m_mode = 1; } } - public void shutdown() - { + public void shutdown() { m_index = m_max + 1; // Make sure that no hyperlink gets created flushChanges(); - if (m_firstElem < m_origStrings.length) - { + if( m_firstElem < m_origStrings.length ) { // If there's more than the limit of the orginal left just emit limit and elided... - if ((m_origStrings.length - m_firstElem) > m_unchangedContextLimit) - { - int endIndex = Math.min( m_firstElem + m_unchangedContextLimit, m_origStrings.length -1 ); - - for (int j = m_firstElem; j < endIndex; j++) - m_sb.append( m_origStrings[j] ); + if( ( m_origStrings.length - m_firstElem ) > m_unchangedContextLimit ) { + final int endIndex = Math.min( m_firstElem + m_unchangedContextLimit, m_origStrings.length -1 ); + for (int j = m_firstElem; j < endIndex; j++) { + m_sb.append( m_origStrings[ j ] ); + } - m_sb.append(m_elidedTailIndicatorHtml); - } - else + m_sb.append( ELIDED_TAIL_INDICATOR_HTML ); + } else { // emit entire tail of original... - { - for (int j = m_firstElem; j < m_origStrings.length; j++) - m_sb.append(m_origStrings[j]); + for( int j = m_firstElem; j < m_origStrings.length; j++ ) { + m_sb.append( m_origStrings[ j ] ); + } } } } - private void addOrig( Chunk chunk ) - { - if( chunk != null ) - { + private void addOrig( final Chunk chunk ) { + if( chunk != null ) { chunk.toString( m_origBuf ); } } - private void addNew( Chunk chunk ) - { - if( chunk != null ) - { + private void addNew( final Chunk chunk ) { + if( chunk != null ) { chunk.toString( m_newBuf ); } } - - - private void flushChanges() - { - - if( m_newBuf.length() + m_origBuf.length() > 0 ) - { + private void flushChanges() { + if( m_newBuf.length() + m_origBuf.length() > 0 ) { // This is the span element which encapsulates anchor and the change itself - m_sb.append( m_changeStartHtml ); + m_sb.append( CHANGE_START_HTML ); // Do we want to have a "back link"? - if( m_emitChangeNextPreviousHyperlinks && m_count > 1 ) - { - m_sb.append( m_backPreIndex ); + if( m_emitChangeNextPreviousHyperlinks && m_count > 1 ) { + m_sb.append( BACK_PRE_INDEX ); m_sb.append( m_count - 1 ); - m_sb.append( m_backPostIndex ); + m_sb.append( BACK_POST_INDEX ); } // An anchor for the change. - if (m_emitChangeNextPreviousHyperlinks) - { - m_sb.append( m_anchorPreIndex ); + if (m_emitChangeNextPreviousHyperlinks) { + m_sb.append( ANCHOR_PRE_INDEX ); m_sb.append( m_count++ ); - m_sb.append( m_anchorPostIndex ); + m_sb.append( ANCHOR_POST_INDEX ); } // ... has been added - if( m_newBuf.length() > 0 ) - { - m_sb.append( m_insertionStartHtml ); + if( m_newBuf.length() > 0 ) { + m_sb.append( INSERTION_START_HTML ); m_sb.append( m_newBuf ); - m_sb.append( m_insertionEndHtml ); + m_sb.append( INSERTION_END_HTML ); } // .. has been removed - if( m_origBuf.length() > 0 ) - { - m_sb.append( m_deletionStartHtml ); + if( m_origBuf.length() > 0 ) { + m_sb.append( DELETION_START_HTML ); m_sb.append( m_origBuf ); - m_sb.append( m_deletionEndHtml ); + m_sb.append( DELETION_END_HTML ); } // Do we want a "forward" link? - if( m_emitChangeNextPreviousHyperlinks && (m_index < m_max) ) - { - m_sb.append( m_forwardPreIndex ); + if( m_emitChangeNextPreviousHyperlinks && (m_index < m_max) ) { + m_sb.append( FORWARD_PRE_INDEX ); m_sb.append( m_count ); // Has already been incremented. - m_sb.append( m_forwardPostIndex ); + m_sb.append( FORWARD_POST_INDEX ); } - m_sb.append( m_changeEndHtml ); + m_sb.append( CHANGE_END_HTML ); // Nuke the buffers. m_origBuf = new StringBuffer(); @@ -474,4 +416,5 @@ public class ContextualDiffProvider implements DiffProvider { m_mode = -1; } } + } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/diff/DiffProvider.java b/jspwiki-main/src/main/java/org/apache/wiki/diff/DiffProvider.java index 774eaa6..4a0aced 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/diff/DiffProvider.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/diff/DiffProvider.java @@ -18,19 +18,19 @@ */ package org.apache.wiki.diff; -import java.io.IOException; -import java.util.Properties; - import org.apache.wiki.WikiContext; import org.apache.wiki.WikiEngine; import org.apache.wiki.WikiProvider; import org.apache.wiki.api.exceptions.NoRequiredPropertyException; +import java.io.IOException; +import java.util.Properties; + + /** * Provides an SPI for creating a diff between two page versions. */ -public interface DiffProvider extends WikiProvider -{ +public interface DiffProvider extends WikiProvider { /** * The return string is to be XHTML compliant ready to display html. No further * processing of this text will be done by the wiki engine. @@ -40,27 +40,23 @@ public interface DiffProvider extends WikiProvider * @param oldWikiText the old text * @param newWikiText the new text */ - String makeDiffHtml(WikiContext context, String oldWikiText, String newWikiText); + String makeDiffHtml( WikiContext context, String oldWikiText, String newWikiText ); /** * If there is no diff provider set, this provider will work instead. */ - class NullDiffProvider implements DiffProvider - { + class NullDiffProvider implements DiffProvider { /** * {@inheritDoc} */ - public String makeDiffHtml(WikiContext ctx, String oldWikiText, String newWikiText) - { + public String makeDiffHtml( final WikiContext ctx, final String oldWikiText, final String newWikiText ) { return "You are using the NullDiffProvider, check your properties file."; } /** * {@inheritDoc} */ - public void initialize(WikiEngine engine, Properties properties) - throws NoRequiredPropertyException, IOException - { + public void initialize( final WikiEngine engine, final Properties properties ) throws NoRequiredPropertyException, IOException { } /** 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 d15d5ce..6643847 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 @@ -19,21 +19,22 @@ package org.apache.wiki.diff; -import java.io.IOException; -import java.util.Properties; - 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.util.ClassUtil; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +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); + private static final Logger log = Logger.getLogger( DifferenceManager.class ); /** * Property value for storing a diff provider. Value is {@value}. @@ -48,42 +49,32 @@ public class DifferenceManager { * @param engine The WikiEngine. * @param props A set of properties. */ - public DifferenceManager(WikiEngine engine, Properties props) { - loadProvider(props); + public DifferenceManager( final WikiEngine engine, final Properties props ) { + loadProvider( props ); + initializeProvider( engine, props ); - initializeProvider(engine, props); - - log.info("Using difference provider: " + m_provider.getProviderInfo()); + log.info( "Using difference provider: " + m_provider.getProviderInfo() ); } - private void loadProvider(Properties props) { - String providerClassName = props.getProperty(PROP_DIFF_PROVIDER, - TraditionalDiffProvider.class.getName()); - + private void loadProvider( final Properties props ) { + final String providerClassName = props.getProperty( PROP_DIFF_PROVIDER, TraditionalDiffProvider.class.getName() ); try { - Class<?> providerClass = ClassUtil.findClass("org.apache.wiki.diff", providerClassName); - m_provider = (DiffProvider) providerClass.newInstance(); - } catch (ClassNotFoundException e) { - log.warn("Failed loading DiffProvider, will use NullDiffProvider.", e); - } catch (InstantiationException e) { - log.warn("Failed loading DiffProvider, will use NullDiffProvider.", e); - } catch (IllegalAccessException e) { + final Class< ? > providerClass = ClassUtil.findClass("org.apache.wiki.diff", providerClassName ); + m_provider = (DiffProvider) providerClass.getDeclaredConstructor().newInstance(); + } catch( final ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e ) { log.warn("Failed loading DiffProvider, will use NullDiffProvider.", e); } - if (null == m_provider) { + if( m_provider == null ) { m_provider = new DiffProvider.NullDiffProvider(); } } - private void initializeProvider(WikiEngine engine, Properties props) { + private void initializeProvider( final WikiEngine engine, final Properties props ) { try { m_provider.initialize(engine, props); - } catch (NoRequiredPropertyException e1) { - log.warn("Failed initializing DiffProvider, will use NullDiffProvider.", e1); - m_provider = new DiffProvider.NullDiffProvider(); //doesn't need init'd - } catch (IOException e1) { + } catch( final NoRequiredPropertyException | IOException e1 ) { log.warn("Failed initializing DiffProvider, will use NullDiffProvider.", e1); m_provider = new DiffProvider.NullDiffProvider(); //doesn't need init'd } @@ -97,17 +88,17 @@ public class DifferenceManager { * @param secondWikiText the new text * @return XHTML, or empty string, if no difference detected. */ - public String makeDiff(WikiContext context, String firstWikiText, String secondWikiText) { - String diff = null; + public String makeDiff( final WikiContext context, final String firstWikiText, final String secondWikiText ) { + String diff; try { - diff = m_provider.makeDiffHtml(context, firstWikiText, secondWikiText); + diff = m_provider.makeDiffHtml( context, firstWikiText, secondWikiText ); - if (diff == null) { + if( diff == null ) { diff = ""; } - } catch (Exception e) { + } catch( final Exception e ) { diff = "Failed to create a diff, check the logs."; - log.warn(diff, e); + log.warn( diff, e ); } return diff; } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/diff/ExternalDiffProvider.java b/jspwiki-main/src/main/java/org/apache/wiki/diff/ExternalDiffProvider.java index 48b920f..fdf7cee 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/diff/ExternalDiffProvider.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/diff/ExternalDiffProvider.java @@ -35,11 +35,10 @@ import java.nio.charset.StandardCharsets; import java.util.Properties; /** - * This DiffProvider allows external command line tools to be used to generate - * the diff. + * This DiffProvider allows external command line tools to be used to generate the diff. */ -public class ExternalDiffProvider implements DiffProvider -{ +public class ExternalDiffProvider implements DiffProvider { + private static final Logger log = Logger.getLogger(ExternalDiffProvider.class); /** @@ -85,13 +84,9 @@ public class ExternalDiffProvider implements DiffProvider * {@inheritDoc} * @see org.apache.wiki.WikiProvider#initialize(org.apache.wiki.WikiEngine, java.util.Properties) */ - public void initialize( WikiEngine engine, Properties properties ) - throws NoRequiredPropertyException, IOException - { + public void initialize( final WikiEngine engine, final Properties properties ) throws NoRequiredPropertyException, IOException { m_diffCommand = properties.getProperty( PROP_DIFFCOMMAND ); - - if( (null == m_diffCommand) || (m_diffCommand.trim().equals( "" )) ) - { + if( m_diffCommand == null || m_diffCommand.trim().equals( "" ) ) { throw new NoRequiredPropertyException( "ExternalDiffProvider missing required property", PROP_DIFFCOMMAND ); } @@ -103,36 +98,33 @@ public class ExternalDiffProvider implements DiffProvider * Makes the diff by calling "diff" program. * {@inheritDoc} */ - public String makeDiffHtml( WikiContext ctx, String p1, String p2 ) - { + public String makeDiffHtml( final WikiContext ctx, final String p1, final String p2 ) { File f1 = null; File f2 = null; String diff = null; - try - { + try { f1 = FileUtil.newTmpFile(p1, m_encoding); f2 = FileUtil.newTmpFile(p2, m_encoding); String cmd = TextUtil.replaceString(m_diffCommand, "%s1", f1.getPath()); cmd = TextUtil.replaceString(cmd, "%s2", f2.getPath()); - String output = FileUtil.runSimpleCommand(cmd, f1.getParent()); + final String output = FileUtil.runSimpleCommand(cmd, f1.getParent()); // FIXME: Should this rely on the system default encoding? - String rawWikiDiff = new String( output.getBytes( StandardCharsets.ISO_8859_1 ), m_encoding ); - - String htmlWikiDiff = TextUtil.replaceEntities( rawWikiDiff ); + final String rawWikiDiff = new String( output.getBytes( StandardCharsets.ISO_8859_1 ), m_encoding ); + final String htmlWikiDiff = TextUtil.replaceEntities( rawWikiDiff ); - if (m_traditionalColorization) //FIXME, see comment near declaration... - diff = colorizeDiff(htmlWikiDiff); - else + if (m_traditionalColorization) { //FIXME, see comment near declaration... + diff = colorizeDiff( htmlWikiDiff ); + } else { diff = htmlWikiDiff; - - } catch (IOException e) { - log.error("Failed to do file diff", e); - } catch (InterruptedException e) { - log.error("Interrupted", e); + } + } catch( final IOException e ) { + log.error("Failed to do file diff", e ); + } catch( final InterruptedException e ) { + log.error("Interrupted", e ); } finally { if( f1 != null ) { f1.delete(); @@ -149,30 +141,26 @@ public class ExternalDiffProvider implements DiffProvider /** * Goes through output provided by a diff command and inserts HTML tags to * make the result more legible. Currently colors lines starting with a + - * green, those starting with - reddish (hm, got to think of color blindness - * here...). + * green, those starting with - reddish (hm, got to think of color blindness here...). */ - static String colorizeDiff(String diffText) throws IOException { + static String colorizeDiff( final String diffText ) throws IOException { if( diffText == null ) { return "Invalid diff - probably something wrong with server setup."; } - String line = null; - String start = null; - String stop = null; + String line; + String start; + String stop; - BufferedReader in = new BufferedReader( new StringReader( diffText ) ); - StringBuilder out = new StringBuilder(); + final BufferedReader in = new BufferedReader( new StringReader( diffText ) ); + final StringBuilder out = new StringBuilder(); out.append("<table class=\"diff\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n"); - while( (line = in.readLine()) != null ) - { + while( (line = in.readLine()) != null ) { stop = CSS_DIFF_CLOSE; - if( line.length() > 0 ) - { - switch( line.charAt( 0 ) ) - { + if( line.length() > 0 ) { + switch( line.charAt( 0 ) ) { case DIFF_ADDED_SYMBOL: start = CSS_DIFF_ADDED; break; @@ -182,18 +170,18 @@ public class ExternalDiffProvider implements DiffProvider default: start = CSS_DIFF_UNCHANGED; } - } - else - { + } else { start = CSS_DIFF_UNCHANGED; } - out.append( start ); - out.append( line.trim() ); - out.append( stop + "\n" ); + out.append( start ) + .append( line.trim() ) + .append( stop ) + .append( "\n" ); } out.append( "</table>\n" ); return out.toString(); } + } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/diff/TraditionalDiffProvider.java b/jspwiki-main/src/main/java/org/apache/wiki/diff/TraditionalDiffProvider.java index 14df7d9..6bd60bf 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/diff/TraditionalDiffProvider.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/diff/TraditionalDiffProvider.java @@ -19,14 +19,6 @@ package org.apache.wiki.diff; -import java.io.IOException; -import java.text.ChoiceFormat; -import java.text.Format; -import java.text.MessageFormat; -import java.text.NumberFormat; -import java.util.Properties; -import java.util.ResourceBundle; - import org.apache.log4j.Logger; import org.apache.wiki.WikiContext; import org.apache.wiki.WikiEngine; @@ -44,29 +36,32 @@ import org.suigeneris.jrcs.diff.delta.Chunk; import org.suigeneris.jrcs.diff.delta.DeleteDelta; import org.suigeneris.jrcs.diff.myers.MyersDiff; +import java.io.IOException; +import java.text.ChoiceFormat; +import java.text.Format; +import java.text.MessageFormat; +import java.text.NumberFormat; +import java.util.Properties; +import java.util.ResourceBundle; + /** * This is the JSPWiki 'traditional' diff. It uses an internal diff engine. - * */ -public class TraditionalDiffProvider implements DiffProvider -{ - private static final Logger log = Logger.getLogger(TraditionalDiffProvider.class); +public class TraditionalDiffProvider implements DiffProvider { + private static final Logger log = Logger.getLogger( TraditionalDiffProvider.class ); private static final String CSS_DIFF_ADDED = "<tr><td class=\"diffadd\">"; private static final String CSS_DIFF_REMOVED = "<tr><td class=\"diffrem\">"; private static final String CSS_DIFF_UNCHANGED = "<tr><td class=\"diff\">"; private static final String CSS_DIFF_CLOSE = "</td></tr>" + Diff.NL; - /** * Constructs the provider. */ - public TraditionalDiffProvider() - { + public TraditionalDiffProvider() { } - /** * {@inheritDoc} * @see org.apache.wiki.WikiProvider#getProviderInfo() @@ -80,9 +75,7 @@ public class TraditionalDiffProvider implements DiffProvider * {@inheritDoc} * @see org.apache.wiki.WikiProvider#initialize(org.apache.wiki.WikiEngine, java.util.Properties) */ - public void initialize(WikiEngine engine, Properties properties) - throws NoRequiredPropertyException, IOException - { + public void initialize( final WikiEngine engine, final Properties properties ) throws NoRequiredPropertyException, IOException { } /** @@ -95,105 +88,91 @@ public class TraditionalDiffProvider implements DiffProvider * * @return Full HTML diff. */ - public String makeDiffHtml( WikiContext ctx, String p1, String p2 ) - { - String diffResult = ""; + public String makeDiffHtml( final WikiContext ctx, final String p1, final String p2 ) { + final String diffResult; - try - { - String[] first = Diff.stringToArray(TextUtil.replaceEntities(p1)); - String[] second = Diff.stringToArray(TextUtil.replaceEntities(p2)); - Revision rev = Diff.diff(first, second, new MyersDiff()); + try { + final String[] first = Diff.stringToArray(TextUtil.replaceEntities(p1)); + final String[] second = Diff.stringToArray(TextUtil.replaceEntities(p2)); + final Revision rev = Diff.diff(first, second, new MyersDiff()); - if( rev == null || rev.size() == 0 ) - { + if( rev == null || rev.size() == 0 ) { // No difference - return ""; } - StringBuffer ret = new StringBuffer(rev.size() * 20); // Guessing how big it will become... + final StringBuffer ret = new StringBuffer(rev.size() * 20); // Guessing how big it will become... - ret.append("<table class=\"diff\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n"); - rev.accept( new RevisionPrint(ctx,ret) ); - ret.append("</table>\n"); + ret.append( "<table class=\"diff\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n" ); + rev.accept( new RevisionPrint( ctx, ret ) ); + ret.append( "</table>\n" ); return ret.toString(); - } - catch( DifferentiationFailedException e ) - { + } catch( final DifferentiationFailedException e ) { diffResult = "makeDiff failed with DifferentiationFailedException"; - log.error(diffResult, e); + log.error( diffResult, e ); } return diffResult; } - private static final class RevisionPrint - implements RevisionVisitor - { - private StringBuffer m_result = null; + private static final class RevisionPrint implements RevisionVisitor { + + private StringBuffer m_result; private WikiContext m_context; private ResourceBundle m_rb; - private RevisionPrint(WikiContext ctx,StringBuffer sb) - { + private RevisionPrint( final WikiContext ctx, final StringBuffer sb ) { m_result = sb; m_context = ctx; m_rb = Preferences.getBundle( ctx, InternationalizationManager.CORE_BUNDLE ); } - public void visit(Revision rev) - { + public void visit( final Revision rev ) { // GNDN (Goes nowhere, does nothing) } - public void visit(AddDelta delta) - { - Chunk changed = delta.getRevised(); - print(changed, m_rb.getString( "diff.traditional.added" ) ); - changed.toString(m_result, CSS_DIFF_ADDED, CSS_DIFF_CLOSE); + public void visit( final AddDelta delta ) { + final Chunk changed = delta.getRevised(); + print( changed, m_rb.getString( "diff.traditional.added" ) ); + changed.toString( m_result, CSS_DIFF_ADDED, CSS_DIFF_CLOSE ); } - public void visit(ChangeDelta delta) - { - Chunk changed = delta.getOriginal(); - print(changed, m_rb.getString( "diff.traditional.changed") ); - changed.toString(m_result, CSS_DIFF_REMOVED, CSS_DIFF_CLOSE); - delta.getRevised().toString(m_result, CSS_DIFF_ADDED, CSS_DIFF_CLOSE); + public void visit( final ChangeDelta delta ) { + final Chunk changed = delta.getOriginal(); + print(changed, m_rb.getString( "diff.traditional.changed" ) ); + changed.toString( m_result, CSS_DIFF_REMOVED, CSS_DIFF_CLOSE ); + delta.getRevised().toString( m_result, CSS_DIFF_ADDED, CSS_DIFF_CLOSE ); } - public void visit(DeleteDelta delta) - { - Chunk changed = delta.getOriginal(); - print(changed, m_rb.getString( "diff.traditional.removed") ); - changed.toString(m_result, CSS_DIFF_REMOVED, CSS_DIFF_CLOSE); + public void visit( final DeleteDelta delta ) { + final Chunk changed = delta.getOriginal(); + print( changed, m_rb.getString( "diff.traditional.removed" ) ); + changed.toString( m_result, CSS_DIFF_REMOVED, CSS_DIFF_CLOSE ); } - private void print(Chunk changed, String type) - { - m_result.append(CSS_DIFF_UNCHANGED); - - String[] choiceString = - { + private void print( final Chunk changed, final String type ) { + m_result.append( CSS_DIFF_UNCHANGED ); + + final String[] choiceString = { m_rb.getString("diff.traditional.oneline"), m_rb.getString("diff.traditional.lines") }; - double[] choiceLimits = { 1, 2 }; - - MessageFormat fmt = new MessageFormat(""); + final double[] choiceLimits = { 1, 2 }; + + final MessageFormat fmt = new MessageFormat(""); fmt.setLocale( Preferences.getLocale(m_context) ); - ChoiceFormat cfmt = new ChoiceFormat( choiceLimits, choiceString ); + final ChoiceFormat cfmt = new ChoiceFormat( choiceLimits, choiceString ); fmt.applyPattern( type ); - Format[] formats = { NumberFormat.getInstance(), cfmt, NumberFormat.getInstance() }; + final Format[] formats = { NumberFormat.getInstance(), cfmt, NumberFormat.getInstance() }; fmt.setFormats( formats ); - - Object[] params = { changed.first() + 1, - changed.size(), - changed.size() }; + + final Object[] params = { changed.first() + 1, + changed.size(), + changed.size() }; m_result.append( fmt.format(params) ); - m_result.append(CSS_DIFF_CLOSE); + m_result.append( CSS_DIFF_CLOSE ); } } }
