001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.visualweb.gravy;
043:
044: import org.netbeans.junit.diff.Diff;
045: import java.io.File;
046: import java.io.FileOutputStream;
047: import java.io.LineNumberReader;
048: import java.io.FileReader;
049: import java.io.FileWriter;
050: import java.io.PrintStream;
051: import java.io.PrintWriter;
052: import java.util.ArrayList;
053:
054: /**
055: * Compares 2 text files and finds lines, which are different.
056: */
057: public class LineDiff implements Diff {
058: private static boolean ignoreCase;
059:
060: /** Creates a new instance of LineDiff */
061: public LineDiff(boolean ignoreCase) {
062: this .ignoreCase = ignoreCase;
063: }
064:
065: /**
066: * Returns true or false depending on mode of file comparison.
067: * @return true, if file comparison is not case-sensitive, otherwise - false.
068: */
069: public static boolean getIgnoreCase() {
070: return ignoreCase;
071: }
072:
073: /**
074: * @param l1 first line to compare
075: * @param l2 second line to compare
076: * @return true if lines equal
077: */
078: private boolean compareLines(String l1, String l2) {
079: if (getIgnoreCase()) {
080: if (l1.equalsIgnoreCase(l2))
081: return true;
082: } else {
083: if (l1.equals(l2))
084: return true;
085: }
086: return false;
087: }
088:
089: /**
090: * @param first first file to compare
091: * @param second second file to compare
092: * @param diff difference file, caller can pass null value, when results are not needed.
093: * @return true iff files differ
094: */
095: public boolean diff(String first, String second, String diff)
096: throws java.io.IOException {
097: File fFirst = new File(first);
098: File fSecond = new File(second);
099: File fDiff = null != diff ? new File(diff) : null;
100: return diff(fFirst, fSecond, fDiff);
101: }
102:
103: /**
104: * @param first first file to compare
105: * @param second second file to compare
106: * @param diff difference file, caller can pass null value, when results are not needed.
107: * @return true iff files differ
108: */
109: public boolean diff(java.io.File firstFile,
110: java.io.File secondFile, java.io.File diffFile)
111: throws java.io.IOException {
112: LineNumberReader first = new LineNumberReader(new FileReader(
113: firstFile));
114: LineNumberReader second = new LineNumberReader(new FileReader(
115: secondFile));
116: String firstLine;
117: String secondLine;
118:
119: if (diffFile == null) {
120: while ((firstLine = first.readLine()) != null) {
121: secondLine = second.readLine();
122: if (secondLine == null) {
123: first.close();
124: second.close();
125: return true;
126: }
127: if (!compareLines(firstLine, secondLine)) {
128: first.close();
129: second.close();
130: return true;
131: }
132: }
133: } else {
134: ArrayList a1, a2, newLines, missingLines;
135:
136: a1 = new ArrayList();
137: while ((firstLine = first.readLine()) != null) {
138: a1.add(firstLine);
139: }
140: a2 = new ArrayList();
141: while ((secondLine = second.readLine()) != null) {
142: a2.add(secondLine);
143: }
144: first.close();
145: second.close();
146: newLines = new ArrayList();
147: missingLines = new ArrayList();
148:
149: int j = 0, bj;
150: boolean found;
151:
152: for (int i = 0; i < a1.size(); i++) {
153: if (j >= a2.size()) {
154: for (int k = i; k < a1.size(); k++) {
155: missingLines.add(k + "> " + a1.get(k));
156: }
157: break;
158: }
159: firstLine = (String) (a1.get(i));
160: secondLine = (String) (a2.get(j));
161: if (!compareLines(firstLine, secondLine)) {
162: found = false;
163: for (int k = j; k < a2.size(); k++) {
164: secondLine = (String) (a2.get(k));
165: if (compareLines(firstLine, secondLine)) {
166: for (int l = j; l < k; l++) {
167: newLines.add(l + "> " + a2.get(l));
168: }
169: j = k;
170: found = true;
171: break;
172: }
173: }
174: if (!found) {
175: missingLines.add(i + "> " + firstLine);
176: j--;
177: }
178: }
179: j++;
180: }
181: if (j < a2.size()) {
182: for (int i = j; i < a2.size(); i++) {
183: newLines.add(i + "> " + a2.get(i));
184: }
185: }
186:
187: if (missingLines.size() > 0 || newLines.size() > 0) {
188: PrintStream pw = null;
189: pw = new PrintStream(new FileOutputStream(diffFile));
190: //pw=System.out;
191: if (missingLines.size() > 0) {
192: pw
193: .println("-----------------------------Missing Lines:-----");
194: for (int i = 0; i < missingLines.size(); i++) {
195: pw.println(missingLines.get(i));
196: }
197: }
198: if (newLines.size() > 0) {
199: pw
200: .println("-----------------------------New Lines:---------");
201: for (int i = 0; i < newLines.size(); i++) {
202: pw.println(newLines.get(i));
203: }
204: }
205: pw.close();
206: return true;
207: }
208: }
209: return false;
210: }
211:
212: public static void main(String[] argv) {
213: try {
214: LineDiff diff = new LineDiff(true);
215: diff.diff("/tmp/diff/test.pass", "/tmp/diff/test.ref",
216: "/tmp/diff/test.diff");
217: } catch (Exception ex) {
218: ex.printStackTrace();
219: }
220: }
221: }
|