View Javadoc

1   package org.codehaus.mojo.dashboard.report.plugin.beans;
2   
3   /*
4    * Copyright 2006 David Vicente
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.regex.Pattern;
20  
21  /**
22   * Checkstyle error statistic class.
23   *
24   * @author <a href="srivollet@objectif-informatique.fr">Sylvain Rivollet</a>
25   * @author <a href="dvicente72@gmail.com">David Vicente</a>
26   */
27  public class CheckstyleError implements Cloneable
28  {
29      /** Regexp to find 'xxx' experssion. */
30      private static final Pattern REGEXP_QUOTE = Pattern.compile( "'.*?'" );
31  
32      /** Regexp to find "is nnn" expression. */
33      private static final Pattern REGEXP_IS_NUMBER = Pattern.compile( "is (\\d)+" );
34  
35      /** Regexp to find "nnn times" expression. */
36      private static final Pattern REGEXP_NUMBER_TIMES = Pattern.compile( "(\\d)+ times" );
37  
38      /** Regexp to find "xxx" expression. */
39      private static final Pattern REGEXP_DOUBELQUOTE = Pattern.compile( "\".*?\"" );
40  
41      /** Regexp to find [xxx] expression. */
42      private static final Pattern REGEXP_BRAKET = Pattern.compile( "\\[.*?\\]" );
43  
44      /**
45       * the Checkstyle class name
46       */
47      private String nameClass;
48  
49      /**
50       * nombre de fois qu'apparait le "typeErreurs"
51       */
52      private int nbIteration;
53  
54      /**
55       * type de classe de test qui apparait dans le fichier checkstyle-result.xml
56       */
57      private String type;
58  
59      /**
60       * message associ� � l'erreur
61       */
62      private String message;
63  
64      /**
65       * Default constructor
66       */
67      public CheckstyleError()
68      {
69          this.nbIteration = 1;
70      }
71  
72      /**
73       * @param type
74       * @param nameClass
75       * @param message
76       * @param nbIteration
77       */
78      public CheckstyleError( String type, String nameClass, String message, int nbIteration )
79      {
80          this.type = type;
81          this.nameClass = nameClass;
82          this.message = message;
83          this.nbIteration = nbIteration;
84      }
85  
86      /**
87       * @param object
88       */
89      public CheckstyleError( CheckstyleError object )
90      {
91          this.type = object.getType();
92          this.nameClass = object.getNameClass();
93          this.message = object.getMessage();
94          this.nbIteration = object.getNbIteration();
95      }
96  
97      public String getNameClass()
98      {
99          return this.nameClass;
100     }
101 
102     public int getNbIteration()
103     {
104         return this.nbIteration;
105     }
106 
107     public String getType()
108     {
109         return this.type;
110     }
111 
112     public String getMessage()
113     {
114         return this.message;
115     }
116 
117     public void setNameClass( String nameClass )
118     {
119         this.nameClass = nameClass;
120     }
121 
122     public void setNbIteration( int nbIteration )
123     {
124         this.nbIteration = nbIteration;
125     }
126 
127     public void setType( String type )
128     {
129         this.type = type;
130     }
131 
132     public void setMessage( String message )
133     {
134         this.message = this.messageTreatment( message );
135     }
136 
137     public boolean equals( Object o )
138     {
139         if ( !( o instanceof CheckstyleError ) )
140         {
141             return false;
142         }
143         else
144         {
145             CheckstyleError checkError = new CheckstyleError( (CheckstyleError) o );
146             return ( this.message == null && checkError.getMessage() == null )
147                             || ( this.message != null && this.message.equals( checkError.getMessage() ) );
148         }
149     }
150 
151 //    public int hashCode()
152 //    {
153 //        if ( this.message == null )
154 //        {
155 //            return 0;
156 //        }
157 //        else
158 //        {
159 //            return this.message.hashCode();
160 //        }
161 //    }
162 
163     public void increment()
164     {
165         this.nbIteration++;
166     }
167 
168     public String toString()
169     {
170         return "[" + this.type + " ; " + this.nameClass + " ; " + this.message + " ; " + this.nbIteration + "]";
171     }
172 
173     private String messageTreatment( String message )
174     {
175         String finalMessage = REGEXP_QUOTE.matcher( message ).replaceAll( "'X'" );
176 
177         finalMessage = REGEXP_IS_NUMBER.matcher( finalMessage ).replaceFirst( "is 'n'" );
178 
179         finalMessage = REGEXP_NUMBER_TIMES.matcher( finalMessage ).replaceFirst( "'n' times" );
180 
181         finalMessage = REGEXP_DOUBELQUOTE.matcher( finalMessage ).replaceAll( "\"X\"" );
182 
183         finalMessage = REGEXP_BRAKET.matcher( finalMessage ).replaceAll( "[...]" );
184 
185         return finalMessage;
186     }
187 
188     /**
189      *
190      */
191     protected Object clone()
192     {
193         Object clone = null;
194         try
195         {
196             clone = super.clone();
197         }
198         catch ( CloneNotSupportedException e )
199         {
200             System.err.println( "CheckstyleError can't clone" );
201         }
202         return clone;
203     }
204 }