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

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