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