KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > incava > analysis > Violation


1 package org.incava.analysis;
2
3 import java.io.*;
4 import java.util.*;
5 import net.sourceforge.pmd.ast.Token;
6
7
8 /**
9  * An error or a warning, associated with a file by a starting and ending
10  * position, and a message.
11  */

12 public class Violation implements Comparable JavaDoc
13 {
14     /**
15      * The message for this violation. This should be only one line, because it
16      * is used in single-line reports.
17      */

18     private String JavaDoc _message;
19    
20     /**
21      * The line where the violation starts.
22      */

23     private int _beginLine;
24
25     /**
26      * The column where the violation starts.
27      */

28     private int _beginColumn;
29
30     /**
31      * The line where the violation ends.
32      */

33     private int _endLine;
34
35     /**
36      * The column where the violation ends.
37      */

38     private int _endColumn;
39
40     /**
41      * Creates a violation from a message and begin and end positions.
42      *
43      * @param message The message applying to this violation.
44      * @param beginLine The line where the violation begins.
45      * @param beginColumn The column where the violation begins.
46      * @param endLine The line where the violation ends.
47      * @param endColumn The column where the violation ends.
48      */

49     public Violation(String JavaDoc message, int beginLine, int beginColumn, int endLine, int endColumn)
50     {
51         _message = message;
52         _beginLine = beginLine;
53         _beginColumn = beginColumn;
54         _endLine = endLine;
55         _endColumn = endColumn;
56
57         tr.Ace.log("[" + _beginLine + ":" + _beginColumn + " .. " + _endLine + ":" + _endColumn + "] (" + _message + ")");
58     }
59
60     /**
61      * Creates a violation from a message and beginning and ending token.
62      *
63      * @param message The message applying to this violation.
64      * @param beginToken The token where the violation begins.
65      * @param endToken The token where the violation ends.
66      */

67     public Violation(String JavaDoc message, Token beginToken, Token endToken)
68     {
69         this(message, beginToken.beginLine, beginToken.beginColumn, endToken.endLine, endToken.endColumn);
70     }
71
72     /**
73      * Creates a violation from a message and a token. The token image is
74      * considered to be the entire length of the violation, i.e., the ending
75      * location is <code>token + token.image.length() - 1</code>.
76      *
77      * @param message The message applying to this violation.
78      * @param token The token to which the violation applies.
79      */

80     public Violation(String JavaDoc message, Token token)
81     {
82         this(message, token.beginLine, token.beginColumn, token.beginLine, token.beginColumn + token.image.length() - 1);
83     }
84
85     /**
86      * Returns the message for this violation. This should be only one line,
87      * because it is used in single-line reports.
88      */

89     public String JavaDoc getMessage()
90     {
91         return _message;
92     }
93    
94     /**
95      * Returns the line where the violation starts.
96      */

97     public int getBeginLine()
98     {
99         return _beginLine;
100     }
101
102     /**
103      * Returns the column where the violation starts.
104      */

105     public int getBeginColumn()
106     {
107         return _beginColumn;
108     }
109
110     /**
111      * Returns the line where the violation ends.
112      */

113     public int getEndLine()
114     {
115         return _endLine;
116     }
117
118     /**
119      * Returns the column where the violation ends.
120      */

121     public int getEndColumn()
122     {
123         return _endColumn;
124     }
125
126     /**
127      * Compares this violation to another. Violations are sorted in order by
128      * their beginning locations, then their end locations.
129      *
130      * @param obj The violation to compare this to.
131      * @return -1, 0, or 1, for less than, equivalent to, or greater than.
132      */

133     public int compareTo(Object JavaDoc obj)
134     {
135         if (equals(obj)) {
136             return 0;
137         }
138         else {
139             Violation v = (Violation)obj;
140             int[][] nums = new int[][] {
141                 { _beginLine, v.getBeginLine() },
142                 { _beginColumn, v.getBeginColumn() },
143                 { _endLine, v.getEndLine() },
144                 { _endColumn, v.getEndColumn() }
145             };
146
147             for (int ni = 0; ni < nums.length; ++ni) {
148                 int diff = nums[ni][0] - nums[ni][1];
149                 if (diff != 0) {
150                     return diff;
151                 }
152             }
153             
154             return _message.compareTo(v.getMessage());
155         }
156     }
157
158     /**
159      * Returns whether the other object is equal to this one. Note that messages
160      * are not compared, only line and column numbers.
161      *
162      * @param obj The violation to compare this to.
163      * @return Whether the other violation is equal to this one.
164      */

165     public boolean equals(Object JavaDoc obj)
166     {
167         Violation v = (Violation)obj;
168         return (_beginLine == v.getBeginLine() &&
169                 _beginColumn == v.getBeginColumn() &&
170                 _endLine == v.getEndLine() &&
171                 _endColumn == v.getEndColumn());
172     }
173
174     /**
175      * Returns this violation, as a string.
176      *
177      * @return This violation, as a string.
178      */

179     public String JavaDoc toString()
180     {
181         return "[" + _beginLine + ":" + _beginColumn + " .. " + _endLine + ":" + _endColumn + "] (" + _message + ")";
182     }
183
184 }
185
Popular Tags