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 19KB

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