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

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