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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/*
* 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.
*/
/* $Id$ */
package org.apache.fop.apps;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import org.xml.sax.SAXException;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.fonts.FontManager;
import org.apache.fop.fonts.FontManagerConfigurator;
import org.apache.fop.util.LogUtil;
/**
* FopFactory configurator
*/
public class FopFactoryConfigurator {
/** Defines if FOP should use an alternative rule to determine text indents */
public static final boolean DEFAULT_BREAK_INDENT_INHERITANCE = false;
/** Defines if FOP should validate the user config strictly */
public static final boolean DEFAULT_STRICT_USERCONFIG_VALIDATION = true;
/** Defines if FOP should use strict validation for FO and user config */
public static final boolean DEFAULT_STRICT_FO_VALIDATION = true;
/** Defines the default page-width */
public static final String DEFAULT_PAGE_WIDTH = "8.26in";
/** Defines the default page-height */
public static final String DEFAULT_PAGE_HEIGHT = "11in";
/** Defines the default source resolution (72dpi) for FOP */
public static final float DEFAULT_SOURCE_RESOLUTION = 72.0f; //dpi
/** Defines the default target resolution (72dpi) for FOP */
public static final float DEFAULT_TARGET_RESOLUTION = 72.0f; //dpi
private static final String PREFER_RENDERER = "prefer-renderer";
/** logger instance */
private final Log log = LogFactory.getLog(FopFactoryConfigurator.class);
/** Fop factory */
private FopFactory factory = null;
/** Fop factory configuration */
private Configuration cfg = null;
/**
* Default constructor
* @param factory fop factory
*/
public FopFactoryConfigurator(FopFactory factory) {
super();
this.factory = factory;
}
/**
* Initializes user agent settings from the user configuration
* file, if present: baseURL, resolution, default page size,...
* @param factory fop factory
* @throws FOPException fop exception
*/
public void configure(FopFactory factory) throws FOPException {
if (log.isDebugEnabled()) {
log.debug("Initializing FopFactory Configuration");
}
// strict configuration
if (cfg.getChild("strict-configuration", false) != null) {
try {
factory.setStrictUserConfigValidation(
cfg.getChild("strict-configuration").getValueAsBoolean());
} catch (ConfigurationException e) {
LogUtil.handleException(log, e, false);
}
}
boolean strict = factory.validateUserConfigStrictly();
// strict fo validation
if (cfg.getChild("strict-validation", false) != null) {
try {
factory.setStrictValidation(
cfg.getChild("strict-validation").getValueAsBoolean());
} catch (ConfigurationException e) {
LogUtil.handleException(log, e, strict);
}
}
// base definitions for relative path resolution
if (cfg.getChild("base", false) != null) {
try {
factory.setBaseURL(
cfg.getChild("base").getValue(null));
} catch (MalformedURLException mfue) {
LogUtil.handleException(log, mfue, strict);
}
}
if (cfg.getChild("hyphenation-base", false) != null) {
try {
factory.setHyphenBaseURL(
cfg.getChild("hyphenation-base").getValue(null));
} catch (MalformedURLException mfue) {
LogUtil.handleException(log, mfue, strict);
}
}
// renderer options
if (cfg.getChild("source-resolution", false) != null) {
factory.setSourceResolution(
cfg.getChild("source-resolution").getValueAsFloat(
FopFactoryConfigurator.DEFAULT_SOURCE_RESOLUTION));
if (log.isDebugEnabled()) {
log.debug("source-resolution set to: " + factory.getSourceResolution()
+ "dpi (px2mm=" + factory.getSourcePixelUnitToMillimeter() + ")");
}
}
if (cfg.getChild("target-resolution", false) != null) {
factory.setTargetResolution(
cfg.getChild("target-resolution").getValueAsFloat(
FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION));
if (log.isDebugEnabled()) {
log.debug("target-resolution set to: " + factory.getTargetResolution()
+ "dpi (px2mm=" + factory.getTargetPixelUnitToMillimeter()
+ ")");
}
}
if (cfg.getChild("break-indent-inheritance", false) != null) {
try {
factory.setBreakIndentInheritanceOnReferenceAreaBoundary(
cfg.getChild("break-indent-inheritance").getValueAsBoolean());
} catch (ConfigurationException e) {
LogUtil.handleException(log, e, strict);
}
}
Configuration pageConfig = cfg.getChild("default-page-settings");
if (pageConfig.getAttribute("height", null) != null) {
factory.setPageHeight(
pageConfig.getAttribute("height", FopFactoryConfigurator.DEFAULT_PAGE_HEIGHT));
if (log.isInfoEnabled()) {
log.info("Default page-height set to: " + factory.getPageHeight());
}
}
if (pageConfig.getAttribute("width", null) != null) {
factory.setPageWidth(
pageConfig.getAttribute("width", FopFactoryConfigurator.DEFAULT_PAGE_WIDTH));
if (log.isInfoEnabled()) {
log.info("Default page-width set to: " + factory.getPageWidth());
}
}
// prefer Renderer over IFDocumentHandler
if (cfg.getChild(PREFER_RENDERER, false) != null) {
try {
factory.getRendererFactory().setRendererPreferred(
cfg.getChild(PREFER_RENDERER).getValueAsBoolean());
} catch (ConfigurationException e) {
LogUtil.handleException(log, e, strict);
}
}
// configure font manager
FontManager fontManager = factory.getFontManager();
FontManagerConfigurator fontManagerConfigurator = new FontManagerConfigurator(cfg);
fontManagerConfigurator.configure(fontManager, strict);
}
/**
* Set the user configuration.
* @param userConfigFile the configuration file
* @throws IOException if an I/O error occurs
* @throws SAXException if a parsing error occurs
*/
public void setUserConfig(File userConfigFile) throws SAXException, IOException {
try {
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
setUserConfig(cfgBuilder.buildFromFile(userConfigFile));
} catch (ConfigurationException e) {
throw new FOPException(e);
}
}
/**
* Set the user configuration from an URI.
* @param uri the URI to the configuration file
* @throws IOException if an I/O error occurs
* @throws SAXException if a parsing error occurs
*/
public void setUserConfig(String uri) throws SAXException, IOException {
try {
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
setUserConfig(cfgBuilder.build(uri));
} catch (ConfigurationException e) {
throw new FOPException(e);
}
}
/**
* Set the user configuration.
* @param cfg avalon configuration
* @throws FOPException if a configuration problem occurs
*/
public void setUserConfig(Configuration cfg) throws FOPException {
this.cfg = cfg;
configure(this.factory);
}
/**
* Get the avalon user configuration.
* @return the user configuration
*/
public Configuration getUserConfig() {
return this.cfg;
}
}
|