KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > image > analyser > XMLReader


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
18 /* $Id: XMLReader.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.image.analyser;
21
22 // Java
23
import java.io.InputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Map JavaDoc;
26
27 // XML
28
import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31
32 // FOP
33
import org.apache.fop.image.FopImage;
34 import org.apache.fop.apps.FOUserAgent;
35
36 // Commons-Logging
37
import org.apache.commons.io.IOUtils;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 /** ImageReader object for XML document image type. */
42 public class XMLReader implements ImageReader {
43
44     /**
45      * logging instance
46      */

47     private Log log = LogFactory.getLog(XMLReader.class);
48
49     private static Map JavaDoc converters = new java.util.HashMap JavaDoc();
50
51     /**
52      * Registers a Converter implementation with XMLReader.
53      *
54      * @param ns The namespace to associate with this converter
55      * @param conv The actual Converter implementation
56      */

57     public static void setConverter(String JavaDoc ns, Converter conv) {
58         converters.put(ns, conv);
59     }
60
61     /** @see org.apache.fop.image.analyser.ImageReader */
62     public FopImage.ImageInfo verifySignature(String JavaDoc uri, InputStream JavaDoc fis,
63             FOUserAgent ua)
64         throws IOException JavaDoc {
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     /**
74      * Returns the MIME type supported by this implementation.
75      *
76      * @return The MIME type
77      */

78     public String JavaDoc getMimeType() {
79         return "text/xml";
80     }
81
82     /**
83      * Creates an ImageInfo object from an XML image read from a stream.
84      *
85      * (todo) This means the external svg document will be loaded twice. Possibly need
86      * a slightly different design for the image stuff.
87      *
88      * @param uri The URI to the image
89      * @param bis The InputStream
90      * @param ua The user agent
91      * @return An ImageInfo object describing the image
92      */

93     protected FopImage.ImageInfo loadImage(String JavaDoc uri, InputStream JavaDoc bis,
94             FOUserAgent ua) {
95         return createDocument(bis, ua);
96     }
97
98     /**
99      * Creates an ImageInfo object from an XML image read from a stream.
100      *
101      * @param is The InputStream
102      * @param ua The user agent
103      * @return An ImageInfo object describing the image
104      */

105     public FopImage.ImageInfo createDocument(InputStream JavaDoc is, FOUserAgent ua) {
106         Document JavaDoc 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 JavaDoc dbf = DocumentBuilderFactory.newInstance();
115             doc = dbf.newDocumentBuilder().parse(is);
116             info.data = doc;
117
118             Element JavaDoc root = doc.getDocumentElement();
119             log.debug("XML image namespace: " + root.getAttribute("xmlns"));
120             String JavaDoc 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 JavaDoc e) {
131             log.warn("Error while constructing image from XML", e);
132             try {
133                 is.reset();
134             } catch (IOException JavaDoc ioe) {
135                 // throw the original exception, not this one
136
}
137             return null;
138         }
139         return info;
140     }
141
142     /**
143      * This interface is to be implemented for XML to image converters.
144      */

145     public static interface Converter {
146
147         /**
148          * This method is called for a DOM document to be converted into an
149          * ImageInfo object.
150          *
151          * @param doc The DOM document to convert
152          * @return An ImageInfo object describing the image
153          */

154         FopImage.ImageInfo convert(Document JavaDoc doc);
155     }
156
157 }
158
159
Popular Tags