View Javadoc

1   /*
2    * Copyright 2006-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.util.report.link;
17  
18  import org.apache.commons.lang.StringUtils;
19  
20  import de.smartics.util.source.MethodInfo;
21  
22  /**
23   * Helper to construct links from a given report instance to other <a
24   * href="http://maven-apache.org/">Maven</a> reports.
25   */
26  public class ExternalReport
27  {
28    // ********************************* Fields *********************************
29  
30    // --- constants ------------------------------------------------------------
31  
32    // --- members --------------------------------------------------------------
33  
34    /**
35     * The report ID this strategy is constructed for.
36     */
37    protected final ReportId reportId;
38  
39    /**
40     * The base path to the report to prefix the constructed link.
41     */
42    private final String basePath;
43  
44    /**
45     * The strategy to construct the link suffix to the base path.
46     */
47    private final LinkConstructorStrategy strategy;
48  
49    // ****************************** Initializer *******************************
50  
51    // ****************************** Constructors ******************************
52  
53    /**
54     * Default constructor.
55     *
56     * @param reportId the report ID this strategy is constructed for.
57     * @param basePath the base path to the report to prefix the constructed link.
58     * @param strategy the strategy to construct the link suffix to the base path.
59     * @throws IllegalArgumentException if <code>helper</code> or
60     *           <code>reportLocation</code> is <code>null</code> or
61     *           <code>reportArtifactId</code> is blank.
62     */
63    public ExternalReport(final ReportId reportId, final String basePath,
64        final LinkConstructorStrategy strategy) throws IllegalArgumentException
65    {
66      checkArguments(reportId, basePath, strategy);
67      this.reportId = reportId;
68      this.basePath = basePath;
69      this.strategy = strategy;
70    }
71  
72    // ****************************** Inner Classes *****************************
73  
74    // ********************************* Methods ********************************
75  
76    // --- init -----------------------------------------------------------------
77  
78    private static void checkArguments(final ReportId reportId,
79        final String basePath, final LinkConstructorStrategy strategy)
80      throws IllegalArgumentException
81    {
82      if (reportId == null)
83      {
84        throw new IllegalArgumentException("The report ID  must not be 'null'.");
85      }
86      if (StringUtils.isBlank(basePath))
87      {
88        throw new IllegalArgumentException("The base path must not be blank.");
89      }
90      if (strategy == null)
91      {
92        throw new IllegalArgumentException("The strategy must not be 'null'.");
93      }
94    }
95  
96    // --- get&set --------------------------------------------------------------
97  
98    /**
99     * Returns the report ID this strategy is constructed for.
100    *
101    * @return the report ID this strategy is constructed for.
102    */
103   public ReportId getReportId()
104   {
105     return reportId;
106   }
107 
108   /**
109    * Returns the base path to the report to prefix the constructed link.
110    *
111    * @return the base path to the report to prefix the constructed link.
112    */
113   public String getBasePath()
114   {
115     return basePath;
116   }
117 
118   // --- business -------------------------------------------------------------
119 
120   /**
121    * Constructs a link to the given Java element.
122    *
123    * @param packageName packageName the name of the package.
124    * @param typeName typeName the name of the Java type.
125    * @param method the test method to link to.
126    * @return the requested relative link.
127    */
128   public String constructLink(final String packageName, final String typeName,
129       final MethodInfo method)
130   {
131     final JavaElementRef ref =
132         new JavaElementRef(packageName, typeName, method);
133     return constructLink(ref);
134   }
135 
136   /**
137    * Constructs a link to the given Java Element.
138    *
139    * @param ref the element to link to.
140    * @return the requested relative link.
141    */
142   public String constructLink(final JavaElementRef ref)
143   {
144     return strategy.constructLink(basePath, ref);
145   }
146 
147   /**
148    * Returns the key to the label to use as a label for the rendered link.
149    *
150    * @return the key to the label to use as a label for the rendered link.
151    */
152   public String getLabelKey()
153   {
154     return strategy.getConfig().getLabelKey();
155   }
156 
157   // --- object basics --------------------------------------------------------
158 
159 }