Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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.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. if (rendMaker == null) {
  269. documentHandlerMaker = getDocumentHandlerMaker(outputFormat);
  270. if (documentHandlerMaker != null) {
  271. outputStreamMissing &= (out == null)
  272. && (documentHandlerMaker.needsOutputStream());
  273. }
  274. } else {
  275. outputStreamMissing &= (out == null) && (rendMaker.needsOutputStream());
  276. }
  277. if (userAgent.getRendererOverride() != null
  278. || rendMaker != null
  279. || documentHandlerMaker != null) {
  280. if (outputStreamMissing) {
  281. throw new FOPException(
  282. "OutputStream has not been set");
  283. }
  284. //Found a Renderer so we need to construct an AreaTreeHandler.
  285. return new AreaTreeHandler(userAgent, outputFormat, out);
  286. } else {
  287. throw new UnsupportedOperationException(
  288. "Don't know how to handle \"" + outputFormat + "\" as an output format."
  289. + " Neither an FOEventHandler, nor a Renderer could be found"
  290. + " for this output format.");
  291. }
  292. }
  293. }
  294. }
  295. /**
  296. * Creates a {@code IFDocumentHandler} object based on the desired output format.
  297. * @param userAgent the user agent for access to configuration
  298. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  299. * @return the new {@code IFDocumentHandler} instance
  300. * @throws FOPException if the document handler cannot be properly constructed
  301. */
  302. public IFDocumentHandler createDocumentHandler(FOUserAgent userAgent, String outputFormat)
  303. throws FOPException {
  304. AbstractIFDocumentHandlerMaker maker = getDocumentHandlerMaker(outputFormat);
  305. if (maker == null) {
  306. throw new UnsupportedOperationException(
  307. "No IF document handler for the requested format available: " + outputFormat);
  308. }
  309. IFDocumentHandler documentHandler = maker.makeIFDocumentHandler(userAgent);
  310. IFDocumentHandlerConfigurator configurator = documentHandler.getConfigurator();
  311. if (configurator != null) {
  312. configurator.configure(documentHandler);
  313. }
  314. return documentHandler;
  315. }
  316. /**
  317. * @return an array of all supported MIME types
  318. */
  319. public String[] listSupportedMimeTypes() {
  320. List lst = new java.util.ArrayList();
  321. Iterator iter = this.rendererMakerMapping.keySet().iterator();
  322. while (iter.hasNext()) {
  323. lst.add(((String)iter.next()));
  324. }
  325. iter = this.eventHandlerMakerMapping.keySet().iterator();
  326. while (iter.hasNext()) {
  327. lst.add(((String)iter.next()));
  328. }
  329. iter = this.documentHandlerMakerMapping.keySet().iterator();
  330. while (iter.hasNext()) {
  331. lst.add(((String)iter.next()));
  332. }
  333. Collections.sort(lst);
  334. return (String[])lst.toArray(new String[lst.size()]);
  335. }
  336. /**
  337. * Discovers Renderer implementations through the classpath and dynamically
  338. * registers them.
  339. */
  340. private void discoverRenderers() {
  341. // add mappings from available services
  342. Iterator providers
  343. = Service.providers(Renderer.class);
  344. if (providers != null) {
  345. while (providers.hasNext()) {
  346. AbstractRendererMaker maker = (AbstractRendererMaker)providers.next();
  347. try {
  348. if (log.isDebugEnabled()) {
  349. log.debug("Dynamically adding maker for Renderer: "
  350. + maker.getClass().getName());
  351. }
  352. addRendererMaker(maker);
  353. } catch (IllegalArgumentException e) {
  354. log.error("Error while adding maker for Renderer", e);
  355. }
  356. }
  357. }
  358. }
  359. /**
  360. * Discovers FOEventHandler implementations through the classpath and dynamically
  361. * registers them.
  362. */
  363. private void discoverFOEventHandlers() {
  364. // add mappings from available services
  365. Iterator providers
  366. = Service.providers(FOEventHandler.class);
  367. if (providers != null) {
  368. while (providers.hasNext()) {
  369. AbstractFOEventHandlerMaker maker = (AbstractFOEventHandlerMaker)providers.next();
  370. try {
  371. if (log.isDebugEnabled()) {
  372. log.debug("Dynamically adding maker for FOEventHandler: "
  373. + maker.getClass().getName());
  374. }
  375. addFOEventHandlerMaker(maker);
  376. } catch (IllegalArgumentException e) {
  377. log.error("Error while adding maker for FOEventHandler", e);
  378. }
  379. }
  380. }
  381. }
  382. /**
  383. * Discovers {@code IFDocumentHandler} implementations through the classpath and dynamically
  384. * registers them.
  385. */
  386. private void discoverDocumentHandlers() {
  387. // add mappings from available services
  388. Iterator providers = Service.providers(IFDocumentHandler.class);
  389. if (providers != null) {
  390. while (providers.hasNext()) {
  391. AbstractIFDocumentHandlerMaker maker = (AbstractIFDocumentHandlerMaker)providers.next();
  392. try {
  393. if (log.isDebugEnabled()) {
  394. log.debug("Dynamically adding maker for IFDocumentHandler: "
  395. + maker.getClass().getName());
  396. }
  397. addDocumentHandlerMaker(maker);
  398. } catch (IllegalArgumentException e) {
  399. log.error("Error while adding maker for IFDocumentHandler", e);
  400. }
  401. }
  402. }
  403. }
  404. }