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.2KB

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