root / trunk / JavaChecker / jsrc / ua / gradsoft / javachecker / Violations.java @ 15107

1
/*
2
 * Violations.java
3
 *
4
 */
5
6
package ua.gradsoft.javachecker;
7
8
import java.io.PrintStream;
9
import java.util.Arrays;
10
import java.util.Iterator;
11
import java.util.Map;
12
import java.util.TreeMap;
13
import ua.gradsoft.termware.TermWareException;
14
import ua.gradsoft.termware.TermWareRuntimeException;
15
16
/**
17
 *Set of violations, found in source
18
 */
19
public class Violations {
20
    
21
    /** Creates a new instance of Violations */
22
    public Violations() {
23
        byTypes_=new TreeMap<String,TypeOfViolation>();
24
    }
25
    
26
    public void addType(String typeOfViolation, String category, String description, boolean enabledByDefault, boolean show)
27
    {
28
        TypeOfViolation o=byTypes_.get(typeOfViolation);
29
        if (o==null) {
30
            o=new TypeOfViolation(typeOfViolation,category,description,enabledByDefault,show, Main.getStatistics());
31
            byTypes_.put(typeOfViolation, o);
32
            Main.getStatistics().addItem(o);
33
        }
34
    }
35
    
36
    
37
    public  void readPreferences(JavaFacts facts)
38
    {        
39
        Iterator it=byTypes_.entrySet().iterator();
40
        while(it.hasNext()){
41
            Map.Entry me=(Map.Entry)it.next();
42
            TypeOfViolation v = (TypeOfViolation)me.getValue();
43
            v.readPreferences(facts);
44
        }
45
        
46
                        // add special meta-property
47
        boolean checkNamePatterns = facts.getBooleanConfigValue("CheckNamePatterns",true);
48
        if (!checkNamePatterns) {
49
            String [] namePatterns =  {"VariablePatterns",                                      
50
                                        "ClassNamePatterns",
51
                                       "MethodNamePatterns",
52
                                       "EnumConstantNamePatterns",
53
                                       "TypeArgumentNamePatterns"};            
54
            for(String s : Arrays.asList(namePatterns)) {
55
                TypeOfViolation v = byTypes_.get(s);
56
                if (v!=null) {
57
                    v.setEnabled(false);
58
                }
59
            }
60
        }
61
62
        
63
    }
64
    
65
    public  String getCategory(String typeOfViolation) 
66
    {
67
        Object o=byTypes_.get(typeOfViolation);
68
        if (o==null) {
69
            return "uncategorized";
70
        }
71
        TypeOfViolation v=(TypeOfViolation)o;
72
        return v.getCategory();
73
    }
74
    
75
    public  void discovered(String typeOfViolation)
76
    {
77
        Object o=byTypes_.get(typeOfViolation);
78
        if (o==null) {
79
            o=byTypes_.get("*");
80
        }
81
        TypeOfViolation v=(TypeOfViolation)o;
82
        v.increment();
83
        return;
84
    }
85
    
86
    public  boolean enabled(String typeOfViolation)
87
    {
88
        TypeOfViolation tov=byTypes_.get(typeOfViolation);
89
        if (tov==null){
90
            // ? - may be throw
91
            return false;
92
        }
93
        return tov.isEnabled();
94
    }
95
    
96
    public void  setEnabled(String typeOfViolation, boolean value) {
97
        TypeOfViolation tov=byTypes_.get(typeOfViolation);
98
        if (tov==null) {
99
            // ? - may be throw or log
100
            return;
101
        }
102
        tov.setEnabled(value);
103
    }
104
    
105
    public int  getCounterByName(String name)
106
    {
107
        TypeOfViolation tov=byTypes_.get(name);
108
        if (tov==null) {
109
            return 0;
110
        }else{
111
            return tov.getCounter();
112
        }
113
    }
114
    
115
   
116
    public  void report(PrintStream out, ReportFormat format)
117
    {
118
        if (format==ReportFormat.HTML) {
119
            out.println("<table>");
120
        }
121
        for(Map.Entry<String,TypeOfViolation> me : byTypes_.entrySet()) {
122
            if (me.getValue().isEnabled()) {
123
               //System.err.println("enabled "+me.getValue().getName());
124
               try {
125
                me.getValue().report(out,format,StatisticScope.ALL);
126
               }catch(TermWareException ex){
127
                   throw new TermWareRuntimeException(ex);
128
               }
129
            }
130
        }
131
        if (format==ReportFormat.HTML) {
132
            out.print("</table>");
133
        }
134
        
135
    }
136
    
137
    
138
    private TreeMap<String,TypeOfViolation> byTypes_;
139
    
140
}