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.

XMLReader.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.analyser;
  19. // Java
  20. import java.io.InputStream;
  21. import java.io.IOException;
  22. import java.util.Map;
  23. // XML
  24. import javax.xml.parsers.DocumentBuilderFactory;
  25. import org.w3c.dom.Document;
  26. import org.w3c.dom.Element;
  27. // FOP
  28. import org.apache.fop.image.FopImage;
  29. import org.apache.fop.util.UnclosableInputStream;
  30. import org.apache.fop.apps.FOUserAgent;
  31. // Commons-Logging
  32. import org.apache.commons.io.IOUtils;
  33. import org.apache.commons.logging.Log;
  34. import org.apache.commons.logging.LogFactory;
  35. /** ImageReader object for XML document image type. */
  36. public class XMLReader implements ImageReader {
  37. /**
  38. * logging instance
  39. */
  40. private Log log = LogFactory.getLog(XMLReader.class);
  41. private static Map converters = new java.util.HashMap();
  42. /**
  43. * Registers a Converter implementation with XMLReader.
  44. *
  45. * @param ns The namespace to associate with this converter
  46. * @param conv The actual Converter implementation
  47. */
  48. public static void setConverter(String ns, Converter conv) {
  49. converters.put(ns, conv);
  50. }
  51. /** {@inheritDoc} */
  52. public FopImage.ImageInfo verifySignature(String uri, InputStream fis,
  53. FOUserAgent ua)
  54. throws IOException {
  55. FopImage.ImageInfo info = loadImage(uri, fis, ua);
  56. if (info != null) {
  57. info.originalURI = uri;
  58. IOUtils.closeQuietly(fis);
  59. }
  60. return info;
  61. }
  62. /**
  63. * Returns the MIME type supported by this implementation.
  64. *
  65. * @return The MIME type
  66. */
  67. public String getMimeType() {
  68. return "text/xml";
  69. }
  70. /**
  71. * Creates an ImageInfo object from an XML image read from a stream.
  72. *
  73. * (todo) This means the external svg document will be loaded twice. Possibly need
  74. * a slightly different design for the image stuff.
  75. *
  76. * @param uri The URI to the image
  77. * @param bis The InputStream
  78. * @param ua The user agent
  79. * @return An ImageInfo object describing the image
  80. */
  81. protected FopImage.ImageInfo loadImage(String uri, InputStream bis,
  82. FOUserAgent ua) {
  83. return createDocument(bis, ua);
  84. }
  85. /**
  86. * Creates an ImageInfo object from an XML image read from a stream.
  87. *
  88. * @param input The InputStream
  89. * @param ua The user agent
  90. * @return An ImageInfo object describing the image
  91. */
  92. public FopImage.ImageInfo createDocument(final InputStream input, final FOUserAgent ua) {
  93. Document doc = null;
  94. FopImage.ImageInfo info = new FopImage.ImageInfo();
  95. info.mimeType = getMimeType();
  96. try {
  97. final InputStream is = new UnclosableInputStream(input);
  98. int length = is.available();
  99. is.mark(length);
  100. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  101. doc = dbf.newDocumentBuilder().parse(is);
  102. info.data = doc;
  103. Element root = doc.getDocumentElement();
  104. log.debug("XML image namespace: " + root.getAttribute("xmlns"));
  105. String ns = root.getAttribute("xmlns");
  106. info.str = ns;
  107. Converter conv = (Converter) converters.get(ns);
  108. if (conv != null) {
  109. FopImage.ImageInfo i = conv.convert(doc);
  110. if (i != null) {
  111. info = i;
  112. }
  113. }
  114. } catch (Exception e) {
  115. log.debug("Error while constructing image from XML", e);
  116. try {
  117. input.reset();
  118. } catch (IOException ioe) {
  119. // throw the original exception, not this one
  120. }
  121. return null;
  122. }
  123. if (info != null) {
  124. try {
  125. input.close();
  126. } catch (IOException io) {
  127. // ignore
  128. }
  129. }
  130. return info;
  131. }
  132. /**
  133. * This interface is to be implemented for XML to image converters.
  134. */
  135. public static interface Converter {
  136. /**
  137. * This method is called for a DOM document to be converted into an
  138. * ImageInfo object.
  139. *
  140. * @param doc The DOM document to convert
  141. * @return An ImageInfo object describing the image
  142. */
  143. FopImage.ImageInfo convert(Document doc);
  144. }
  145. }