001: package fat;
002:
003: import fit.*;
004: import java.io.*;
005: import java.text.ParseException;
006:
007: public class AnnotationFixture extends ColumnFixture {
008: public String OriginalHTML;
009: public int Row;
010: public int Column;
011:
012: public String OverwriteCellBody;
013: public String AddToCellBody;
014:
015: public String OverwriteCellTag;
016: public String OverwriteEndCellTag;
017: public String AddToCellTag;
018:
019: public String OverwriteRowTag;
020: public String OverwriteEndRowTag;
021: public String AddToRowTag;
022:
023: public String OverwriteTableTag;
024: public String OverwriteEndTableTag;
025: public String AddToTableTag;
026:
027: public String AddCellFollowing;
028: public String RemoveFollowingCell;
029:
030: public String AddRowFollowing;
031: public String RemoveFollowingRow;
032:
033: public String AddTableFollowing;
034:
035: public String ResultingHTML() throws Exception {
036: Parse table = new Parse(OriginalHTML);
037: Parse row = table.at(0, Row - 1);
038: Parse cell = row.at(0, Column - 1);
039:
040: if (OverwriteCellBody != null)
041: cell.body = OverwriteCellBody;
042: if (AddToCellBody != null)
043: cell.addToBody(AddToCellBody);
044:
045: if (OverwriteCellTag != null)
046: cell.tag = OverwriteCellTag;
047: if (OverwriteEndCellTag != null)
048: cell.end = OverwriteEndCellTag;
049: if (AddToCellTag != null)
050: cell.addToTag(stripDelimiters(AddToCellTag));
051:
052: if (OverwriteRowTag != null)
053: row.tag = OverwriteRowTag;
054: if (OverwriteEndRowTag != null)
055: row.end = OverwriteEndRowTag;
056: if (AddToRowTag != null)
057: row.addToTag(stripDelimiters(AddToRowTag));
058:
059: if (OverwriteTableTag != null)
060: table.tag = OverwriteTableTag;
061: if (OverwriteEndTableTag != null)
062: table.end = OverwriteEndTableTag;
063: if (AddToTableTag != null)
064: table.addToTag(stripDelimiters(AddToTableTag));
065:
066: if (AddCellFollowing != null)
067: addParse(cell, AddCellFollowing, new String[] { "td" });
068: if (RemoveFollowingCell != null)
069: removeParse(cell);
070:
071: if (AddRowFollowing != null)
072: addParse(row, AddRowFollowing, new String[] { "tr", "td" });
073: if (RemoveFollowingRow != null)
074: removeParse(row);
075:
076: if (AddTableFollowing != null)
077: addParse(table, AddTableFollowing, new String[] { "table",
078: "tr", "td" });
079:
080: return GenerateOutput(table);
081: }
082:
083: private void addParse(Parse parse, String newString, String[] tags)
084: throws ParseException {
085: Parse newParse = new Parse(newString, tags);
086: newParse.more = parse.more;
087: newParse.trailer = parse.trailer;
088: parse.more = newParse;
089: parse.trailer = null;
090: }
091:
092: private void removeParse(Parse parse) {
093: parse.trailer = parse.more.trailer;
094: parse.more = parse.more.more;
095: }
096:
097: private String stripDelimiters(String s) {
098: return s.replaceAll("^\\[", "").replaceAll("]$", "");
099: }
100:
101: // code smell note: copied from DocumentParseFixture
102: private String GenerateOutput(Parse document) throws ParseException {
103: StringWriter result = new StringWriter();
104: document.print(new PrintWriter(result));
105: return result.toString().trim();
106: }
107: }
|