1 package org.apache.maven.doxia.book.services.validation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.logging.AbstractLogEnabled;
23 import org.codehaus.plexus.util.StringUtils;
24 import org.apache.maven.doxia.book.model.BookModel;
25 import org.apache.maven.doxia.book.model.Chapter;
26
27
28
29
30
31
32
33
34
35 public class DefaultBookValidator
36 extends AbstractLogEnabled
37 implements BookValidator
38 {
39
40
41
42
43
44 public ValidationResult validateBook( BookModel book )
45 {
46 ValidationResult result = new ValidationResult();
47
48 if ( StringUtils.isEmpty( book.getId() ) )
49 {
50 result.getErrors().add( "Book is missing id." );
51 }
52
53 if ( StringUtils.isEmpty( book.getTitle() ) )
54 {
55 result.getErrors().add( "Book is missing title." );
56 }
57
58 if ( book.getChapters().size() == 0 )
59 {
60 result.getErrors().add( "The book must have at least one chaper" );
61 }
62 else
63 {
64 for ( Chapter chapter : book.getChapters() )
65 {
66 validateChapter( result, chapter );
67
68
69 }
70 }
71
72 return result;
73 }
74
75
76
77
78
79
80
81
82
83
84
85 private void validateChapter( ValidationResult result, Chapter chapter )
86 {
87 if ( StringUtils.isEmpty( chapter.getId() ) )
88 {
89 result.getErrors().add( "Each chapter has to have an id." );
90
91 return;
92 }
93
94 if ( StringUtils.isEmpty( chapter.getTitle() ) )
95 {
96 result.getErrors().add( "Missing title. Chapter id: " + chapter.getId() );
97 }
98
99 if ( chapter.getSections().size() == 0 )
100 {
101 result.getErrors().add( "Chapter doesn't have any sections. Chapter id: " + chapter.getId() );
102 }
103 }
104 }