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.exceptions.report.renderer.html;
17  
18  import org.apache.commons.lang.StringUtils;
19  
20  import com.thoughtworks.qdox.model.DocletTag;
21  import com.thoughtworks.qdox.model.JavaAnnotatedElement;
22  import com.thoughtworks.qdox.model.JavaClass;
23  import com.thoughtworks.qdox.model.JavaField;
24  
25  import de.smartics.exceptions.report.renderer.ValueTagRenderer;
26  
27  /**
28   * Default implementation that renders the value tags as HTML.
29   */
30  public class HtmlValueTagRenderer implements ValueTagRenderer
31  {
32    // ********************************* Fields *********************************
33  
34    // --- constants ------------------------------------------------------------
35  
36    // --- members --------------------------------------------------------------
37  
38    // ****************************** Initializer *******************************
39  
40    // ****************************** Constructors ******************************
41  
42    // ****************************** Inner Classes *****************************
43  
44    // ********************************* Methods ********************************
45  
46    // --- init -----------------------------------------------------------------
47  
48    // --- get&set --------------------------------------------------------------
49  
50    // --- business -------------------------------------------------------------
51  
52    @Override
53    public StringBuilder render(final StringBuilder buffer, final DocletTag tag)
54    {
55      final String reference = tag.getValue();
56      if (StringUtils.isBlank(reference))
57      {
58        final JavaAnnotatedElement doc = tag.getContext();
59        if (doc instanceof JavaField)
60        {
61          final JavaField fieldDoc = (JavaField) doc;
62          appendConstantValue(buffer, fieldDoc);
63        }
64      }
65      else
66      {
67        JavaAnnotatedElement doc = tag.getContext();
68        if(doc instanceof JavaField)
69        {
70          doc = ((JavaField) doc).getDeclaringClass();
71        }
72  
73        if (doc instanceof JavaClass)
74        {
75          final JavaClass classDoc = (JavaClass) doc;
76          final String className = getClassName(reference);
77          if (StringUtils.isNotBlank(className))
78          {
79            final JavaClass referencedClassDoc =
80                classDoc.getJavaClassLibrary().getJavaClass(className);
81            appendConstantValue(buffer, reference, referencedClassDoc);
82          }
83          else
84          {
85            appendConstantValue(buffer, reference, classDoc);
86          }
87        }
88      }
89      return buffer;
90    }
91  
92    /**
93     * Appends the constant value of the member within the given class referenced
94     * by <code>reference</code>.
95     *
96     * @param buffer the buffer to append the constant value to (if any).
97     * @param reference the reference to the member (required to be member of the
98     *          given class.
99     * @param classDoc the class to search for the referenced member.
100    * @return the reference to the buffer for convenience.
101    */
102   private static StringBuilder appendConstantValue(final StringBuilder buffer,
103       final String reference, final JavaClass classDoc)
104   {
105     final String member = getMemberName(reference);
106     if (classDoc != null)
107     {
108       if (StringUtils.isNotBlank(member))
109       {
110         for (JavaField fieldDoc : classDoc.getFields())
111         {
112           if (member.equals(fieldDoc.getName()))
113           {
114             appendConstantValue(buffer, fieldDoc);
115             return buffer;
116           }
117         }
118         buffer.append("<code>").append(reference).append("</code>");
119       }
120     }
121     else
122     {
123       buffer.append("<code>");
124       if (StringUtils.isNotBlank(member))
125       {
126         buffer.append(reference.replace('#', '.'));
127       }
128       else
129       {
130         buffer.append(reference);
131       }
132       buffer.append("</code>");
133     }
134     return buffer;
135   }
136 
137   /**
138    * Appends the constant value of the field (if the field is a constant).
139    *
140    * @param buffer the buffer to append to.
141    * @param fieldDoc the access to the constant value.
142    */
143   private static void appendConstantValue(final StringBuilder buffer,
144       final JavaField fieldDoc)
145   {
146     final Object value = fieldDoc.getInitializationExpression();// constantValue();
147     if (value != null)
148     {
149       buffer.append(value);
150     }
151   }
152 
153   /**
154    * Returns the class (including package) part of the reference.
155    *
156    * @param reference the reference used in the value tag.
157    * @return the class (including package) part of the reference.
158    */
159   private static String getClassName(final String reference)
160   {
161     final int index = reference.indexOf('#');
162     if (index != -1)
163     {
164       final String className = reference.substring(0, index);
165       return className;
166     }
167     return reference;
168   }
169 
170   /**
171    * Returns the member part of the reference.
172    *
173    * @param reference the reference used in the value tag.
174    * @return the member part of the reference.
175    */
176   private static String getMemberName(final String reference)
177   {
178     final int index = reference.indexOf('#');
179     if (index != -1 && index + 1 < reference.length())
180     {
181       final String memberName = reference.substring(index + 1);
182       return memberName;
183     }
184     return null;
185   }
186 
187   // --- object basics --------------------------------------------------------
188 
189 }