Skip to content

Commit

Permalink
Better AlignEncoder output for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Kleppmann committed Feb 28, 2009
1 parent 3dab8ea commit 234806e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
53 changes: 40 additions & 13 deletions src/fc/xml/diff/encode/AlignEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,29 @@
import fc.xml.diff.Segment;
import fc.xml.xas.Item;


/**
* AlignEncoder produces human-readable textual output showing how the sequences
* from each document align side by side. Useful for observing what is going on.
*/
public class AlignEncoder implements DiffEncoder {

enum SectionPos {
SECTION_START("\\"),
SECTION_MIDDLE("|"),
SECTION_END("/"),
NONE(" ");

private final String marker;

private SectionPos(String marker) {
this.marker = marker;
}

public String getMarker() {
return marker;
}
}

public void encodeDiff(List<Item> base, List<Item> doc, List<Segment<Item>> matches,
List<Item> preamble, OutputStream out) throws IOException {
Expand All @@ -43,36 +65,40 @@ public static <E> void encodeDiff(List<E> base, List<E> doc, List<Segment<E>> ma
// dump ins (ins dumped after copies)
for (int i = 0; i < s.getInsert().size(); i++)
emitLine(pw, -1, "-", s.getInsert().get(i), s.getOp() == UPDATE,
s.getPosition() + i, lpt, rpt);
s.getPosition() + i, lpt, rpt, SectionPos.NONE);
} else if (s.getOffset() == pos && s.getOp() != INSERT) {
found = true;
match = s;
int slen = s.getLength();
for (int i = 0; i < slen; i++) {
Object updated = null;
if (s.getOp() == UPDATE) updated = i < s.getInsert().size() ? s.getInsert().get(
i)
: "-";
if (s.getOp() == UPDATE) updated = i < s.getInsert().size() ? s.getInsert().get(i) : "-";
else updated = base.get(pos);
if (i > 2 && slen > 5 && i < slen - 2 && s.getOp() != UPDATE) {
if (i < 5)
pw.println(StringUtil.format(".",
-(EVENT_COLWIDTH + POS_COLWIDTH + 2)));
SectionPos sectionPos = (slen == 1) ? SectionPos.NONE :
(i == 0) ? SectionPos.SECTION_START :
(i == slen - 1) ? SectionPos.SECTION_END : SectionPos.SECTION_MIDDLE;
// For long copied sections, print only the first & last 3 lines, and dots in between
if (i > 2 && slen > 9 && i < slen - 3 && s.getOp() != UPDATE) {
if (i < 6) {
String dots = (i != 4) ? ":" : "(skipped) :";
pw.println(StringUtil.format(dots, -(EVENT_COLWIDTH + POS_COLWIDTH + 2)) +
StringUtil.format(SectionPos.SECTION_MIDDLE.getMarker(), -(EVENT_COLWIDTH + POS_COLWIDTH + 5)));
}
} else emitLine(pw, pos, base.get(pos), updated, s.getOp() == UPDATE,
s.getPosition() + i, lpt, rpt);
s.getPosition() + i, lpt, rpt, sectionPos);
pos++;
}
// update && ins > basematch
if (s.getOp() == UPDATE) {
for (int i = s.getLength(); i < s.getInsert().size(); i++)
emitLine(pw, -1, "-", s.getInsert().get(i), s.getOp() == UPDATE,
s.getPosition() + i, lpt, rpt);
s.getPosition() + i, lpt, rpt, SectionPos.NONE);
}
} else match = null;
}
if (!found) {
// Dump del
emitLine(pw, pos, base.get(pos), "-", false, -1, lpt, rpt);
emitLine(pw, pos, base.get(pos), "-", false, -1, lpt, rpt, SectionPos.NONE);
pos++;
}
}
Expand All @@ -84,7 +110,7 @@ public static <E> void encodeDiff(List<E> base, List<E> doc, List<Segment<E>> ma


private static void emitLine(PrintWriter out, int pos, Object base, Object mod, boolean update,
int rpos, PosTransformer lpt, PosTransformer rpt) {
int rpos, PosTransformer lpt, PosTransformer rpt, SectionPos sectionPos) {
String baseS = Util.toPrintable(base.toString());
String brS = Util.toPrintable(mod.toString());
if (baseS.length() > EVENT_COLWIDTH)
Expand All @@ -96,7 +122,8 @@ private static void emitLine(PrintWriter out, int pos, Object base, Object mod,
out.println(StringUtil.format(lpt.transform(pos), -POS_COLWIDTH, ' ') + " " +
StringUtil.format(baseS, -EVENT_COLWIDTH, ' ') + " " +
StringUtil.format(brS, EVENT_COLWIDTH, ' ') + (update ? " *" : " ") +
StringUtil.format(rpt.transform(rpos), POS_COLWIDTH, ' '));
StringUtil.format(rpt.transform(rpos), -POS_COLWIDTH, ' ') + " " +
sectionPos.getMarker());
}

public static final PosTransformer DEFAULT_PT = new DefaultPosTranformer();
Expand Down
4 changes: 4 additions & 0 deletions src/fc/xml/diff/encode/DiffEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
import fc.xml.diff.Segment;
import fc.xml.xas.Item;

/**
* A DiffEncoder takes the two input document and the list of matched segments, and encodes
* it in a particular diff format. A variety of different formats are implemented.
*/
public interface DiffEncoder {

public void encodeDiff(List<Item> base, List<Item> doc, List<Segment<Item>> matches,
Expand Down

0 comments on commit 234806e

Please sign in to comment.