KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > incava > doctorj > ItemCommentSpellCheck


1 package org.incava.doctorj;
2
3 import java.util.*;
4 import net.sourceforge.pmd.ast.*;
5 import org.incava.javadoc.*;
6 import org.incava.lang.StringExt;
7 import org.incava.text.LineMapping;
8 import org.incava.text.Location;
9
10
11 public class ItemCommentSpellCheck extends CommentSpellCheck
12 {
13     public final static int NUM_CLOSEST_MATCHES = 6;
14
15     private ItemDocAnalyzer _analyzer;
16
17     private JavadocElement _desc;
18
19     private LineMapping _lines = null;
20
21     public void check(ItemDocAnalyzer analyzer, JavadocElement desc)
22     {
23         _analyzer = analyzer;
24         _desc = desc;
25         _lines = null;
26         
27         super.check(_desc.text);
28     }
29     
30     protected String JavaDoc makeMessage(String JavaDoc word, Map nearMatches)
31     {
32         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("Word '" + word + "' appears to be misspelled. ");
33         if (nearMatches.size() == 0) {
34             buf.append("No close matches");
35         }
36         else {
37             buf.append("Closest matches: ");
38             
39             Iterator it = nearMatches.values().iterator();
40             List msgWords = new ArrayList();
41             
42             while (it.hasNext() && msgWords.size() < NUM_CLOSEST_MATCHES) {
43                 List matches = (List)it.next();
44                 Iterator mit = matches.iterator();
45                 
46                 while (mit.hasNext() && msgWords.size() < NUM_CLOSEST_MATCHES) {
47                     String JavaDoc w = (String JavaDoc)mit.next();
48                     msgWords.add(w);
49                 }
50             }
51
52             buf.append(StringExt.join(msgWords, ", "));
53         }
54         return buf.toString();
55     }
56
57     protected void wordMisspelled(String JavaDoc word, int position, Map nearMatches)
58     {
59         tr.Ace.log("word", word);
60
61         if (_lines == null) {
62             _lines = new LineMapping(_desc.text, _desc.start.line, _desc.start.column);
63         }
64         
65         Location start = _lines.getLocation(position);
66         Location end = _lines.getLocation(position + word.length() - 1);
67         String JavaDoc msg = makeMessage(word, nearMatches);
68         
69         _analyzer.addViolation(msg, start, end);
70     }
71 }
72
Popular Tags