001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: XMLReader.java 542447 2007-05-29 07:51:52Z vhennebert $ */
019:
020: package org.apache.fop.image.analyser;
021:
022: // Java
023: import java.io.InputStream;
024: import java.io.IOException;
025: import java.util.Map;
026:
027: // XML
028: import javax.xml.parsers.DocumentBuilderFactory;
029: import org.w3c.dom.Document;
030: import org.w3c.dom.Element;
031:
032: // FOP
033: import org.apache.fop.image.FopImage;
034: import org.apache.fop.util.UnclosableInputStream;
035: import org.apache.fop.apps.FOUserAgent;
036:
037: // Commons-Logging
038: import org.apache.commons.io.IOUtils;
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041:
042: /** ImageReader object for XML document image type. */
043: public class XMLReader implements ImageReader {
044:
045: /**
046: * logging instance
047: */
048: private Log log = LogFactory.getLog(XMLReader.class);
049:
050: private static Map converters = new java.util.HashMap();
051:
052: /**
053: * Registers a Converter implementation with XMLReader.
054: *
055: * @param ns The namespace to associate with this converter
056: * @param conv The actual Converter implementation
057: */
058: public static void setConverter(String ns, Converter conv) {
059: converters.put(ns, conv);
060: }
061:
062: /** @see org.apache.fop.image.analyser.ImageReader */
063: public FopImage.ImageInfo verifySignature(String uri,
064: InputStream fis, FOUserAgent ua) throws IOException {
065: FopImage.ImageInfo info = loadImage(uri, fis, ua);
066: if (info != null) {
067: info.originalURI = uri;
068: IOUtils.closeQuietly(fis);
069: }
070: return info;
071: }
072:
073: /**
074: * Returns the MIME type supported by this implementation.
075: *
076: * @return The MIME type
077: */
078: public String getMimeType() {
079: return "text/xml";
080: }
081:
082: /**
083: * Creates an ImageInfo object from an XML image read from a stream.
084: *
085: * (todo) This means the external svg document will be loaded twice. Possibly need
086: * a slightly different design for the image stuff.
087: *
088: * @param uri The URI to the image
089: * @param bis The InputStream
090: * @param ua The user agent
091: * @return An ImageInfo object describing the image
092: */
093: protected FopImage.ImageInfo loadImage(String uri, InputStream bis,
094: FOUserAgent ua) {
095: return createDocument(bis, ua);
096: }
097:
098: /**
099: * Creates an ImageInfo object from an XML image read from a stream.
100: *
101: * @param input The InputStream
102: * @param ua The user agent
103: * @return An ImageInfo object describing the image
104: */
105: public FopImage.ImageInfo createDocument(final InputStream input,
106: final FOUserAgent ua) {
107: Document doc = null;
108: FopImage.ImageInfo info = new FopImage.ImageInfo();
109: info.mimeType = getMimeType();
110:
111: try {
112: final InputStream is = new UnclosableInputStream(input);
113: int length = is.available();
114: is.mark(length);
115:
116: DocumentBuilderFactory dbf = DocumentBuilderFactory
117: .newInstance();
118: doc = dbf.newDocumentBuilder().parse(is);
119: info.data = doc;
120:
121: Element root = doc.getDocumentElement();
122: log.debug("XML image namespace: "
123: + root.getAttribute("xmlns"));
124: String ns = root.getAttribute("xmlns");
125: info.str = ns;
126:
127: Converter conv = (Converter) converters.get(ns);
128: if (conv != null) {
129: FopImage.ImageInfo i = conv.convert(doc);
130: if (i != null) {
131: info = i;
132: }
133: }
134: } catch (Exception e) {
135: log.debug("Error while constructing image from XML", e);
136: try {
137: input.reset();
138: } catch (IOException ioe) {
139: // throw the original exception, not this one
140: }
141: return null;
142: }
143: if (info != null) {
144: try {
145: input.close();
146: } catch (IOException io) {
147: // ignore
148: }
149: }
150: return info;
151: }
152:
153: /**
154: * This interface is to be implemented for XML to image converters.
155: */
156: public static interface Converter {
157:
158: /**
159: * This method is called for a DOM document to be converted into an
160: * ImageInfo object.
161: *
162: * @param doc The DOM document to convert
163: * @return An ImageInfo object describing the image
164: */
165: FopImage.ImageInfo convert(Document doc);
166: }
167:
168: }
|