Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PreloaderWMF.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.image.loader.batik;
  19. import java.io.DataInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import javax.xml.transform.Source;
  23. import org.apache.commons.io.EndianUtils;
  24. import org.apache.commons.io.IOUtils;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.apache.batik.transcoder.wmf.WMFConstants;
  28. import org.apache.batik.transcoder.wmf.tosvg.WMFRecordStore;
  29. import org.apache.xmlgraphics.image.loader.ImageContext;
  30. import org.apache.xmlgraphics.image.loader.ImageInfo;
  31. import org.apache.xmlgraphics.image.loader.ImageSize;
  32. import org.apache.xmlgraphics.image.loader.impl.AbstractImagePreloader;
  33. import org.apache.xmlgraphics.image.loader.util.ImageUtil;
  34. import org.apache.xmlgraphics.io.XmlSourceUtil;
  35. import org.apache.fop.util.UnclosableInputStream;
  36. /**
  37. * Image preloader for WMF images (Windows Metafile).
  38. */
  39. public class PreloaderWMF extends AbstractImagePreloader {
  40. /** Logger instance */
  41. private static Log log = LogFactory.getLog(PreloaderWMF.class);
  42. private boolean batikAvailable = true;
  43. /** {@inheritDoc} */
  44. public ImageInfo preloadImage(String uri, Source src, ImageContext context)
  45. throws IOException {
  46. if (!ImageUtil.hasInputStream(src)) {
  47. return null;
  48. }
  49. ImageInfo info = null;
  50. if (batikAvailable) {
  51. try {
  52. Loader loader = new Loader();
  53. info = loader.getImage(uri, src, context);
  54. } catch (NoClassDefFoundError e) {
  55. batikAvailable = false;
  56. log.warn("Batik not in class path", e);
  57. return null;
  58. }
  59. }
  60. if (info != null) {
  61. XmlSourceUtil.closeQuietly(src); //Image is fully read
  62. }
  63. return info;
  64. }
  65. /**
  66. * This method is put in another class so that the class loader does not
  67. * attempt to load Batik related classes when constructing the WMFPreloader
  68. * class.
  69. */
  70. private final class Loader {
  71. private Loader() {
  72. }
  73. private ImageInfo getImage(String uri, Source src,
  74. ImageContext context) {
  75. // parse document and get the size attributes of the svg element
  76. InputStream in = new UnclosableInputStream(XmlSourceUtil.needInputStream(src));
  77. try {
  78. in.mark(4 + 1);
  79. DataInputStream din = new DataInputStream(in);
  80. int magic = EndianUtils.swapInteger(din.readInt());
  81. din.reset();
  82. if (magic != WMFConstants.META_ALDUS_APM) {
  83. return null; //Not a WMF file
  84. }
  85. WMFRecordStore wmfStore = new WMFRecordStore();
  86. wmfStore.read(din);
  87. IOUtils.closeQuietly(din);
  88. int width = wmfStore.getWidthUnits();
  89. int height = wmfStore.getHeightUnits();
  90. int dpi = wmfStore.getMetaFileUnitsPerInch();
  91. ImageInfo info = new ImageInfo(uri, "image/x-wmf");
  92. ImageSize size = new ImageSize();
  93. size.setSizeInPixels(width, height);
  94. size.setResolution(dpi);
  95. size.calcSizeFromPixels();
  96. info.setSize(size);
  97. ImageWMF img = new ImageWMF(info, wmfStore);
  98. info.getCustomObjects().put(ImageInfo.ORIGINAL_IMAGE, img);
  99. return info;
  100. } catch (NoClassDefFoundError ncdfe) {
  101. try {
  102. in.reset();
  103. } catch (IOException ioe) {
  104. // we're more interested in the original exception
  105. }
  106. batikAvailable = false;
  107. log.warn("Batik not in class path", ncdfe);
  108. return null;
  109. } catch (IOException e) {
  110. // If the svg is invalid then it throws an IOException
  111. // so there is no way of knowing if it is an svg document
  112. log.debug("Error while trying to load stream as an WMF file: "
  113. + e.getMessage());
  114. // assuming any exception means this document is not svg
  115. // or could not be loaded for some reason
  116. try {
  117. in.reset();
  118. } catch (IOException ioe) {
  119. // we're more interested in the original exception
  120. }
  121. return null;
  122. }
  123. }
  124. }
  125. }