View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.doxia.module.xdoc;
20  
21  import javax.swing.text.html.HTML.Attribute;
22  
23  import java.io.StringWriter;
24  import java.io.Writer;
25  
26  import org.apache.maven.doxia.sink.Sink;
27  import org.apache.maven.doxia.sink.SinkEventAttributes;
28  import org.apache.maven.doxia.sink.impl.AbstractSinkTest;
29  import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
30  import org.junit.jupiter.api.Test;
31  
32  import static org.apache.maven.doxia.util.HtmlTools.escapeHTML;
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  
35  /**
36   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
37   * @since 1.0
38   */
39  public class XdocSinkTest extends AbstractSinkTest {
40  
41      @Test
42      public void section1() {
43          String title = "Title1";
44          getSink().section1();
45          getSink().sectionTitle1();
46          getSink().text(title);
47          getSink().sectionTitle1_();
48          getSink().section1_();
49          getSink().flush();
50          getSink().close();
51  
52          String actual = getSinkContent();
53          String expected = getSection1Block(title);
54  
55          assertEquals(expected, actual, "Wrong section1 block!");
56      }
57  
58      @Test
59      public void section2() {
60          String title = "Title2";
61          getSink().section2();
62          getSink().sectionTitle2();
63          getSink().text(title);
64          getSink().sectionTitle2_();
65          getSink().section2_();
66          getSink().flush();
67          getSink().close();
68  
69          String actual = getSinkContent();
70          String expected = getSection2Block(title);
71  
72          assertEquals(expected, actual, "Wrong section2 block!");
73      }
74  
75      protected String outputExtension() {
76          return "xml";
77      }
78  
79      protected Sink createSink(Writer writer) {
80          return new XdocSink(writer, "UTF-8");
81      }
82  
83      protected boolean isXmlSink() {
84          return true;
85      }
86  
87      protected String getTitleBlock(String title) {
88          return "<title>" + title + "</title>";
89      }
90  
91      protected String getAuthorBlock(String author) {
92          return author;
93      }
94  
95      protected String getDateBlock(String date) {
96          return date;
97      }
98  
99      protected String getHeadBlock() {
100         return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
101                 + "<document xmlns=\"http://maven.apache.org/XDOC/2.0\" "
102                 + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
103                 + "xsi:schemaLocation=\"http://maven.apache.org/XDOC/2.0 https://maven.apache.org/xsd/xdoc-2.0.xsd\">"
104                 + "<properties></properties>";
105     }
106 
107     protected String getBodyBlock() {
108         return "<body></body></document>";
109     }
110 
111     protected String getArticleBlock() {
112         return "<article></article>";
113     }
114 
115     protected String getNavigationBlock() {
116         return "<nav></nav>";
117     }
118 
119     protected String getSidebarBlock() {
120         return "<aside></aside>";
121     }
122 
123     protected String getSection1Block(String title) {
124         return "<section name=\"" + title + "\"></section>";
125     }
126 
127     protected String getSection2Block(String title) {
128         return "<subsection name=\"" + title + "\"></subsection>";
129     }
130 
131     protected String getSection3Block(String title) {
132         return "<header>" + EOL + "<h3>" + title + "</h3></header>";
133     }
134 
135     protected String getSection4Block(String title) {
136         return "<header>" + EOL + "<h4>" + title + "</h4></header>";
137     }
138 
139     protected String getSection5Block(String title) {
140         return "<header>" + EOL + "<h5>" + title + "</h5></header>";
141     }
142 
143     protected String getSection6Block(String title) {
144         return "<header>" + EOL + "<h6>" + title + "</h6></header>";
145     }
146 
147     protected String getHeaderBlock() {
148         return "<header></header>";
149     }
150 
151     protected String getContentBlock() {
152         return "<main>" + EOL + "<div class=\"content\"></div></main>";
153     }
154 
155     protected String getFooterBlock() {
156         return "<footer></footer>";
157     }
158 
159     protected String getListBlock(String item) {
160         return "<ul>\n<li>" + item + "</li></ul>";
161     }
162 
163     protected String getNumberedListBlock(String item) {
164         return "<ol style=\"list-style-type: lower-roman;\">\n<li>" + item + "</li></ol>";
165     }
166 
167     protected String getDefinitionListBlock(String definum, String definition) {
168         return "<dl>\n<dt>" + definum + "</dt>\n<dd>" + definition + "</dd></dl>";
169     }
170 
171     protected String getFigureBlock(String source, String caption) {
172         String figureBlock = "<figure><img src=\"" + escapeHTML(source) + "\" />";
173         if (caption != null) {
174             figureBlock += "<figcaption>" + caption + "</figcaption>";
175         }
176         figureBlock += "</figure>";
177         return figureBlock;
178     }
179 
180     protected String getTableBlock(String cell, String caption) {
181         return "<table border=\"0\"><caption>" + caption + "</caption>\n<tr>\n"
182                 + "<td style=\"text-align: center;\">" + cell + "</td>\n"
183                 + "<td>" + cell + "</td>\n"
184                 + "<td>" + cell + "</td></tr></table>";
185     }
186 
187     @Override
188     protected String getTableWithHeaderBlock(String... rowPrefixes) {
189         return "<table border=\"0\">\n<tr>\n<th>" + rowPrefixes[0] + "0</th>\n<th>" + rowPrefixes[0] + "1</th>\n<th>"
190                 + rowPrefixes[0] + "2</th></tr>\n"
191                 + "<tr>\n<td style=\"text-align: left;\">" + rowPrefixes[1]
192                 + "0</td>\n<td style=\"text-align: right;\">" + rowPrefixes[1]
193                 + "1</td>\n<td style=\"text-align: center;\">" + rowPrefixes[1] + "2</td></tr>\n"
194                 + "<tr>\n<td style=\"text-align: left;\">" + rowPrefixes[2]
195                 + "0</td>\n<td style=\"text-align: right;\">" + rowPrefixes[2]
196                 + "1</td>\n<td style=\"text-align: center;\">" + rowPrefixes[2] + "2</td></tr>"
197                 + "</table>";
198     }
199 
200     protected String getParagraphBlock(String text) {
201         return "<p>" + text + "</p>";
202     }
203 
204     protected String getDataBlock(String value, String text) {
205         return "<data value=\"" + value + "\">" + text + "</data>";
206     }
207 
208     protected String getTimeBlock(String datetime, String text) {
209         return "<time datetime=\"" + datetime + "\">" + text + "</time>";
210     }
211 
212     protected String getAddressBlock(String text) {
213         return "<address>" + text + "</address>";
214     }
215 
216     protected String getBlockquoteBlock(String text) {
217         return "<blockquote>" + text + "</blockquote>";
218     }
219 
220     protected String getDivisionBlock(String text) {
221         return "<div>" + text + "</div>";
222     }
223 
224     protected String getVerbatimBlock(String text) {
225         return "<pre>" + text + "</pre>";
226     }
227 
228     protected String getVerbatimSourceBlock(String text) {
229         return "<source>" + text + "</source>";
230     }
231 
232     protected String getHorizontalRuleBlock() {
233         return "<hr />";
234     }
235 
236     protected String getPageBreakBlock() {
237         return "<!-- PB -->";
238     }
239 
240     protected String getAnchorBlock(String anchor) {
241         return "<a id=\"" + anchor + "\">" + anchor + "</a>";
242     }
243 
244     protected String getLinkBlock(String link, String text) {
245         return "<a href=\"" + link + "\">" + text + "</a>";
246     }
247 
248     protected String getInlineBlock(String text) {
249         return text;
250     }
251 
252     protected String getInlineItalicBlock(String text) {
253         return "<i>" + text + "</i>";
254     }
255 
256     protected String getInlineBoldBlock(String text) {
257         return "<b>" + text + "</b>";
258     }
259 
260     protected String getInlineCodeBlock(String text) {
261         return "<code>" + text + "</code>";
262     }
263 
264     protected String getInlineDeleteBlock(String text) {
265         return "<del>" + text + "</del>";
266     }
267 
268     protected String getLineBreakBlock() {
269         return "<br />";
270     }
271 
272     protected String getLineBreakOpportunityBlock() {
273         return "<wbr />";
274     }
275 
276     protected String getNonBreakingSpaceBlock() {
277         return "&#160;";
278     }
279 
280     protected String getTextBlock(String text) {
281         // TODO: need to be able to retreive those from outside the sink
282         return escapeHTML(text);
283     }
284 
285     protected String getRawTextBlock(String text) {
286         return "~,_=,_-,_+,_*,_[,_],_<,_>,_{,_},_\\";
287     }
288 
289     /**
290      * Test verbatim.
291      */
292     @Test
293     public void verbatimSource() {
294         Writer writer = new StringWriter();
295         try (XdocSink sink = new XdocSink(writer)) {
296             sink.verbatim(null);
297             sink.verbatim_();
298             sink.verbatim(SinkEventAttributeSet.SOURCE);
299             sink.verbatim_();
300         }
301         assertEquals("<pre></pre><source></source>", writer.toString());
302     }
303 
304     /**
305      * Test link.
306      */
307     @Test
308     void linkWithTarget() {
309         Writer writer = new StringWriter();
310         try (XdocSink sink = new XdocSink(writer)) {
311             sink.link("name");
312             sink.link_();
313             SinkEventAttributes attrs = new SinkEventAttributeSet();
314             attrs.addAttribute(Attribute.TARGET, "nirvana");
315             sink.link("name", attrs);
316             sink.link_();
317         }
318         assertEquals("<a href=\"name\"></a><a target=\"nirvana\" href=\"name\"></a>", writer.toString());
319     }
320 
321     @Override
322     protected String getCommentBlock(String text) {
323         return "<!--" + toXmlComment(text) + "-->";
324     }
325 
326     @Override
327     protected String getCommentBlockFollowedByParagraph(String comment, String paragraph) {
328         return getCommentBlock(comment) + getParagraphBlock(paragraph); // no line break in between
329     }
330 }