View Javadoc

1   package org.apache.maven.doxia.book.services.validation;
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 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   * Default implementation of BookValidator.
29   *
30   * @plexus.component
31   *
32   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
33   * @version $Id: DefaultBookValidator.java 1090706 2011-04-09 23:15:28Z hboutemy $
34   */
35  public class DefaultBookValidator
36      extends AbstractLogEnabled
37      implements BookValidator
38  {
39      // ----------------------------------------------------------------------
40      // BookValidator Implementation
41      // ----------------------------------------------------------------------
42  
43      /** {@inheritDoc} */
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                  // TODO: Validate the chapter id
69              }
70          }
71  
72          return result;
73      }
74  
75      // ----------------------------------------------------------------------
76      // Private
77      // ----------------------------------------------------------------------
78  
79      /**
80       * Validate a Chapter.
81       *
82       * @param result the ValidationResult to receive the results.
83       * @param chapter the chapter to validate.
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 }