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