View Javadoc

1   package org.apache.maven.doxia.module.fo;
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.BufferedOutputStream;
23  import java.io.File;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.OutputStream;
27  import java.util.Date;
28  
29  import javax.xml.transform.Result;
30  import javax.xml.transform.Transformer;
31  import javax.xml.transform.TransformerConfigurationException;
32  import javax.xml.transform.TransformerException;
33  import javax.xml.transform.TransformerFactory;
34  import javax.xml.transform.sax.SAXResult;
35  import javax.xml.transform.stream.StreamSource;
36  
37  import org.apache.fop.apps.FOPException;
38  import org.apache.fop.apps.FOUserAgent;
39  import org.apache.fop.apps.Fop;
40  import org.apache.fop.apps.FopFactory;
41  import org.apache.fop.apps.MimeConstants;
42  import org.apache.maven.doxia.document.DocumentModel;
43  import org.codehaus.plexus.util.IOUtil;
44  import org.codehaus.plexus.util.StringUtils;
45  
46  /**
47   * <code>FO Sink</code> utilities.
48   *
49   * @author ltheussl
50   * @version $Id: FoUtils.java 785531 2009-06-17 09:47:59Z ltheussl $
51   * @since 1.1
52   */
53  public class FoUtils
54  {
55      /** To reuse the FopFactory **/
56      private static final FopFactory FOP_FACTORY = FopFactory.newInstance();
57  
58      /** To reuse the TransformerFactory **/
59      private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
60  
61      /**
62       * Converts an FO file to a PDF file using FOP.
63       *
64       * @param fo the FO file, not null.
65       * @param pdf the target PDF file, not null.
66       * @param resourceDir The base directory for relative path resolution, could be null.
67       * If null, defaults to the parent directory of fo.
68       * @param documentModel the document model to add PDF metadatas like author, title and keywords, could be null.
69       * @throws javax.xml.transform.TransformerException In case of a conversion problem.
70       * @since 1.1.1
71       */
72      public static void convertFO2PDF( File fo, File pdf, String resourceDir, DocumentModel documentModel )
73          throws TransformerException
74      {
75          FOUserAgent foUserAgent = getDefaultUserAgent( fo, resourceDir );
76  
77          if ( documentModel != null && documentModel.getMeta() != null )
78          {
79              // http://xmlgraphics.apache.org/fop/embedding.html#user-agent
80              String authors = documentModel.getMeta().getAllAuthorNames();
81              if ( StringUtils.isNotEmpty( authors ) )
82              {
83                  foUserAgent.setAuthor( authors );
84              }
85              if ( StringUtils.isNotEmpty( documentModel.getMeta().getTitle() ) )
86              {
87                  foUserAgent.setTitle( documentModel.getMeta().getTitle() );
88              }
89              String keywords = documentModel.getMeta().getAllKeyWords();
90              if ( StringUtils.isNotEmpty( keywords ) )
91              {
92                  foUserAgent.setKeywords( keywords );
93              }
94              if ( StringUtils.isNotEmpty( documentModel.getMeta().getCreator() ) )
95              {
96                  foUserAgent.setCreator( documentModel.getMeta().getCreator() );
97              }
98              if ( StringUtils.isNotEmpty( documentModel.getMeta().getGenerator() ) )
99              {
100                 foUserAgent.setProducer( documentModel.getMeta().getGenerator() );
101             }
102             if ( documentModel.getMeta().getCreationDate() != null )
103             {
104                 foUserAgent.setCreationDate( documentModel.getMeta().getCreationDate() );
105             }
106         }
107 
108         if ( foUserAgent.getCreator() == null )
109         {
110             foUserAgent.setCreator( System.getProperty( "user.name" ) );
111         }
112         if ( foUserAgent.getCreationDate() == null )
113         {
114             foUserAgent.setCreationDate( new Date() );
115         }
116 
117         convertFO2PDF( fo, pdf, resourceDir, foUserAgent );
118     }
119 
120     /**
121      * Converts an FO file to a PDF file using FOP.
122      *
123      * @param fo the FO file, not null.
124      * @param pdf the target PDF file, not null.
125      * @param resourceDir The base directory for relative path resolution, could be null.
126      * If null, defaults to the parent directory of fo.
127      * @param foUserAgent the FOUserAgent to use.
128      *      May be null, in which case a default user agent will be used.
129      * @throws javax.xml.transform.TransformerException In case of a conversion problem.
130      * @since 1.1.1
131      */
132     public static void convertFO2PDF( File fo, File pdf, String resourceDir, FOUserAgent foUserAgent )
133         throws TransformerException
134     {
135         FOUserAgent userAgent = ( foUserAgent == null ? getDefaultUserAgent( fo, resourceDir ) : foUserAgent );
136 
137         OutputStream out = null;
138         try
139         {
140             try
141             {
142                 out = new BufferedOutputStream( new FileOutputStream( pdf ) );
143             }
144             catch ( IOException e )
145             {
146                 throw new TransformerException( e );
147             }
148 
149             Result res = null;
150             try
151             {
152                 Fop fop = FOP_FACTORY.newFop( MimeConstants.MIME_PDF, userAgent, out );
153                 res = new SAXResult( fop.getDefaultHandler() );
154             }
155             catch ( FOPException e )
156             {
157                 throw new TransformerException( e );
158             }
159 
160             Transformer transformer = null;
161             try
162             {
163                 // identity transformer
164                 transformer = TRANSFORMER_FACTORY.newTransformer();
165             }
166             catch ( TransformerConfigurationException e )
167             {
168                 throw new TransformerException( e );
169             }
170 
171             transformer.transform( new StreamSource( fo ), res );
172         }
173         finally
174         {
175             IOUtil.close( out );
176         }
177     }
178 
179     /**
180      * Converts an FO file to a PDF file using FOP.
181      *
182      * @param fo the FO file, not null.
183      * @param pdf the target PDF file, not null.
184      * @param resourceDir The base directory for relative path resolution, could be null.
185      * If null, defaults to the parent directory of fo.
186      * @throws javax.xml.transform.TransformerException In case of a conversion problem.
187      * @see #convertFO2PDF(File, File, String, DocumentModel)
188      */
189     public static void convertFO2PDF( File fo, File pdf, String resourceDir )
190         throws TransformerException
191     {
192         convertFO2PDF( fo, pdf, resourceDir, (DocumentModel) null );
193     }
194 
195     /**
196      * Returns a base URL to be used by the FOUserAgent.
197      *
198      * @param fo the FO file.
199      * @param resourceDir the resource directory.
200      * @return String.
201      */
202     private static String getBaseURL( File fo, String resourceDir )
203     {
204         String url = null;
205 
206         if ( resourceDir == null )
207         {
208             url = "file:///" + fo.getParent() + "/";
209         }
210         else
211         {
212             url = "file:///" + resourceDir + "/";
213         }
214 
215         return url;
216     }
217 
218     private static FOUserAgent getDefaultUserAgent( File fo, String resourceDir )
219     {
220         FOUserAgent foUserAgent = FOP_FACTORY.newFOUserAgent();
221         foUserAgent.setBaseURL( getBaseURL( fo, resourceDir ) );
222 
223         return foUserAgent;
224     }
225 
226     private FoUtils()
227     {
228         // Utility class
229     }
230 }