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 java.io.File;
19  
20  import org.apache.commons.lang.NullArgumentException;
21  import org.apache.commons.lang.StringUtils;
22  
23  import de.smartics.util.source.MethodInfo;
24  
25  /**
26   * Base implementation of the {@link LinkConstructorStrategy} interface.
27   */
28  public abstract class AbstractLinkConstructorStrategy implements
29      LinkConstructorStrategy
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    // --- members --------------------------------------------------------------
36  
37    /**
38     * The configuration of this strategy instance.
39     */
40    protected final LinkConstructorStrategyConfig config;
41  
42    // ****************************** Initializer *******************************
43  
44    // ****************************** Constructors ******************************
45  
46    /**
47     * Default constructor.
48     *
49     * @param config the configuration of this strategy instance.
50     * @throws NullArgumentException if <code>config</code> is <code>null</code>.
51     */
52    protected AbstractLinkConstructorStrategy(
53        final LinkConstructorStrategyConfig config) throws NullArgumentException
54    {
55      checkArguments(config);
56      this.config = config;
57    }
58  
59    // ****************************** Inner Classes *****************************
60  
61    // ********************************* Methods ********************************
62  
63    // --- init -----------------------------------------------------------------
64  
65    private static void checkArguments(final LinkConstructorStrategyConfig config)
66      throws NullArgumentException
67    {
68      if (config == null)
69      {
70        throw new NullArgumentException("config");
71      }
72    }
73  
74    // --- get&set --------------------------------------------------------------
75  
76    /**
77     * Returns the configuration of this strategy instance.
78     *
79     * @return the configuration of this strategy instance.
80     */
81    public LinkConstructorStrategyConfig getConfig()
82    {
83      return config;
84    }
85  
86    // --- business -------------------------------------------------------------
87  
88    /**
89     * Constructs a path to the specified Java element.
90     *
91     * @param baseLink the base link to prepend.
92     * @param ref the element in the report to reference.
93     * @return the path to the element or <code>null</code> if the referenced
94     *         element does not exit (only the existence of the file is checked,
95     *         not the element within the file).
96     */
97    public String constructLink(final String baseLink, final JavaElementRef ref)
98    {
99      if (isReferencedReportExisting(ref))
100     {
101       final StringBuilder buffer = new StringBuilder(64);
102 
103       buffer.append(baseLink);
104       appendBasePathSeparator(buffer);
105       addQualifiedType(buffer, ref);
106 
107       final MethodInfo memberName = ref.getElementName();
108       if (config.isReferenceMember() && memberName != null)
109       {
110         appendMemberPart(buffer, memberName);
111       }
112 
113       return buffer.toString();
114     }
115     return null;
116   }
117 
118   private boolean isReferencedReportExisting(final JavaElementRef ref)
119   {
120     final File baseDir = config.getReportLocation();
121     final StringBuilder buffer = new StringBuilder();
122     constructFilePath(buffer, ref);
123     final String filePath = buffer.toString();
124     final File file = new File(baseDir, filePath);
125     return file.exists();
126   }
127 
128   protected void constructFilePath(final StringBuilder buffer,
129       final JavaElementRef ref)
130   {
131     addQualifiedType(buffer, ref);
132   }
133 
134   protected void addQualifiedType(final StringBuilder buffer,
135       final JavaElementRef ref)
136   {
137     final String packageName = ref.getPackageName();
138     if (StringUtils.isNotBlank(packageName))
139     {
140       addPackagePart(buffer, packageName);
141     }
142     final String typeName = ref.getTypeName();
143     appendTypePart(buffer, typeName);
144   }
145 
146   /**
147    * Appends the separator between the base path and the package part.
148    *
149    * @param buffer the buffer to append to.
150    * @impl Appends a single slash.
151    */
152   protected void appendBasePathSeparator(final StringBuilder buffer)
153   {
154     buffer.append('/');
155   }
156 
157   /**
158    * Adds the package part of the link.
159    *
160    * @param buffer the buffer to append to.
161    * @param packageName the name of the package to render.
162    */
163   protected abstract void addPackagePart(final StringBuilder buffer,
164       final String packageName);
165 
166   /**
167    * Adds the type part of the link.
168    *
169    * @param buffer the buffer to append to.
170    * @param typeName the name of the package to render.
171    * @impl The default implementation adds the extension separated by a dot.
172    */
173   protected void appendTypePart(final StringBuilder buffer,
174       final String typeName)
175   {
176     buffer.append(typeName).append('.').append(config.getExtension());
177   }
178 
179   /**
180    * Adds the member part of the link if the configuration requires the member
181    * part to be rendered.
182    *
183    * @param buffer the buffer to append to.
184    * @param method the name of the member to render.
185    */
186   protected void appendMemberPart(final StringBuilder buffer,
187       final MethodInfo method)
188   {
189     buffer.append('#').append(method.getMethodSignature());
190   }
191 
192   // --- object basics --------------------------------------------------------
193 
194 }