KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > merge > LineComparator


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.compare.internal.merge;
12
13 import java.io.*;
14 import java.util.ArrayList JavaDoc;
15 import org.eclipse.compare.rangedifferencer.IRangeComparator;
16
17 /**
18  * This implementation of IRangeComparator breaks an input stream into lines.
19  */

20 class LineComparator implements IRangeComparator {
21
22     private String JavaDoc[] fLines;
23
24     public LineComparator(InputStream is, String JavaDoc encoding) throws UnsupportedEncodingException {
25         
26         BufferedReader br = new BufferedReader(new InputStreamReader(is, encoding));
27         String JavaDoc line;
28         ArrayList JavaDoc ar = new ArrayList JavaDoc();
29         try {
30             while ((line = br.readLine()) != null)
31                 ar.add(line);
32         } catch (IOException e) {
33                 // silently ignored
34
}
35 // try {
36
// is.close();
37
// } catch (IOException e1) {
38
// }
39
fLines = (String JavaDoc[]) ar.toArray(new String JavaDoc[ar.size()]);
40     }
41
42     String JavaDoc getLine(int ix) {
43         return fLines[ix];
44     }
45
46     /* (non-Javadoc)
47      * @see org.eclipse.compare.rangedifferencer.IRangeComparator#getRangeCount()
48      */

49     public int getRangeCount() {
50         return fLines.length;
51     }
52
53     /* (non-Javadoc)
54      * @see org.eclipse.compare.rangedifferencer.IRangeComparator#rangesEqual(int, org.eclipse.compare.rangedifferencer.IRangeComparator, int)
55      */

56     public boolean rangesEqual(int thisIndex, IRangeComparator other,
57             int otherIndex) {
58         String JavaDoc s1 = fLines[thisIndex];
59         String JavaDoc s2 = ((LineComparator) other).fLines[otherIndex];
60         return s1.equals(s2);
61     }
62
63     /* (non-Javadoc)
64      * @see org.eclipse.compare.rangedifferencer.IRangeComparator#skipRangeComparison(int, int, org.eclipse.compare.rangedifferencer.IRangeComparator)
65      */

66     public boolean skipRangeComparison(int length, int maxLength, IRangeComparator other) {
67         return false;
68     }
69 }
70
Popular Tags