View Javadoc
1   package org.apache.maven.plugins.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.figureGraphics( "images/icon_" + level + "_sml.gif" );
61      }
62  
63      /**
64       * Render an icon of given level with associated text.
65       * @param level one of <code>INFO</code>, <code>WARNING</code> or <code>ERROR</code> constants
66       * @param textType one of <code>NO_TEXT</code>, <code>TEXT_SIMPLE</code>, <code>TEXT_TITLE</code> or
67       * <code>TEXT_ABBREV</code> constants
68       */
69      public void iconSeverity( String level, int textType )
70      {
71          sink.figureGraphics( "images/icon_" + level + "_sml.gif" );
72  
73          if ( textType > 0 )
74          {
75              sink.nonBreakingSpace();
76  
77              sink.text( bundle.getString( "report.checkstyle." + level + suffix( textType ) ) );
78          }
79      }
80  
81      /**
82       * Render an info icon.
83       */
84      public void iconInfo()
85      {
86          iconSeverity( INFO );
87      }
88  
89      /**
90       * Render an info icon with associated text.
91       * @param textType one of <code>NO_TEXT</code>, <code>TEXT_SIMPLE</code>, <code>TEXT_TITLE</code> or
92       * <code>TEXT_ABBREV</code> constants
93       */
94      public void iconInfo( int textType )
95      {
96          iconSeverity( INFO, textType );
97      }
98  
99      /**
100      * Render a warning icon.
101      */
102     public void iconWarning()
103     {
104         iconSeverity( WARNING );
105     }
106 
107     /**
108      * Render a warning icon with associated text.
109      * @param textType one of <code>NO_TEXT</code>, <code>TEXT_SIMPLE</code>, <code>TEXT_TITLE</code> or
110      * <code>TEXT_ABBREV</code> constants
111      */
112     public void iconWarning( int textType )
113     {
114         iconSeverity( WARNING, textType );
115     }
116 
117     /**
118      * Render an error icon.
119      */
120     public void iconError()
121     {
122         iconSeverity( ERROR );
123     }
124 
125     /**
126      * Render an error icon with associated text.
127      * @param textType one of <code>NO_TEXT</code>, <code>TEXT_SIMPLE</code>, <code>TEXT_TITLE</code> or
128      * <code>TEXT_ABBREV</code> constants
129      */
130     public void iconError( int textType )
131     {
132         iconSeverity( ERROR, textType );
133     }
134 
135     private String suffix( int textType )
136     {
137         switch ( textType )
138         {
139             case TEXT_TITLE:
140                 return "s";
141             case TEXT_ABBREV:
142                 return "s.abbrev";
143             default:
144                 return "";
145         }
146     }
147 }