001: /*
002: Copyright 2001 Nicholas Allen (nallen@freenet.co.uk)
003: This file is part of JavaCVS.
004: JavaCVS is free software; you can redistribute it and/or modify
005: it under the terms of the GNU General Public License as published by
006: the Free Software Foundation; either version 2 of the License, or
007: (at your option) any later version.
008: JavaCVS is distributed in the hope that it will be useful,
009: but WITHOUT ANY WARRANTY; without even the implied warranty of
010: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: GNU General Public License for more details.
012: You should have received a copy of the GNU General Public License
013: along with JavaCVS; if not, write to the Free Software
014: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
015: */
016: package allensoft.diff;
017:
018: import java.io.BufferedReader;
019: import java.io.EOFException;
020: import java.io.IOException;
021: import java.io.Reader;
022:
023: /** Proceses differences and applies them to a Reader. The output is controlled by overridable
024: printXXXX methods. By default these methods simply output the result of applying the
025: differences to System.out. */
026: public class DiffProcessor {
027: private BufferedReader m_In;
028: private DiffParser m_DiffParser;
029:
030: /** Creates a new DiffProcessor that applies the differnces returned from <code>diffParser</code>
031: to <code>file1</code>. */
032: public DiffProcessor(Reader file1, DiffParser diffParser) {
033: m_In = new BufferedReader(file1);
034: m_DiffParser = diffParser;
035: }
036:
037: /** Runs this DiffProcessor. */
038: public void run() throws IOException, DiffException {
039: int nLineNumInFile1 = 1;
040: int nLineNumInFile2 = 1;
041: String sLine;
042:
043: while (true) {
044: Difference d = m_DiffParser.getNextDifference();
045:
046: if (d == null) {
047: while ((sLine = m_In.readLine()) != null) {
048: printLineInFile1(nLineNumInFile1, DiffType.NONE,
049: sLine);
050: printLineInFile2(nLineNumInFile2, DiffType.NONE,
051: sLine);
052: nLineNumInFile1++;
053: nLineNumInFile2++;
054: }
055:
056: break;
057: }
058:
059: if (d instanceof Insertion) {
060: Insertion insertion = (Insertion) d;
061:
062: while (nLineNumInFile1 <= insertion
063: .getStartLineInFile1()) {
064: sLine = m_In.readLine();
065:
066: if (sLine == null)
067: throw new EOFException();
068:
069: printLineInFile1(nLineNumInFile1, DiffType.NONE,
070: sLine);
071: printLineInFile2(nLineNumInFile2, DiffType.NONE,
072: sLine);
073: nLineNumInFile1++;
074: nLineNumInFile2++;
075: }
076:
077: nLineNumInFile2 = insertion.getStartLineInFile2();
078:
079: String sInsertedText = insertion.getInsertedText();
080: int i = 0;
081:
082: while (i < sInsertedText.length()) {
083: int nStartOfNextLine = sInsertedText.indexOf('\n',
084: i);
085: sLine = (nStartOfNextLine == -1) ? sInsertedText
086: .substring(i) : sInsertedText.substring(i,
087: nStartOfNextLine);
088:
089: printNonExistantLineInFile1();
090: printLineInFile2(nLineNumInFile2,
091: DiffType.INSERTION, sLine);
092: nLineNumInFile2++;
093:
094: if (nStartOfNextLine == -1)
095: break;
096:
097: i = nStartOfNextLine + 1;
098: }
099: } else if (d instanceof Change) {
100: Change change = (Change) d;
101:
102: while (nLineNumInFile1 < change.getStartLineInFile1()) {
103: sLine = m_In.readLine();
104:
105: if (sLine == null)
106: throw new EOFException();
107:
108: printLineInFile1(nLineNumInFile1, DiffType.NONE,
109: sLine);
110: printLineInFile2(nLineNumInFile2, DiffType.NONE,
111: sLine);
112: nLineNumInFile1++;
113: nLineNumInFile2++;
114: }
115:
116: int nNumLinesPrintedInFile1 = 0;
117: int nNumLinesPrintedInFile2 = 0;
118:
119: while (nLineNumInFile1 <= change.getEndLineInFile1()) {
120: sLine = m_In.readLine();
121:
122: if (sLine == null)
123: throw new EOFException();
124:
125: printLineInFile1(nLineNumInFile1, DiffType.CHANGE,
126: sLine);
127: nLineNumInFile1++;
128: nNumLinesPrintedInFile1++;
129: }
130:
131: nLineNumInFile2 = change.getStartLineInFile2();
132:
133: String sChangedText = change.getToText();
134: int i = 0;
135:
136: while (i < sChangedText.length()) {
137: int nStartOfNextLine = sChangedText
138: .indexOf('\n', i);
139: sLine = (nStartOfNextLine == -1) ? sChangedText
140: .substring(i) : sChangedText.substring(i,
141: nStartOfNextLine);
142:
143: printLineInFile2(nLineNumInFile2, DiffType.CHANGE,
144: sLine);
145: nLineNumInFile2++;
146: nNumLinesPrintedInFile2++;
147:
148: if (nStartOfNextLine == -1)
149: break;
150:
151: i = nStartOfNextLine + 1;
152: }
153:
154: if (nNumLinesPrintedInFile1 > nNumLinesPrintedInFile2) {
155: for (int j = nNumLinesPrintedInFile2; j < nNumLinesPrintedInFile1; j++)
156: printNonExistantLineInFile2();
157: } else if (nNumLinesPrintedInFile2 > nNumLinesPrintedInFile1) {
158: for (int j = nNumLinesPrintedInFile1; j < nNumLinesPrintedInFile2; j++)
159: printNonExistantLineInFile1();
160: }
161: } else if (d instanceof Deletion) {
162: Deletion deletion = (Deletion) d;
163:
164: while (nLineNumInFile1 < deletion.getStartLineInFile1()) {
165: sLine = m_In.readLine();
166:
167: if (sLine == null)
168: throw new EOFException();
169:
170: printLineInFile1(nLineNumInFile1, DiffType.NONE,
171: sLine);
172: printLineInFile2(nLineNumInFile2, DiffType.NONE,
173: sLine);
174: nLineNumInFile1++;
175: nLineNumInFile2++;
176: }
177:
178: while (nLineNumInFile1 <= deletion.getEndLineInFile1()) {
179: sLine = m_In.readLine();
180:
181: if (sLine == null)
182: throw new EOFException();
183:
184: printLineInFile1(nLineNumInFile1,
185: DiffType.DELETION, sLine);
186: printNonExistantLineInFile2();
187: nLineNumInFile1++;
188: }
189: }
190: }
191: }
192:
193: /** Displays a line in File1. File1 is the file provided by the Reader supplied at construction time. */
194: protected void printLineInFile1(int nLineNum, DiffType type,
195: String sLine) {
196: }
197:
198: /** Displays a non-existant line in File1. File1 is the file provided by the Reader supplied at construction time. */
199: protected void printNonExistantLineInFile1() {
200: }
201:
202: /** Displays a line in File2. File2 is the file resulting from applying the differences to File1. */
203: protected void printLineInFile2(int nLineNum, DiffType type,
204: String sLine) {
205: System.out.println(sLine);
206: }
207:
208: /** Displays a non-existant line in File2. File2 is the file resulting from applying the differences to File1. */
209: protected void printNonExistantLineInFile2() {
210: }
211: }
|