View Javadoc

1   /*
2    * Copyright 2007-2013 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.exceptions.sdoc;
17  
18  import java.util.ArrayList;
19  import java.util.LinkedList;
20  import java.util.List;
21  
22  import org.apache.commons.lang.StringUtils;
23  
24  import com.thoughtworks.qdox.model.DocletTag;
25  import com.thoughtworks.qdox.model.JavaAnnotatedElement;
26  import com.thoughtworks.qdox.model.JavaClass;
27  import com.thoughtworks.qdox.model.JavaField;
28  
29  /**
30   * Extracts tag information form the Javadoc.
31   */
32  class TagInfo
33  {
34    // ********************************* Fields *********************************
35  
36    // --- constants ------------------------------------------------------------
37  
38    // --- members --------------------------------------------------------------
39  
40    /**
41     * The information extracted from the all level Javadoc tag
42     * <code>sdoc.category</code>.
43     */
44    private final String category;
45  
46    /**
47     * The information extracted from the all level Javadoc tag
48     * <code>sdoc.subcategory</code>.
49     */
50    private final String subcategory;
51  
52    /**
53     * The information extracted from the all level Javadoc tag
54     * <code>sdoc.tag</code> that may appear multiple times.
55     */
56    private final List<String> tags = new ArrayList<String>();
57  
58    /**
59     * The information extracted from the all level Javadoc tag
60     * <code>sdoc.parent</code> that may appear multiple times.
61     */
62    private final List<String> parents = new ArrayList<String>();
63  
64    /**
65     * The information extracted from the field level Javadoc tag
66     * <code>sdoc.name</code>.
67     */
68    private final String name;
69  
70    /**
71     * The information extracted from the field level Javadoc tag
72     * <code>sdoc.sortKey</code>.
73     */
74    private final String sortKey;
75  
76    /**
77     * The information extracted from the field level Javadoc tag
78     * <code>sdoc.shortDescription</code>.
79     */
80    private final String shortDescription;
81  
82    /**
83     * The information extracted from the field level Javadoc tag
84     * <code>sdoc.notes</code>.
85     */
86    private final String notes;
87  
88    /**
89     * The information extracted from the field level Javadoc tag
90     * <code>sdoc.codeType</code>.
91     */
92    private final String codeType;
93  
94    // ****************************** Initializer *******************************
95  
96    // ****************************** Constructors ******************************
97  
98    /**
99     * Default constructor.
100    */
101   public TagInfo(final JavaClass classDoc, final JavaField fieldDoc)
102   {
103     this.category = extractSingleValue(classDoc, fieldDoc, "sdoc.category");
104     this.subcategory =
105         extractSingleValue(classDoc, fieldDoc, "sdoc.subcategory");
106     extractMultiValue(classDoc, "sdoc.tag", tags);
107     extractMultiValue(fieldDoc, "sdoc.tag", tags);
108     extractMultiValue(classDoc, "sdoc.parent", parents);
109     extractMultiValue(fieldDoc, "sdoc.parent", parents);
110 
111     this.name = extractSingleValue(fieldDoc, "sdoc.name");
112     this.sortKey = extractSingleValue(fieldDoc, "sdoc.sortKey");
113     this.shortDescription =
114         extractSingleValue(fieldDoc, "sdoc.shortDescription");
115     this.notes = extractSingleValue(fieldDoc, "sdoc.notes");
116     this.codeType = calcCodeType(classDoc, fieldDoc);
117     ;
118   }
119 
120   // ****************************** Inner Classes *****************************
121 
122   // ********************************* Methods ********************************
123 
124   // --- init -----------------------------------------------------------------
125 
126   private String calcCodeType(final JavaClass classDoc, final JavaField fieldDoc)
127   {
128     String type = extractSingleValue(fieldDoc, "sdoc.codeType");
129     if (StringUtils.isBlank(type))
130     {
131       type = "error";
132     }
133 
134     return type;
135   }
136 
137   private String extractSingleValue(final JavaAnnotatedElement doc1,
138       final JavaAnnotatedElement doc2, final String name)
139   {
140     String value = extractSingleValue(doc2, name);
141     if (StringUtils.isBlank(value))
142     {
143       value = extractSingleValue(doc1, name);
144     }
145     return value;
146   }
147 
148   private String extractSingleValue(final JavaAnnotatedElement doc,
149       final String name)
150   {
151     final List<DocletTag> tags = getTagsByName(doc, name);
152     if (!tags.isEmpty())
153     {
154       final String text = getValue(tags.get(0));
155       if (StringUtils.isNotBlank(text))
156       {
157         return text;
158       }
159     }
160     return null;
161   }
162 
163   private static String getValue(final DocletTag docletTag)
164   {
165     final String name = docletTag.getName();
166     final int index = name.indexOf('=');
167     if (index != -1 && index < name.length() - 1)
168     {
169       final String value = name.substring(index + 1);
170       return value;
171     }
172     else
173     {
174       return docletTag.getValue();
175     }
176   }
177 
178   private static List<DocletTag> getTagsByName(final JavaAnnotatedElement doc,
179       final String name)
180   {
181     final List<DocletTag> specifiedTags = new LinkedList<DocletTag>();
182     final String tag = name + '=';
183     for (final DocletTag docletTag : doc.getTags())
184     {
185       final String currentName = docletTag.getName();
186       if (currentName.equals(name) && currentName.startsWith(tag))
187       {
188         specifiedTags.add(docletTag);
189       }
190     }
191     return specifiedTags;
192   }
193 
194   private void extractMultiValue(final JavaAnnotatedElement doc,
195       final String name, final List<String> list)
196   {
197     final List<DocletTag> tags = getTagsByName(doc, name);
198     for (final DocletTag tag : tags)
199     {
200       final String text = tag.getValue();
201       if (StringUtils.isNotBlank(text))
202       {
203         list.add(text);
204       }
205     }
206   }
207 
208   // --- get&set --------------------------------------------------------------
209 
210   /**
211    * Returns the information extracted from the type level Javadoc tag
212    * <code>sdoc.category</code>.
213    *
214    * @return the information extracted from the type level Javadoc tag
215    *         <code>sdoc.category</code>.
216    */
217   public String getCategory()
218   {
219     return category;
220   }
221 
222   /**
223    * Returns the information extracted from the type level Javadoc tag
224    * <code>sdoc.subcategory</code>.
225    *
226    * @return the information extracted from the type level Javadoc tag
227    *         <code>sdoc.subcategory</code>.
228    */
229   public String getSubcategory()
230   {
231     return subcategory;
232   }
233 
234   /**
235    * Returns the information extracted from the all level Javadoc tag
236    * <code>sdoc.tag</code> that may appear multiple times.
237    *
238    * @return the information extracted from the all level Javadoc tag
239    *         <code>sdoc.tag</code>.
240    */
241   public List<String> getTags()
242   {
243     return tags;
244   }
245 
246   /**
247    * Returns the information extracted from the all level Javadoc tag
248    * <code>sdoc.parent</code> that may appear multiple times.
249    *
250    * @return the information extracted from the all level Javadoc tag
251    *         <code>sdoc.parent</code>.
252    */
253   public List<String> getParents()
254   {
255     return parents;
256   }
257 
258   /**
259    * Returns the information extracted from the field level Javadoc tag
260    * <code>sdoc.name</code>.
261    *
262    * @return the information extracted from the field level Javadoc tag
263    *         <code>sdoc.name</code>.
264    */
265   public String getName()
266   {
267     return name;
268   }
269 
270   /**
271    * Returns the information extracted from the field level Javadoc tag
272    * <code>sdoc.sort-key</code>.
273    *
274    * @return the information extracted from the field level Javadoc tag
275    *         <code>sdoc.sort-key</code>.
276    */
277   public String getSortKey()
278   {
279     return sortKey;
280   }
281 
282   /**
283    * Returns the information extracted from the field level Javadoc tag
284    * <code>sdoc.short-description</code>.
285    *
286    * @return the information extracted from the field level Javadoc tag
287    *         <code>sdoc.short-description</code>.
288    */
289   public String getShortDescription()
290   {
291     return shortDescription;
292   }
293 
294   /**
295    * Returns the information extracted from the field level Javadoc tag
296    * <code>sdoc.notes</code>.
297    *
298    * @return the information extracted from the field level Javadoc tag
299    *         <code>sdoc.notes</code>.
300    */
301   public String getNotes()
302   {
303     return notes;
304   }
305 
306   /**
307    * Returns the information extracted from the field level Javadoc tag
308    * <code>sdoc.code-type</code>.
309    *
310    * @return the information extracted from the field level Javadoc tag
311    *         <code>sdoc.code-type</code>.
312    */
313   public String getCodeType()
314   {
315     return codeType;
316   }
317 
318   // --- business -------------------------------------------------------------
319 
320   // --- object basics --------------------------------------------------------
321 
322 }