Skip to content
This repository
Browse code

Fixed a couple of small issues

  • Loading branch information...
commit f14394766ea443b87e58a7f0b7c2ea0e14444e05 1 parent fdc5aa2
Nicholas C. Zakas authored November 26, 2009
12  CHANGELOG
... ...
@@ -0,0 +1,12 @@
  1
+CSSEmbed
  2
+Copyright (c) 2009 Nicholas C. Zakas. All rights reserved.
  3
+
  4
+0.2.7
  5
+
  6
+  * Added dchaplinsky's patch for warning about duplicate images.
  7
+  * Fixed http://github.com/nzakas/cssembed/issues/#issue/5, 
  8
+    "Specifying the same input and output results in empty file".
  9
+
  10
+0.2.6
  11
+
  12
+  * First public release.
2  README
@@ -12,7 +12,7 @@ CSS files.
12 12
  Usage
13 13
 -------------------------------------------------------------------------------
14 14
 
15  
-Usage: java -jar cssembed-x.y.z.jar [options] [input files]
  15
+Usage: java -jar cssembed-x.y.z.jar [options] [input file]
16 16
 
17 17
 Global Options
18 18
   -h, --help            Displays this information.
2  ant.properties
@@ -5,7 +5,7 @@ doc.dir = doc
5 5
 build.dir = build
6 6
 res.dir = res
7 7
 product.name = cssembed
8  
-version.number = 0.2.6
  8
+version.number = 0.2.7
9 9
 jar.name = ${product.name}-${version.number}.jar
10 10
 core.jar.name = ${product.name}-core-${version.number}.jar
11 11
 dist.package.name = ${product.name}-${version.number}
1  build.xml
@@ -47,6 +47,7 @@
47 47
         </javac>
48 48
         <copy todir="${build.dir}/testclasses/net/nczonline/web/cssembed">
49 49
             <fileset dir="${tests.dir}" includes="*.png"/>
  50
+            <fileset dir="${tests.dir}" includes="*.css"/>
50 51
         </copy>
51 52
     </target>
52 53
  
