Coverage Report - org.apache.maven.doxia.book.services.indexer.DefaultBookIndexer
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultBookIndexer
73%
19/26
83%
5/6
4,333
 
 1  
 package org.apache.maven.doxia.book.services.indexer;
 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.io.FileNotFoundException;
 23  
 import java.io.FileReader;
 24  
 
 25  
 import org.apache.maven.doxia.Doxia;
 26  
 import org.apache.maven.doxia.book.BookDoxiaException;
 27  
 import org.apache.maven.doxia.book.context.BookContext;
 28  
 import org.apache.maven.doxia.book.context.BookIndex;
 29  
 import org.apache.maven.doxia.book.model.BookModel;
 30  
 import org.apache.maven.doxia.book.model.Chapter;
 31  
 import org.apache.maven.doxia.book.model.Section;
 32  
 import org.apache.maven.doxia.index.IndexEntry;
 33  
 import org.apache.maven.doxia.index.IndexingSink;
 34  
 import org.apache.maven.doxia.parser.ParseException;
 35  
 import org.apache.maven.doxia.parser.manager.ParserNotFoundException;
 36  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 37  
 
 38  
 /**
 39  
  * Default implementation of BookIndexer.
 40  
  *
 41  
  * @plexus.component
 42  
  *
 43  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 44  
  * @version $Id: DefaultBookIndexer.java 1090706 2011-04-09 23:15:28Z hboutemy $
 45  
  */
 46  4
 public class DefaultBookIndexer
 47  
     extends AbstractLogEnabled
 48  
     implements BookIndexer
 49  
 {
 50  
     /**
 51  
      * @plexus.requirement
 52  
      */
 53  
     private Doxia doxia;
 54  
 
 55  
     // ----------------------------------------------------------------------
 56  
     // BookIndexer Implementation
 57  
     // ----------------------------------------------------------------------
 58  
 
 59  
     /** {@inheritDoc} */
 60  
     public void indexBook( BookModel book, BookContext bookContext )
 61  
         throws BookDoxiaException
 62  
     {
 63  14
         BookIndex index = new BookIndex();
 64  
 
 65  14
         for ( Chapter chapter : book.getChapters() )
 66  
         {
 67  28
             indexChapter( bookContext, index, chapter );
 68  
         }
 69  
 
 70  14
         bookContext.setIndex( index );
 71  14
     }
 72  
 
 73  
     // ----------------------------------------------------------------------
 74  
     // Private
 75  
     // ----------------------------------------------------------------------
 76  
 
 77  
     /**
 78  
      * Index a chapter.
 79  
      *
 80  
      * @param context the BookContext.
 81  
      * @param bookEntry the IndexEntry.
 82  
      * @param chapter the Chapter to index.
 83  
      * @throws BookDoxiaException if the chapter cannot be indexed.
 84  
      */
 85  
     private void indexChapter( BookContext context, IndexEntry bookEntry, Chapter chapter )
 86  
         throws BookDoxiaException
 87  
     {
 88  28
         IndexEntry chapterEntry = new IndexEntry( bookEntry, chapter.getId( ) );
 89  
 
 90  28
         chapterEntry.setTitle( chapter.getTitle() );
 91  
 
 92  28
         for ( Section section : chapter.getSections() )
 93  
         {
 94  56
             indexSection( context, chapterEntry, section );
 95  
         }
 96  28
     }
 97  
 
 98  
     /**
 99  
      * Index a section.
 100  
      *
 101  
      * @param bookContext the BookContext.
 102  
      * @param chapterEntry the IndexEntry.
 103  
      * @param section the Section to index.
 104  
      * @throws BookDoxiaException if the section cannot be indexed.
 105  
      */
 106  
     private void indexSection( BookContext bookContext, IndexEntry chapterEntry, Section section )
 107  
         throws BookDoxiaException
 108  
     {
 109  56
         BookContext.BookFile bookFile = (BookContext.BookFile) bookContext.getFiles().get( section.getId() );
 110  
 
 111  56
         if ( bookFile == null )
 112  
         {
 113  0
             throw new BookDoxiaException( "No document that matches section with id="
 114  
                         + section.getId() + "." );
 115  
         }
 116  
 
 117  
         // ----------------------------------------------------------------------
 118  
         //
 119  
         // ----------------------------------------------------------------------
 120  
 
 121  56
         IndexEntry sectionEntry = new IndexEntry( chapterEntry, section.getId() );
 122  
 
 123  56
         IndexingSink sink = new IndexingSink( sectionEntry );
 124  
 
 125  
         try
 126  
         {
 127  56
             doxia.parse( new FileReader( bookFile.getFile() ), bookFile.getParserId(), sink );
 128  
         }
 129  0
         catch ( ParserNotFoundException e )
 130  
         {
 131  0
             throw new BookDoxiaException( "Parser not found: "
 132  
                         + bookFile.getParserId() + ".", e );
 133  
         }
 134  0
         catch ( ParseException e )
 135  
         {
 136  0
             throw new BookDoxiaException( "Error while parsing document: "
 137  
                         + bookFile.getFile().getAbsolutePath() + ".", e );
 138  
         }
 139  0
         catch ( FileNotFoundException e )
 140  
         {
 141  0
             throw new BookDoxiaException( "Could not find document: "
 142  
                         + bookFile.getFile().getAbsolutePath() + ".", e );
 143  56
         }
 144  
 
 145  56
         sectionEntry.setTitle( sink.getTitle() );
 146  56
     }
 147  
 }