From 2d39fa7f95efb645d2ae704524d91db7899274ef Mon Sep 17 00:00:00 2001
From: kitfox
Date: Sat, 7 Aug 2010 09:59:11 +0000
Subject: Added support for inline images.
git-svn-id: https://svn.java.net/svn/svgsalamander~svn/trunk/svg-core@79 7dc7fa77-23fb-e6ad-8e2e-c86bd48ed22b
---
nbproject/build-impl.xml | 246 +-
nbproject/genfiles.properties | 4 +-
src/main/java/com/kitfox/svg/ImageSVG.java | 19 +-
src/main/java/com/kitfox/svg/SVGUniverse.java | 57 +-
src/main/java/com/kitfox/svg/app/data/Handler.java | 76 +
.../com/kitfox/svg/app/data/HandlerFactory.java | 27 +
src/test/res/1C2EC147.png | Bin 0 -> 48520 bytes
src/test/res/1C2EC148.png | Bin 0 -> 48520 bytes
src/test/res/Monitor.svg | 84 +
src/test/res/data-uri-scheme-test-image.svg | 23 +
src/test/res/drawing.svg | 9361 ++++++++++++++++++++
src/test/res/missing.svg | 1090 +++
12 files changed, 10893 insertions(+), 94 deletions(-)
create mode 100644 src/main/java/com/kitfox/svg/app/data/Handler.java
create mode 100644 src/main/java/com/kitfox/svg/app/data/HandlerFactory.java
create mode 100644 src/test/res/1C2EC147.png
create mode 100644 src/test/res/1C2EC148.png
create mode 100644 src/test/res/Monitor.svg
create mode 100644 src/test/res/data-uri-scheme-test-image.svg
create mode 100644 src/test/res/drawing.svg
create mode 100644 src/test/res/missing.svg
diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml
index eae6ae1..10405b2 100755
--- a/nbproject/build-impl.xml
+++ b/nbproject/build-impl.xml
@@ -80,6 +80,7 @@ is divided into following sections:
+
@@ -108,23 +109,40 @@ is divided into following sections:
-
+
-
-
+
+
-
-
-
-
-
+
+
+
+
-
-
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -186,6 +204,8 @@ is divided into following sections:
+
+
@@ -217,11 +237,53 @@ is divided into following sections:
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -246,6 +308,8 @@ is divided into following sections:
+
+
@@ -396,14 +460,65 @@ is divided into following sections:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -489,13 +610,13 @@ is divided into following sections:
-
+
-
+
-
+
@@ -510,78 +631,35 @@ is divided into following sections:
java -cp "${run.classpath.with.dist.jar}" ${main.class}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
+
+
+
To run this application from the command line without Ant, try:
java -jar "${dist.jar.resolved}"
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+ To run this application from the command line without Ant, try:
+
+ java -jar "${dist.jar.resolved}"
-
+
-
+
@@ -697,7 +775,7 @@ is divided into following sections:
-
+
@@ -715,7 +793,7 @@ is divided into following sections:
Must select some files in the IDE or set javac.includes
-
+
diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties
index 7c6ac93..66110a4 100755
--- a/nbproject/genfiles.properties
+++ b/nbproject/genfiles.properties
@@ -4,5 +4,5 @@ build.xml.stylesheet.CRC32=be360661
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=478066e1
-nbproject/build-impl.xml.script.CRC32=659f9806
-nbproject/build-impl.xml.stylesheet.CRC32=576378a2@1.32.1.45
+nbproject/build-impl.xml.script.CRC32=371d3a48
+nbproject/build-impl.xml.stylesheet.CRC32=78c6a6ee@1.38.1.45
diff --git a/src/main/java/com/kitfox/svg/ImageSVG.java b/src/main/java/com/kitfox/svg/ImageSVG.java
index f8d3a54..e2fb168 100644
--- a/src/main/java/com/kitfox/svg/ImageSVG.java
+++ b/src/main/java/com/kitfox/svg/ImageSVG.java
@@ -27,6 +27,7 @@
package com.kitfox.svg;
+import com.kitfox.svg.app.data.Handler;
import com.kitfox.svg.xml.*;
import java.awt.*;
@@ -79,13 +80,20 @@ public class ImageSVG extends RenderableElement
if (getPres(sty.setName("xlink:href")))
{
URI src = sty.getURIValue(getXMLBase());
- try {
- imageSrc = src.toURL();
+ if ("data".equals(src.getScheme()))
+ {
+ imageSrc = new URL(null, src.toASCIIString(), new Handler());
}
- catch (Exception e)
+ else
{
- e.printStackTrace();
- imageSrc = null;
+ try {
+ imageSrc = src.toURL();
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ imageSrc = null;
+ }
}
}
}
@@ -94,7 +102,6 @@ public class ImageSVG extends RenderableElement
throw new SVGException(e);
}
-
diagram.getUniverse().registerImage(imageSrc);
//Set widths if not set
diff --git a/src/main/java/com/kitfox/svg/SVGUniverse.java b/src/main/java/com/kitfox/svg/SVGUniverse.java
index 8fdf19d..f99279b 100644
--- a/src/main/java/com/kitfox/svg/SVGUniverse.java
+++ b/src/main/java/com/kitfox/svg/SVGUniverse.java
@@ -35,6 +35,7 @@ import java.beans.PropertyChangeSupport;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
@@ -51,7 +52,6 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.zip.GZIPInputStream;
import javax.imageio.ImageIO;
-import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -173,7 +173,60 @@ public class SVGUniverse implements Serializable
{
return (Font)loadedFonts.get(fontName);
}
-
+
+ URL registerImage(URI imageURI)
+ {
+ String scheme = imageURI.getScheme();
+ if (scheme.equals("data"))
+ {
+ String path = imageURI.getRawSchemeSpecificPart();
+ int idx = path.indexOf(';');
+ String mime = path.substring(0, idx);
+ String content = path.substring(idx + 1);
+
+ if (content.startsWith("base64"))
+ {
+ content = content.substring(6);
+ try {
+ byte[] buf = new sun.misc.BASE64Decoder().decodeBuffer(content);
+ ByteArrayInputStream bais = new ByteArrayInputStream(buf);
+ BufferedImage img = ImageIO.read(bais);
+
+ URL url;
+ int urlIdx = 0;
+ while (true)
+ {
+ url = new URL("inlineImage", "localhost", "img" + urlIdx);
+ if (!loadedImages.containsKey(url))
+ {
+ break;
+ }
+ urlIdx++;
+ }
+
+ SoftReference ref = new SoftReference(img);
+ loadedImages.put(url, ref);
+
+ return url;
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+ return null;
+ }
+ else
+ {
+ try {
+ URL url = imageURI.toURL();
+ registerImage(url);
+ return url;
+ } catch (MalformedURLException ex) {
+ ex.printStackTrace();
+ }
+ return null;
+ }
+ }
+
void registerImage(URL imageURL)
{
if (loadedImages.containsKey(imageURL)) return;
diff --git a/src/main/java/com/kitfox/svg/app/data/Handler.java b/src/main/java/com/kitfox/svg/app/data/Handler.java
new file mode 100644
index 0000000..9cd3525
--- /dev/null
+++ b/src/main/java/com/kitfox/svg/app/data/Handler.java
@@ -0,0 +1,76 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package com.kitfox.svg.app.data;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+/**
+ *
+ * @author kitfox
+ */
+public class Handler extends URLStreamHandler
+{
+ class Connection extends URLConnection
+ {
+ String mime;
+ byte[] buf;
+
+ public Connection(URL url)
+ {
+ super(url);
+
+ String path = url.getPath();
+ int idx = path.indexOf(';');
+ mime = path.substring(0, idx);
+ String content = path.substring(idx + 1);
+
+ if (content.startsWith("base64,"))
+ {
+ content = content.substring(7);
+ try {
+ buf = new sun.misc.BASE64Decoder().decodeBuffer(content);
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+ }
+
+ public void connect() throws IOException
+ {
+ }
+
+ public String getHeaderField(String name)
+ {
+ if ("content-type".equals(name))
+ {
+ return mime;
+ }
+
+ return super.getHeaderField(name);
+ }
+
+ public InputStream getInputStream() throws IOException
+ {
+ return new ByteArrayInputStream(buf);
+ }
+
+// public Object getContent() throws IOException
+// {
+// BufferedImage img = ImageIO.read(getInputStream());
+// }
+ }
+
+ protected URLConnection openConnection(URL u) throws IOException
+ {
+ return new Connection(u);
+ }
+
+}
diff --git a/src/main/java/com/kitfox/svg/app/data/HandlerFactory.java b/src/main/java/com/kitfox/svg/app/data/HandlerFactory.java
new file mode 100644
index 0000000..e1ed1b5
--- /dev/null
+++ b/src/main/java/com/kitfox/svg/app/data/HandlerFactory.java
@@ -0,0 +1,27 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package com.kitfox.svg.app.data;
+
+import java.net.URLStreamHandler;
+import java.net.URLStreamHandlerFactory;
+
+/**
+ *
+ * @author kitfox
+ */
+public class HandlerFactory implements URLStreamHandlerFactory
+{
+ static Handler handler = new Handler();
+
+ public URLStreamHandler createURLStreamHandler(String protocol)
+ {
+ if ("data".equals(protocol))
+ {
+ return handler;
+ }
+ return null;
+ }
+}
diff --git a/src/test/res/1C2EC147.png b/src/test/res/1C2EC147.png
new file mode 100644
index 0000000..e33dd29
Binary files /dev/null and b/src/test/res/1C2EC147.png differ
diff --git a/src/test/res/1C2EC148.png b/src/test/res/1C2EC148.png
new file mode 100644
index 0000000..e33dd29
Binary files /dev/null and b/src/test/res/1C2EC148.png differ
diff --git a/src/test/res/Monitor.svg b/src/test/res/Monitor.svg
new file mode 100644
index 0000000..9e07a3f
--- /dev/null
+++ b/src/test/res/Monitor.svg
@@ -0,0 +1,84 @@
+
+
+
\ No newline at end of file
diff --git a/src/test/res/data-uri-scheme-test-image.svg b/src/test/res/data-uri-scheme-test-image.svg
new file mode 100644
index 0000000..a6356be
--- /dev/null
+++ b/src/test/res/data-uri-scheme-test-image.svg
@@ -0,0 +1,23 @@
+
+
+
+
+]>
+
diff --git a/src/test/res/drawing.svg b/src/test/res/drawing.svg
new file mode 100644
index 0000000..afd2368
--- /dev/null
+++ b/src/test/res/drawing.svg
@@ -0,0 +1,9361 @@
+
+
+
+
+]>
+
diff --git a/src/test/res/missing.svg b/src/test/res/missing.svg
new file mode 100644
index 0000000..8fb23d2
--- /dev/null
+++ b/src/test/res/missing.svg
@@ -0,0 +1,1090 @@
+
+
+
+
+]>
+
--
cgit v1.2.3-55-g7522