11  src/net/nczonline/web/cssembed/CSSEmbed.java
@@ -47,7 +47,6 @@ public static void main(String[] args) {
47 47
         String charset = null;
48 48
         String outputFilename = null;
49 49
         Writer out = null;
50  
-        String mimeType = null;
51 50
         Reader in = null;
52 51
         String root;
53 52
         
@@ -99,6 +98,10 @@ public static void main(String[] args) {
99 98
             String inputFilename = fileArgs[0];                     
100 99
             in = new InputStreamReader(new FileInputStream(inputFilename), charset);
101 100
             
  101
+            CSSURLEmbedder embedder = new CSSURLEmbedder(in, verbose);            
  102
+            
  103
+            //close in case writing to the same file
  104
+            in.close(); in = null;
102 105
             
103 106
             //get root for relative URLs
104 107
             root = (String) parser.getOptionValue(rootOpt);
@@ -133,10 +136,8 @@ public static void main(String[] args) {
133 136
             }            
134 137
             
135 138
             //set verbose option
136  
-            CSSURLEmbedder.setVerbose(verbose);
137  
-            
138  
-            //do the embedding
139  
-            CSSURLEmbedder.embedImages(in, out, root);         
  139
+            embedder.embedImages(out, root);
  140
+            out.close();
140 141
             
141 142
         } catch (CmdLineParser.OptionException e) {
142 143
             usage();
61  src/net/nczonline/web/cssembed/CSSURLEmbedder.java
@@ -27,9 +27,11 @@
27 27
 import java.io.File;
28 28
 import java.io.IOException;
29 29
 import java.io.Reader;
  30
+import java.io.StringReader;
30 31
 import java.io.StringWriter;
31 32
 import java.io.Writer;
32 33
 import java.net.URL;
  34
+import java.util.HashMap;
33 35
 import java.util.HashSet;
34 36
 import net.nczonline.web.datauri.DataURIGenerator;
35 37
 
@@ -39,8 +41,9 @@
39 41
  */
40 42
 public class CSSURLEmbedder { 
41 43
     
42  
-    private static boolean verbose = false;
  44
+    private boolean verbose = false;
43 45
     private static HashSet<String> imageTypes;
  46
+    private String code = null;
44 47
     
45 48
     static {
46 49
         imageTypes = new HashSet<String>();
@@ -51,14 +54,27 @@
51 54
     }
52 55
     
53 56
     //--------------------------------------------------------------------------
  57
+    // Constructors
  58
+    //--------------------------------------------------------------------------    
  59
+    
  60
+    public CSSURLEmbedder(Reader in) throws IOException {
  61
+        this(in, false);
  62
+    }
  63
+    
  64
+    public CSSURLEmbedder(Reader in, boolean verbose) throws IOException {
  65
+        this.code = readCode(in);
  66
+        this.verbose = verbose;
  67
+    }
  68
+    
  69
+    //--------------------------------------------------------------------------
54 70
     // Get/Set verbose flag
55 71
     //--------------------------------------------------------------------------    
56 72
     
57  
-    public static boolean getVerbose(){
  73
+    public boolean getVerbose(){
58 74
         return verbose;
59 75
     }
60 76
     
61  
-    public static void setVerbose(boolean newVerbose){
  77
+    public void setVerbose(boolean newVerbose){
62 78
         verbose = newVerbose;
63 79
     }
64 80
     
@@ -68,24 +84,24 @@ public static void setVerbose(boolean newVerbose){
68 84
     
69 85
     /**
70 86
      * Embeds data URI images into a CSS file.
71  
-     * @param in The CSS source code.
72 87
      * @param out The place to write out the source code.
73 88
      * @throws java.io.IOException
74 89
      */
75  
-    public static void embedImages(Reader in, Writer out) throws IOException {
76  
-        embedImages(in, out, null);
  90
+    public void embedImages(Writer out) throws IOException {
  91
+        embedImages(out, null);
77 92
     }
78 93
         
79 94
     /**
80 95
      * Embeds data URI images into a CSS file.
81  
-     * @param in The CSS source code.
82 96
      * @param out The place to write out the source code.
83 97
      * @param root The root to prepend to any relative paths.
84 98
      * @throws java.io.IOException
85 99
      */
86  
-    public static void embedImages(Reader in, Writer out, String root) throws IOException {
87  
-        BufferedReader reader = new BufferedReader(in);        
  100
+    public void embedImages(Writer out, String root) throws IOException {
  101
+        BufferedReader reader = new BufferedReader(new StringReader(code));        
88 102
         StringBuilder builder = new StringBuilder();
  103
+        HashMap<String,Integer> foundMedia = new HashMap<String,Integer>();
  104
+        
89 105
         String line;
90 106
         int lineNum = 1;        
91 107
         
@@ -121,6 +137,14 @@ public static void embedImages(Reader in, Writer out, String root) throws IOExce
121 137
                         }                         
122 138
                     }
123 139
                     
  140
+                    if (foundMedia.containsKey(url)){
  141
+                        if (verbose){
  142
+                            System.err.println("[WARNING] Duplicate URL '" + url + "' found at line " + lineNum + ", previously declared at line " + foundMedia.get(url) + ".");
  143
+                        }                        
  144
+                    }
  145
+                    
  146
+                    foundMedia.put(url, lineNum);                    
  147
+                    
124 148
                     String newUrl = url;
125 149
                     
126 150
                     if (verbose){
@@ -149,9 +173,9 @@ public static void embedImages(Reader in, Writer out, String root) throws IOExce
149 173
             
150 174
             lineNum++;
151 175
         }
152  
-        
153  
-        out.write(builder.toString());
154  
-        
  176
+        reader.close();
  177
+
  178
+        out.write(builder.toString());        
155 179
     }
156 180
     
157 181
     /**
@@ -163,7 +187,7 @@ public static void embedImages(Reader in, Writer out, String root) throws IOExce
163 187
      * @return The appropriate data URI to use.
164 188
      * @throws java.io.IOException
165 189
      */
166  
-    private static String getImageURIString(String url, String originalUrl) throws IOException {
  190
+    private String getImageURIString(String url, String originalUrl) throws IOException {
167 191
         
168 192
         //check the extension - only encode for images
169 193
         String fileType = url.substring(url.lastIndexOf(".") + 1);
@@ -214,5 +238,16 @@ private static String getImageURIString(String url, String originalUrl) throws I
214 238
         
215 239
     }
216 240
     
  241
+    private String readCode(Reader in) throws IOException {
  242
+        StringBuilder builder = new StringBuilder();
  243
+        int c;
  244
+        
  245
+        while ((c = in.read()) != -1){
  246
+            builder.append((char)c);
  247
+        }
  248
+        
  249
+        in.close();
  250
+        return builder.toString();
  251
+    }
217 252
             
218 253
 }
48  tests/net/nczonline/web/cssembed/CSSURLEmbedderTest.java
@@ -5,9 +5,16 @@
5 5
 
6 6
 package net.nczonline.web.cssembed;
7 7
 
  8
+import java.io.File;
  9
+import java.io.FileInputStream;
  10
+import java.io.FileOutputStream;
8 11
 import java.io.IOException;
  12
+import java.io.InputStreamReader;
  13
+import java.io.OutputStreamWriter;
  14
+import java.io.Reader;
9 15
 import java.io.StringReader;
10 16
 import java.io.StringWriter;
  17
+import java.io.Writer;
11 18
 import org.junit.After;
12 19
 import org.junit.Before;
13 20
 import org.junit.Test;
@@ -20,17 +27,19 @@
20 27
 public class CSSURLEmbedderTest {
21 28
     
22 29
     private static String folderDataURI = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACIAAAAbCAMAAAAu7K2VAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAAwUExURWxsbNbW1v/rhf/ge//3kf/Ub9/f3/b29oeHh/7LZv/0juazTktLS8WSLf//mf/////BPrAAAAB4SURBVHja3NLdCoAgDIbhqbXZz2f3f7eZWUpMO67nQEReBqK0vaLPJohYegnSYqSdYAtRGvUYVpJhPpx7z2piLSqsJQ73oY1ztGREuEwBpCUTwpAt7cRmncRlnWTMoCdcXxmrdiMxngpvtDcSNkX9AvTnv9uyCzAAgzAw+dNAwOQAAAAASUVORK5CYII=";
23  
-
  30
+    private CSSURLEmbedder embedder;
  31
+    
24 32
     public CSSURLEmbedderTest() {
25 33
     }
26  
-
  34
+    
27 35
     @Before
28 36
     public void setUp() {
29  
-        CSSURLEmbedder.setVerbose(true);
  37
+
30 38
     }
31 39
 
32 40
     @After
33 41
     public void tearDown() {
  42
+        embedder = null;
34 43
     }
35 44
     
36 45
     @Test
@@ -39,7 +48,8 @@ public void testAbsoluteLocalFile() throws IOException {
39 48
         String code = "background: url(folder.png);";
40 49
         
41 50
         StringWriter writer = new StringWriter();
42  
-        CSSURLEmbedder.embedImages(new StringReader(code), writer, filename.substring(0, filename.lastIndexOf("/")+1));
  51
+        embedder = new CSSURLEmbedder(new StringReader(code), true);
  52
+        embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
43 53
         
44 54
         String result = writer.toString();
45 55
         assertEquals("background: url(" + folderDataURI + ");", result);
@@ -51,7 +61,8 @@ public void testAbsoluteLocalFileMultipleOneLine() throws IOException {
51 61
         String code = "background: url(folder.png); background: url(folder.png);";
52 62
         
53 63
         StringWriter writer = new StringWriter();
54  
-        CSSURLEmbedder.embedImages(new StringReader(code), writer, filename.substring(0, filename.lastIndexOf("/")+1));
  64
+        embedder = new CSSURLEmbedder(new StringReader(code), true);
  65
+        embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
55 66
         
56 67
         String result = writer.toString();
57 68
         assertEquals("background: url(" + folderDataURI + "); background: url(" + folderDataURI + ");", result);
@@ -63,7 +74,8 @@ public void testAbsoluteLocalFileWithDoubleQuotes() throws IOException {
63 74
         String code = "background: url(\"folder.png\");";
64 75
         
65 76
         StringWriter writer = new StringWriter();
66  
-        CSSURLEmbedder.embedImages(new StringReader(code), writer, filename.substring(0, filename.lastIndexOf("/")+1));
  77
+        embedder = new CSSURLEmbedder(new StringReader(code), true);
  78
+        embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
67 79
         
68 80
         String result = writer.toString();
69 81
         assertEquals("background: url(" + folderDataURI + ");", result);
@@ -75,11 +87,33 @@ public void testAbsoluteLocalFileWithSingleQuotes() throws IOException {
75 87
         String code = "background: url('folder.png');";
76 88
         
77 89
         StringWriter writer = new StringWriter();
78  
-        CSSURLEmbedder.embedImages(new StringReader(code), writer, filename.substring(0, filename.lastIndexOf("/")+1));
  90
+        embedder = new CSSURLEmbedder(new StringReader(code), true);
  91
+        embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
79 92
         
80 93
         String result = writer.toString();
81 94
         assertEquals("background: url(" + folderDataURI + ");", result);
82 95
     }     
83 96
     
  97
+    @Test
  98
+    public void testReadFromAndWriteToSameFile() throws IOException {
  99
+        String filename = CSSURLEmbedderTest.class.getResource("samefiletest.css").getPath().replace("%20", " ");
  100
+        File file = new File(filename);
  101
+        Reader in = new InputStreamReader(new FileInputStream(file));
  102
+        
  103
+        embedder = new CSSURLEmbedder(in, true);
  104
+        in.close();
  105
+        
  106
+        Writer writer = new OutputStreamWriter(new FileOutputStream(file));        
  107
+        embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
  108
+        writer.close();
  109
+        
  110
+        in = new InputStreamReader(new FileInputStream(file));
  111
+        char[] chars = new char[(int)file.length()];
  112
+        in.read(chars, 0, (int)file.length());
  113
+        in.close();
  114
+        
  115
+        String result = new String(chars);
  116
+        assertEquals("background: url(" + folderDataURI + ");", result);
  117
+    }
84 118
     
85 119
 }
1  tests/samefiletest.css
... ...
@@ -0,0 +1 @@
  1
+background: url('folder.png');

0 notes on commit f143947

Please sign in to comment.
Something went wrong with that request. Please try again.