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-2006 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: /*
043: * LineDiff.java
044: *
045: * Created on March 28, 2002, 9:49 AM
046: */
047:
048: package lib;
049:
050: import org.netbeans.junit.diff.Diff;
051: import java.io.File;
052: import java.io.FileOutputStream;
053: import java.io.LineNumberReader;
054: import java.io.FileReader;
055: import java.io.FileWriter;
056: import java.io.PrintStream;
057: import java.io.PrintWriter;
058: import java.util.ArrayList;
059:
060: /**
061: *
062: * @author jlahoda, ehucka
063: */
064: public class LineDiff implements Diff {
065:
066: private boolean ignoreCase;
067:
068: /** Creates a new instance of LineDiff */
069: public LineDiff(boolean ignoreCase) {
070: this .ignoreCase = ignoreCase;
071: }
072:
073: public boolean getIgnoreCase() {
074: return ignoreCase;
075: }
076:
077: /**
078: * @param l1 first line to compare
079: * @param l2 second line to compare
080: * @return true if lines equal
081: */
082: private boolean compareLines(String l1, String l2) {
083: if (getIgnoreCase()) {
084: if (l1.equalsIgnoreCase(l2))
085: return true;
086: } else {
087: if (l1.equals(l2))
088: return true;
089: }
090: return false;
091: }
092:
093: /**
094: * @param first first file to compare
095: * @param second second file to compare
096: * @param diff difference file, caller can pass null value, when results are not needed.
097: * @return true iff files differ
098: */
099: public boolean diff(String first, String second, String diff)
100: throws java.io.IOException {
101: File fFirst = new File(first);
102: File fSecond = new File(second);
103: File fDiff = null != diff ? new File(diff) : null;
104: return diff(fFirst, fSecond, fDiff);
105: }
106:
107: /**
108: * @param first first file to compare
109: * @param second second file to compare
110: * @param diff difference file, caller can pass null value, when results are not needed.
111: * @return true iff files differ
112: */
113: public boolean diff(java.io.File firstFile,
114: java.io.File secondFile, java.io.File diffFile)
115: throws java.io.IOException {
116: LineNumberReader first = new LineNumberReader(new FileReader(
117: firstFile));
118: LineNumberReader second = new LineNumberReader(new FileReader(
119: secondFile));
120: String firstLine;
121: String secondLine;
122:
123: if (diffFile == null) {
124: while ((firstLine = first.readLine()) != null) {
125: secondLine = second.readLine();
126: if (secondLine == null) {
127: first.close();
128: second.close();
129: return true;
130: }
131: if (!compareLines(firstLine, secondLine)) {
132: first.close();
133: second.close();
134: return true;
135: }
136: }
137: } else {
138: ArrayList a1, a2, newLines, missingLines;
139:
140: a1 = new ArrayList();
141: while ((firstLine = first.readLine()) != null) {
142: a1.add(firstLine);
143: }
144: a2 = new ArrayList();
145: while ((secondLine = second.readLine()) != null) {
146: a2.add(secondLine);
147: }
148: first.close();
149: second.close();
150: newLines = new ArrayList();
151: missingLines = new ArrayList();
152:
153: int j = 0, bj;
154: boolean found;
155:
156: for (int i = 0; i < a1.size(); i++) {
157: if (j >= a2.size()) {
158: for (int k = i; k < a1.size(); k++) {
159: missingLines.add(k + "> " + a1.get(k));
160: }
161: break;
162: }
163: firstLine = (String) (a1.get(i));
164: secondLine = (String) (a2.get(j));
165: if (!compareLines(firstLine, secondLine)) {
166: found = false;
167: for (int k = j; k < a2.size(); k++) {
168: secondLine = (String) (a2.get(k));
169: if (compareLines(firstLine, secondLine)) {
170: for (int l = j; l < k; l++) {
171: newLines.add(l + "> " + a2.get(l));
172: }
173: j = k;
174: found = true;
175: break;
176: }
177: }
178: if (!found) {
179: missingLines.add(i + "> " + firstLine);
180: j--;
181: }
182: }
183: j++;
184: }
185: if (j < a2.size()) {
186: for (int i = j; i < a2.size(); i++) {
187: newLines.add(i + "> " + a2.get(i));
188: }
189: }
190:
191: if (missingLines.size() > 0 || newLines.size() > 0) {
192: PrintStream pw = null;
193: pw = new PrintStream(new FileOutputStream(diffFile));
194: //pw=System.out;
195: if (missingLines.size() > 0) {
196: pw
197: .println("-----------------------------Missing Lines:-----");
198: for (int i = 0; i < missingLines.size(); i++) {
199: pw.println(missingLines.get(i));
200: }
201: }
202: if (newLines.size() > 0) {
203: pw
204: .println("-----------------------------New Lines:---------");
205: for (int i = 0; i < newLines.size(); i++) {
206: pw.println(newLines.get(i));
207: }
208: }
209: pw.close();
210: return true;
211: }
212: }
213: return false;
214: }
215:
216: public static void main(String[] argv) {
217: try {
218: LineDiff diff = new LineDiff(true);
219: diff.diff("/tmp/diff/test.pass", "/tmp/diff/test.ref",
220: "/tmp/diff/test.diff");
221: } catch (Exception ex) {
222: ex.printStackTrace();
223: }
224: }
225: }
|