Permalink
Browse files

Fixed a couple of small issues

  • Loading branch information...
1 parent fdc5aa2 commit f14394766ea443b87e58a7f0b7c2ea0e14444e05 Nicholas C. Zakas committed Nov 26, 2009
View
@@ -0,0 +1,12 @@
+CSSEmbed
+Copyright (c) 2009 Nicholas C. Zakas. All rights reserved.
+
+0.2.7
+
+ * Added dchaplinsky's patch for warning about duplicate images.
+ * Fixed http://github.com/nzakas/cssembed/issues/#issue/5,
+ "Specifying the same input and output results in empty file".
+
+0.2.6
+
+ * First public release.
View
@@ -12,7 +12,7 @@ CSS files.
Usage
-------------------------------------------------------------------------------
-Usage: java -jar cssembed-x.y.z.jar [options] [input files]
+Usage: java -jar cssembed-x.y.z.jar [options] [input file]
Global Options
-h, --help Displays this information.
View
@@ -5,7 +5,7 @@ doc.dir = doc
build.dir = build
res.dir = res
product.name = cssembed
-version.number = 0.2.6
+version.number = 0.2.7
jar.name = ${product.name}-${version.number}.jar
core.jar.name = ${product.name}-core-${version.number}.jar
dist.package.name = ${product.name}-${version.number}
View
@@ -47,6 +47,7 @@
</javac>
<copy todir="${build.dir}/testclasses/net/nczonline/web/cssembed">
<fileset dir="${tests.dir}" includes="*.png"/>
+ <fileset dir="${tests.dir}" includes="*.css"/>
</copy>
</target>
@@ -47,7 +47,6 @@ public static void main(String[] args) {
String charset = null;
String outputFilename = null;
Writer out = null;
- String mimeType = null;
Reader in = null;
String root;
@@ -99,6 +98,10 @@ public static void main(String[] args) {
String inputFilename = fileArgs[0];
in = new InputStreamReader(new FileInputStream(inputFilename), charset);
+ CSSURLEmbedder embedder = new CSSURLEmbedder(in, verbose);
+
+ //close in case writing to the same file
+ in.close(); in = null;
//get root for relative URLs
root = (String) parser.getOptionValue(rootOpt);
@@ -133,10 +136,8 @@ public static void main(String[] args) {
}
//set verbose option
- CSSURLEmbedder.setVerbose(verbose);
-
- //do the embedding
- CSSURLEmbedder.embedImages(in, out, root);
+ embedder.embedImages(out, root);
+ out.close();
} catch (CmdLineParser.OptionException e) {
usage();
@@ -27,9 +27,11 @@
import java.io.File;
import java.io.IOException;
import java.io.Reader;
+import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
+import java.util.HashMap;
import java.util.HashSet;
import net.nczonline.web.datauri.DataURIGenerator;
@@ -39,8 +41,9 @@
*/
public class CSSURLEmbedder {
- private static boolean verbose = false;
+ private boolean verbose = false;
private static HashSet<String> imageTypes;
+ private String code = null;
static {
imageTypes = new HashSet<String>();
@@ -51,14 +54,27 @@
}
//--------------------------------------------------------------------------
+ // Constructors
+ //--------------------------------------------------------------------------
+
+ public CSSURLEmbedder(Reader in) throws IOException {
+ this(in, false);
+ }
+
+ public CSSURLEmbedder(Reader in, boolean verbose) throws IOException {
+ this.code = readCode(in);
+ this.verbose = verbose;
+ }
+
+ //--------------------------------------------------------------------------
// Get/Set verbose flag
//--------------------------------------------------------------------------
- public static boolean getVerbose(){
+ public boolean getVerbose(){
return verbose;
}
- public static void setVerbose(boolean newVerbose){
+ public void setVerbose(boolean newVerbose){
verbose = newVerbose;
}
@@ -68,24 +84,24 @@ public static void setVerbose(boolean newVerbose){
/**
* Embeds data URI images into a CSS file.
- * @param in The CSS source code.
* @param out The place to write out the source code.
* @throws java.io.IOException
*/
- public static void embedImages(Reader in, Writer out) throws IOException {
- embedImages(in, out, null);
+ public void embedImages(Writer out) throws IOException {
+ embedImages(out, null);
}
/**
* Embeds data URI images into a CSS file.
- * @param in The CSS source code.
* @param out The place to write out the source code.
* @param root The root to prepend to any relative paths.
* @throws java.io.IOException
*/
- public static void embedImages(Reader in, Writer out, String root) throws IOException {
- BufferedReader reader = new BufferedReader(in);
+ public void embedImages(Writer out, String root) throws IOException {
+ BufferedReader reader = new BufferedReader(new StringReader(code));
StringBuilder builder = new StringBuilder();
+ HashMap<String,Integer> foundMedia = new HashMap<String,Integer>();
+
String line;
int lineNum = 1;
@@ -121,6 +137,14 @@ public static void embedImages(Reader in, Writer out, String root) throws IOExce
}
}
+ if (foundMedia.containsKey(url)){
+ if (verbose){
+ System.err.println("[WARNING] Duplicate URL '" + url + "' found at line " + lineNum + ", previously declared at line " + foundMedia.get(url) + ".");
+ }
+ }
+
+ foundMedia.put(url, lineNum);
+
String newUrl = url;
if (verbose){
@@ -149,9 +173,9 @@ public static void embedImages(Reader in, Writer out, String root) throws IOExce
lineNum++;
}
-
- out.write(builder.toString());
-
+ reader.close();
+
+ out.write(builder.toString());
}
/**
@@ -163,7 +187,7 @@ public static void embedImages(Reader in, Writer out, String root) throws IOExce
* @return The appropriate data URI to use.
* @throws java.io.IOException
*/
- private static String getImageURIString(String url, String originalUrl) throws IOException {
+ private String getImageURIString(String url, String originalUrl) throws IOException {
//check the extension - only encode for images
String fileType = url.substring(url.lastIndexOf(".") + 1);
@@ -214,5 +238,16 @@ private static String getImageURIString(String url, String originalUrl) throws I
}
+ private String readCode(Reader in) throws IOException {
+ StringBuilder builder = new StringBuilder();
+ int c;
+
+ while ((c = in.read()) != -1){
+ builder.append((char)c);
+ }
+
+ in.close();
+ return builder.toString();
+ }
}
@@ -5,9 +5,16 @@
package net.nczonline.web.cssembed;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
+import java.io.Writer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -20,17 +27,19 @@
public class CSSURLEmbedderTest {
private static String folderDataURI = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACIAAAAbCAMAAAAu7K2VAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAAwUExURWxsbNbW1v/rhf/ge//3kf/Ub9/f3/b29oeHh/7LZv/0juazTktLS8WSLf//mf/////BPrAAAAB4SURBVHja3NLdCoAgDIbhqbXZz2f3f7eZWUpMO67nQEReBqK0vaLPJohYegnSYqSdYAtRGvUYVpJhPpx7z2piLSqsJQ73oY1ztGREuEwBpCUTwpAt7cRmncRlnWTMoCdcXxmrdiMxngpvtDcSNkX9AvTnv9uyCzAAgzAw+dNAwOQAAAAASUVORK5CYII=";
-
+ private CSSURLEmbedder embedder;
+
public CSSURLEmbedderTest() {
}
-
+
@Before
public void setUp() {
- CSSURLEmbedder.setVerbose(true);
+
}
@After
public void tearDown() {
+ embedder = null;
}
@Test
@@ -39,7 +48,8 @@ public void testAbsoluteLocalFile() throws IOException {
String code = "background: url(folder.png);";
StringWriter writer = new StringWriter();
- CSSURLEmbedder.embedImages(new StringReader(code), writer, filename.substring(0, filename.lastIndexOf("/")+1));
+ embedder = new CSSURLEmbedder(new StringReader(code), true);
+ embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
String result = writer.toString();
assertEquals("background: url(" + folderDataURI + ");", result);
@@ -51,7 +61,8 @@ public void testAbsoluteLocalFileMultipleOneLine() throws IOException {
String code = "background: url(folder.png); background: url(folder.png);";
StringWriter writer = new StringWriter();
- CSSURLEmbedder.embedImages(new StringReader(code), writer, filename.substring(0, filename.lastIndexOf("/")+1));
+ embedder = new CSSURLEmbedder(new StringReader(code), true);
+ embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
String result = writer.toString();
assertEquals("background: url(" + folderDataURI + "); background: url(" + folderDataURI + ");", result);
@@ -63,7 +74,8 @@ public void testAbsoluteLocalFileWithDoubleQuotes() throws IOException {
String code = "background: url(\"folder.png\");";
StringWriter writer = new StringWriter();
- CSSURLEmbedder.embedImages(new StringReader(code), writer, filename.substring(0, filename.lastIndexOf("/")+1));
+ embedder = new CSSURLEmbedder(new StringReader(code), true);
+ embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
String result = writer.toString();
assertEquals("background: url(" + folderDataURI + ");", result);
@@ -75,11 +87,33 @@ public void testAbsoluteLocalFileWithSingleQuotes() throws IOException {
String code = "background: url('folder.png');";
StringWriter writer = new StringWriter();
- CSSURLEmbedder.embedImages(new StringReader(code), writer, filename.substring(0, filename.lastIndexOf("/")+1));
+ embedder = new CSSURLEmbedder(new StringReader(code), true);
+ embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
String result = writer.toString();
assertEquals("background: url(" + folderDataURI + ");", result);
}
+ @Test
+ public void testReadFromAndWriteToSameFile() throws IOException {
+ String filename = CSSURLEmbedderTest.class.getResource("samefiletest.css").getPath().replace("%20", " ");
+ File file = new File(filename);
+ Reader in = new InputStreamReader(new FileInputStream(file));
+
+ embedder = new CSSURLEmbedder(in, true);
+ in.close();
+
+ Writer writer = new OutputStreamWriter(new FileOutputStream(file));
+ embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
+ writer.close();
+
+ in = new InputStreamReader(new FileInputStream(file));
+ char[] chars = new char[(int)file.length()];
+ in.read(chars, 0, (int)file.length());
+ in.close();
+
+ String result = new String(chars);
+ assertEquals("background: url(" + folderDataURI + ");", result);
+ }
}
@@ -0,0 +1 @@
+background: url('folder.png');

0 comments on commit f143947

Please sign in to comment.