Permalink
Browse files

Merge branch 'max-uri-length' of git://github.com/fearphage/cssembed …

…into max-uri-length

Conflicts:
	src/net/nczonline/web/cssembed/CSSEmbed.java
  • Loading branch information...
2 parents 4c60b2d + cf25dd8 commit 34a8a24ba0de4b5e52670d745625beb4099a0c8a @tivac tivac committed Apr 19, 2011
Showing with 36 additions and 13 deletions.
  1. +22 −12 src/net/nczonline/web/cssembed/CSSEmbed.java
  2. +14 −1 src/net/nczonline/web/cssembed/CSSURLEmbedder.java
@@ -63,7 +63,7 @@ public static void main(String[] args) {
CmdLineParser.Option mhtmlOpt = parser.addBooleanOption("mhtml");
CmdLineParser.Option mhtmlRootOpt = parser.addStringOption("mhtmlroot");
CmdLineParser.Option skipMissingOpt = parser.addBooleanOption("skip-missing");
-
+ CmdLineParser.Option uriLengthOpt = parser.addIntegerOption('u', "maxuri");
try {
@@ -114,14 +114,22 @@ public static void main(String[] args) {
if (mhtml && mhtmlRoot == null){
throw new Exception("Must use --mhtmlroot when using --mhtml.");
}
+ int maxurilength = 0;
+ Integer uriOption = (Integer) parser.getOptionValue(uriLengthOpt);
+ if (uriOption != null){
+ maxurilength = uriOption.intValue();
+ if (maxurilength < 0){
+ maxurilength = 0;
+ }
+ }
//are missing files ok?
boolean skipMissingFiles = parser.getOptionValue(skipMissingOpt) != null;
if(skipMissingFiles) {
options = options | CSSURLEmbedder.SKIP_MISSING_OPTION;
}
- CSSURLEmbedder embedder = new CSSURLEmbedder(in, options, verbose);
+ CSSURLEmbedder embedder = new CSSURLEmbedder(in, options, verbose, maxurilength);
embedder.setMHTMLRoot(mhtmlRoot);
//close in case writing to the same file
@@ -200,16 +208,18 @@ public static void main(String[] args) {
*/
private static void usage() {
System.out.println(
- "\nUsage: java -jar cssembed-x.y.z.jar [options] [input file]\n\n"
+ "\nUsage: java -jar cssembed-x.y.z.jar [options] [input file]\n\n"
- + "Global Options\n"
- + " -h, --help Displays this information.\n"
- + " --charset <charset> Character set of the input file.\n"
- + " --mhtml Enable MHTML mode.\n"
- + " --mhtmlroot <root> Use <root> as the MHTML root for the file.\n"
- + " -v, --verbose Display informational messages and warnings.\n"
- + " --root <root> Prepends <root> to all relative URLs.\n"
- + " --skip-missing Don't throw an error for missing image files.\n"
- + " -o <file> Place the output into <file>. Defaults to stdout.");
+ + "Global Options\n"
+ + " -h, --help Displays this information.\n"
+ + " --charset <charset> Character set of the input file.\n"
+ + " --mhtml Enable MHTML mode.\n"
+ + " --mhtmlroot <root> Use <root> as the MHTML root for the file.\n"
+ + " -v, --verbose Display informational messages and warnings.\n"
+ + " --root <root> Prepends <root> to all relative URLs.\n"
+ + " --skip-missing Don't throw an error for missing image files.\n"
+ + " -u, --maxuri length Maximum length of data URI to use.\n"
+ + " -o <file> Place the output into <file>. Defaults to stdout."
+ );
}
}
@@ -62,6 +62,7 @@
private int options = 1;
private String mhtmlRoot = "";
private String outputFilename = "";
+ private int maxurilength;
//--------------------------------------------------------------------------
// Constructors
@@ -80,9 +81,14 @@ public CSSURLEmbedder(Reader in, boolean verbose) throws IOException {
}
public CSSURLEmbedder(Reader in, int options, boolean verbose) throws IOException {
+ this(in,1,verbose,0);
+ }
+
+ public CSSURLEmbedder(Reader in, int options, boolean verbose, int maxurilength) throws IOException {
this.code = readCode(in);
this.verbose = verbose;
this.options = options;
+ this.maxurilength = maxurilength;
}
//--------------------------------------------------------------------------
@@ -149,6 +155,7 @@ public void embedImages(Writer out, String root) throws IOException {
StringBuilder builder = new StringBuilder();
StringBuilder mhtmlHeader = new StringBuilder();
HashMap<String,Integer> foundMedia = new HashMap<String,Integer>();
+ int conversions = 0;
String line;
int lineNum = 1;
@@ -224,6 +231,11 @@ public void embedImages(Writer out, String root) throws IOException {
if (uriString.length() > 32768){
System.err.println("[WARNING] File " + newUrl + " creates a data URI larger than 32KB. IE8 can't display data URI images this large. Skipping.");
builder.append(url);
+ } else if (maxurilength > 0 && uriString.length() > maxurilength){
+ if (verbose) {
+ System.err.println("[INFO] File " + newUrl + " creates a data URI longer than " + maxurilength + " characters. Skipping.");
+ }
+ builder.append(url);
} else {
/*
@@ -248,6 +260,7 @@ public void embedImages(Writer out, String root) throws IOException {
builder.append(getMHTMLPath());
builder.append("!");
builder.append(entryName);
+ conversions++;
} else if (hasOption(DATAURI_OPTION)){
builder.append(uriString);
}
@@ -273,7 +286,7 @@ public void embedImages(Writer out, String root) throws IOException {
}
reader.close();
- if (hasOption(MHTML_OPTION)){
+ if (hasOption(MHTML_OPTION) && conversions > 0){
//Add one more boundary to fix IE/Vista issue
mhtmlHeader.append("\n--");

0 comments on commit 34a8a24

Please sign in to comment.