View Javadoc

1   /*
2    * Copyright 2009-2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.apidoc;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.List;
22  import java.util.Locale;
23  import java.util.ResourceBundle;
24  
25  import de.smartics.analysis.javadoc.log.message.IssueMessage;
26  
27  /**
28   * Configuration to control the rendering of the report.
29   *
30   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
31   * @version $Revision:591 $
32   */
33  public final class JavadocMessageConfig // NOPMD
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    /**
40     * The resource bundle to fetch messages to be rendered into the report.
41     */
42    private final ResourceBundle bundle;
43  
44    /**
45     * The location where the XRef report is written to.
46     */
47    private final String xrefLocation;
48  
49    /**
50     * The flag that indicates if notice messages should be rendered in the report
51     * (<code>true</code>) or if only error and warn messages are rendered (
52     * <code>false</code>).
53     */
54    private final boolean noticeMessagesRendered;
55  
56    /**
57     * The root directories in which the source files reside. The file names are
58     * expected to be disjunct so that no directory is contained in another one.
59     */
60    private final Collection<String> sourceRoots;
61  
62    /**
63     * The list of message fragments that are used to filter messages. If a
64     * message contains a fragment found in the list, it will be suppressed.
65     */
66    private final Collection<String> messageFilter;
67  
68    // --- members --------------------------------------------------------------
69  
70    // ****************************** Initializer *******************************
71  
72    // ****************************** Constructors ******************************
73  
74    /**
75     * Default constructor.
76     *
77     * @param builder the builder information to construct the configuration
78     *          instance.
79     */
80    private JavadocMessageConfig(final Builder builder)
81    {
82      this.sourceRoots = builder.sourceRoots;
83      this.bundle = builder.bundle;
84      this.xrefLocation = builder.xrefLocation;
85      this.noticeMessagesRendered = builder.noticeMessagesRendered;
86      this.messageFilter = builder.messageFilter;
87    }
88  
89    // ****************************** Inner Classes *****************************
90  
91    /**
92     * Builder for the configuration instance.
93     */
94    public static final class Builder
95    {
96      // ******************************** Fields ********************************
97  
98      // --- constants ----------------------------------------------------------
99  
100     // --- members ------------------------------------------------------------
101 
102     /**
103      * The resource bundle to fetch messages to be rendered into the report.
104      */
105     private ResourceBundle bundle;
106 
107     /**
108      * The location where the XRef report is written to.
109      */
110     private String xrefLocation;
111 
112     /**
113      * The flag that indicates if notice messages should be rendered in the
114      * report (<code>true</code>) or if only error and warn messages are
115      * rendered ( <code>false</code>).
116      */
117     private boolean noticeMessagesRendered;
118 
119     /**
120      * The root directories in which the source files reside. The file names are
121      * expected to be disjunct so that no directory is contained in another one.
122      */
123     private Collection<String> sourceRoots;
124 
125     /**
126      * The list of message fragments that are used to filter messages. If a
127      * message contains a fragment found in the list, it will be suppressed.
128      */
129     private Collection<String> messageFilter;
130 
131     // ***************************** Initializer ******************************
132 
133     // ***************************** Constructors *****************************
134 
135     // ***************************** Inner Classes ****************************
136 
137     // ******************************** Methods *******************************
138 
139     // --- init ---------------------------------------------------------------
140 
141     // --- get&set ------------------------------------------------------------
142 
143     /**
144      * Sets the resource bundle to fetch messages to be rendered into the
145      * report.
146      *
147      * @param bundle the resource bundle to fetch messages to be rendered into
148      *          the report.
149      */
150     public void setBundle(final ResourceBundle bundle)
151     {
152       this.bundle = bundle;
153     }
154 
155     /**
156      * Sets the location where the XRef report is written to.
157      *
158      * @param xrefLocation the location where the XRef report is written to.
159      */
160     public void setXrefLocation(final String xrefLocation)
161     {
162       this.xrefLocation = xrefLocation;
163     }
164 
165     // --- business -----------------------------------------------------------
166 
167     /**
168      * The builder to create the configuration.
169      *
170      * @return the configuration instance.
171      * @throws IllegalArgumentException if the builder information does not meet
172      *           the constraints.
173      */
174     public JavadocMessageConfig build() throws IllegalArgumentException
175     {
176       final StringBuilder buffer = new StringBuilder();
177       if (sourceRoots == null || sourceRoots.isEmpty())
178       {
179         buffer.append("The sourceRoots must neither be 'null' nor empty.\n");
180       }
181       if (bundle == null)
182       {
183         buffer.append("The resource bundle must not be 'null'.\n");
184       }
185       if (xrefLocation == null)
186       {
187         buffer.append("The xrefLocation must not be 'null'.\n");
188       }
189       if (messageFilter == null)
190       {
191         messageFilter = Collections.emptyList();
192       }
193 
194       if (buffer.length() > 0)
195       {
196         throw new IllegalArgumentException(buffer.toString());
197       }
198 
199       return new JavadocMessageConfig(this);
200     }
201 
202     /**
203      * Sets the flag that indicates if notice messages should be rendered in the
204      * report (<code>true</code>) or if only error and warn messages are
205      * rendered ( <code>false</code>).
206      *
207      * @param noticeMessagesRendered the flag that indicates if notice messages
208      *          should be rendered in the report (<code>true</code>) or if only
209      *          error and warn messages are rendered ( <code>false</code>).
210      */
211     public void setNoticeMessagesRendered(final boolean noticeMessagesRendered)
212     {
213       this.noticeMessagesRendered = noticeMessagesRendered;
214     }
215 
216     /**
217      * Sets the root directories in which the source files reside. The file
218      * names are expected to be disjunct so that no directory is contained in
219      * another one.
220      *
221      * @param sourceRoots the root directories in which the source files reside.
222      */
223     public void setSourceRoots(final Collection<String> sourceRoots)
224     {
225       this.sourceRoots = sourceRoots;
226     }
227 
228     /**
229      * Sets the list of message fragments that are used to filter messages. If a
230      * message contains a fragment found in the list, it will be suppressed.
231      *
232      * @param messageFilter the list of message fragments that are used to
233      *          filter messages.
234      */
235     public void setMessageFilter(final Collection<String> messageFilter)
236     {
237       this.messageFilter = messageFilter;
238     }
239 
240     // --- object basics ------------------------------------------------------
241   }
242 
243   // ********************************* Methods ********************************
244 
245   // --- init -----------------------------------------------------------------
246 
247   // --- get&set --------------------------------------------------------------
248 
249   /**
250    * Returns the root directories in which the source files reside. The file
251    * names are expected to be disjunct so that no directory is contained in
252    * another one.
253    *
254    * @return the root directories in which the source files reside.
255    */
256   public Collection<String> getSourceRoots()
257   {
258     return sourceRoots;
259   }
260 
261   /**
262    * Returns the resource bundle to fetch messages to be rendered into the
263    * report.
264    *
265    * @return the resource bundle to fetch messages to be rendered into the
266    *         report.
267    */
268   public ResourceBundle getBundle()
269   {
270     return bundle;
271   }
272 
273   /**
274    * Returns the locale to use for the report.
275    *
276    * @return the locale to use for the report.
277    */
278   public Locale getLocale()
279   {
280     return bundle.getLocale();
281   }
282 
283   /**
284    * Returns the location where the XRef report is written to.
285    *
286    * @return the location where the XRef report is written to.
287    */
288   public String getXrefLocation()
289   {
290     return xrefLocation;
291   }
292 
293   /**
294    * Returns the text to be rendered in the footer. Contains markup so it is
295    * required to be rendered as raw text.
296    *
297    * @return the footer text.
298    */
299   public String getFooterText()
300   {
301     return bundle.getString("report.footer");
302   }
303 
304   /**
305    * Returns the flag that indicates if notice messages should be rendered in
306    * the report (<code>true</code>) or if only error and warn messages are
307    * rendered ( <code>false</code>).
308    *
309    * @return the flag that indicates if notice messages should be rendered in
310    *         the report (<code>true</code>) or if only error and warn messages
311    *         are rendered ( <code>false</code>).
312    */
313   public boolean isNoticeMessagesRendered()
314   {
315     return noticeMessagesRendered;
316   }
317 
318   // --- business -------------------------------------------------------------
319 
320   /**
321    * Creates a copy of the passed in messages that contains only those messages
322    * that are note filtered.
323    *
324    * @param messages the messages to filter.
325    * @return the filtered messages.
326    */
327   public List<IssueMessage> filter(final List<IssueMessage> messages)
328   {
329     final List<IssueMessage> filteredMessages =
330         new ArrayList<IssueMessage>(messages.size());
331     for (final IssueMessage message : messages)
332     {
333       if (!filter(message))
334       {
335         filteredMessages.add(message);
336       }
337     }
338     return filteredMessages;
339   }
340 
341   /**
342    * Checks if the message should be filtered or not. A filtered message is not
343    * shown.
344    *
345    * @param message the message to check.
346    * @return <code>true</code> if the message is filtered and is to be
347    *         suppressed, <code>false</code> otherwise.
348    */
349   private boolean filter(final IssueMessage message)
350   {
351     final String text = message.getMessage();
352     for (final String fragment : messageFilter)
353     {
354       if (text.contains(fragment))
355       {
356         return true;
357       }
358     }
359 
360     return false;
361   }
362 
363   // --- object basics --------------------------------------------------------
364 
365 }