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 | 2 | private static final FopFactory FOP_FACTORY = FopFactory.newInstance(); |
57 | |
|
58 | |
|
59 | 2 | 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 | 8 | FOUserAgent foUserAgent = getDefaultUserAgent( fo, resourceDir ); |
76 | |
|
77 | 8 | if ( documentModel != null && documentModel.getMeta() != null ) |
78 | |
{ |
79 | |
|
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 | |
|
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 | 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 | |
|
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 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
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 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
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 | |
|
229 | 0 | } |
230 | |
} |