KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > diff > TextComparator


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.diff;
17
18 import org.eclipse.compare.rangedifferencer.IRangeComparator;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24
25 /**
26  * Comparator for comparing text on a line-by-line basis.
27  */

28 public class TextComparator implements IRangeComparator {
29     private String JavaDoc[] lines;
30
31     /**
32      * @param text the input text as one big string (thus containing newlines)
33      */

34     public TextComparator(String JavaDoc text) {
35         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new StringReader JavaDoc(text));
36
37         ArrayList JavaDoc lines = new ArrayList JavaDoc(50);
38         try {
39             String JavaDoc line = reader.readLine();
40             while (line != null) {
41                 lines.add(line);
42                 line = reader.readLine();
43             }
44         } catch (IOException JavaDoc e) {
45             throw new RuntimeException JavaDoc("Unexpected: got exception while reading from String object.", e);
46         }
47         this.lines = (String JavaDoc[])lines.toArray(new String JavaDoc[lines.size()]);
48     }
49
50     public int getRangeCount() {
51         return lines.length;
52     }
53
54     public boolean rangesEqual(int thisIndex, IRangeComparator other, int otherIndex) {
55         String JavaDoc thisLine = getLine(thisIndex);
56         String JavaDoc otherLine = ((TextComparator)other).getLine(otherIndex);
57         return thisLine.equals(otherLine);
58     }
59
60     public boolean skipRangeComparison(int length, int maxLength, IRangeComparator other) {
61         return false;
62     }
63
64     public String JavaDoc getLine(int index) {
65         return index < lines.length ? lines[index] : "";
66     }
67 }
68
Popular Tags