View Javadoc

1   package org.apache.maven.doxia.module.fo;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.StringReader;
24  import java.io.Writer;
25  
26  import org.apache.maven.doxia.document.DocumentMeta;
27  import org.apache.maven.doxia.document.DocumentModel;
28  import org.apache.maven.doxia.document.DocumentTOC;
29  import org.apache.maven.doxia.document.DocumentTOCItem;
30  
31  import org.apache.maven.doxia.parser.XhtmlBaseParser;
32  import org.apache.maven.doxia.sink.Sink;
33  import org.apache.maven.doxia.sink.AbstractSinkTest;
34  import org.apache.maven.doxia.sink.SinkTestDocument;
35  
36  /**
37   * <code>FO Sink</code> Test case.
38   *
39   * @version $Id: FoSinkTest.java 946933 2010-05-21 08:39:07Z ltheussl $
40   */
41  public class FoSinkTest
42      extends AbstractSinkTest
43  {
44      private FoConfiguration config;
45  
46      // ----------------------------------------------------------------------
47      // Specific test methods
48      // ----------------------------------------------------------------------
49  
50      /**
51       * Uses fop to generate a pdf from a test document.
52       * @throws Exception If the conversion fails.
53       */
54      public void testConvertFO2PDF()
55          throws Exception
56      {
57          String fileName = "test";
58          // first create fo
59          FoSink fosink = new FoSink( getTestWriter( fileName ) );
60          fosink.beginDocument();
61          SinkTestDocument.generate( fosink );
62          fosink.endDocument();
63          fosink.close();
64  
65          // then generate PDF
66          fo2pdf( fileName );
67      }
68  
69      /**
70       * Uses fop to generate an aggregated pdf from two test documents.
71       * @throws Exception If the conversion fails.
72       */
73      public void testAggregateMode()
74          throws Exception
75      {
76          FoAggregateSink fosink = new FoAggregateSink( getTestWriter( "aggregate" ) );
77  
78          fosink.setDocumentModel( getModel() );
79  
80          fosink.beginDocument();
81  
82          fosink.coverPage();
83  
84          fosink.toc();
85  
86          fosink.setDocumentName( "doc1" );
87          fosink.setDocumentTitle( "Document 1" );
88          SinkTestDocument.generate( fosink );
89  
90          // re-use the same source
91          fosink.setDocumentName( "doc2" );
92          fosink.setDocumentTitle( "Document 2" );
93          SinkTestDocument.generate( fosink );
94  
95          fosink.endDocument();
96  
97          // then generate PDF
98          fo2pdf( "aggregate" );
99      }
100 
101     private DocumentModel getModel()
102     {
103         DocumentModel model = new DocumentModel();
104         model.setToc( getToc() );
105         model.setMeta( getMeta() );
106         return model;
107     }
108 
109     private DocumentMeta getMeta()
110     {
111         DocumentMeta meta = new DocumentMeta();
112         meta.setAuthor( "The Apache Maven Project" );
113         meta.setTitle( "Doxia FO Sink" );
114         return meta;
115     }
116 
117     private DocumentTOC getToc()
118     {
119         DocumentTOCItem item1 = new DocumentTOCItem();
120         item1.setName( "First document" );
121         item1.setRef( "doc1.apt" );
122 
123         DocumentTOCItem item2 = new DocumentTOCItem();
124         item2.setName( "Second document" );
125         item2.setRef( "doc2.xml" );
126 
127         DocumentTOC toc = new DocumentTOC();
128         toc.setName( "What's in here" );
129         toc.addItem( item1 );
130         toc.addItem( item2 );
131 
132         return toc;
133     }
134 
135     // ----------------------------------------------------------------------
136     // Abstract methods the individual SinkTests must provide
137     // ----------------------------------------------------------------------
138 
139     /** {@inheritDoc} */
140     protected String outputExtension()
141     {
142         return "fo";
143     }
144 
145     /** {@inheritDoc} */
146     protected Sink createSink( Writer writer )
147     {
148         return new FoSink( writer );
149     }
150 
151     /** {@inheritDoc} */
152     protected boolean isXmlSink()
153     {
154         return true;
155     }
156 
157     /** {@inheritDoc} */
158     protected String getTitleBlock( String title )
159     {
160         String attribs = getConfig().getAttributeString( "doc.header.title" );
161         return EOL + "<fo:block" + attribs + ">" + title + "</fo:block>" + EOL;
162     }
163 
164     /** {@inheritDoc} */
165     protected String getAuthorBlock( String author )
166     {
167         String attribs = getConfig().getAttributeString( "doc.header.author" );
168         return EOL + "<fo:block" + attribs + ">" + author + "</fo:block>" + EOL;
169     }
170 
171     /** {@inheritDoc} */
172     protected String getDateBlock( String date )
173     {
174         String attribs = getConfig().getAttributeString( "doc.header.date" );
175         return EOL + "<fo:block" + attribs + ">" + date + "</fo:block>" + EOL;
176     }
177 
178     // TODO
179     protected String getHeadBlock()
180     {
181         return "";
182     }
183 
184     // TODO: remove
185     public void testHead()
186     {
187         String expected = "";
188         assertEquals( "Wrong head!", expected, getHeadBlock() );
189     }
190 
191     /** {@inheritDoc} */
192     protected String getBodyBlock()
193     {
194         return EOL + "</fo:flow>" + EOL + "</fo:page-sequence>" + EOL + "</fo:root>" + EOL;
195     }
196 
197     /** {@inheritDoc} */
198     protected String getSectionTitleBlock( String title )
199     {
200         return title;
201     }
202 
203     /** {@inheritDoc} */
204     protected String getSection1Block( String title )
205     {
206         String attribs = getConfig().getAttributeString( "body.text" );
207         String attrib2 = getConfig().getAttributeString( "body.h1" );
208         return EOL + EOL + "<fo:block" + attribs + ">" + EOL + EOL + "<fo:block" + attrib2 + ">1   " + title
209             + "</fo:block>" + EOL + "</fo:block>" + EOL;
210     }
211 
212     /** {@inheritDoc} */
213     protected String getSection2Block( String title )
214     {
215         String attribs = getConfig().getAttributeString( "body.text" );
216         String attrib2 = getConfig().getAttributeString( "body.h2" );
217         return EOL + EOL + "<fo:block" + attribs + ">" + EOL + EOL + "<fo:block" + attrib2 + ">0.1   " + title
218             + "</fo:block>" + EOL + "</fo:block>" + EOL;
219     }
220 
221     /** {@inheritDoc} */
222     protected String getSection3Block( String title )
223     {
224         String attribs = getConfig().getAttributeString( "body.text" );
225         String attrib2 = getConfig().getAttributeString( "body.h3" );
226         return EOL + EOL + "<fo:block" + attribs + ">" + EOL + EOL + "<fo:block" + attrib2 + ">0.0.1   " + title
227             + "</fo:block>" + EOL + "</fo:block>" + EOL;
228     }
229 
230     /** {@inheritDoc} */
231     protected String getSection4Block( String title )
232     {
233         String attribs = getConfig().getAttributeString( "body.text" );
234         String attrib2 = getConfig().getAttributeString( "body.h4" );
235         return EOL + EOL + "<fo:block" + attribs + ">" + EOL + EOL + "<fo:block" + attrib2 + ">" + title
236             + "</fo:block>" + EOL + "</fo:block>" + EOL;
237     }
238 
239     /** {@inheritDoc} */
240     protected String getSection5Block( String title )
241     {
242         String attribs = getConfig().getAttributeString( "body.text" );
243         String attrib2 = getConfig().getAttributeString( "body.h5" );
244         return EOL + EOL + "<fo:block" + attribs + ">" + EOL + EOL + "<fo:block" + attrib2 + ">" + title
245             + "</fo:block>" + EOL + "</fo:block>" + EOL;
246     }
247 
248     /** {@inheritDoc} */
249     protected String getListBlock( String item )
250     {
251         String attribs = getConfig().getAttributeString( "list" );
252         String itemAttribs = getConfig().getAttributeString( "list.item" );
253         return EOL + EOL + "<fo:list-block" + attribs + ">" + EOL + "<fo:list-item" + itemAttribs
254             + "><fo:list-item-label><fo:block>&#8226;</fo:block></fo:list-item-label>" + EOL + EOL
255             + "<fo:list-item-body" + itemAttribs + ">" + EOL + "<fo:block>" + item + "</fo:block>" + EOL
256             + "</fo:list-item-body>" + EOL + "</fo:list-item>" + EOL + "</fo:list-block>" + EOL;
257     }
258 
259     /** {@inheritDoc} */
260     protected String getNumberedListBlock( String item )
261     {
262         String attribs = getConfig().getAttributeString( "list" );
263         String itemAttribs = getConfig().getAttributeString( "list.item" );
264         return EOL + EOL + "<fo:list-block" + attribs + ">" + EOL + "<fo:list-item" + itemAttribs + ">" + EOL
265             + "<fo:list-item-label>" + EOL + "<fo:block>i.</fo:block>" + EOL + "</fo:list-item-label>" + EOL + EOL
266             + "<fo:list-item-body" + itemAttribs + ">" + EOL + "<fo:block>" + item + "</fo:block>" + EOL
267             + "</fo:list-item-body>" + EOL + "</fo:list-item>" + EOL + "</fo:list-block>" + EOL;
268     }
269 
270     /** {@inheritDoc} */
271     protected String getDefinitionListBlock( String definum, String definition )
272     {
273         String dlAtts = getConfig().getAttributeString( "dl" );
274         String dtAtts = getConfig().getAttributeString( "dt" );
275         String ddAtts = getConfig().getAttributeString( "dd" );
276         return EOL + EOL + "<fo:block" + dlAtts + ">" + EOL + "<fo:block" + dtAtts + ">" + definum + "</fo:block>"
277             + EOL + EOL + EOL + "<fo:block" + ddAtts + ">" + definition + "</fo:block>" + EOL + "</fo:block>"
278             + EOL;
279     }
280 
281     /** {@inheritDoc} */
282     protected String getFigureBlock( String source, String caption )
283     {
284         String dlAtts = getConfig().getAttributeString( "figure.display" );
285         String dtAtts = getConfig().getAttributeString( "figure.graphics" );
286         String ddAtts = getConfig().getAttributeString( "figure.caption" );
287         return EOL + EOL + "<fo:block" + dlAtts + "><fo:external-graphic" + " src=\"" + source + "\"" + dtAtts
288             + "/>" + EOL + EOL + "<fo:block" + ddAtts + ">" + caption + "</fo:block>" + EOL + "</fo:block>" + EOL;
289     }
290 
291     /** {@inheritDoc} */
292     protected String getTableBlock( String cell, String caption )
293     {
294         String dlAtts = getConfig().getAttributeString( "table.padding" );
295         String dtAtts = getConfig().getAttributeString( "table.layout" );
296         String ddAtts = getConfig().getAttributeString( "table.body.row" );
297         // String deAtts = getConfig().getAttributeString( "table.body.cell" );
298 
299         return EOL + EOL + "<fo:block" + dlAtts + ">" + EOL + "<fo:table" + dtAtts + ">" + EOL
300             + "<fo:table-column column-width=\"proportional-column-width(1)\"/>" + EOL + EOL + "<fo:table-body>"
301             + EOL + "<fo:table-row" + ddAtts
302             + "><fo:table-cell column-number=\"1\" padding-after=\"1.5pt\" padding-end=\"5pt\" "
303             + "keep-together.within-column=\"always\" padding-start=\"2.5pt\" "
304             + "background-color=\"#eeeeee\" padding-before=\"4pt\">" + EOL + "<fo:block line-height=\"1.2em\" "
305             + "text-align=\"center\" font-family=\"Helvetica,sans-serif\" font-size=\"9pt\">" + EOL + cell
306             + "</fo:block>" + EOL + "</fo:table-cell>" + EOL + "</fo:table-row>" + EOL + "</fo:table-body>" + EOL
307             + "</fo:table>" + EOL + "</fo:block>" + EOL + EOL
308             + "<fo:block white-space-collapse=\"true\" space-after=\"6pt\" space-before=\"3pt\" "
309             + "font-family=\"Garamond,serif\" line-height=\"12pt\" text-align=\"center\" font-size=\"11pt\">"
310             + "Table_caption</fo:block>" + EOL;
311     }
312 
313     /** {@inheritDoc} */
314     protected String getParagraphBlock( String text )
315     {
316         String attribs = getConfig().getAttributeString( "normal.paragraph" );
317         return EOL + "<fo:block" + attribs + ">" + text + "</fo:block>" + EOL;
318     }
319 
320     /** {@inheritDoc} */
321     protected String getVerbatimBlock( String text )
322     {
323         String attribs = getConfig().getAttributeString( "body.source" );
324         return EOL + "<fo:block" + attribs + ">" + text + "</fo:block>" + EOL;
325     }
326 
327     /** {@inheritDoc} */
328     protected String getHorizontalRuleBlock()
329     {
330         String attribs = getConfig().getAttributeString( "body.rule" );
331         return EOL + EOL + "<fo:block>" + EOL + "<fo:leader" + attribs + " /></fo:block>" + EOL;
332     }
333 
334     /** {@inheritDoc} */
335     protected String getPageBreakBlock()
336     {
337         return EOL + "<fo:block break-before=\"page\" />" + EOL;
338     }
339 
340     /** {@inheritDoc} */
341     protected String getAnchorBlock( String anchor )
342     {
343         // all anchors get '#' pre-pended
344         return EOL + "<fo:inline id=\"#" + anchor + "\">" + anchor + "</fo:inline>";
345     }
346 
347     /** {@inheritDoc} */
348     protected String getLinkBlock( String link, String text )
349     {
350         String attribs = getConfig().getAttributeString( "href.internal" );
351         return EOL + "<fo:basic-link internal-destination=\"" + link + "\">" + EOL + "<fo:inline" + attribs + ">"
352             + text + "</fo:inline></fo:basic-link>";
353     }
354 
355     /** {@inheritDoc} */
356     protected String getItalicBlock( String text )
357     {
358         String attribs = getConfig().getAttributeString( "italic" );
359         return EOL + "<fo:inline" + attribs + ">" + text + "</fo:inline>";
360     }
361 
362     /** {@inheritDoc} */
363     protected String getBoldBlock( String text )
364     {
365         String attribs = getConfig().getAttributeString( "bold" );
366         return EOL + "<fo:inline" + attribs + ">" + text + "</fo:inline>";
367     }
368 
369     /** {@inheritDoc} */
370     protected String getMonospacedBlock( String text )
371     {
372         String attribs = getConfig().getAttributeString( "monospace" );
373         return EOL + "<fo:inline" + attribs + ">" + text + "</fo:inline>";
374     }
375 
376     /** {@inheritDoc} */
377     protected String getLineBreakBlock()
378     {
379         return EOL + EOL + "<fo:block />";
380     }
381 
382     /** {@inheritDoc} */
383     protected String getNonBreakingSpaceBlock()
384     {
385         return "&#160;";
386     }
387 
388     /** {@inheritDoc} */
389     protected String getTextBlock( String text )
390     {
391         return FoSink.escaped( text, false );
392     }
393 
394     /** {@inheritDoc} */
395     protected String getRawTextBlock( String text )
396     {
397         return text;
398     }
399 
400     // ----------------------------------------------------------------------
401     // Auxiliary methods
402     // ----------------------------------------------------------------------
403 
404     private void fo2pdf( String baseName )
405         throws Exception
406     {
407         // File outputDirectory = new File( getBasedirFile(), getOutputDir() );
408         File outputDirectory = new File( getBasedir(), outputBaseDir() + getOutputDir() );
409         File resourceDirectory = new File( getBasedirFile(), "target/test-classes" );
410         File foFile = new File( outputDirectory, baseName + "." + outputExtension() );
411         File pdfFile = new File( outputDirectory, baseName + ".pdf" );
412         FoUtils.convertFO2PDF( foFile, pdfFile, resourceDirectory.getCanonicalPath() );
413     }
414 
415     private FoConfiguration getConfig()
416     {
417         if ( config == null )
418         {
419             config = ( (FoSink) getSink() ).getFoConfiguration();
420         }
421 
422         return config;
423     }
424 
425     /** {@inheritDoc} */
426     protected String getCommentBlock( String text )
427     {
428         return "<!-- Simple comment with - - - - -->";
429     }
430 
431     /**
432      * DOXIA-357
433      *
434      * @throws Exception if any
435      */
436     public void testTableCaption()
437         throws Exception
438     {
439         StringBuffer html = new StringBuffer();
440         html.append( "<table>" ).append( EOL );
441         html.append( "<caption>caption table</caption>" ).append( EOL );
442         html.append( "<tr>" ).append( EOL );
443         html.append( "<td>foo</td>" ).append( EOL );
444         html.append( "</tr>" ).append( EOL );
445         html.append( "<tr>" ).append( EOL );
446         html.append( "<td>bar</td>" ).append( EOL );
447         html.append( "</tr>" ).append( EOL );
448         html.append( "</table>" ).append( EOL );
449 
450         String fileName = "testTableCaption";
451 
452         // first create fo
453         FoSink fosink = new FoSink( getTestWriter( fileName ) );
454         fosink.beginDocument();
455         SinkTestDocument.generateHead( fosink );
456 
457         fosink.body();
458         XhtmlBaseParser parser = new XhtmlBaseParser();
459         parser.parse( new StringReader( html.toString() ), fosink );
460         fosink.body_();
461 
462         fosink.endDocument();
463         fosink.close();
464 
465         // then generate PDF
466         fo2pdf( fileName );
467     }
468 
469     /**
470      * @throws Exception if any
471      */
472     public void testNestedTables()
473         throws Exception
474     {
475         StringBuffer html = new StringBuffer();
476         html.append( "<table>" ).append( EOL );
477         html.append( "<caption>first caption</caption>" ).append( EOL );
478         html.append( "<tr>" ).append( EOL );
479         html.append( "<td>foo</td>" ).append( EOL );
480         html.append( "</tr>" ).append( EOL );
481         html.append( "<tr>" ).append( EOL );
482         html.append( "<td>" ).append( EOL );
483 
484         html.append( "<table>" ).append( EOL );
485         html.append( "<caption>second caption</caption>" ).append( EOL );
486         html.append( "<tr>" ).append( EOL );
487         html.append( "<td>foo</td>" ).append( EOL );
488         html.append( "<td>bar</td>" ).append( EOL );
489         html.append( "</tr>" ).append( EOL );
490         html.append( "<tr>" ).append( EOL );
491         html.append( "<td>foo1</td>" ).append( EOL );
492         html.append( "<td>" ).append( EOL );
493 
494         html.append( "<table>" ).append( EOL );
495         html.append( "<caption>third caption</caption>" ).append( EOL );
496         html.append( "<tr>" ).append( EOL );
497         html.append( "<td>foo1</td>" ).append( EOL );
498         html.append( "<td>bar1</td>" ).append( EOL );
499         html.append( "</tr>" ).append( EOL );
500         html.append( "</table>" ).append( EOL );
501         html.append( "</td>" ).append( EOL );
502 
503         html.append( "</tr>" ).append( EOL );
504         html.append( "</table>" ).append( EOL );
505 
506         html.append( "</td>" ).append( EOL );
507         html.append( "</tr>" ).append( EOL );
508         html.append( "</table>" ).append( EOL );
509 
510         String fileName = "testNestedTables";
511 
512         // first create fo
513         FoSink fosink = new FoSink( getTestWriter( fileName ) );
514         fosink.beginDocument();
515         SinkTestDocument.generateHead( fosink );
516 
517         fosink.body();
518         XhtmlBaseParser parser = new XhtmlBaseParser();
519         parser.parse( new StringReader( html.toString() ), fosink );
520         fosink.body_();
521 
522         fosink.endDocument();
523         fosink.close();
524 
525         // then generate PDF
526         fo2pdf( fileName );
527     }
528 }