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.tag;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.eclipse.compare.rangedifferencer.IRangeComparator;
022:
023: /**
024: * Takes a String and generates tokens/atoms that can be used by LCS. This
025: * comparator is used specifically for arguments inside HTML tags.
026: */
027: public class ArgumentComparator implements IAtomSplitter {
028:
029: private List<Atom> atoms = new ArrayList<Atom>(5);
030:
031: public ArgumentComparator(String s) {
032: generateAtoms(s);
033: }
034:
035: private void generateAtoms(String s) {
036: if (atoms.size() > 0)
037: throw new IllegalStateException(
038: "Atoms can only be generated once");
039:
040: StringBuilder currentWord = new StringBuilder(30);
041:
042: for (int i = 0; i < s.length(); i++) {
043: char c = s.charAt(i);
044:
045: if (c == '<' || c == '>') {
046: if (currentWord.length() > 0) {
047: atoms.add(new TextAtom(currentWord.toString()));
048: currentWord.setLength(0);
049: }
050: atoms.add(new TextAtom("" + c));
051: currentWord.setLength(0);
052: } else if (DelimiterAtom.isValidDelimiter("" + c)) {
053: // a delimiter
054: if (currentWord.length() > 0) {
055: atoms.add(new TextAtom(currentWord.toString()));
056: currentWord.setLength(0);
057: }
058: atoms.add(new DelimiterAtom(c));
059: } else {
060: currentWord.append(c);
061: }
062: }
063: if (currentWord.length() > 0) {
064: atoms.add(new TextAtom(currentWord.toString()));
065: currentWord.setLength(0);
066: }
067: }
068:
069: public Atom getAtom(int i) {
070: if (i < 0 || i >= atoms.size())
071: throw new IndexOutOfBoundsException(
072: "There is no Atom with index " + i);
073: return atoms.get(i);
074: }
075:
076: public int getRangeCount() {
077: return atoms.size();
078: }
079:
080: public boolean rangesEqual(int this Index, IRangeComparator other,
081: int otherIndex) {
082: ArgumentComparator tc2;
083: try {
084: tc2 = (ArgumentComparator) other;
085: } catch (ClassCastException e) {
086: return false;
087: }
088: return tc2.getAtom(otherIndex).equalsIdentifier(
089: getAtom(this Index));
090: }
091:
092: public boolean skipRangeComparison(int length, int maxLength,
093: IRangeComparator other) {
094: return false;
095: }
096:
097: public String substring(int startAtom, int endAtom) {
098: if (startAtom == endAtom)
099: return "";
100: else {
101: StringBuilder result = new StringBuilder();
102: for (int i = startAtom; i < endAtom; i++) {
103: result.append(atoms.get(i).getFullText());
104: }
105: return result.toString();
106: }
107: }
108:
109: public String substring(int startAtom) {
110: return substring(startAtom, atoms.size());
111: }
112:
113: }
|