Coverage Report - org.apache.maven.doxia.book.services.io.DefaultBookIo
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultBookIo
64%
22/34
64%
9/14
7
 
 1  
 package org.apache.maven.doxia.book.services.io;
 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.apache.maven.doxia.book.model.io.xpp3.BookModelXpp3Reader;
 23  
 import org.apache.maven.doxia.book.model.BookModel;
 24  
 import org.apache.maven.doxia.book.BookDoxiaException;
 25  
 import org.apache.maven.doxia.book.context.BookContext;
 26  
 import org.apache.maven.doxia.module.site.SiteModule;
 27  
 import org.apache.maven.doxia.module.site.manager.SiteModuleManager;
 28  
 import org.codehaus.plexus.util.IOUtil;
 29  
 import org.codehaus.plexus.util.ReaderFactory;
 30  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 31  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 32  
 
 33  
 import java.io.IOException;
 34  
 import java.io.File;
 35  
 import java.io.Reader;
 36  
 import java.util.Collection;
 37  
 import java.util.Map;
 38  
 import java.util.TreeMap;
 39  
 import java.util.List;
 40  
 
 41  
 /**
 42  
  * <p>DefaultBookIo class.</p>
 43  
  *
 44  
  * @plexus.component
 45  
  * @author <a href="mailto:trygve.laugstol@objectware.no">Trygve Laugst&oslash;l</a>
 46  
  * @version $Id: DefaultBookIo.java 1090706 2011-04-09 23:15:28Z hboutemy $
 47  
  */
 48  2
 public class DefaultBookIo
 49  
     extends AbstractLogEnabled
 50  
     implements BookIo
 51  
 {
 52  
     /**
 53  
      * @plexus.requirement
 54  
      */
 55  
     private SiteModuleManager siteModuleManager;
 56  
 
 57  
     // -----------------------------------------------------------------------
 58  
     // DefaultBookIo Implementation
 59  
     // -----------------------------------------------------------------------
 60  
 
 61  
     /** {@inheritDoc} */
 62  
     public BookModel readBook( File bookDescriptor )
 63  
         throws BookDoxiaException
 64  
     {
 65  2
         Reader reader = null;
 66  
         try
 67  
         {
 68  2
             reader = ReaderFactory.newXmlReader( bookDescriptor );
 69  2
             return new BookModelXpp3Reader().read( reader, true );
 70  
         }
 71  0
         catch ( IOException e )
 72  
         {
 73  0
             throw new BookDoxiaException( "Error while reading book descriptor.", e );
 74  
         }
 75  0
         catch ( XmlPullParserException e )
 76  
         {
 77  0
             throw new BookDoxiaException( "Error while reading book descriptor.", e );
 78  
         }
 79  
         finally
 80  
         {
 81  2
             IOUtil.close( reader );
 82  
         }
 83  
     }
 84  
 
 85  
     /** {@inheritDoc} */
 86  
     public void loadFiles( BookContext context, List<File> files )
 87  
     {
 88  
         // ----------------------------------------------------------------------
 89  
         // Find all the files, map the file names to ids
 90  
         // ----------------------------------------------------------------------
 91  
 
 92  7
         Collection<SiteModule> siteModules = siteModuleManager.getSiteModules();
 93  
 
 94  7
         for ( SiteModule siteModule : siteModules )
 95  
         {
 96  28
             String extension = siteModule.getExtension();
 97  
 
 98  28
             String sourceDirectory = File.separator + siteModule.getSourceDirectory() + File.separator;
 99  
 
 100  28
             String parserId = siteModule.getParserId();
 101  
 
 102  28
             for ( File file : files )
 103  
             {
 104  112
                 String name = file.getName();
 105  
 
 106  112
                 String path = file.getAbsolutePath();
 107  
 
 108  
                 // first check if the file path contains one of the recognized source dir identifiers
 109  
                 // (there's trouble if a pathname contains 2 identifiers), then match file extensions (not unique).
 110  
 
 111  112
                 if ( path.indexOf( sourceDirectory ) != -1 )
 112  
                 {
 113  0
                     name = name.substring( 0, name.length() - extension.length() - 1 );
 114  
 
 115  0
                     context.getFiles().put( name, new BookContext.BookFile( file, parserId ) );
 116  
                 }
 117  112
                 else if ( name.endsWith( extension ) )
 118  
                 {
 119  28
                     name = name.substring( 0, name.length() - extension.length() - 1 );
 120  
 
 121  
                     // don't overwrite if it's there already
 122  28
                     if ( !context.getFiles().containsKey( name ) )
 123  
                     {
 124  28
                         context.getFiles().put( name, new BookContext.BookFile( file, parserId ) );
 125  
                     }
 126  
                 }
 127  112
             }
 128  28
         }
 129  
 
 130  7
         if ( getLogger().isDebugEnabled() )
 131  
         {
 132  0
             getLogger().debug( "Dumping document <-> id mapping:" );
 133  
 
 134  0
             Map<String, BookContext.BookFile> map = new TreeMap<String, BookContext.BookFile>( context.getFiles() );
 135  
 
 136  0
             for ( Map.Entry<String, BookContext.BookFile> entry : map.entrySet() )
 137  
             {
 138  0
                 BookContext.BookFile file = entry.getValue();
 139  
 
 140  0
                 getLogger().debug( " " + entry.getKey() + "=" + file.getFile() + ", parser: " + file.getParserId() );
 141  0
             }
 142  
         }
 143  7
     }
 144  
 }