Coverage Report - org.apache.maven.doxia.module.fo.FoUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FoUtils
59%
34/57
20%
5/24
4
 
 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  2
     private static final FopFactory FOP_FACTORY = FopFactory.newInstance();
 57  
 
 58  
     /** To reuse the TransformerFactory **/
 59  2
     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  8
         FOUserAgent foUserAgent = getDefaultUserAgent( fo, resourceDir );
 76  
 
 77  8
         if ( documentModel != null && documentModel.getMeta() != null )
 78  
         {
 79  
             // http://xmlgraphics.apache.org/fop/embedding.html#user-agent
 80  0
             String authors = documentModel.getMeta().getAllAuthorNames();
 81  0
             if ( StringUtils.isNotEmpty( authors ) )
 82  
             {
 83  0
                 foUserAgent.setAuthor( authors );
 84  
             }
 85  0
             if ( StringUtils.isNotEmpty( documentModel.getMeta().getTitle() ) )
 86  
             {
 87  0
                 foUserAgent.setTitle( documentModel.getMeta().getTitle() );
 88  
             }
 89  0
             String keywords = documentModel.getMeta().getAllKeyWords();
 90  0
             if ( StringUtils.isNotEmpty( keywords ) )
 91  
             {
 92  0
                 foUserAgent.setKeywords( keywords );
 93  
             }
 94  0
             if ( StringUtils.isNotEmpty( documentModel.getMeta().getCreator() ) )
 95  
             {
 96  0
                 foUserAgent.setCreator( documentModel.getMeta().getCreator() );
 97  
             }
 98  0
             if ( StringUtils.isNotEmpty( documentModel.getMeta().getGenerator() ) )
 99  
             {
 100  0
                 foUserAgent.setProducer( documentModel.getMeta().getGenerator() );
 101  
             }
 102  0
             if ( documentModel.getMeta().getCreationDate() != null )
 103  
             {
 104  0
                 foUserAgent.setCreationDate( documentModel.getMeta().getCreationDate() );
 105  
             }
 106  
         }
 107  
 
 108  8
         if ( foUserAgent.getCreator() == null )
 109  
         {
 110  8
             foUserAgent.setCreator( System.getProperty( "user.name" ) );
 111  
         }
 112  8
         if ( foUserAgent.getCreationDate() == null )
 113  
         {
 114  8
             foUserAgent.setCreationDate( new Date() );
 115  
         }
 116  
 
 117  8
         convertFO2PDF( fo, pdf, resourceDir, foUserAgent );
 118  8
     }
 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  8
         FOUserAgent userAgent = ( foUserAgent == null ? getDefaultUserAgent( fo, resourceDir ) : foUserAgent );
 136  
 
 137  8
         OutputStream out = null;
 138  
         try
 139  
         {
 140  
             try
 141  
             {
 142  8
                 out = new BufferedOutputStream( new FileOutputStream( pdf ) );
 143  
             }
 144  0
             catch ( IOException e )
 145  
             {
 146  0
                 throw new TransformerException( e );
 147  8
             }
 148  
 
 149  8
             Result res = null;
 150  
             try
 151  
             {
 152  8
                 Fop fop = FOP_FACTORY.newFop( MimeConstants.MIME_PDF, userAgent, out );
 153  8
                 res = new SAXResult( fop.getDefaultHandler() );
 154  
             }
 155  0
             catch ( FOPException e )
 156  
             {
 157  0
                 throw new TransformerException( e );
 158  8
             }
 159  
 
 160  8
             Transformer transformer = null;
 161  
             try
 162  
             {
 163  
                 // identity transformer
 164  8
                 transformer = TRANSFORMER_FACTORY.newTransformer();
 165  
             }
 166  0
             catch ( TransformerConfigurationException e )
 167  
             {
 168  0
                 throw new TransformerException( e );
 169  8
             }
 170  
 
 171  8
             transformer.transform( new StreamSource( fo ), res );
 172  
         }
 173  
         finally
 174  
         {
 175  8
             IOUtil.close( out );
 176  8
         }
 177  8
     }
 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  8
         convertFO2PDF( fo, pdf, resourceDir, (DocumentModel) null );
 193  8
     }
 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  8
         String url = null;
 205  
 
 206  8
         if ( resourceDir == null )
 207  
         {
 208  0
             url = "file:///" + fo.getParent() + "/";
 209  
         }
 210  
         else
 211  
         {
 212  8
             url = "file:///" + resourceDir + "/";
 213  
         }
 214  
 
 215  8
         return url;
 216  
     }
 217  
 
 218  
     private static FOUserAgent getDefaultUserAgent( File fo, String resourceDir )
 219  
     {
 220  8
         FOUserAgent foUserAgent = FOP_FACTORY.newFOUserAgent();
 221  8
         foUserAgent.setBaseURL( getBaseURL( fo, resourceDir ) );
 222  
 
 223  8
         return foUserAgent;
 224  
     }
 225  
 
 226  
     private FoUtils()
 227  0
     {
 228  
         // Utility class
 229  0
     }
 230  
 }