Permalink
Browse files

Issue #22

A missing local file shouldn't throw an error all the way up the chain & stop execution, so add a try/catch block to handle it.
  • Loading branch information...
1 parent fe72468 commit 7909b491701c4aa829d51572212a8f3d52103be2 @tivac tivac committed Apr 18, 2011
Showing with 29 additions and 22 deletions.
  1. +29 −22 src/net/nczonline/web/cssembed/CSSURLEmbedder.java
@@ -26,6 +26,7 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
+import java.io.FileNotFoundException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
@@ -303,33 +304,39 @@ private String getImageURIString(String url, String originalUrl) throws IOExcept
if (imageTypes.contains(fileType)){
DataURIGenerator.setVerbose(verbose);
-
+
StringWriter writer = new StringWriter();
- if (url.startsWith("http://")){
- if (verbose){
- System.err.println("[INFO] Downloading '" + url + "' to generate data URI.");
- }
-
- DataURIGenerator.generate(new URL(url), writer);
-
- } else {
+ try {
+ if (url.startsWith("http://")){
+ if (verbose){
+ System.err.println("[INFO] Downloading '" + url + "' to generate data URI.");
+ }
+
+ DataURIGenerator.generate(new URL(url), writer);
+
+ } else {
+ if (verbose){
+ System.err.println("[INFO] Opening file '" + url + "' to generate data URI.");
+ }
+
+ File file = new File(url);
+
+ if (verbose && !file.isFile()){
+ System.err.println("[INFO] Could not find file '" + file.getCanonicalPath() + "'.");
+ }
+
+ DataURIGenerator.generate(new File(url), writer);
+ }
+
if (verbose){
- System.err.println("[INFO] Opening file '" + url + "' to generate data URI.");
- }
-
- File file = new File(url);
-
- if (verbose && !file.isFile()){
- System.err.println("[INFO] Could not find file '" + file.getCanonicalPath() + "'.");
- }
+ System.err.println("[INFO] Generated data URI for '" + url + "'.");
+ }
+ } catch (FileNotFoundException e){
+ System.err.println("[INFO] Could not find file. " + e.getMessage() + " Skipping.");
- DataURIGenerator.generate(new File(url), writer);
+ writer.write(originalUrl);
}
-
- if (verbose){
- System.err.println("[INFO] Generated data URI for '" + url + "'.");
- }
return writer.toString();

0 comments on commit 7909b49

Please sign in to comment.