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.Collection;
19  
20  import de.smartics.analysis.javadoc.log.message.IssueLocation;
21  import de.smartics.analysis.javadoc.log.message.IssueMessage;
22  
23  /**
24   * Wraps a message to provide report specific information.
25   *
26   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
27   * @version $Revision:591 $
28   */
29  class IssueMessageWrapper
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    // --- members --------------------------------------------------------------
36  
37    /**
38     * The message being wrapped.
39     */
40    private final IssueMessage message;
41  
42    /**
43     * The name of the class the issue is located in. The value is
44     * <code>null</code> if no class can be determined.
45     */
46    private String className;
47  
48    /**
49     * The reference to the class in the XRef report.
50     */
51    private String xref;
52  
53    // ****************************** Initializer *******************************
54  
55    // ****************************** Constructors ******************************
56  
57    /**
58     * Default constructor.
59     *
60     * @param sourceRoots the base directories of the project where sources are
61     *          found that are examined by the Javadoc tool. Used to construct
62     *          references to the XRef report.
63     * @param xrefLocation the location where the XRef report is written to.
64     * @param message the message being wrapped.
65     */
66    public IssueMessageWrapper(final Collection<String> sourceRoots,
67        final String xrefLocation, final IssueMessage message)
68    {
69      this.message = message;
70      init(sourceRoots, xrefLocation);
71    }
72  
73    // ****************************** Inner Classes *****************************
74  
75    // ********************************* Methods ********************************
76  
77    // --- init -----------------------------------------------------------------
78  
79    /**
80     * Initializes the class name and xref value.
81     *
82     * @param sourceRoots the base directories of the project where sources are
83     *          found that are examined by the Javadoc tool. Used to construct
84     *          references to the XRef report.
85     * @param xrefLocation the location where the XRef report is written to.
86     */
87    private void init(final Collection<String> sourceRoots,
88        final String xrefLocation)
89    {
90      final IssueLocation location = message.getLocation();
91      if (location != null)
92      {
93        final String fileName = location.getFileName();
94        final int dotIndex = fileName.lastIndexOf('.');
95        final String baseDir = getBaseDir(sourceRoots, fileName);
96        if (baseDir != null && dotIndex != -1)
97        {
98          final String classNamePath =
99              fileName.substring(baseDir.length() + 1, dotIndex).replace('\\',
100                 '/');
101         this.xref = xrefLocation + '/' + classNamePath + ".html";
102         this.className = classNamePath.replace('/', '.');
103       }
104     }
105   }
106 
107   // --- get&set --------------------------------------------------------------
108 
109   /**
110    * Finds the source root the file is stored in. This is required to construct
111    * the class name and the reference to the XRef report.
112    *
113    * @param sourceRoots the base directories of the project where sources are
114    *          found that are examined by the Javadoc tool. Used to construct
115    *          references to the XRef report.
116    * @param fileName the name of the source file wrapped.
117    * @return the base directory the file references by <code>fileName</code> is
118    *         part of or <code>null</code> if none of the source roots is the
119    *         prefix of the given file.
120    */
121   private String getBaseDir(final Collection<String> sourceRoots,
122       final String fileName)
123   {
124     for (String baseDir : sourceRoots)
125     {
126       if (fileName.startsWith(baseDir))
127       {
128         return baseDir;
129       }
130     }
131     return null;
132   }
133 
134   /**
135    * Returns the message being wrapped.
136    *
137    * @return the message being wrapped.
138    */
139   public IssueMessage getMessage()
140   {
141     return message;
142   }
143 
144   /**
145    * Returns the name of the class the issue is located in. The value is
146    * <code>null</code> if no class can be determined.
147    *
148    * @return the name of the class the issue is located in.
149    */
150   public String getClassName()
151   {
152     return className;
153   }
154 
155   /**
156    * Returns the reference to the class in the XRef report.
157    *
158    * @return the reference to the class in the XRef report.
159    */
160   public String getXref()
161   {
162     return xref;
163   }
164 
165   // --- business -------------------------------------------------------------
166 
167   // --- object basics --------------------------------------------------------
168 
169 }