View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package com.isenshi.util;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.seasar.tuigwaa.system.Constants;
22  
23  
24  public class Diff {
25  
26  	private String[] oldLines;
27  
28  	private String[] newLines;
29  
30  	private int oldLineNum;
31  
32  	private int newLineNum;
33  
34  	private int[][] weight;
35  
36  	private List lines = new ArrayList();
37  
38  	public Diff(String oldText, String newText) {
39  		this.oldLines = oldText.split(Constants.LINEBREAK_CODE);
40  		this.newLines = newText.split(Constants.LINEBREAK_CODE);
41  		this.oldLineNum = oldLines.length;
42  		this.newLineNum = newLines.length;
43  		computeLCS();
44  		setLines();
45  	}
46  
47  	private void computeLCS() {
48  		this.weight = new int[oldLineNum + 1][newLineNum + 1];
49  		for (int i = oldLineNum - 1; i >= 0; i--) {
50  			for (int j = newLineNum - 1; j >= 0; j--) {
51  				if (oldLines[i].equals(newLines[j]))
52  					weight[i][j] = weight[i + 1][j + 1] + 1;
53  				else
54  					weight[i][j] = Math.max(weight[i + 1][j], weight[i][j + 1]);
55  			}
56  		}
57  	}
58  
59  	private void setLines() {
60  		int i = 0, j = 0;
61  		while (i < oldLineNum && j < newLineNum) {
62  			if (oldLines[i].equals(newLines[j])) {
63  				lines.add(new Line(oldLines[i]));
64  				i++;
65  				j++;
66  			} else if (weight[i + 1][j] >= weight[i][j + 1]) {
67  				lines.add(new Line(oldLines[i++], true, false));
68  			} else {
69  				lines.add(new Line(newLines[j++], false, true));
70  			}
71  		}
72  
73  		while (i < oldLineNum || j < newLineNum) {
74  			if (i == oldLineNum) {
75  				lines.add(new Line(newLines[j++], false, true));
76  			} else if (j == newLineNum) {
77  				lines.add(new Line(oldLines[i++], true, false));
78  			}
79  		}
80  	}
81  
82  	public List getLines() {
83  		return lines;
84  	}
85  
86  	public class Line {
87  
88  		private String text;
89  
90  		private boolean deleteFlag;
91  
92  		private boolean insertFlag;
93  
94  		public Line(String text) {
95  			this(text, false, false);
96  		}
97  
98  		public Line(String text, boolean deleteFlag, boolean insertFlag) {
99  			this.text = text;
100 			this.deleteFlag = deleteFlag;
101 			this.insertFlag = insertFlag;
102 		}
103 
104 		public boolean isInsertFlag() {
105 			return insertFlag;
106 		}
107 
108 		public boolean isDeleteFlag() {
109 			return deleteFlag;
110 		}
111 
112 		public String getText() {
113 			return text;
114 		}
115 	}
116 }