File: FoUtils.java

package info (click to toggle)
doxia 1.1.4-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,900 kB
  • ctags: 6,031
  • sloc: java: 41,993; xml: 7,132; sh: 11; makefile: 5
file content (202 lines) | stat: -rw-r--r-- 7,004 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package org.apache.maven.doxia.module.fo;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.Date;

import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.FopFactoryBuilder;
import org.apache.fop.apps.MimeConstants;
import org.apache.maven.doxia.document.DocumentModel;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;

/**
 * <code>FO Sink</code> utilities.
 *
 * @author ltheussl
 * @version $Id: FoUtils.java 785531 2009-06-17 09:47:59Z ltheussl $
 * @since 1.1
 */
public class FoUtils
{
    /** To reuse the TransformerFactory **/
    private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();


    private static void prepareUserAgent( FOUserAgent foUserAgent, DocumentModel documentModel ) {
        if ( documentModel != null && documentModel.getMeta() != null )
        {
            // http://xmlgraphics.apache.org/fop/embedding.html#user-agent
            String authors = documentModel.getMeta().getAllAuthorNames();
            if ( StringUtils.isNotEmpty( authors ) )
            {
                foUserAgent.setAuthor( authors );
            }
            if ( StringUtils.isNotEmpty( documentModel.getMeta().getTitle() ) )
            {
                foUserAgent.setTitle( documentModel.getMeta().getTitle() );
            }
            String keywords = documentModel.getMeta().getAllKeyWords();
            if ( StringUtils.isNotEmpty( keywords ) )
            {
                foUserAgent.setKeywords( keywords );
            }
            if ( StringUtils.isNotEmpty( documentModel.getMeta().getCreator() ) )
            {
                foUserAgent.setCreator( documentModel.getMeta().getCreator() );
            }
            if ( StringUtils.isNotEmpty( documentModel.getMeta().getGenerator() ) )
            {
                foUserAgent.setProducer( documentModel.getMeta().getGenerator() );
            }
            if ( documentModel.getMeta().getCreationDate() != null )
            {
                foUserAgent.setCreationDate( documentModel.getMeta().getCreationDate() );
            }
        }

        if ( foUserAgent.getCreator() == null )
        {
            foUserAgent.setCreator( System.getProperty( "user.name" ) );
        }
        if ( foUserAgent.getCreationDate() == null )
        {
            foUserAgent.setCreationDate( new Date() );
        }
    }

    /**
     * Converts an FO file to a PDF file using FOP.
     *
     * @param fo the FO file, not null.
     * @param pdf the target PDF file, not null.
     * @param resourceDir The base directory for relative path resolution, could be null.
     * If null, defaults to the parent directory of fo.
     * @param documentModel the document model to add PDF metadatas like author, title and keywords, could be null.
     * @throws javax.xml.transform.TransformerException In case of a conversion problem.
     * @since 1.1.1
     */
    public static void convertFO2PDF( File fo, File pdf, String resourceDir, DocumentModel documentModel )
        throws TransformerException
    {
        OutputStream out = null;
        try
        {
            try
            {
                out = new BufferedOutputStream( new FileOutputStream( pdf ) );
            }
            catch ( IOException e )
            {
                throw new TransformerException( e );
            }

            Result res = null;
            try
            {
                URI baseURI = getBaseURI( fo, resourceDir );
                FopFactory fopFactory = new FopFactoryBuilder( baseURI ).build();
                FOUserAgent userAgent = fopFactory.newFOUserAgent();
                prepareUserAgent( userAgent, documentModel );
                Fop fop = fopFactory.newFop( MimeConstants.MIME_PDF, userAgent, out );
                res = new SAXResult( fop.getDefaultHandler() );
            }
            catch ( FOPException e )
            {
                throw new TransformerException( e );
            }

            Transformer transformer = null;
            try
            {
                // identity transformer
                transformer = TRANSFORMER_FACTORY.newTransformer();
            }
            catch ( TransformerConfigurationException e )
            {
                throw new TransformerException( e );
            }

            transformer.transform( new StreamSource( fo ), res );
        }
        finally
        {
            IOUtil.close( out );
        }
    }

    /**
     * Converts an FO file to a PDF file using FOP.
     *
     * @param fo the FO file, not null.
     * @param pdf the target PDF file, not null.
     * @param resourceDir The base directory for relative path resolution, could be null.
     * If null, defaults to the parent directory of fo.
     * @throws javax.xml.transform.TransformerException In case of a conversion problem.
     * @see #convertFO2PDF(File, File, String, DocumentModel)
     */
    public static void convertFO2PDF( File fo, File pdf, String resourceDir )
        throws TransformerException
    {
        convertFO2PDF( fo, pdf, resourceDir, (DocumentModel) null );
    }

    /**
     * Returns a base URI.
     *
     * @param fo the FO file.
     * @param resourceDir the resource directory.
     * @return URI.
     */
    private static URI getBaseURI( File fo, String resourceDir )
    {
        if ( resourceDir == null )
        {
            return fo.getParentFile().toURI();
        }
        else
        {
            return new File( resourceDir + "/" ).toURI();
        }
    }

    private FoUtils()
    {
        // Utility class
    }
}