001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
018    
019    import java.io.Reader;
020    
021    import java.util.List;
022    
023    /**
024     * This class can compare two different versions of a text. Source refers to the
025     * earliest version of the text and target refers to a modified version of
026     * source. Changes are considered either as a removal from the source or as an
027     * addition to the target. This class detects changes to an entire line and also
028     * detects changes within lines, such as, removal or addition of characters.
029     * Take a look at <code>DiffTest</code> to see the expected inputs and outputs.
030     *
031     * @author Bruno Farache
032     * @see    com.liferay.portal.kernel.util.DiffUtil
033     */
034    public class DiffUtil {
035    
036            /**
037             * This is a diff method with default values.
038             *
039             * @param  source the source text
040             * @param  target the modified version of the source text
041             * @return an array containing two lists of <code>DiffResults</code>, the
042             *         first element contains DiffResults related to changes in source
043             *         and the second element to changes in target
044             */
045            public static List<DiffResult>[] diff(Reader source, Reader target) {
046                    return getDiff().diff(source, target);
047            }
048    
049            /**
050             * The main entrance of this class. This method will compare the two texts,
051             * highlight the changes by enclosing them with markers and return a list of
052             * <code>DiffResults</code>.
053             *
054             * @param  source the source text
055             * @param  target the modified version of the source text
056             * @param  addedMarkerStart the marker to indicate the start of text added
057             *         to the source
058             * @param  addedMarkerEnd the marker to indicate the end of text added to
059             *         the source
060             * @param  deletedMarkerStart the marker to indicate the start of text
061             *         deleted from the source
062             * @param  deletedMarkerEnd the marker to indicate the end of text deleted
063             *         from the source
064             * @param  margin the vertical margin to use in displaying differences
065             *         between changed line changes
066             * @return an array containing two lists of <code>DiffResults</code>, the
067             *         first element contains DiffResults related to changes in source
068             *         and the second element to changes in target
069             */
070            public static List<DiffResult>[] diff(
071                    Reader source, Reader target, String addedMarkerStart,
072                    String addedMarkerEnd, String deletedMarkerStart,
073                    String deletedMarkerEnd, int margin) {
074    
075                    return getDiff().diff(
076                            source, target, addedMarkerStart, addedMarkerEnd,
077                            deletedMarkerStart, deletedMarkerEnd, margin);
078            }
079    
080            public static Diff getDiff() {
081                    PortalRuntimePermission.checkGetBeanProperty(DiffUtil.class);
082    
083                    return _diff;
084            }
085    
086            public void setDiff(Diff diff) {
087                    PortalRuntimePermission.checkSetBeanProperty(getClass());
088    
089                    _diff = diff;
090            }
091    
092            private static Diff _diff;
093    
094    }