View Javadoc

1   package org.apache.maven.doxia.book;
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.context.BookContext;
23  import org.apache.maven.doxia.book.model.BookModel;
24  import org.apache.maven.doxia.book.services.indexer.BookIndexer;
25  import org.apache.maven.doxia.book.services.io.BookIo;
26  import org.apache.maven.doxia.book.services.renderer.BookRenderer;
27  import org.apache.maven.doxia.book.services.validation.BookValidator;
28  import org.apache.maven.doxia.book.services.validation.ValidationResult;
29  import org.codehaus.plexus.logging.AbstractLogEnabled;
30  
31  import java.io.File;
32  import java.util.Collections;
33  import java.util.List;
34  import java.util.Locale;
35  import java.util.Map;
36  import java.util.Set;
37  
38  /**
39   * Default implementation of BookDoxia.
40   *
41   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
42   * @version $Id: DefaultBookDoxia.java 1090706 2011-04-09 23:15:28Z hboutemy $
43   * @plexus.component
44   */
45  public class DefaultBookDoxia
46      extends AbstractLogEnabled
47      implements BookDoxia
48  {
49      /**
50       * @plexus.requirement
51       */
52      private BookIo bookIo;
53  
54      /**
55       * @plexus.requirement
56       */
57      private BookValidator bookValidator;
58  
59      /**
60       * @plexus.requirement
61       */
62      private BookIndexer bookIndexer;
63  
64      /**
65       * @plexus.requirement role="org.apache.maven.doxia.book.services.renderer.BookRenderer"
66       */
67      private Map<String, BookRenderer> bookRenderers;
68  
69      // ----------------------------------------------------------------------
70      // BookDoxia Implementation
71      // ----------------------------------------------------------------------
72  
73      /** {@inheritDoc} */
74      public BookModel loadBook( File bookDescriptor )
75          throws BookDoxiaException
76      {
77          return bookIo.readBook( bookDescriptor );
78      }
79  
80      /** {@inheritDoc} */
81      public void renderBook( BookModel book, String bookRendererId, List<File> files, File outputDirectory )
82          throws BookDoxiaException
83      {
84          renderBook( book, bookRendererId, files, outputDirectory, Locale.getDefault(), "UTF-8", "UTF-8" );
85      }
86  
87      /** {@inheritDoc} */
88      public void renderBook( BookModel book, String bookRendererId, List<File> files, File outputDirectory,
89                              Locale locale, String inputEncoding, String outputEncoding )
90          throws BookDoxiaException
91      {
92          // ----------------------------------------------------------------------
93          //
94          // ----------------------------------------------------------------------
95  
96          ValidationResult validationResult = bookValidator.validateBook( book );
97  
98          if ( !validationResult.isAllOk() )
99          {
100             throw new InvalidBookDescriptorException( validationResult );
101         }
102 
103         // ----------------------------------------------------------------------
104         // Create and initialize the context
105         // ----------------------------------------------------------------------
106 
107         BookContext context = new BookContext();
108 
109         context.setBook( book );
110 
111         context.setOutputDirectory( outputDirectory );
112 
113         context.setLocale( locale );
114 
115         context.setInputEncoding( inputEncoding );
116 
117         context.setOutputEncoding( outputEncoding );
118 
119         // -----------------------------------------------------------------------
120         //
121         // -----------------------------------------------------------------------
122 
123         bookIo.loadFiles( context, files );
124 
125         // ----------------------------------------------------------------------
126         // Generate indexes
127         // ----------------------------------------------------------------------
128 
129         bookIndexer.indexBook( book, context );
130 
131         // ----------------------------------------------------------------------
132         // Render the book
133         // ----------------------------------------------------------------------
134 
135         BookRenderer bookRenderer = bookRenderers.get( bookRendererId );
136 
137         if ( bookRenderer == null )
138         {
139             throw new BookDoxiaException( "No such book renderer '" + bookRendererId + "'." );
140         }
141 
142         bookRenderer.renderBook( context );
143     }
144 
145     /**
146      * Returns a Set of ids of the BookRenderers that are available in this BookDoxia.
147      *
148      * @return Set
149      */
150     public Set<String> getAvailableBookRenderers()
151     {
152         return Collections.unmodifiableSet( bookRenderers.keySet() );
153     }
154 
155 }