1 package org.apache.maven.doxia.module.fo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
48
49
50
51
52
53 public class FoUtils
54 {
55
56 private static final FopFactory FOP_FACTORY = FopFactory.newInstance();
57
58
59 private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
60
61
62
63
64
65
66
67
68
69
70
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
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
122
123
124
125
126
127
128
129
130
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
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
181
182
183
184
185
186
187
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
197
198
199
200
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
229 }
230 }