001: package org.incava.analysis;
002:
003: import java.io.*;
004: import java.util.*;
005: import net.sourceforge.pmd.ast.Token;
006:
007: /**
008: * An error or a warning, associated with a file by a starting and ending
009: * position, and a message.
010: */
011: public class Violation implements Comparable {
012: /**
013: * The message for this violation. This should be only one line, because it
014: * is used in single-line reports.
015: */
016: private String _message;
017:
018: /**
019: * The line where the violation starts.
020: */
021: private int _beginLine;
022:
023: /**
024: * The column where the violation starts.
025: */
026: private int _beginColumn;
027:
028: /**
029: * The line where the violation ends.
030: */
031: private int _endLine;
032:
033: /**
034: * The column where the violation ends.
035: */
036: private int _endColumn;
037:
038: /**
039: * Creates a violation from a message and begin and end positions.
040: *
041: * @param message The message applying to this violation.
042: * @param beginLine The line where the violation begins.
043: * @param beginColumn The column where the violation begins.
044: * @param endLine The line where the violation ends.
045: * @param endColumn The column where the violation ends.
046: */
047: public Violation(String message, int beginLine, int beginColumn,
048: int endLine, int endColumn) {
049: _message = message;
050: _beginLine = beginLine;
051: _beginColumn = beginColumn;
052: _endLine = endLine;
053: _endColumn = endColumn;
054:
055: tr.Ace.log("[" + _beginLine + ":" + _beginColumn + " .. "
056: + _endLine + ":" + _endColumn + "] (" + _message + ")");
057: }
058:
059: /**
060: * Creates a violation from a message and beginning and ending token.
061: *
062: * @param message The message applying to this violation.
063: * @param beginToken The token where the violation begins.
064: * @param endToken The token where the violation ends.
065: */
066: public Violation(String message, Token beginToken, Token endToken) {
067: this (message, beginToken.beginLine, beginToken.beginColumn,
068: endToken.endLine, endToken.endColumn);
069: }
070:
071: /**
072: * Creates a violation from a message and a token. The token image is
073: * considered to be the entire length of the violation, i.e., the ending
074: * location is <code>token + token.image.length() - 1</code>.
075: *
076: * @param message The message applying to this violation.
077: * @param token The token to which the violation applies.
078: */
079: public Violation(String message, Token token) {
080: this (message, token.beginLine, token.beginColumn,
081: token.beginLine, token.beginColumn
082: + token.image.length() - 1);
083: }
084:
085: /**
086: * Returns the message for this violation. This should be only one line,
087: * because it is used in single-line reports.
088: */
089: public String getMessage() {
090: return _message;
091: }
092:
093: /**
094: * Returns the line where the violation starts.
095: */
096: public int getBeginLine() {
097: return _beginLine;
098: }
099:
100: /**
101: * Returns the column where the violation starts.
102: */
103: public int getBeginColumn() {
104: return _beginColumn;
105: }
106:
107: /**
108: * Returns the line where the violation ends.
109: */
110: public int getEndLine() {
111: return _endLine;
112: }
113:
114: /**
115: * Returns the column where the violation ends.
116: */
117: public int getEndColumn() {
118: return _endColumn;
119: }
120:
121: /**
122: * Compares this violation to another. Violations are sorted in order by
123: * their beginning locations, then their end locations.
124: *
125: * @param obj The violation to compare this to.
126: * @return -1, 0, or 1, for less than, equivalent to, or greater than.
127: */
128: public int compareTo(Object obj) {
129: if (equals(obj)) {
130: return 0;
131: } else {
132: Violation v = (Violation) obj;
133: int[][] nums = new int[][] {
134: { _beginLine, v.getBeginLine() },
135: { _beginColumn, v.getBeginColumn() },
136: { _endLine, v.getEndLine() },
137: { _endColumn, v.getEndColumn() } };
138:
139: for (int ni = 0; ni < nums.length; ++ni) {
140: int diff = nums[ni][0] - nums[ni][1];
141: if (diff != 0) {
142: return diff;
143: }
144: }
145:
146: return _message.compareTo(v.getMessage());
147: }
148: }
149:
150: /**
151: * Returns whether the other object is equal to this one. Note that messages
152: * are not compared, only line and column numbers.
153: *
154: * @param obj The violation to compare this to.
155: * @return Whether the other violation is equal to this one.
156: */
157: public boolean equals(Object obj) {
158: Violation v = (Violation) obj;
159: return (_beginLine == v.getBeginLine()
160: && _beginColumn == v.getBeginColumn()
161: && _endLine == v.getEndLine() && _endColumn == v
162: .getEndColumn());
163: }
164:
165: /**
166: * Returns this violation, as a string.
167: *
168: * @return This violation, as a string.
169: */
170: public String toString() {
171: return "[" + _beginLine + ":" + _beginColumn + " .. "
172: + _endLine + ":" + _endColumn + "] (" + _message + ")";
173: }
174:
175: }
|