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

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