001/*
002 * Copyright 2025 The Apache Software Foundation.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.apache.wiki.diff;
017
018import com.github.difflib.DiffUtils;
019import com.github.difflib.patch.AbstractDelta;
020import com.github.difflib.patch.Patch;
021import java.io.IOException;
022import java.util.List;
023import java.util.Properties;
024import org.apache.wiki.api.core.Context;
025import org.apache.wiki.api.core.Engine;
026import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
027import org.apache.wiki.util.TextUtil;
028
029/**
030 * SVN/Git style diff provider. Uses the DiffLib, ASF 2.0 licensed.
031 *
032 * @since 3.0.0
033 */
034public class SvnStyleDiffProvider implements DiffProvider {
035
036    public static final String CSS_DIFF_ADDED = "<tr class=\"diffadd\"><td>";
037    public static final String CSS_DIFF_REMOVED = "<tr class=\"diffrem\"><td>";
038    public static final String CSS_DIFF_UNCHANGED = "<tr class=\"diff\"><td>";
039    public static final String CSS_DIFF_CLOSE = "</td></tr>\n";
040    public static final String CELL_CHANGE = "</td><td>";
041
042    @Override
043    public String makeDiffHtml(Context context, String originalText, String modifiedText) {
044
045        List<String> original = originalText.lines().toList();
046        List<String> modified = modifiedText.lines().toList();
047        StringBuilder ret = new StringBuilder();
048        ret.append("<table class=\"diff\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");
049
050        Patch<String> patch = DiffUtils.diff(original, modified);
051        int lineNumber = 1;
052        int currentOriginalLine = 0;
053        int currentModifiedLine = 0;
054
055        for (AbstractDelta<String> delta : patch.getDeltas()) {
056            int originalPosition = delta.getSource().getPosition();
057            int modifiedPosition = delta.getTarget().getPosition();
058
059            // Output unchanged lines before the delta
060            while (currentOriginalLine < originalPosition && currentModifiedLine < modifiedPosition) {
061                ret.append(CSS_DIFF_UNCHANGED);
062                ret.append(lineNumber).append(CELL_CHANGE);
063                ret.append(TextUtil.replaceEntities(original.get(currentOriginalLine)));
064                ret.append(CSS_DIFF_CLOSE);
065                //System.out.println("  " + lineNumber + " " + original.get(currentOriginalLine));
066                lineNumber++;
067                currentOriginalLine++;
068                currentModifiedLine++;
069            }
070
071            List<String> originalLines = delta.getSource().getLines();
072            List<String> revisedLines = delta.getTarget().getLines();
073
074            for (String line : originalLines) {
075                ret.append(CSS_DIFF_REMOVED);
076                ret.append(lineNumber).append(CELL_CHANGE);
077                ret.append(TextUtil.replaceEntities(line));
078                ret.append(CSS_DIFF_CLOSE);
079                //System.out.println("- " + lineNumber + " " + line);
080                lineNumber++;
081                currentOriginalLine++;
082            }
083
084            for (String line : revisedLines) {
085                ret.append(CSS_DIFF_ADDED);
086                ret.append(lineNumber).append(CELL_CHANGE);
087                ret.append(TextUtil.replaceEntities(line));
088                ret.append(CSS_DIFF_CLOSE);
089                //System.out.println("+ " + lineNumber + " " + line);
090                lineNumber++;
091                currentModifiedLine++;
092            }
093        }
094
095        // Output any remaining unchanged lines at the end
096        while (currentOriginalLine < original.size() && currentModifiedLine < modified.size()) {
097            ret.append(CSS_DIFF_UNCHANGED);
098            ret.append(lineNumber).append(CELL_CHANGE);
099            ret.append(TextUtil.replaceEntities(original.get(currentOriginalLine)));
100            ret.append(CSS_DIFF_CLOSE);
101
102            //System.out.println("  " + lineNumber + " " + original.get(currentOriginalLine));
103            lineNumber++;
104            currentOriginalLine++;
105            currentModifiedLine++;
106        }
107        ret.append("</table>\n");
108        return ret.toString();
109    }
110
111    @Override
112    public void initialize(Engine engine, Properties properties) throws NoRequiredPropertyException, IOException {
113    }
114
115    @Override
116    public String getProviderInfo() {
117        return "SvnStyleDiffProvider";
118    }
119
120}