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;
17  
18  import java.util.List;
19  
20  import com.thoughtworks.qdox.model.DocletTag;
21  import com.thoughtworks.qdox.model.JavaAnnotatedElement;
22  
23  import de.smartics.exceptions.report.utils.InlineJavadocTags;
24  
25  /**
26   * Renderer for Javadoc information.
27   */
28  public final class JavadocRenderer
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    // --- members --------------------------------------------------------------
35  
36    /**
37     * The factory to create the renderers to use.
38     */
39    private final RendererFactory factory;
40  
41    // ****************************** Initializer *******************************
42  
43    // ****************************** Constructors ******************************
44  
45    /**
46     * Default constructor.
47     *
48     * @param factory the factory to create the renderers to use.
49     */
50    public JavadocRenderer(final RendererFactory factory)
51    {
52      this.factory = factory;
53    }
54  
55    // ****************************** Inner Classes *****************************
56  
57    // ********************************* Methods ********************************
58  
59    // --- init -----------------------------------------------------------------
60  
61    // --- get&set --------------------------------------------------------------
62  
63    // --- business -------------------------------------------------------------
64  
65    /**
66     * Renders the information found in a Javadoc.
67     *
68     * @param doc the comment to render.
69     * @return the rendered string.
70     */
71    public String render(final JavaAnnotatedElement doc)
72    {
73      final StringBuilder buffer = new StringBuilder(256);
74  
75      renderInlineTags(doc, buffer);
76      renderTags(doc, buffer);
77  
78      return buffer.toString();
79    }
80  
81    private void renderInlineTags(final JavaAnnotatedElement doc,
82        final StringBuilder buffer)
83    {
84      final InlineJavadocTags tags = new InlineJavadocTags(doc);
85  
86      for (final DocletTag tag : tags)
87      {
88        final String tagName = tag.getName();
89        if ("link".equals(tagName))
90        {
91          final LinkTagRenderer renderer = factory.createLinkTagRenderer();
92          renderer.render(buffer, tag);
93        }
94        else if ("linkplain".equals(tagName))
95        {
96          final LinkPlainTagRenderer renderer =
97              factory.createLinkPlainTagRenderer();
98          renderer.render(buffer, tag);
99        }
100       else if ("literal".equals(tagName))
101       {
102         final LiteralTagRenderer renderer = factory.createLiteralTagRenderer();
103         renderer.render(buffer, tag);
104       }
105       else if ("code".equals(tagName))
106       {
107         final CodeTagRenderer renderer = factory.createCodeTagRenderer();
108         renderer.render(buffer, tag);
109       }
110       else if ("value".equals(tagName))
111       {
112         final ValueTagRenderer renderer = factory.createValueTagRenderer();
113         renderer.render(buffer, tag);
114       }
115       else
116       {
117         final TextRenderer renderer = factory.createTextRenderer();
118         renderer.render(buffer, tag.getValue());
119       }
120     }
121   }
122 
123   private void renderTags(final JavaAnnotatedElement doc,
124       final StringBuilder buffer)
125   {
126     final List<DocletTag> tags = doc.getTags();
127 
128     if (!tags.isEmpty())
129     {
130       final TagListRenderer listRenderer = factory.createTagListRenderer();
131       listRenderer.renderListStart(buffer);
132       for (final DocletTag tag : tags)
133       {
134         final String tagName = tag.getName();
135         if ("see".equals(tagName))
136         {
137           final SeeTagRenderer renderer = factory.createSeeTagRenderer();
138           renderer.render(buffer, tag);
139         }
140         else if ("version".equals(tagName))
141         {
142           final SinceTagRenderer renderer = factory.createSinceTagRenderer();
143           renderer.render(buffer, tag);
144         }
145         else if ("since".equals(tagName))
146         {
147           final VersionTagRenderer renderer =
148               factory.createVersionTagRenderer();
149           renderer.render(buffer, tag);
150         }
151       }
152       listRenderer.renderListEnd(buffer);
153     }
154   }
155 
156   // --- object basics --------------------------------------------------------
157 
158 }