View Javadoc

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  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          Reader reader = null;
66          try
67          {
68              reader = ReaderFactory.newXmlReader( bookDescriptor );
69              return new BookModelXpp3Reader().read( reader, true );
70          }
71          catch ( IOException e )
72          {
73              throw new BookDoxiaException( "Error while reading book descriptor.", e );
74          }
75          catch ( XmlPullParserException e )
76          {
77              throw new BookDoxiaException( "Error while reading book descriptor.", e );
78          }
79          finally
80          {
81              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          Collection<SiteModule> siteModules = siteModuleManager.getSiteModules();
93  
94          for ( SiteModule siteModule : siteModules )
95          {
96              String extension = siteModule.getExtension();
97  
98              String sourceDirectory = File.separator + siteModule.getSourceDirectory() + File.separator;
99  
100             String parserId = siteModule.getParserId();
101 
102             for ( File file : files )
103             {
104                 String name = file.getName();
105 
106                 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                 if ( path.indexOf( sourceDirectory ) != -1 )
112                 {
113                     name = name.substring( 0, name.length() - extension.length() - 1 );
114 
115                     context.getFiles().put( name, new BookContext.BookFile( file, parserId ) );
116                 }
117                 else if ( name.endsWith( extension ) )
118                 {
119                     name = name.substring( 0, name.length() - extension.length() - 1 );
120 
121                     // don't overwrite if it's there already
122                     if ( !context.getFiles().containsKey( name ) )
123                     {
124                         context.getFiles().put( name, new BookContext.BookFile( file, parserId ) );
125                     }
126                 }
127             }
128         }
129 
130         if ( getLogger().isDebugEnabled() )
131         {
132             getLogger().debug( "Dumping document <-> id mapping:" );
133 
134             Map<String, BookContext.BookFile> map = new TreeMap<String, BookContext.BookFile>( context.getFiles() );
135 
136             for ( Map.Entry<String, BookContext.BookFile> entry : map.entrySet() )
137             {
138                 BookContext.BookFile file = entry.getValue();
139 
140                 getLogger().debug( " " + entry.getKey() + "=" + file.getFile() + ", parser: " + file.getParserId() );
141             }
142         }
143     }
144 }