View Javadoc
1   package org.apache.maven.doxia.sink;
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.IOException;
23  import java.io.Writer;
24  
25  /**
26   * A simple text-based implementation of the <code>Sink</code> interface.
27   * Useful for testing purposes.
28   */
29  public class TextSink
30      extends AbstractSink
31  {
32  
33      /** For writing the result. */
34      private final Writer out;
35  
36      /** Constructor.
37       * @param writer The writer for writing the result.
38       */
39      public TextSink( Writer writer )
40      {
41          this.out = writer;
42      }
43  
44  
45      /** {@inheritDoc} */
46      public void head()
47      {
48          writeln( "begin:head" );
49      }
50  
51      /** {@inheritDoc} */
52      public void head_()
53      {
54          writeln( "end:head" );
55      }
56  
57      /** {@inheritDoc} */
58      public void body()
59      {
60          writeln( "begin:body" );
61      }
62  
63      /** {@inheritDoc} */
64      public void body_()
65      {
66          writeln( "end:body" );
67      }
68  
69      /** {@inheritDoc} */
70      public void section1()
71      {
72          write( "begin:section1" );
73      }
74  
75      /** {@inheritDoc} */
76      public void section1_()
77      {
78          writeln( "end:section1" );
79      }
80  
81      /** {@inheritDoc} */
82      public void section2()
83      {
84          write( "begin:section2" );
85      }
86  
87      /** {@inheritDoc} */
88      public void section2_()
89      {
90          writeln( "end:section2" );
91      }
92  
93      /** {@inheritDoc} */
94      public void section3()
95      {
96          write( "begin:section3" );
97      }
98  
99      /** {@inheritDoc} */
100     public void section3_()
101     {
102         writeln( "end:section3" );
103     }
104 
105     /** {@inheritDoc} */
106     public void section4()
107     {
108         write( "begin:section4" );
109     }
110 
111     /** {@inheritDoc} */
112     public void section4_()
113     {
114         writeln( "end:section4" );
115     }
116 
117     /** {@inheritDoc} */
118     public void section5()
119     {
120         write( "begin:section5" );
121     }
122 
123     /** {@inheritDoc} */
124     public void section5_()
125     {
126         writeln( "end:section5" );
127     }
128 
129     /** {@inheritDoc} */
130     public void list()
131     {
132         writeln( "begin:list" );
133     }
134 
135     /** {@inheritDoc} */
136     public void list_()
137     {
138         writeln( "end:list" );
139     }
140 
141     /** {@inheritDoc} */
142     public void listItem()
143     {
144         write( "begin:listItem" );
145     }
146 
147     /** {@inheritDoc} */
148     public void listItem_()
149     {
150         writeln( "end:listItem" );
151     }
152 
153     /** {@inheritDoc} */
154     public void numberedList( int numbering )
155     {
156         writeln( "begin:numberedList, numbering: " + numbering );
157     }
158 
159     /** {@inheritDoc} */
160     public void numberedList_()
161     {
162         writeln( "end:numberedList" );
163     }
164 
165     /** {@inheritDoc} */
166     public void numberedListItem()
167     {
168         write( "begin:numberedListItem" );
169     }
170 
171     /** {@inheritDoc} */
172     public void numberedListItem_()
173     {
174         writeln( "end:numberedListItem" );
175     }
176 
177     /** {@inheritDoc} */
178     public void definitionList()
179     {
180         writeln( "begin:definitionList" );
181     }
182 
183     /** {@inheritDoc} */
184     public void definitionList_()
185     {
186         writeln( "end:definitionList" );
187     }
188 
189     /** {@inheritDoc} */
190     public void definitionListItem()
191     {
192         write( "begin:definitionListItem" );
193     }
194 
195     /** {@inheritDoc} */
196     public void definitionListItem_()
197     {
198         writeln( "end:definitionListItem" );
199     }
200 
201     /** {@inheritDoc} */
202     public void definition()
203     {
204         write( "begin:definition" );
205     }
206 
207     /** {@inheritDoc} */
208     public void definition_()
209     {
210         writeln( "end:definition" );
211     }
212 
213     /** {@inheritDoc} */
214     public void figure()
215     {
216         write( "begin:figure" );
217     }
218 
219     /** {@inheritDoc} */
220     public void figure_()
221     {
222         writeln( "end:figure" );
223     }
224 
225     /** {@inheritDoc} */
226     public void table()
227     {
228         writeln( "begin:table" );
229     }
230 
231     /** {@inheritDoc} */
232     public void table_()
233     {
234         writeln( "end:table" );
235     }
236 
237     /** {@inheritDoc} */
238     public void tableRows( int[] justification, boolean grid )
239     {
240         writeln( "begin:tableRows" );
241     }
242 
243     /** {@inheritDoc} */
244     public void tableRows_()
245     {
246         writeln( "end:tableRows" );
247     }
248 
249     /** {@inheritDoc} */
250     public void tableRow()
251     {
252         write( "begin:tableRow" );
253     }
254 
255     /** {@inheritDoc} */
256     public void tableRow_()
257     {
258         writeln( "end:tableRow" );
259     }
260 
261     /** {@inheritDoc} */
262     public void title()
263     {
264         write( "begin:title" );
265     }
266 
267     /** {@inheritDoc} */
268     public void title_()
269     {
270         writeln( "end:title" );
271     }
272 
273     /** {@inheritDoc} */
274     public void author()
275     {
276         write( "begin:author" );
277     }
278 
279     /** {@inheritDoc} */
280     public void author_()
281     {
282         writeln( "end:author" );
283     }
284 
285     /** {@inheritDoc} */
286     public void date()
287     {
288         write( "begin:date" );
289     }
290 
291     /** {@inheritDoc} */
292     public void date_()
293     {
294         writeln( "end:date" );
295     }
296 
297     /** {@inheritDoc} */
298     public void sectionTitle()
299     {
300         write( "begin:sectionTitle" );
301     }
302 
303     /** {@inheritDoc} */
304     public void sectionTitle_()
305     {
306         writeln( "end:sectionTitle" );
307     }
308 
309     /** {@inheritDoc} */
310     public void sectionTitle1()
311     {
312         write( "begin:sectionTitle1" );
313     }
314 
315     /** {@inheritDoc} */
316     public void sectionTitle1_()
317     {
318         writeln( "end:sectionTitle1" );
319     }
320 
321     /** {@inheritDoc} */
322     public void sectionTitle2()
323     {
324         write( "begin:sectionTitle2" );
325     }
326 
327     /** {@inheritDoc} */
328     public void sectionTitle2_()
329     {
330         writeln( "end:sectionTitle2" );
331     }
332 
333     /** {@inheritDoc} */
334     public void sectionTitle3()
335     {
336         write( "begin:sectionTitle3" );
337     }
338 
339     /** {@inheritDoc} */
340     public void sectionTitle3_()
341     {
342         writeln( "end:sectionTitle3" );
343     }
344 
345     /** {@inheritDoc} */
346     public void sectionTitle4()
347     {
348         write( "begin:sectionTitle4" );
349     }
350 
351     /** {@inheritDoc} */
352     public void sectionTitle4_()
353     {
354         writeln( "end:sectionTitle4" );
355     }
356 
357     /** {@inheritDoc} */
358     public void sectionTitle5()
359     {
360         write( "begin:sectionTitle5" );
361     }
362 
363     /** {@inheritDoc} */
364     public void sectionTitle5_()
365     {
366         writeln( "end:sectionTitle5" );
367     }
368 
369     /** {@inheritDoc} */
370     public void paragraph()
371     {
372         write( "begin:paragraph" );
373     }
374 
375     /** {@inheritDoc} */
376     public void paragraph_()
377     {
378         writeln( "end:paragraph" );
379     }
380 
381     /** {@inheritDoc} */
382     public void verbatim( boolean boxed )
383     {
384         write( "begin:verbatim, boxed: " + boxed );
385     }
386 
387     /** {@inheritDoc} */
388     public void verbatim_()
389     {
390         writeln( "end:verbatim" );
391     }
392 
393     /** {@inheritDoc} */
394     public void definedTerm()
395     {
396         write( "begin:definedTerm" );
397     }
398 
399     /** {@inheritDoc} */
400     public void definedTerm_()
401     {
402         writeln( "end:definedTerm" );
403     }
404 
405     /** {@inheritDoc} */
406     public void figureCaption()
407     {
408         write( "begin:figureCaption" );
409     }
410 
411     /** {@inheritDoc} */
412     public void figureCaption_()
413     {
414         writeln( "end:figureCaption" );
415     }
416 
417     /** {@inheritDoc} */
418     public void tableCell()
419     {
420         write( "begin:tableCell" );
421     }
422 
423     /** {@inheritDoc} */
424     public void tableCell( String width )
425     {
426         write( "begin:tableCell, width: " + width );
427     }
428 
429     /** {@inheritDoc} */
430     public void tableCell_()
431     {
432         writeln( "end:tableCell" );
433     }
434 
435     /** {@inheritDoc} */
436     public void tableHeaderCell()
437     {
438         write( "begin:tableHeaderCell" );
439     }
440 
441     /** {@inheritDoc} */
442     public void tableHeaderCell( String width )
443     {
444         write( "begin:tableHeaderCell, width: " + width );
445     }
446 
447     /** {@inheritDoc} */
448     public void tableHeaderCell_()
449     {
450         writeln( "end:tableHeaderCell" );
451     }
452 
453     /** {@inheritDoc} */
454     public void tableCaption()
455     {
456         write( "begin:tableCaption" );
457     }
458 
459     /** {@inheritDoc} */
460     public void tableCaption_()
461     {
462         writeln( "end:tableCaption" );
463     }
464 
465     /** {@inheritDoc} */
466     public void figureGraphics( String name )
467     {
468         write( "figureGraphics, name: " + name );
469     }
470 
471     /** {@inheritDoc} */
472     public void horizontalRule()
473     {
474         write( "horizontalRule" );
475     }
476 
477     /** {@inheritDoc} */
478     public void pageBreak()
479     {
480         write( "pageBreak" );
481     }
482 
483     /** {@inheritDoc} */
484     public void anchor( String name )
485     {
486         write( "begin:anchor, name: " + name  );
487     }
488 
489     /** {@inheritDoc} */
490     public void anchor_()
491     {
492         writeln( "end:anchor" );
493     }
494 
495     /** {@inheritDoc} */
496     public void link( String name )
497     {
498         write( "begin:link, name: " + name  );
499     }
500 
501     /** {@inheritDoc} */
502     public void link_()
503     {
504         writeln( "end:link" );
505     }
506 
507     /** {@inheritDoc} */
508     public void italic()
509     {
510         write( "begin:italic" );
511     }
512 
513     /** {@inheritDoc} */
514     public void italic_()
515     {
516         writeln( "end:italic" );
517     }
518 
519     /** {@inheritDoc} */
520     public void bold()
521     {
522         write( "begin:bold" );
523     }
524 
525     /** {@inheritDoc} */
526     public void bold_()
527     {
528         writeln( "end:bold" );
529     }
530 
531     /** {@inheritDoc} */
532     public void monospaced()
533     {
534         write( "begin:monospaced" );
535     }
536 
537     /** {@inheritDoc} */
538     public void monospaced_()
539     {
540         writeln( "end:monospaced" );
541     }
542 
543     /** {@inheritDoc} */
544     public void lineBreak()
545     {
546         write( "lineBreak" );
547     }
548 
549     /** {@inheritDoc} */
550     public void nonBreakingSpace()
551     {
552         write( "nonBreakingSpace" );
553     }
554 
555     /** {@inheritDoc} */
556     public void text( String text )
557     {
558         write( "text: " + text );
559     }
560 
561     /** {@inheritDoc} */
562     public void rawText( String text )
563     {
564         write( "rawText: " + text );
565     }
566 
567     /** {@inheritDoc} */
568     public void comment( String comment )
569     {
570         write( "comment: " + comment );
571     }
572 
573     /** {@inheritDoc} */
574     public void flush()
575     {
576         try
577         {
578             out.flush();
579         }
580         catch ( IOException e )
581         {
582             getLog().warn( "Could not flush sink: " + e.getMessage(), e );
583         }
584     }
585 
586     /** {@inheritDoc} */
587     public void close()
588     {
589         try
590         {
591             out.close();
592         }
593         catch ( IOException e )
594         {
595             getLog().warn( "Could not close sink: " + e.getMessage(), e );
596         }
597     }
598 
599     /** {@inheritDoc} */
600     public void head( SinkEventAttributes attributes )
601     {
602         head();
603     }
604 
605     /** {@inheritDoc} */
606     public void title( SinkEventAttributes attributes )
607     {
608         title();
609     }
610 
611     /** {@inheritDoc} */
612     public void author( SinkEventAttributes attributes )
613     {
614         author();
615     }
616 
617     /** {@inheritDoc} */
618     public void date( SinkEventAttributes attributes )
619     {
620         date();
621     }
622 
623     /** {@inheritDoc} */
624     public void body( SinkEventAttributes attributes )
625     {
626         body();
627     }
628 
629     /** {@inheritDoc} */
630     public void section( int level, SinkEventAttributes attributes )
631     {
632         write( "begin:section" + level );
633     }
634 
635     /** {@inheritDoc} */
636     public void section_( int level )
637     {
638         writeln( "end:section" + level );
639     }
640 
641     /** {@inheritDoc} */
642     public void sectionTitle( int level, SinkEventAttributes attributes )
643     {
644         write( "begin:sectionTitle" + level );
645     }
646 
647     /** {@inheritDoc} */
648     public void sectionTitle_( int level )
649     {
650         writeln( "end:sectionTitle" + level );
651     }
652 
653     /** {@inheritDoc} */
654     public void list( SinkEventAttributes attributes )
655     {
656         list();
657     }
658 
659     /** {@inheritDoc} */
660     public void listItem( SinkEventAttributes attributes )
661     {
662         listItem();
663     }
664 
665     /** {@inheritDoc} */
666     public void numberedList( int numbering, SinkEventAttributes attributes )
667     {
668         numberedList( numbering );
669     }
670 
671     /** {@inheritDoc} */
672     public void numberedListItem( SinkEventAttributes attributes )
673     {
674         numberedListItem();
675     }
676 
677     /** {@inheritDoc} */
678     public void definitionList( SinkEventAttributes attributes )
679     {
680         definitionList();
681     }
682 
683     /** {@inheritDoc} */
684     public void definitionListItem( SinkEventAttributes attributes )
685     {
686         definitionListItem();
687     }
688 
689     /** {@inheritDoc} */
690     public void definition( SinkEventAttributes attributes )
691     {
692         definition();
693     }
694 
695     /** {@inheritDoc} */
696     public void definedTerm( SinkEventAttributes attributes )
697     {
698         definedTerm();
699     }
700 
701     /** {@inheritDoc} */
702     public void figure( SinkEventAttributes attributes )
703     {
704         write( "begin:figure" + SinkUtils.getAttributeString( attributes ) );
705     }
706 
707     /** {@inheritDoc} */
708     public void figureCaption( SinkEventAttributes attributes )
709     {
710         figureCaption();
711     }
712 
713     /** {@inheritDoc} */
714     public void figureGraphics( String src, SinkEventAttributes attributes )
715     {
716         figureGraphics( src );
717     }
718 
719     /** {@inheritDoc} */
720     public void table( SinkEventAttributes attributes )
721     {
722         table();
723     }
724 
725     /** {@inheritDoc} */
726     public void tableRow( SinkEventAttributes attributes )
727     {
728         tableRow();
729     }
730 
731     /** {@inheritDoc} */
732     public void tableCell( SinkEventAttributes attributes )
733     {
734         tableCell();
735     }
736 
737     /** {@inheritDoc} */
738     public void tableHeaderCell( SinkEventAttributes attributes )
739     {
740         tableHeaderCell();
741     }
742 
743     /** {@inheritDoc} */
744     public void tableCaption( SinkEventAttributes attributes )
745     {
746         tableCaption();
747     }
748 
749     /** {@inheritDoc} */
750     public void paragraph( SinkEventAttributes attributes )
751     {
752         paragraph();
753     }
754 
755     /** {@inheritDoc} */
756     public void verbatim( SinkEventAttributes attributes )
757     {
758         boolean boxed = false;
759 
760         if ( attributes != null && attributes.isDefined( SinkEventAttributes.DECORATION ) )
761         {
762             boxed = "boxed".equals(
763                 attributes.getAttribute( SinkEventAttributes.DECORATION ).toString() );
764         }
765 
766         verbatim( boxed );
767     }
768 
769     /** {@inheritDoc} */
770     public void horizontalRule( SinkEventAttributes attributes )
771     {
772         horizontalRule();
773     }
774 
775     /** {@inheritDoc} */
776     public void anchor( String name, SinkEventAttributes attributes )
777     {
778         anchor( name );
779     }
780 
781     /** {@inheritDoc} */
782     public void link( String name, SinkEventAttributes attributes )
783     {
784         link( name );
785     }
786 
787     /** {@inheritDoc} */
788     public void lineBreak( SinkEventAttributes attributes )
789     {
790         lineBreak();
791     }
792 
793     /** {@inheritDoc} */
794     public void text( String text, SinkEventAttributes attributes )
795     {
796         text( text );
797     }
798 
799     /** {@inheritDoc} */
800     public void unknown( String name, Object[] requiredParams, SinkEventAttributes attributes )
801     {
802         write( "unknown: " + name );
803     }
804 
805     /**
806      * Writes the given string + EOL.
807      *
808      * @param text The text to write.
809      */
810     private void write( String text )
811     {
812         try
813         {
814             out.write( text + EOL );
815         }
816         catch ( IOException e )
817         {
818             getLog().warn( "Could not write to sink: " + e.getMessage(), e );
819         }
820     }
821 
822     /**
823      * Writes the given string + two EOLs.
824      *
825      * @param text The text to write.
826      */
827     private void writeln( String text )
828     {
829         write( text );
830         write( EOL );
831     }
832 }