View Javadoc

1   /*
2    * Copyright 2010-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.testdoc.collect.extractor;
17  
18  import java.util.Map;
19  import java.util.Map.Entry;
20  
21  import javax.lang.model.element.AnnotationMirror;
22  import javax.lang.model.element.AnnotationValue;
23  import javax.lang.model.element.Element;
24  
25  import org.apache.commons.lang.StringUtils;
26  
27  import de.smartics.testdoc.annotations.TestDocHint;
28  import de.smartics.testdoc.core.doc.SortKey;
29  
30  /**
31   * Extracts {@link TestDocHint} information.
32   *
33   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
34   * @version $Revision:591 $
35   */
36  class TestDocHintsExtractor
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    // --- members --------------------------------------------------------------
43  
44    // ****************************** Initializer *******************************
45  
46    // ****************************** Constructors ******************************
47  
48    // ****************************** Inner Classes *****************************
49  
50    /**
51     * The hint information from the annotation.
52     */
53    static final class Hints
54    {
55      /**
56       * The sentence provided by the hint.
57       */
58      private String sentence;
59  
60      /**
61       * The main index key.
62       */
63      public String mainSortKey;
64  
65      /**
66       * The index key.
67       */
68      public String indexSortKey;
69  
70      Hints()
71      {
72      }
73  
74      /**
75       * Returns the sentence provided by the hint.
76       *
77       * @return the sentence provided by the hint.
78       */
79      public String getSentence()
80      {
81        return sentence;
82      }
83  
84      // /**
85      // * Returns the sentence provided by the hint.
86      // *
87      // * @param defaultValue the default value if the hint provides no sentence.
88      // * @return the sentence provided by the hint.
89      // */
90      // public String getSentence(final String defaultValue)
91      // {
92      // return hasSentence() ? sentence : defaultValue;
93      // }
94  
95      /**
96       * Checks if the hint provides a non-blank sentence.
97       *
98       * @return <code>true</code> if a non-blank sentence is provided,
99       *         <code>false</code> if the sentence is blank.
100      */
101     public boolean hasSentence()
102     {
103       return StringUtils.isNotBlank(sentence);
104     }
105 
106     /**
107      * Returns the main index key.
108      *
109      * @return the main index key.
110      */
111     public String getMainSortKey()
112     {
113       return mainSortKey;
114     }
115 
116     /**
117      * Returns the index key.
118      *
119      * @return the index key.
120      */
121     public String getIndexSortKey()
122     {
123       return indexSortKey;
124     }
125 
126     /**
127      * Creates a sort key upon the key information in this hints instance with
128      * the given hints as defaults.
129      *
130      * @param defaultHints the defaults if this instance provides no
131      *          information.
132      * @return the generated sort key.
133      */
134     public SortKey createSortKey(final Hints defaultHints)
135     {
136       final String mainIndexKey =
137           calcIndexKey(getMainSortKey(),
138               (defaultHints != null) ? defaultHints.getMainSortKey() : null);
139       final String indexKey =
140           calcIndexKey(getIndexSortKey(),
141               (defaultHints != null) ? defaultHints.getIndexSortKey() : null);
142       final SortKey sortKey = new SortKey(mainIndexKey, indexKey);
143       return sortKey;
144     }
145 
146     private static String calcIndexKey(final String key, final String defaultKey)
147     {
148       if (key == null)
149       {
150         return defaultKey;
151       }
152       return key;
153     }
154   }
155 
156   // ********************************* Methods ********************************
157 
158   // --- init -----------------------------------------------------------------
159 
160   // --- get&set --------------------------------------------------------------
161 
162   // --- business -------------------------------------------------------------
163 
164   /**
165    * Reads the hints from the annotation.
166    *
167    * @param element the element to check for {@link TestDocHint} annotations.
168    * @return the hints of the annotation. The value is never <code>null</code>,
169    *         but the hint instance may provide no information.
170    */
171   Hints readHints(final Element element)
172   {
173     final Hints hints = new Hints();
174     for (final AnnotationMirror annotation : element.getAnnotationMirrors())
175     {
176       if (TestDocHint.class.getName().equals(
177           annotation.getAnnotationType().toString()))
178       {
179         return readHints(hints, annotation);
180       }
181     }
182 
183     return hints;
184   }
185 
186   private static Hints readHints(final Hints hints,
187       final AnnotationMirror annotation)
188   {
189     final Map<? extends Element, ? extends AnnotationValue> map =
190         annotation.getElementValues();
191     for (final Map.Entry<? extends Element, ? extends AnnotationValue> entry : map
192         .entrySet())
193     {
194       addHint(hints, entry);
195     }
196     return hints;
197   }
198 
199   private static void addHint(final Hints hints,
200       final Map.Entry<? extends Element, ? extends AnnotationValue> entry)
201   {
202     final String key = entry.getKey().toString();
203     if (isKey(key, "sentence()"))
204     {
205       final Object value = entry.getValue().getValue();
206       if (value instanceof String)
207       {
208         hints.sentence = (String) value;
209       }
210     }
211     if (isKey(key, "mainSortKey()"))
212     {
213       hints.mainSortKey = calcStringValue(entry);
214     }
215     if (isKey(key, "indexSortKey()"))
216     {
217       hints.indexSortKey = calcStringValue(entry);
218     }
219   }
220 
221   private static boolean isKey(final String key, final String suffix)
222   {
223     return key != null && key.trim().endsWith(suffix);
224   }
225 
226   private static String calcStringValue(
227       final Entry<? extends Element, ? extends AnnotationValue> entry)
228   {
229     final Object value = entry.getValue().getValue();
230 
231     if (value instanceof String)
232     {
233       final String stringValue = (String) value;
234       if (StringUtils.isNotBlank(stringValue))
235       {
236         return stringValue;
237       }
238     }
239     return null;
240   }
241 
242   // --- object basics --------------------------------------------------------
243 
244 }