001: /*
002: * Copyright 2007 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.diff.html.ancestor;
017:
018: import java.util.List;
019: import java.util.Locale;
020:
021: import org.eclipse.compare.rangedifferencer.IRangeComparator;
022: import org.eclipse.compare.rangedifferencer.RangeDifference;
023: import org.eclipse.compare.rangedifferencer.RangeDifferencer;
024: import org.outerj.daisy.diff.html.dom.TagNode;
025:
026: /**
027: * A comparator used when calculating the difference in ancestry of two Nodes.
028: */
029: public class AncestorComparator implements IRangeComparator {
030:
031: private List<TagNode> ancestors;
032:
033: public AncestorComparator(List<TagNode> ancestors) {
034: this .ancestors = ancestors;
035: }
036:
037: public int getRangeCount() {
038: return ancestors.size();
039: }
040:
041: public boolean rangesEqual(int owni, IRangeComparator otherComp,
042: int otheri) {
043: AncestorComparator other;
044: try {
045: other = (AncestorComparator) otherComp;
046: } catch (ClassCastException e) {
047: return false;
048: }
049:
050: return other.getAncestor(otheri).isSameTag(getAncestor(owni));
051: }
052:
053: public boolean skipRangeComparison(int arg0, int arg1,
054: IRangeComparator arg2) {
055: return false;
056: }
057:
058: public TagNode getAncestor(int i) {
059: return ancestors.get(i);
060: }
061:
062: private String compareTxt = "";
063:
064: public String getCompareTxt() {
065: return compareTxt;
066: }
067:
068: public AncestorComparatorResult getResult(AncestorComparator other,
069: Locale locale) {
070:
071: AncestorComparatorResult result = new AncestorComparatorResult();
072:
073: RangeDifference[] differences = RangeDifferencer
074: .findDifferences(other, this );
075:
076: if (differences.length == 0)
077: return result;
078:
079: ChangeTextGenerator changeTxt = new ChangeTextGenerator(this ,
080: other, locale);
081:
082: result.setChanged(true);
083: result.setChanges(changeTxt.getChanged(differences).toString());
084:
085: return result;
086:
087: }
088:
089: public int getDistance(AncestorComparator other) {
090: RangeDifference[] differences = RangeDifferencer
091: .findDifferences(other, this );
092:
093: int distance = 0;
094:
095: for (RangeDifference d : differences) {
096: distance += d.rightLength() + d.leftLength();
097: }
098: return distance;
099: }
100:
101: }
|