1 17 18 19 20 package org.apache.fop.image.analyser; 21 22 import java.io.InputStream ; 24 import java.io.IOException ; 25 import java.util.Map ; 26 27 import javax.xml.parsers.DocumentBuilderFactory ; 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Element ; 31 32 import org.apache.fop.image.FopImage; 34 import org.apache.fop.apps.FOUserAgent; 35 36 import org.apache.commons.io.IOUtils; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 42 public class XMLReader implements ImageReader { 43 44 47 private Log log = LogFactory.getLog(XMLReader.class); 48 49 private static Map converters = new java.util.HashMap (); 50 51 57 public static void setConverter(String ns, Converter conv) { 58 converters.put(ns, conv); 59 } 60 61 62 public FopImage.ImageInfo verifySignature(String uri, InputStream fis, 63 FOUserAgent ua) 64 throws IOException { 65 FopImage.ImageInfo info = loadImage(uri, fis, ua); 66 info.originalURI = uri; 67 if (info != null) { 68 IOUtils.closeQuietly(fis); 69 } 70 return info; 71 } 72 73 78 public String getMimeType() { 79 return "text/xml"; 80 } 81 82 93 protected FopImage.ImageInfo loadImage(String uri, InputStream bis, 94 FOUserAgent ua) { 95 return createDocument(bis, ua); 96 } 97 98 105 public FopImage.ImageInfo createDocument(InputStream is, FOUserAgent ua) { 106 Document doc = null; 107 FopImage.ImageInfo info = new FopImage.ImageInfo(); 108 info.mimeType = getMimeType(); 109 110 try { 111 int length = is.available(); 112 is.mark(length); 113 114 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 115 doc = dbf.newDocumentBuilder().parse(is); 116 info.data = doc; 117 118 Element root = doc.getDocumentElement(); 119 log.debug("XML image namespace: " + root.getAttribute("xmlns")); 120 String ns = root.getAttribute("xmlns"); 121 info.str = ns; 122 123 Converter conv = (Converter) converters.get(ns); 124 if (conv != null) { 125 FopImage.ImageInfo i = conv.convert(doc); 126 if (i != null) { 127 info = i; 128 } 129 } 130 } catch (Exception e) { 131 log.warn("Error while constructing image from XML", e); 132 try { 133 is.reset(); 134 } catch (IOException ioe) { 135 } 137 return null; 138 } 139 return info; 140 } 141 142 145 public static interface Converter { 146 147 154 FopImage.ImageInfo convert(Document doc); 155 } 156 157 } 158 159 | Popular Tags |