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.maven.export.sink;
17  
18  import org.apache.maven.doxia.sink.Sink;
19  
20  import de.smartics.maven.util.report.MessageHelper;
21  
22  /**
23   * Helper to render parts of a Sink page.
24   *
25   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
26   * @version $Revision:591 $
27   */
28  class RenderHelper
29  { // NOPMD
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    // --- members --------------------------------------------------------------
35  
36    /**
37     * The resource bundles with messages to display as labels in the generated
38     * report.
39     */
40    private final MessageHelper messages;
41  
42    /**
43     * The output medium to write to.
44     */
45    private final Sink sink;
46  
47    // ****************************** Initializer *******************************
48  
49    // ****************************** Constructors ******************************
50  
51    /**
52     * Default constructor.
53     *
54     * @param messages the resource bundles with messages to display as labels in
55     *          the generated report.
56     * @param sink the output medium to write to.
57     */
58    public RenderHelper(final MessageHelper messages, final Sink sink)
59    {
60      this.messages = messages;
61      this.sink = sink;
62    }
63  
64    // ****************************** Inner Classes *****************************
65  
66    // ********************************* Methods ********************************
67  
68    // --- init -----------------------------------------------------------------
69  
70    // --- get&set --------------------------------------------------------------
71  
72    // --- business -------------------------------------------------------------
73  
74    void renderIndexTableStart()
75    {
76      sink.table();
77      sink.tableRow();
78      sink.tableHeaderCell(messages.getLabel("report.table.header.counter.width"));
79      final String topicLabel = messages.getLabel("report.table.header.counter");
80      sink.text(topicLabel);
81      sink.tableHeaderCell_();
82      sink.tableHeaderCell();
83      final String valueLabel = messages.getLabel("report.table.header.index");
84      sink.text(valueLabel);
85      sink.tableHeaderCell_();
86      sink.tableRow_();
87    }
88  
89    void renderIndexRow(final int counter, final String label, final String id)
90    {
91      sink.tableRow();
92      sink.tableCell();
93      sink.text(String.valueOf(counter));
94      sink.tableCell_();
95      sink.tableCell();
96      renderInPageLink(label, id);
97      sink.tableCell_();
98      sink.tableRow_();
99    }
100 
101   void renderIndexRow(final int counter, final String content)
102   {
103     renderIndexRow(counter, content, content);
104   }
105 
106   void renderIndexTableEnd()
107   {
108     sink.table_();
109   }
110 
111   void renderInPageLink(final String id)
112   {
113     final String label = messages.getLabel(id);
114     renderInPageLink(label, id);
115   }
116 
117   void renderInPageLink(final String label, final String id)
118   {
119     final String link = '#' + normalizeKey(id);
120     renderLink(label, link);
121   }
122 
123   void renderLink(final String label, final String link)
124   {
125     sink.link(link);
126     sink.text(label);
127     sink.link_();
128   }
129 
130   void renderAnchor(final String id)
131   {
132     final String normalized = normalizeKey(id);
133     sink.anchor(normalized);
134     sink.anchor_();
135   }
136 
137   /**
138    * Normalizes the key to an issue information to be a valid key element to
139    * render as anchor name.
140    *
141    * @param key the key to normalize.
142    * @return the normalized key.
143    */
144   public static String normalizeKey(final String key)
145   {
146     if (key == null || "".equals(key))
147     {
148       return key;
149     }
150 
151     final char[] trimmed = key.trim().toCharArray();
152     boolean uppercase = false;
153     int currentPos = 0;
154     for (int i = 0; i < trimmed.length; i++, currentPos++)
155     {
156       final char c = trimmed[i];
157       if (Character.isWhitespace(c))
158       {
159         uppercase = true;
160         --currentPos;
161       }
162       else if (uppercase)
163       {
164         trimmed[currentPos] = Character.toUpperCase(c);
165         uppercase = false;
166       }
167       else
168       {
169         trimmed[currentPos] = c;
170       }
171     }
172 
173     return new String(trimmed).substring(0, currentPos);
174   }
175 
176   void renderSectionStart(final int level, final String name)
177   {
178     renderSectionStart(level);
179 
180     renderAnchor(name);
181     renderSectionTitle(level, name);
182   }
183 
184   private void renderSectionTitle(final int level, final String name)
185   {
186     switch (level)
187     {
188       case 1:
189         sink.sectionTitle1();
190         sink.text(name);
191         sink.sectionTitle1_();
192         break;
193       case 2:
194         sink.sectionTitle2();
195         sink.text(name);
196         sink.sectionTitle2_();
197         break;
198       case 3:
199         sink.sectionTitle3();
200         sink.text(name);
201         sink.sectionTitle3_();
202         break;
203       case 4:
204         sink.sectionTitle4();
205         sink.text(name);
206         sink.sectionTitle4_();
207         break;
208       case 5:
209         sink.sectionTitle5();
210         sink.text(name);
211         sink.sectionTitle5_();
212         break;
213       default:
214         sink.sectionTitle5();
215         sink.text(name);
216         sink.sectionTitle5_();
217         break;
218     }
219   }
220 
221   private void renderSectionStart(final int level)
222   {
223     switch (level)
224     {
225       case 1:
226         sink.section1();
227         break;
228       case 2:
229         sink.section2();
230         break;
231       case 3:
232         sink.section3();
233         break;
234       case 4:
235         sink.section4();
236         break;
237       case 5:
238         sink.section5();
239         break;
240       default:
241         sink.section5();
242         break;
243     }
244   }
245 
246   void renderSectionEnd(final int level)
247   {
248     switch (level)
249     {
250       case 1:
251         sink.section1_();
252         break;
253       case 2:
254         sink.section2_();
255         break;
256       case 3:
257         sink.section3_();
258         break;
259       case 4:
260         sink.section4_();
261         break;
262       case 5:
263         sink.section5_();
264         break;
265       default:
266         sink.section5_();
267         break;
268     }
269   }
270 
271   // --- object basics --------------------------------------------------------
272 
273 }