You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RendererFactory.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render;
  19. import java.io.OutputStream;
  20. import java.util.Collections;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.xmlgraphics.util.Service;
  27. import org.apache.fop.apps.FOPException;
  28. import org.apache.fop.apps.FOUserAgent;
  29. import org.apache.fop.area.AreaTreeHandler;
  30. import org.apache.fop.fo.FOEventHandler;
  31. import org.apache.fop.render.intermediate.AbstractIFDocumentHandlerMaker;
  32. import org.apache.fop.render.intermediate.EventProducingFilter;
  33. import org.apache.fop.render.intermediate.IFContext;
  34. import org.apache.fop.render.intermediate.IFDocumentHandler;
  35. import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
  36. import org.apache.fop.render.intermediate.IFRenderer;
  37. /**
  38. * Factory for FOEventHandlers and Renderers.
  39. */
  40. public class RendererFactory {
  41. /** the logger */
  42. private static Log log = LogFactory.getLog(RendererFactory.class);
  43. private Map rendererMakerMapping = new java.util.HashMap();
  44. private Map eventHandlerMakerMapping = new java.util.HashMap();
  45. private Map documentHandlerMakerMapping = new java.util.HashMap();
  46. private final boolean rendererPreferred;
  47. /**
  48. * Main constructor.
  49. * @param rendererPreferred Controls whether a {@link Renderer} is preferred over a
  50. * {@link IFDocumentHandler} if both are available for the same MIME type. True to prefer the
  51. * {@link Renderer}, false to prefer the {@link IFDocumentHandler}.
  52. */
  53. public RendererFactory(boolean rendererPreferred) {
  54. discoverRenderers();
  55. discoverFOEventHandlers();
  56. discoverDocumentHandlers();
  57. this.rendererPreferred = rendererPreferred;
  58. }
  59. /**
  60. * Indicates whether a {@link Renderer} is preferred over a {@link IFDocumentHandler} if
  61. * both are available for the same MIME type.
  62. * @return true if the {@link Renderer} is preferred,
  63. * false if the {@link IFDocumentHandler} is preferred.
  64. */
  65. public boolean isRendererPreferred() {
  66. return this.rendererPreferred;
  67. }
  68. /**
  69. * Add a new RendererMaker. If another maker has already been registered for a
  70. * particular MIME type, this call overwrites the existing one.
  71. * @param maker the RendererMaker
  72. */
  73. public void addRendererMaker(AbstractRendererMaker maker) {
  74. String[] mimes = maker.getSupportedMimeTypes();
  75. for (int i = 0; i < mimes.length; i++) {
  76. //This overrides any renderer previously set for a MIME type
  77. if (rendererMakerMapping.get(mimes[i]) != null) {
  78. log.trace("Overriding renderer for " + mimes[i]
  79. + " with " + maker.getClass().getName());
  80. }
  81. rendererMakerMapping.put(mimes[i], maker);
  82. }
  83. }
  84. /**
  85. * Add a new FOEventHandlerMaker. If another maker has already been registered for a
  86. * particular MIME type, this call overwrites the existing one.
  87. * @param maker the FOEventHandlerMaker
  88. */
  89. public void addFOEventHandlerMaker(AbstractFOEventHandlerMaker maker) {
  90. String[] mimes = maker.getSupportedMimeTypes();
  91. for (int i = 0; i < mimes.length; i++) {
  92. //This overrides any event handler previously set for a MIME type
  93. if (eventHandlerMakerMapping.get(mimes[i]) != null) {
  94. log.trace("Overriding FOEventHandler for " + mimes[i]
  95. + " with " + maker.getClass().getName());
  96. }
  97. eventHandlerMakerMapping.put(mimes[i], maker);
  98. }
  99. }
  100. /**
  101. * Add a new document handler maker. If another maker has already been registered for a
  102. * particular MIME type, this call overwrites the existing one.
  103. * @param maker the intermediate format document handler maker
  104. */
  105. public void addDocumentHandlerMaker(AbstractIFDocumentHandlerMaker maker) {
  106. String[] mimes = maker.getSupportedMimeTypes();
  107. for (int i = 0; i < mimes.length; i++) {
  108. //This overrides any renderer previously set for a MIME type
  109. if (documentHandlerMakerMapping.get(mimes[i]) != null) {
  110. log.trace("Overriding document handler for " + mimes[i]
  111. + " with " + maker.getClass().getName());
  112. }
  113. documentHandlerMakerMapping.put(mimes[i], maker);
  114. }
  115. }
  116. /**
  117. * Add a new RendererMaker. If another maker has already been registered for a
  118. * particular MIME type, this call overwrites the existing one.
  119. * @param className the fully qualified class name of the RendererMaker
  120. */
  121. public void addRendererMaker(String className) {
  122. try {
  123. AbstractRendererMaker makerInstance
  124. = (AbstractRendererMaker)Class.forName(className).newInstance();
  125. addRendererMaker(makerInstance);
  126. } catch (ClassNotFoundException e) {
  127. throw new IllegalArgumentException("Could not find "
  128. + className);
  129. } catch (InstantiationException e) {
  130. throw new IllegalArgumentException("Could not instantiate "
  131. + className);
  132. } catch (IllegalAccessException e) {
  133. throw new IllegalArgumentException("Could not access "
  134. + className);
  135. } catch (ClassCastException e) {
  136. throw new IllegalArgumentException(className
  137. + " is not an "
  138. + AbstractRendererMaker.class.getName());
  139. }
  140. }
  141. /**
  142. * Add a new FOEventHandlerMaker. If another maker has already been registered for a
  143. * particular MIME type, this call overwrites the existing one.
  144. * @param className the fully qualified class name of the FOEventHandlerMaker
  145. */
  146. public void addFOEventHandlerMaker(String className) {
  147. try {
  148. AbstractFOEventHandlerMaker makerInstance
  149. = (AbstractFOEventHandlerMaker)Class.forName(className).newInstance();
  150. addFOEventHandlerMaker(makerInstance);
  151. } catch (ClassNotFoundException e) {
  152. throw new IllegalArgumentException("Could not find "
  153. + className);
  154. } catch (InstantiationException e) {
  155. throw new IllegalArgumentException("Could not instantiate "
  156. + className);
  157. } catch (IllegalAccessException e) {
  158. throw new IllegalArgumentException("Could not access "
  159. + className);
  160. } catch (ClassCastException e) {
  161. throw new IllegalArgumentException(className
  162. + " is not an "
  163. + AbstractFOEventHandlerMaker.class.getName());
  164. }
  165. }
  166. /**
  167. * Add a new document handler maker. If another maker has already been registered for a
  168. * particular MIME type, this call overwrites the existing one.
  169. * @param className the fully qualified class name of the document handler maker
  170. */
  171. public void addDocumentHandlerMaker(String className) {
  172. try {
  173. AbstractIFDocumentHandlerMaker makerInstance
  174. = (AbstractIFDocumentHandlerMaker)Class.forName(className).newInstance();
  175. addDocumentHandlerMaker(makerInstance);
  176. } catch (ClassNotFoundException e) {
  177. throw new IllegalArgumentException("Could not find "
  178. + className);
  179. } catch (InstantiationException e) {
  180. throw new IllegalArgumentException("Could not instantiate "
  181. + className);
  182. } catch (IllegalAccessException e) {
  183. throw new IllegalArgumentException("Could not access "
  184. + className);
  185. } catch (ClassCastException e) {
  186. throw new IllegalArgumentException(className
  187. + " is not an "
  188. + AbstractIFDocumentHandlerMaker.class.getName());
  189. }
  190. }
  191. /**
  192. * Returns a RendererMaker which handles the given MIME type.
  193. * @param mime the requested output format
  194. * @return the requested RendererMaker or null if none is available
  195. */
  196. public AbstractRendererMaker getRendererMaker(String mime) {
  197. AbstractRendererMaker maker
  198. = (AbstractRendererMaker)rendererMakerMapping.get(mime);
  199. return maker;
  200. }
  201. /**
  202. * Returns a FOEventHandlerMaker which handles the given MIME type.
  203. * @param mime the requested output format
  204. * @return the requested FOEventHandlerMaker or null if none is available
  205. */
  206. public AbstractFOEventHandlerMaker getFOEventHandlerMaker(String mime) {
  207. AbstractFOEventHandlerMaker maker
  208. = (AbstractFOEventHandlerMaker)eventHandlerMakerMapping.get(mime);
  209. return maker;
  210. }
  211. /**
  212. * Returns a RendererMaker which handles the given MIME type.
  213. * @param mime the requested output format
  214. * @return the requested RendererMaker or null if none is available
  215. */
  216. private AbstractIFDocumentHandlerMaker getDocumentHandlerMaker(String mime) {
  217. AbstractIFDocumentHandlerMaker maker
  218. = (AbstractIFDocumentHandlerMaker)documentHandlerMakerMapping.get(mime);
  219. return maker;
  220. }
  221. /**
  222. * Creates a Renderer object based on render-type desired
  223. * @param userAgent the user agent for access to configuration
  224. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  225. * @return the new Renderer instance
  226. * @throws FOPException if the renderer cannot be properly constructed
  227. */
  228. public Renderer createRenderer(FOUserAgent userAgent, String outputFormat)
  229. throws FOPException {
  230. if (userAgent.getDocumentHandlerOverride() != null) {
  231. return createRendererForDocumentHandler(userAgent.getDocumentHandlerOverride());
  232. } else if (userAgent.getRendererOverride() != null) {
  233. return userAgent.getRendererOverride();
  234. } else {
  235. Renderer renderer;
  236. if (isRendererPreferred()) {
  237. //Try renderer first
  238. renderer = tryRendererMaker(userAgent, outputFormat);
  239. if (renderer == null) {
  240. renderer = tryIFDocumentHandlerMaker(userAgent, outputFormat);
  241. }
  242. } else {
  243. //Try document handler first
  244. renderer = tryIFDocumentHandlerMaker(userAgent, outputFormat);
  245. if (renderer == null) {
  246. renderer = tryRendererMaker(userAgent, outputFormat);
  247. }
  248. }
  249. if (renderer == null) {
  250. throw new UnsupportedOperationException(
  251. "No renderer for the requested format available: " + outputFormat);
  252. }
  253. return renderer;
  254. }
  255. }
  256. private Renderer tryIFDocumentHandlerMaker(FOUserAgent userAgent, String outputFormat)
  257. throws FOPException {
  258. AbstractIFDocumentHandlerMaker documentHandlerMaker
  259. = getDocumentHandlerMaker(outputFormat);
  260. if (documentHandlerMaker != null) {
  261. IFDocumentHandler documentHandler = createDocumentHandler(
  262. userAgent, outputFormat);
  263. return createRendererForDocumentHandler(documentHandler);
  264. } else {
  265. return null;
  266. }
  267. }
  268. private Renderer tryRendererMaker(FOUserAgent userAgent, String outputFormat)
  269. throws FOPException {
  270. AbstractRendererMaker maker = getRendererMaker(outputFormat);
  271. if (maker != null) {
  272. Renderer rend = maker.makeRenderer(userAgent);
  273. maker.configureRenderer(userAgent, rend);
  274. return rend;
  275. } else {
  276. return null;
  277. }
  278. }
  279. private Renderer createRendererForDocumentHandler(IFDocumentHandler documentHandler) {
  280. IFRenderer rend = new IFRenderer(documentHandler.getContext().getUserAgent());
  281. rend.setDocumentHandler(documentHandler);
  282. return rend;
  283. }
  284. /**
  285. * Creates FOEventHandler instances based on the desired output.
  286. * @param userAgent the user agent for access to configuration
  287. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  288. * @param out the OutputStream where the output is written to (if applicable)
  289. * @return the newly constructed FOEventHandler
  290. * @throws FOPException if the FOEventHandler cannot be properly constructed
  291. */
  292. public FOEventHandler createFOEventHandler(FOUserAgent userAgent,
  293. String outputFormat, OutputStream out) throws FOPException {
  294. if (userAgent.getFOEventHandlerOverride() != null) {
  295. return userAgent.getFOEventHandlerOverride();
  296. } else {
  297. AbstractFOEventHandlerMaker maker = getFOEventHandlerMaker(outputFormat);
  298. if (maker != null) {
  299. return maker.makeFOEventHandler(userAgent, out);
  300. } else {
  301. AbstractRendererMaker rendMaker = getRendererMaker(outputFormat);
  302. AbstractIFDocumentHandlerMaker documentHandlerMaker = null;
  303. boolean outputStreamMissing = (userAgent.getRendererOverride() == null)
  304. && (userAgent.getDocumentHandlerOverride() == null);
  305. if (rendMaker == null) {
  306. documentHandlerMaker = getDocumentHandlerMaker(outputFormat);
  307. if (documentHandlerMaker != null) {
  308. outputStreamMissing &= (out == null)
  309. && (documentHandlerMaker.needsOutputStream());
  310. }
  311. } else {
  312. outputStreamMissing &= (out == null) && (rendMaker.needsOutputStream());
  313. }
  314. if (userAgent.getRendererOverride() != null
  315. || rendMaker != null
  316. || userAgent.getDocumentHandlerOverride() != null
  317. || documentHandlerMaker != null) {
  318. if (outputStreamMissing) {
  319. throw new FOPException(
  320. "OutputStream has not been set");
  321. }
  322. //Found a Renderer so we need to construct an AreaTreeHandler.
  323. return new AreaTreeHandler(userAgent, outputFormat, out);
  324. } else {
  325. throw new UnsupportedOperationException(
  326. "Don't know how to handle \"" + outputFormat + "\" as an output format."
  327. + " Neither an FOEventHandler, nor a Renderer could be found"
  328. + " for this output format.");
  329. }
  330. }
  331. }
  332. }
  333. /**
  334. * Creates a {@link IFDocumentHandler} object based on the desired output format.
  335. * @param userAgent the user agent for access to configuration
  336. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  337. * @return the new {@link IFDocumentHandler} instance
  338. * @throws FOPException if the document handler cannot be properly constructed
  339. */
  340. public IFDocumentHandler createDocumentHandler(FOUserAgent userAgent, String outputFormat)
  341. throws FOPException {
  342. if (userAgent.getDocumentHandlerOverride() != null) {
  343. return userAgent.getDocumentHandlerOverride();
  344. }
  345. AbstractIFDocumentHandlerMaker maker = getDocumentHandlerMaker(outputFormat);
  346. if (maker == null) {
  347. throw new UnsupportedOperationException(
  348. "No IF document handler for the requested format available: " + outputFormat);
  349. }
  350. IFDocumentHandler documentHandler = maker.makeIFDocumentHandler(new IFContext(userAgent));
  351. // TODO: do all the configuration in the makeIfDocumentHandler method, that would beam when
  352. // you ask for a document handler, a configured one is returned to you. Getting it and
  353. // configuring it in two steps doesn't make sense.
  354. IFDocumentHandlerConfigurator configurator = documentHandler.getConfigurator();
  355. if (configurator != null) {
  356. configurator.configure(documentHandler);
  357. }
  358. return new EventProducingFilter(documentHandler, userAgent);
  359. }
  360. /**
  361. * @return an array of all supported MIME types
  362. */
  363. public String[] listSupportedMimeTypes() {
  364. List lst = new java.util.ArrayList();
  365. Iterator iter = this.rendererMakerMapping.keySet().iterator();
  366. while (iter.hasNext()) {
  367. lst.add(iter.next());
  368. }
  369. iter = this.eventHandlerMakerMapping.keySet().iterator();
  370. while (iter.hasNext()) {
  371. lst.add(iter.next());
  372. }
  373. iter = this.documentHandlerMakerMapping.keySet().iterator();
  374. while (iter.hasNext()) {
  375. lst.add(iter.next());
  376. }
  377. Collections.sort(lst);
  378. return (String[])lst.toArray(new String[lst.size()]);
  379. }
  380. /**
  381. * Discovers Renderer implementations through the classpath and dynamically
  382. * registers them.
  383. */
  384. private void discoverRenderers() {
  385. // add mappings from available services
  386. Iterator providers
  387. = Service.providers(Renderer.class);
  388. if (providers != null) {
  389. while (providers.hasNext()) {
  390. AbstractRendererMaker maker = (AbstractRendererMaker)providers.next();
  391. try {
  392. if (log.isDebugEnabled()) {
  393. log.debug("Dynamically adding maker for Renderer: "
  394. + maker.getClass().getName());
  395. }
  396. addRendererMaker(maker);
  397. } catch (IllegalArgumentException e) {
  398. log.error("Error while adding maker for Renderer", e);
  399. }
  400. }
  401. }
  402. }
  403. /**
  404. * Discovers FOEventHandler implementations through the classpath and dynamically
  405. * registers them.
  406. */
  407. private void discoverFOEventHandlers() {
  408. // add mappings from available services
  409. Iterator providers
  410. = Service.providers(FOEventHandler.class);
  411. if (providers != null) {
  412. while (providers.hasNext()) {
  413. AbstractFOEventHandlerMaker maker = (AbstractFOEventHandlerMaker)providers.next();
  414. try {
  415. if (log.isDebugEnabled()) {
  416. log.debug("Dynamically adding maker for FOEventHandler: "
  417. + maker.getClass().getName());
  418. }
  419. addFOEventHandlerMaker(maker);
  420. } catch (IllegalArgumentException e) {
  421. log.error("Error while adding maker for FOEventHandler", e);
  422. }
  423. }
  424. }
  425. }
  426. /**
  427. * Discovers {@link IFDocumentHandler} implementations through the classpath and dynamically
  428. * registers them.
  429. */
  430. private void discoverDocumentHandlers() {
  431. // add mappings from available services
  432. Iterator providers = Service.providers(IFDocumentHandler.class);
  433. if (providers != null) {
  434. while (providers.hasNext()) {
  435. AbstractIFDocumentHandlerMaker maker
  436. = (AbstractIFDocumentHandlerMaker)providers.next();
  437. try {
  438. if (log.isDebugEnabled()) {
  439. log.debug("Dynamically adding maker for IFDocumentHandler: "
  440. + maker.getClass().getName());
  441. }
  442. addDocumentHandlerMaker(maker);
  443. } catch (IllegalArgumentException e) {
  444. log.error("Error while adding maker for IFDocumentHandler", e);
  445. }
  446. }
  447. }
  448. }
  449. }