aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/image/analyser/XMLReader.java
blob: 5bce255f75868e4bc91e92bd4dfdf2b8c4b6d572 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * $Id$
 * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources.
 */

package org.apache.fop.image.analyser;

// Java
import java.io.BufferedInputStream;
import java.io.IOException;

import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.*;
import org.w3c.dom.DOMImplementation;

import java.io.File;
import java.net.URL;
import java.util.HashMap;

import org.apache.fop.image.FopImage;
import org.apache.fop.fo.FOUserAgent;
import org.apache.fop.image.XMLImage;

/**
 * ImageReader object for XML document image type.
 */
public class XMLReader implements ImageReader {
    private static HashMap converters = new HashMap();

    public static void setConverter(String ns, Converter conv) {
        converters.put(ns, conv);
    }

    public XMLReader() {
    }

    public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis,
                                   FOUserAgent ua) throws IOException {
        return loadImage(uri, fis, ua);
    }

    public String getMimeType() {
        return "text/xml";
    }

    /**
     * This means the external svg document will be loaded twice.
     * Possibly need a slightly different design for the image stuff.
     */
    protected FopImage.ImageInfo loadImage(String uri, BufferedInputStream fis,
                                   FOUserAgent ua) {
        return createDocument(fis, ua);
    }

    public FopImage.ImageInfo createDocument(BufferedInputStream is, FOUserAgent ua) {
        Document doc = null;
        FopImage.ImageInfo info = new FopImage.ImageInfo();
        info.mimeType = getMimeType();

        try {
            int length = is.available();
            is.mark(length);

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            doc = dbf.newDocumentBuilder().parse(is);
            info.data = doc;

            Element root = doc.getDocumentElement();
            ua.getLogger().debug("ns:" + root.getAttribute("xmlns"));
            String ns = root.getAttribute("xmlns");
            info.str = ns;

            Converter conv = (Converter)converters.get(ns);
            if(conv != null) {
                FopImage.ImageInfo i = conv.convert(doc);
                if(i != null) {
                    info = i;
                }
            }
        } catch (Exception e) {
            //e.printStackTrace();
            try {
                is.reset();
            } catch (IOException ioe) { }
            return null;
        }
        return info;
    }

    public static interface Converter {
        public FopImage.ImageInfo convert(Document doc);
    }
}