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