From 9551fc304e04f5014746eb4631aadf7953b46a83 Mon Sep 17 00:00:00 2001 From: kitfox Date: Thu, 28 Feb 2008 06:46:15 +0000 Subject: Added Base64 stream codecs. Added Main-Class attribute to generated jars. git-svn-id: https://svn.java.net/svn/svgsalamander~svn/trunk/svg-core@52 7dc7fa77-23fb-e6ad-8e2e-c86bd48ed22b --- build.xml | 6 ++ nbproject/build-impl.xml | 12 ++- nbproject/genfiles.properties | 4 +- nbproject/project.properties | 2 + nbproject/project.xml | 1 + src/main/java/com/kitfox/svg/ImageSVG.java | 9 ++- .../java/com/kitfox/svg/xml/Base64InputStream.java | 82 ++++++++++++++++++++ .../com/kitfox/svg/xml/Base64OutputStream.java | 90 ++++++++++++++++++++++ src/main/java/com/kitfox/svg/xml/Base64Util.java | 32 ++++++++ .../java/com/kitfox/svg/xml/StyleAttribute.java | 2 +- 10 files changed, 233 insertions(+), 7 deletions(-) create mode 100755 src/main/java/com/kitfox/svg/xml/Base64InputStream.java create mode 100755 src/main/java/com/kitfox/svg/xml/Base64OutputStream.java create mode 100755 src/main/java/com/kitfox/svg/xml/Base64Util.java diff --git a/build.xml b/build.xml index 16e2b14..74bb53c 100755 --- a/build.xml +++ b/build.xml @@ -93,6 +93,12 @@ + + + + + + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml index 1bf7540..02bb0a6 100755 --- a/nbproject/build-impl.xml +++ b/nbproject/build-impl.xml @@ -90,6 +90,7 @@ is divided into following sections: + @@ -147,6 +148,7 @@ is divided into following sections: + Must set src.www.dir Must set src.dir Must set src.java.dir Must set src.res.dir @@ -173,7 +175,7 @@ is divided into following sections: - + @@ -192,7 +194,7 @@ is divided into following sections: - + @@ -352,6 +354,7 @@ is divided into following sections: + @@ -370,7 +373,7 @@ is divided into following sections: Must select some files in the IDE or set javac.includes - + @@ -504,6 +507,9 @@ is divided into following sections: + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties index 345b3d4..9d45de5 100755 --- a/nbproject/genfiles.properties +++ b/nbproject/genfiles.properties @@ -3,6 +3,6 @@ build.xml.script.CRC32=91ddbaab 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=f852a4d9 -nbproject/build-impl.xml.script.CRC32=7235f0eb +nbproject/build-impl.xml.data.CRC32=478066e1 +nbproject/build-impl.xml.script.CRC32=a04a2883 nbproject/build-impl.xml.stylesheet.CRC32=e327d66c diff --git a/nbproject/project.properties b/nbproject/project.properties index 5bb10d9..e09d66c 100755 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -22,6 +22,7 @@ file.reference.ant.jar=..\\libraries\\ant.jar file.reference.javacc.jar=..\\libraries\\javacc.jar file.reference.main-java=src/main/java file.reference.main-res=src/main/res +file.reference.svgsalamander-www=../www file.reference.test-java=src/test/java file.reference.test-res=src/test/res includes=** @@ -74,5 +75,6 @@ source.encoding=UTF-8 src.dir=${file.reference.main-java} src.java.dir=src\\gen\\java src.res.dir=src\\gen\\res +src.www.dir=${file.reference.svgsalamander-www} test.res.dir=${file.reference.test-res} test.src.dir=${file.reference.test-java} diff --git a/nbproject/project.xml b/nbproject/project.xml index 289b995..3253810 100755 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -6,6 +6,7 @@ svg-salamander-core 1.6.5 + diff --git a/src/main/java/com/kitfox/svg/ImageSVG.java b/src/main/java/com/kitfox/svg/ImageSVG.java index 75aaf79..6f4b301 100644 --- a/src/main/java/com/kitfox/svg/ImageSVG.java +++ b/src/main/java/com/kitfox/svg/ImageSVG.java @@ -80,7 +80,14 @@ public class ImageSVG extends RenderableElement if (getPres(sty.setName("xlink:href"))) { URI src = sty.getURIValue(getXMLBase()); - imageSrc = src.toURL(); + try { + imageSrc = src.toURL(); + } + catch (Exception e) + { + e.printStackTrace(); + imageSrc = null; + } } } catch (Exception e) diff --git a/src/main/java/com/kitfox/svg/xml/Base64InputStream.java b/src/main/java/com/kitfox/svg/xml/Base64InputStream.java new file mode 100755 index 0000000..3851c39 --- /dev/null +++ b/src/main/java/com/kitfox/svg/xml/Base64InputStream.java @@ -0,0 +1,82 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.kitfox.svg.xml; + +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * + * @author kitfox + */ +public class Base64InputStream extends FilterInputStream +{ + int buf; //Cached bytes to read + int bufSize; //Number of bytes waiting to be read from buffer + boolean drain = false; //After set, read no more chunks + + public Base64InputStream(InputStream in) + { + super(in); + } + + public int read() throws IOException + { + if (drain && bufSize == 0) + { + return -1; + } + + if (bufSize == 0) + { + //Read next chunk into 4 byte buffer + int chunk = in.read(); + if (chunk == -1) + { + drain = true; + return -1; + } + + //get remaining 3 bytes + for (int i = 0; i < 3; ++i) + { + int value = in.read(); + if (value == -1) + { + throw new IOException("Early termination of base64 stream"); + } + chunk = (chunk << 8) | (value & 0xff); + } + + //Check for special termination characters + if ((chunk & 0xffff) == (((byte)'=' << 8) | (byte)'=')) + { + bufSize = 1; + drain = true; + } + else if ((chunk & 0xff) == (byte)'=') + { + bufSize = 2; + drain = true; + } + else + { + bufSize = 3; + } + + //Fill buffer with decoded characters + for (int i = 0; i < bufSize + 1; ++i) + { + buf = (buf << 6) | Base64Util.decodeByte((chunk >> 24) & 0xff); + chunk <<= 8; + } + } + + //Return nth remaing bte & decrement counter + return (buf >> (--bufSize * 8)) & 0xff; + } +} diff --git a/src/main/java/com/kitfox/svg/xml/Base64OutputStream.java b/src/main/java/com/kitfox/svg/xml/Base64OutputStream.java new file mode 100755 index 0000000..db167de --- /dev/null +++ b/src/main/java/com/kitfox/svg/xml/Base64OutputStream.java @@ -0,0 +1,90 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.kitfox.svg.xml; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +/** + * + * @author kitfox + */ +public class Base64OutputStream extends FilterOutputStream +{ + int buf; + int numBytes; + int numChunks; + + public Base64OutputStream(OutputStream out) + { + super(out); + } + + public void flush() throws IOException + { + out.flush(); + } + + public void close() throws IOException + { + switch (numBytes) + { + case 1: + buf <<= 4; + out.write(getBase64Byte(1)); + out.write(getBase64Byte(0)); + out.write('='); + out.write('='); + break; + case 2: + buf <<= 2; + out.write(getBase64Byte(2)); + out.write(getBase64Byte(1)); + out.write(getBase64Byte(0)); + out.write('='); + break; + case 3: + out.write(getBase64Byte(3)); + out.write(getBase64Byte(2)); + out.write(getBase64Byte(1)); + out.write(getBase64Byte(0)); + break; + default: + assert false; + } + + out.close(); + } + + public void write(int b) throws IOException + { + buf = (buf << 8) | (0xff & b); + numBytes++; + + if (numBytes == 3) + { + out.write(getBase64Byte(3)); + out.write(getBase64Byte(2)); + out.write(getBase64Byte(1)); + out.write(getBase64Byte(0)); + + numBytes = 0; + numChunks++; + if (numChunks == 16) + { +// out.write('\r'); +// out.write('\n'); + numChunks = 0; + } + } + } + + public byte getBase64Byte(int index) + { + return Base64Util.encodeByte((buf >> (index * 6)) & 0x3f); + } +} diff --git a/src/main/java/com/kitfox/svg/xml/Base64Util.java b/src/main/java/com/kitfox/svg/xml/Base64Util.java new file mode 100755 index 0000000..a95b9d4 --- /dev/null +++ b/src/main/java/com/kitfox/svg/xml/Base64Util.java @@ -0,0 +1,32 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.kitfox.svg.xml; + +/** + * + * @author kitfox + */ +public class Base64Util +{ + static final byte[] valueToBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes(); + static final byte[] base64ToValue = new byte[128]; + static { + for (int i = 0; i < valueToBase64.length; ++i) + { + base64ToValue[valueToBase64[i]] = (byte)i; + } + } + + static public byte encodeByte(int value) + { + return valueToBase64[value]; + } + + static public byte decodeByte(int base64Char) + { + return base64ToValue[base64Char]; + } +} diff --git a/src/main/java/com/kitfox/svg/xml/StyleAttribute.java b/src/main/java/com/kitfox/svg/xml/StyleAttribute.java index 70cf71a..a3c9e63 100644 --- a/src/main/java/com/kitfox/svg/xml/StyleAttribute.java +++ b/src/main/java/com/kitfox/svg/xml/StyleAttribute.java @@ -226,7 +226,7 @@ public class StyleAttribute implements Serializable { try { String fragment = parseURLFn(); - if (fragment == null) fragment = stringValue; + if (fragment == null) fragment = stringValue.replaceAll("\\s+", ""); if (fragment == null) return null; //====================== -- cgit v1.2.3-55-g7522