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.

PreloaderPlan.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.plan;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import javax.xml.transform.ErrorListener;
  22. import javax.xml.transform.Source;
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.TransformerException;
  25. import javax.xml.transform.TransformerFactory;
  26. import javax.xml.transform.dom.DOMResult;
  27. import javax.xml.transform.stream.StreamSource;
  28. import org.w3c.dom.Document;
  29. import org.w3c.dom.Element;
  30. import org.apache.commons.logging.Log;
  31. import org.apache.commons.logging.LogFactory;
  32. import org.apache.xmlgraphics.image.loader.Image;
  33. import org.apache.xmlgraphics.image.loader.ImageContext;
  34. import org.apache.xmlgraphics.image.loader.ImageInfo;
  35. import org.apache.xmlgraphics.image.loader.ImageSize;
  36. import org.apache.xmlgraphics.image.loader.impl.AbstractImagePreloader;
  37. import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
  38. import org.apache.xmlgraphics.image.loader.util.ImageUtil;
  39. import org.apache.fop.util.DefaultErrorListener;
  40. import org.apache.fop.util.UnclosableInputStream;
  41. /**
  42. * Image preloader for Plan images.
  43. */
  44. public class PreloaderPlan extends AbstractImagePreloader {
  45. /** Logger instance */
  46. private static Log log = LogFactory.getLog(PreloaderPlan.class);
  47. /** {@inheritDoc} */
  48. public ImageInfo preloadImage(String uri, Source src, ImageContext context)
  49. throws IOException {
  50. if (!ImageUtil.hasInputStream(src)) {
  51. //TODO Remove this and support DOMSource and possibly SAXSource
  52. return null;
  53. }
  54. ImageInfo info = getImage(uri, src, context);
  55. if (info != null) {
  56. ImageUtil.closeQuietly(src); //Image is fully read
  57. }
  58. return info;
  59. }
  60. private ImageInfo getImage(String uri, Source src, ImageContext context) throws IOException {
  61. InputStream in = new UnclosableInputStream(ImageUtil.needInputStream(src));
  62. try {
  63. Document planDoc = getDocument(in);
  64. Element rootEl = planDoc.getDocumentElement();
  65. if (!PlanElementMapping.NAMESPACE.equals(
  66. rootEl.getNamespaceURI())) {
  67. in.reset();
  68. return null;
  69. }
  70. //Have to render the plan to know its size
  71. PlanRenderer pr = new PlanRenderer();
  72. Document svgDoc = pr.createSVGDocument(planDoc);
  73. float width = pr.getWidth();
  74. float height = pr.getHeight();
  75. //Return converted SVG image
  76. ImageInfo info = new ImageInfo(uri, "image/svg+xml");
  77. final ImageSize size = new ImageSize();
  78. size.setSizeInMillipoints(
  79. Math.round(width * 1000),
  80. Math.round(height * 1000));
  81. //Set the resolution to that of the FOUserAgent
  82. size.setResolution(context.getSourceResolution());
  83. size.calcPixelsFromSize();
  84. info.setSize(size);
  85. //The whole image had to be loaded for this, so keep it
  86. Image image = new ImageXMLDOM(info, svgDoc,
  87. svgDoc.getDocumentElement().getNamespaceURI());
  88. info.getCustomObjects().put(ImageInfo.ORIGINAL_IMAGE, image);
  89. return info;
  90. } catch (TransformerException e) {
  91. try {
  92. in.reset();
  93. } catch (IOException ioe) {
  94. // we're more interested in the original exception
  95. }
  96. log.debug("Error while trying to parsing a Plan file: "
  97. + e.getMessage());
  98. return null;
  99. }
  100. }
  101. private Document getDocument(InputStream in) throws TransformerException {
  102. TransformerFactory tFactory = TransformerFactory.newInstance();
  103. //Custom error listener to minimize output to console
  104. ErrorListener errorListener = new DefaultErrorListener(log);
  105. tFactory.setErrorListener(errorListener);
  106. Transformer transformer = tFactory.newTransformer();
  107. transformer.setErrorListener(errorListener);
  108. Source source = new StreamSource(in);
  109. DOMResult res = new DOMResult();
  110. transformer.transform(source, res);
  111. Document doc = (Document)res.getNode();
  112. return doc;
  113. }
  114. }