View Javadoc
1   package org.apache.maven.plugin.checkstyle;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ResourceBundle;
23  
24  import org.apache.maven.doxia.sink.Sink;
25  
26  /**
27   * A little tool to deal with info/warning/error icons in Checkstyle reports, with eventual text.
28   *
29   * @since 2.13
30   */
31  public class IconTool
32  {
33      public static final String INFO = "info";
34  
35      public static final String WARNING = "warning";
36  
37      public static final String ERROR = "error";
38  
39      public static final int NO_TEXT = 0;
40      public static final int TEXT_SIMPLE = 1;
41      public static final int TEXT_TITLE = 2;
42      public static final int TEXT_ABBREV = 3;
43  
44      private final Sink sink;
45  
46      private final ResourceBundle bundle;
47  
48      public IconTool( Sink sink, ResourceBundle bundle )
49      {
50          this.sink = sink;
51          this.bundle = bundle;
52      }
53  
54      /**
55       * Render a simple icon of given level.
56       * @param level one of <code>INFO</code>, <code>WARNING</code> or <code>ERROR</code> constants
57       */
58      public void iconSeverity( String level )
59      {
60          sink.figure();
61          sink.figureGraphics( "images/icon_" + level + "_sml.gif" );
62          sink.figure_();
63      }
64  
65      /**
66       * Render an icon of given level with associated text.
67       * @param level one of <code>INFO</code>, <code>WARNING</code> or <code>ERROR</code> constants
68       * @param textType one of <code>NO_TEXT</code>, <code>TEXT_SIMPLE</code>, <code>TEXT_TITLE</code> or
69       * <code>TEXT_ABBREV</code> constants
70       */
71      public void iconSeverity( String level, int textType )
72      {
73          sink.figure();
74          sink.figureGraphics( "images/icon_" + level + "_sml.gif" );
75          sink.figure_();
76  
77          if ( textType > 0 )
78          {
79              sink.nonBreakingSpace();
80  
81              sink.text( bundle.getString( "report.checkstyle." + level + suffix( textType ) ) );
82          }
83      }
84  
85      /**
86       * Render an info icon.
87       */
88      public void iconInfo()
89      {
90          iconSeverity( INFO );
91      }
92  
93      /**
94       * Render an info icon with associated text.
95       * @param textType one of <code>NO_TEXT</code>, <code>TEXT_SIMPLE</code>, <code>TEXT_TITLE</code> or
96       * <code>TEXT_ABBREV</code> constants
97       */
98      public void iconInfo( int textType )
99      {
100         iconSeverity( INFO, textType );
101     }
102 
103     /**
104      * Render a warning icon.
105      */
106     public void iconWarning()
107     {
108         iconSeverity( WARNING );
109     }
110 
111     /**
112      * Render a warning icon with associated text.
113      * @param textType one of <code>NO_TEXT</code>, <code>TEXT_SIMPLE</code>, <code>TEXT_TITLE</code> or
114      * <code>TEXT_ABBREV</code> constants
115      */
116     public void iconWarning( int textType )
117     {
118         iconSeverity( WARNING, textType );
119     }
120 
121     /**
122      * Render an error icon.
123      */
124     public void iconError()
125     {
126         iconSeverity( ERROR );
127     }
128 
129     /**
130      * Render an error icon with associated text.
131      * @param textType one of <code>NO_TEXT</code>, <code>TEXT_SIMPLE</code>, <code>TEXT_TITLE</code> or
132      * <code>TEXT_ABBREV</code> constants
133      */
134     public void iconError( int textType )
135     {
136         iconSeverity( ERROR, textType );
137     }
138 
139     private String suffix( int textType )
140     {
141         switch ( textType )
142         {
143             case TEXT_TITLE:
144                 return "s";
145             case TEXT_ABBREV:
146                 return "s.abbrev";
147             default:
148                 return "";
149         }
150     }
151 }