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.StringEscapeUtils;
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.SeeTagRenderer;
26  
27  /**
28   * Default implementation that renders the see and link tags as HTML.
29   */
30  public class HtmlSeeTagRenderer implements SeeTagRenderer
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    // CHECKSTYLE:OFF
53    @Override
54    public StringBuilder render(final StringBuilder buffer, final DocletTag tag)
55    {
56      final String text = tag.getValue();
57      final String textFragment;
58      final int length = text.length();
59      if (text.startsWith("<")) // NOPMD
60      {
61        textFragment = text;
62      }
63      else if (length > 2 && text.startsWith("\"") && text.endsWith("\""))
64      {
65        textFragment =
66            "<i>" + StringEscapeUtils.escapeHtml(text.substring(1, length - 1))
67                + "</i>";
68      }
69      else if (length > 1 && text.startsWith("#")) // NOPMD
70      {
71        final JavaAnnotatedElement context = tag.getContext();
72        if (context instanceof JavaField)
73        {
74          final JavaClass type = ((JavaField) context).getDeclaringClass();
75          final String fqn = type.getFullyQualifiedName();
76          final String id = text.substring(1);
77          textFragment = "<a href=\"#" + fqn + '_' + id + "\">" + id + "</a>";
78        }
79        else
80        {
81          textFragment = StringEscapeUtils.escapeHtml(text);
82        }
83      }
84      else if (length > 1 && text.contains("#"))
85      {
86        final JavaClass classDoc = Utils.referencedClass(tag);
87        if (classDoc != null && classDoc.isA("de.smartics.exceptions.core.Code"))
88        {
89          final String id = Utils.referencedMemberName(tag);
90          final String fqn = classDoc.getFullyQualifiedName();
91          textFragment =
92              "<a href=\"#" + fqn + '_' + id + "\">" + classDoc.getName() + '.'
93                  + id + "</a>";
94        }
95        else
96        {
97          textFragment = StringEscapeUtils.escapeHtml(text);
98        }
99      }
100     else
101     {
102       textFragment = StringEscapeUtils.escapeHtml(text);
103     }
104     buffer.append("<dt>See:</dt><dd>").append(textFragment).append("</dd>\n");
105     return buffer;
106   }
107   // CHECKSTYLE:ON
108 
109   // --- object basics --------------------------------------------------------
110 
111 }