diff options
author | Keiron Liddle <keiron@apache.org> | 2002-03-08 11:00:20 +0000 |
---|---|---|
committer | Keiron Liddle <keiron@apache.org> | 2002-03-08 11:00:20 +0000 |
commit | f0e488222854d0a838094936b6929e083c15230b (patch) | |
tree | fff738398f5429595b48cfe4270ab9cd91ef98f6 /src/org/apache/fop/image | |
parent | 8f766d59eba2df9b0600b2fad11b098cb589a64f (diff) | |
download | xmlgraphics-fop-f0e488222854d0a838094936b6929e083c15230b.tar.gz xmlgraphics-fop-f0e488222854d0a838094936b6929e083c15230b.zip |
renders images and ifo to pdf doc
added xml reader for external graphics
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194696 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/org/apache/fop/image')
-rw-r--r-- | src/org/apache/fop/image/AbstractFopImage.java | 6 | ||||
-rw-r--r-- | src/org/apache/fop/image/FopImage.java | 1 | ||||
-rw-r--r-- | src/org/apache/fop/image/ImageFactory.java | 4 | ||||
-rw-r--r-- | src/org/apache/fop/image/XMLImage.java (renamed from src/org/apache/fop/image/SVGImage.java) | 30 | ||||
-rw-r--r-- | src/org/apache/fop/image/analyser/ImageReaderFactory.java | 3 | ||||
-rw-r--r-- | src/org/apache/fop/image/analyser/SVGReader.java | 65 | ||||
-rw-r--r-- | src/org/apache/fop/image/analyser/XMLReader.java | 98 |
7 files changed, 180 insertions, 27 deletions
diff --git a/src/org/apache/fop/image/AbstractFopImage.java b/src/org/apache/fop/image/AbstractFopImage.java index 3fa021eea..9b3a91d73 100644 --- a/src/org/apache/fop/image/AbstractFopImage.java +++ b/src/org/apache/fop/image/AbstractFopImage.java @@ -100,7 +100,7 @@ public abstract class AbstractFopImage implements FopImage { this.imageInfo = info; this.m_width = this.imageInfo.width; this.m_height = this.imageInfo.height; - loaded = loaded & DIMENSIONS; + loaded = loaded | DIMENSIONS; } public String getMimeType() { @@ -121,12 +121,12 @@ public abstract class AbstractFopImage implements FopImage { if(!success) { return false; } - loaded = loaded & DIMENSIONS; + loaded = loaded | DIMENSIONS; } if(((type & BITMAP) != 0) && ((loaded & BITMAP) == 0)) { success = success && loadBitmap(ua); if(success) { - loaded = loaded & BITMAP; + loaded = loaded | BITMAP; } } return success; diff --git a/src/org/apache/fop/image/FopImage.java b/src/org/apache/fop/image/FopImage.java index 703616aae..d7b60a62c 100644 --- a/src/org/apache/fop/image/FopImage.java +++ b/src/org/apache/fop/image/FopImage.java @@ -70,6 +70,7 @@ public interface FopImage { public int height; public Object data; public String mimeType; + public String str; } } diff --git a/src/org/apache/fop/image/ImageFactory.java b/src/org/apache/fop/image/ImageFactory.java index e2217ba56..6eccd37c2 100644 --- a/src/org/apache/fop/image/ImageFactory.java +++ b/src/org/apache/fop/image/ImageFactory.java @@ -186,7 +186,9 @@ public class ImageFactory { imgClassName = "org.apache.fop.image.JimiImage"; // imgClassName = "org.apache.fop.image.JAIImage"; } else if ("image/svg+xml".equals(imgMimeType)) { - imgClassName = "org.apache.fop.image.SVGImage"; + imgClassName = "org.apache.fop.image.XMLImage"; + } else if ("text/xml".equals(imgMimeType)) { + imgClassName = "org.apache.fop.image.XMLImage"; } if (imgClassName == null) { log.error("Unsupported image type (" + diff --git a/src/org/apache/fop/image/SVGImage.java b/src/org/apache/fop/image/XMLImage.java index 95d7b5d09..a6e47df50 100644 --- a/src/org/apache/fop/image/SVGImage.java +++ b/src/org/apache/fop/image/XMLImage.java @@ -9,7 +9,7 @@ package org.apache.fop.image; // Java import java.net.URL; -import org.w3c.dom.svg.SVGDocument; +import org.w3c.dom.Document; // FOP import org.apache.fop.apps.Driver; @@ -17,20 +17,20 @@ import org.apache.fop.image.analyser.ImageReader; import org.apache.fop.image.analyser.SVGReader; import org.apache.fop.fo.FOUserAgent; -import org.apache.batik.dom.svg.SAXSVGDocumentFactory; - /** * @see AbstractFopImage * @see FopImage */ -public class SVGImage extends AbstractFopImage { - SVGDocument doc; +public class XMLImage extends AbstractFopImage { + Document doc; + String ns = ""; - public SVGImage(URL href, FopImage.ImageInfo imgInfo) { + public XMLImage(URL href, FopImage.ImageInfo imgInfo) { super(href, imgInfo); - if(imgInfo.data instanceof SVGDocument) { - doc = (SVGDocument)imgInfo.data; + if(imgInfo.data instanceof Document) { + doc = (Document)imgInfo.data; } + ns = imgInfo.str; } /** @@ -45,20 +45,14 @@ public class SVGImage extends AbstractFopImage { } protected boolean loadData(FOUserAgent ua) { - try { - SAXSVGDocumentFactory factory = - new SAXSVGDocumentFactory(SVGImage.getParserName()); - doc = factory.createDocument(this.m_href.toExternalForm()); - } catch (Exception e) { - ua.getLogger().error("Could not load external SVG: " - + e.getMessage(), e); - return false; - } return true; } - public SVGDocument getSVGDocument() { + public Document getDocument() { return doc; } + public String getNameSpace() { + return ns; + } } diff --git a/src/org/apache/fop/image/analyser/ImageReaderFactory.java b/src/org/apache/fop/image/analyser/ImageReaderFactory.java index 8d45e48a7..66d6aeb35 100644 --- a/src/org/apache/fop/image/analyser/ImageReaderFactory.java +++ b/src/org/apache/fop/image/analyser/ImageReaderFactory.java @@ -30,7 +30,10 @@ public class ImageReaderFactory { formats.add(new PNGReader()); formats.add(new TIFFReader()); formats.add(new EPSReader()); + // the xml parser through batik closes the stream when finished + // so there is a workaround in the SVGReader formats.add(new SVGReader()); + formats.add(new XMLReader()); }; // TODO - a way to add other readers diff --git a/src/org/apache/fop/image/analyser/SVGReader.java b/src/org/apache/fop/image/analyser/SVGReader.java index feecadcb2..8791b3420 100644 --- a/src/org/apache/fop/image/analyser/SVGReader.java +++ b/src/org/apache/fop/image/analyser/SVGReader.java @@ -15,7 +15,7 @@ import org.w3c.dom.svg.SVGDocument; import org.w3c.dom.svg.SVGSVGElement; // FOP -import org.apache.fop.image.SVGImage; +import org.apache.fop.image.XMLImage; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; @@ -38,6 +38,7 @@ import org.w3c.dom.DOMImplementation; import org.apache.batik.dom.svg.SVGDOMImplementation; import java.io.File; +import java.io.InputStream; import java.net.URL; import java.util.List; import java.util.ArrayList; @@ -94,18 +95,69 @@ public class SVGReader implements ImageReader { * the SVGReader class. */ class Loader { - private FopImage.ImageInfo getImage(String uri, BufferedInputStream fis, + private FopImage.ImageInfo getImage(String uri, InputStream fis, FOUserAgent ua) { // parse document and get the size attributes of the svg element try { + int length = 5; + fis.mark(length); + byte[] b = new byte[length]; + fis.read(b); + String start = new String(b); + fis.reset(); + + if(start.equals("<?xml")) { + // we have xml, might be another doc + // so stop batik from closing the stream + final InputStream input = fis; + fis = new InputStream() { + public int read() throws IOException { + return input.read(); + } + + public int read(byte[] b) throws IOException { + return input.read(b); + } + + public int read(byte[] b, int off, int len) throws IOException { + return input.read(b, off, len); + } + + public long skip(long n) throws IOException { + return input.skip(n); + } + + public int available() throws IOException { + return input.available(); + } + + public void mark(int rl) { + input.mark(rl); + } + + public boolean markSupported() { + return input.markSupported(); + } + + public void reset() throws IOException { + input.reset(); + } + + public void close() throws IOException { + } + }; + } + FopImage.ImageInfo info = new FopImage.ImageInfo(); + info.mimeType = getMimeType(); + info.str = SVGDOMImplementation.SVG_NAMESPACE_URI; - int length = fis.available(); - fis.mark(length); + length = fis.available(); + fis.mark(length + 1); SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory( - SVGImage.getParserName()); + XMLImage.getParserName()); SVGDocument doc = factory.createDocument(uri, fis); info.data = doc; @@ -138,6 +190,9 @@ public class SVGReader implements ImageReader { return info; } catch (NoClassDefFoundError ncdfe) { + try { + fis.reset(); + } catch (IOException ioe) { } batik = false; //ua.getLogger().error("Batik not in class path", ncdfe); return null; diff --git a/src/org/apache/fop/image/analyser/XMLReader.java b/src/org/apache/fop/image/analyser/XMLReader.java new file mode 100644 index 000000000..ab66be981 --- /dev/null +++ b/src/org/apache/fop/image/analyser/XMLReader.java @@ -0,0 +1,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); + } +} + |