View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.doxia.sink.impl;
20  
21  import java.util.LinkedList;
22  import java.util.List;
23  import java.util.Stack;
24  
25  import org.apache.maven.doxia.sink.SinkEventAttributes;
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   */
33  public class WellformednessCheckingSink extends AbstractSink {
34      private final Stack<String> elements = new Stack<>();
35  
36      private final List<String> errors = new LinkedList<>();
37  
38      @Override
39      public void head_() {
40          checkWellformedness("head");
41      }
42  
43      @Override
44      public void body_() {
45          checkWellformedness("body");
46      }
47  
48      @Override
49      public void article_() {
50          checkWellformedness("article");
51      }
52  
53      @Override
54      public void navigation_() {
55          checkWellformedness("navigation");
56      }
57  
58      @Override
59      public void sidebar_() {
60          checkWellformedness("sidebar");
61      }
62  
63      @Override
64      public void header_() {
65          checkWellformedness("header");
66      }
67  
68      @Override
69      public void content_() {
70          checkWellformedness("content");
71      }
72  
73      @Override
74      public void footer_() {
75          checkWellformedness("footer");
76      }
77  
78      @Override
79      public void list_() {
80          checkWellformedness("list");
81      }
82  
83      @Override
84      public void listItem_() {
85          checkWellformedness("listItem");
86      }
87  
88      @Override
89      public void numberedList_() {
90          checkWellformedness("numberedList");
91      }
92  
93      @Override
94      public void numberedListItem_() {
95          checkWellformedness("numberedListItem");
96      }
97  
98      @Override
99      public void definitionList_() {
100         checkWellformedness("definitionList");
101     }
102 
103     @Override
104     public void definitionListItem_() {
105         checkWellformedness("definitionListItem");
106     }
107 
108     @Override
109     public void definition_() {
110         checkWellformedness("definition");
111     }
112 
113     @Override
114     public void figure_() {
115         checkWellformedness("figure");
116     }
117 
118     @Override
119     public void table_() {
120         checkWellformedness("table");
121     }
122 
123     @Override
124     public void tableRows_() {
125         checkWellformedness("tableRows");
126     }
127 
128     @Override
129     public void tableRow_() {
130         checkWellformedness("tableRow");
131     }
132 
133     @Override
134     public void title_() {
135         checkWellformedness("title");
136     }
137 
138     @Override
139     public void author_() {
140         checkWellformedness("author");
141     }
142 
143     @Override
144     public void date_() {
145         checkWellformedness("date");
146     }
147 
148     @Override
149     public void paragraph_() {
150         checkWellformedness("paragraph");
151     }
152 
153     @Override
154     public void data_() {
155         checkWellformedness("data");
156     }
157 
158     @Override
159     public void time_() {
160         checkWellformedness("time");
161     }
162 
163     @Override
164     public void address_() {
165         checkWellformedness("address");
166     }
167 
168     @Override
169     public void blockquote_() {
170         checkWellformedness("blockquote");
171     }
172 
173     @Override
174     public void division_() {
175         checkWellformedness("division");
176     }
177 
178     @Override
179     public void verbatim_() {
180         checkWellformedness("verbatim");
181     }
182 
183     @Override
184     public void definedTerm_() {
185         checkWellformedness("definedTerm");
186     }
187 
188     @Override
189     public void figureCaption_() {
190         checkWellformedness("figureCaption");
191     }
192 
193     @Override
194     public void tableCell_() {
195         checkWellformedness("tableCell");
196     }
197 
198     @Override
199     public void tableHeaderCell_() {
200         checkWellformedness("tableHeaderCell");
201     }
202 
203     @Override
204     public void tableCaption_() {
205         checkWellformedness("tableCaption");
206     }
207 
208     @Override
209     public void pageBreak() {
210         // nop
211     }
212 
213     @Override
214     public void anchor_() {
215         checkWellformedness("anchor");
216     }
217 
218     @Override
219     public void link_() {
220         checkWellformedness("link");
221     }
222 
223     @Override
224     public void inline_() {
225         checkWellformedness("inline");
226     }
227 
228     @Override
229     public void italic() {
230         startElement("italic");
231     }
232 
233     @Override
234     public void italic_() {
235         checkWellformedness("italic");
236     }
237 
238     @Override
239     public void bold() {
240         startElement("bold");
241     }
242 
243     @Override
244     public void bold_() {
245         checkWellformedness("bold");
246     }
247 
248     @Override
249     public void monospaced() {
250         startElement("monospaced");
251     }
252 
253     @Override
254     public void monospaced_() {
255         checkWellformedness("monospaced");
256     }
257 
258     @Override
259     public void nonBreakingSpace() {
260         // nop
261     }
262 
263     @Override
264     public void rawText(String text) {
265         // nop
266     }
267 
268     @Override
269     public void comment(String comment) {
270         // nop
271     }
272 
273     @Override
274     public void flush() {
275         // nop
276     }
277 
278     @Override
279     public void close() {
280         this.elements.clear();
281         this.errors.clear();
282     }
283 
284     /**
285      * Finds out wether the wellformedness-contraints of the model have been
286      * violated.
287      *
288      * @return false for non-wellformed models
289      */
290     public boolean isWellformed() {
291         return errors.isEmpty();
292     }
293 
294     /**
295      * Gets the offending element that breaks the wellformedness as well
296      * as the exepected element.
297      *
298      * @return the expected and acual elements
299      */
300     public String getOffender() {
301         if (isWellformed()) {
302             return null;
303         }
304 
305         return errors.get(errors.size() - 1);
306     }
307 
308     /**
309      * Gets the list of errors found during wellformedness-check
310      *
311      * @return a list of String of error messages
312      */
313     public List<String> getOffenders() {
314         return errors;
315     }
316 
317     /**
318      * Checks wether a newly encountered end-tag breaks the wellformedness
319      * of the model.
320      *
321      * @param actual the local-name of the encountered element
322      */
323     private void checkWellformedness(String actual) {
324         String expected = elements.pop();
325 
326         if (!expected.equals(actual)) {
327             errors.add("Encountered closing: " + actual + ", expected " + expected);
328         }
329     }
330 
331     /**
332      * Starts a new element and puts it on the stack in order to calculate
333      * wellformedness of the model at a later point of time.
334      *
335      * @param string the local-name of the start-tag
336      */
337     private void startElement(String string) {
338         elements.push(string);
339     }
340 
341     @Override
342     public void head(SinkEventAttributes attributes) {
343         startElement("head");
344     }
345 
346     @Override
347     public void title(SinkEventAttributes attributes) {
348         startElement("title");
349     }
350 
351     @Override
352     public void author(SinkEventAttributes attributes) {
353         startElement("author");
354     }
355 
356     @Override
357     public void date(SinkEventAttributes attributes) {
358         startElement("date");
359     }
360 
361     @Override
362     public void body(SinkEventAttributes attributes) {
363         startElement("body");
364     }
365 
366     @Override
367     public void article(SinkEventAttributes attributes) {
368         startElement("article");
369     }
370 
371     @Override
372     public void navigation(SinkEventAttributes attributes) {
373         startElement("navigation");
374     }
375 
376     @Override
377     public void sidebar(SinkEventAttributes attributes) {
378         startElement("sidebar");
379     }
380 
381     @Override
382     public void section(int level, SinkEventAttributes attributes) {
383         startElement("section" + level);
384     }
385 
386     @Override
387     public void section_(int level) {
388         checkWellformedness("section" + level);
389     }
390 
391     @Override
392     public void sectionTitle(int level, SinkEventAttributes attributes) {
393         startElement("sectionTitle" + level);
394     }
395 
396     @Override
397     public void sectionTitle_(int level) {
398         checkWellformedness("sectionTitle" + level);
399     }
400 
401     @Override
402     public void header(SinkEventAttributes attributes) {
403         startElement("header");
404     }
405 
406     @Override
407     public void content(SinkEventAttributes attributes) {
408         startElement("content");
409     }
410 
411     @Override
412     public void footer(SinkEventAttributes attributes) {
413         startElement("footer");
414     }
415 
416     @Override
417     public void list(SinkEventAttributes attributes) {
418         startElement("list");
419     }
420 
421     @Override
422     public void listItem(SinkEventAttributes attributes) {
423         startElement("listItem");
424     }
425 
426     @Override
427     public void numberedList(int numbering, SinkEventAttributes attributes) {
428         startElement("numberedList");
429     }
430 
431     @Override
432     public void numberedListItem(SinkEventAttributes attributes) {
433         startElement("numberedListItem");
434     }
435 
436     @Override
437     public void definitionList(SinkEventAttributes attributes) {
438         startElement("definitionList");
439     }
440 
441     @Override
442     public void definitionListItem(SinkEventAttributes attributes) {
443         startElement("definitionListItem");
444     }
445 
446     @Override
447     public void definition(SinkEventAttributes attributes) {
448         startElement("definition");
449     }
450 
451     @Override
452     public void definedTerm(SinkEventAttributes attributes) {
453         startElement("definedTerm");
454     }
455 
456     @Override
457     public void figure(SinkEventAttributes attributes) {
458         startElement("figure");
459     }
460 
461     @Override
462     public void figureCaption(SinkEventAttributes attributes) {
463         startElement("figureCaption");
464     }
465 
466     @Override
467     public void figureGraphics(String src, SinkEventAttributes attributes) {
468         // nop
469     }
470 
471     @Override
472     public void table(SinkEventAttributes attributes) {
473         startElement("table");
474     }
475 
476     @Override
477     public void tableRows(int[] justification, boolean grid) {
478         startElement("tableRows");
479     }
480 
481     @Override
482     public void tableRow(SinkEventAttributes attributes) {
483         startElement("tableRow");
484     }
485 
486     @Override
487     public void tableCell(SinkEventAttributes attributes) {
488         startElement("tableCell");
489     }
490 
491     @Override
492     public void tableHeaderCell(SinkEventAttributes attributes) {
493         startElement("tableHeaderCell");
494     }
495 
496     @Override
497     public void tableCaption(SinkEventAttributes attributes) {
498         startElement("tableCaption");
499     }
500 
501     @Override
502     public void paragraph(SinkEventAttributes attributes) {
503         startElement("paragraph");
504     }
505 
506     @Override
507     public void data(String value, SinkEventAttributes attributes) {
508         startElement("data");
509     }
510 
511     @Override
512     public void time(String datetime, SinkEventAttributes attributes) {
513         startElement("time");
514     }
515 
516     @Override
517     public void address(SinkEventAttributes attributes) {
518         startElement("address");
519     }
520 
521     @Override
522     public void blockquote(SinkEventAttributes attributes) {
523         startElement("blockquote");
524     }
525 
526     @Override
527     public void division(SinkEventAttributes attributes) {
528         startElement("division");
529     }
530 
531     @Override
532     public void verbatim(SinkEventAttributes attributes) {
533         startElement("verbatim");
534     }
535 
536     @Override
537     public void horizontalRule(SinkEventAttributes attributes) {
538         // nop
539     }
540 
541     @Override
542     public void anchor(String name, SinkEventAttributes attributes) {
543         startElement("anchor");
544     }
545 
546     @Override
547     public void link(String name, SinkEventAttributes attributes) {
548         startElement("link");
549     }
550 
551     @Override
552     public void inline(SinkEventAttributes attributes) {
553         startElement("inline");
554     }
555 
556     @Override
557     public void lineBreak(SinkEventAttributes attributes) {
558         // nop
559     }
560 
561     @Override
562     public void lineBreakOpportunity(SinkEventAttributes attributes) {
563         // nop
564     }
565 
566     @Override
567     public void text(String text, SinkEventAttributes attributes) {
568         // nop
569     }
570 
571     @Override
572     public void unknown(String name, Object[] requiredParams, SinkEventAttributes attributes) {
573         // ignore
574     }
575 }