Skip to content
This repository has been archived by the owner on Nov 16, 2018. It is now read-only.

Commit

Permalink
Added option 'mhtmlfile' to direct MHTML to file separate from CSS.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarker committed Jan 4, 2012
1 parent 22fdfc0 commit 2e1f829
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
39 changes: 35 additions & 4 deletions src/net/nczonline/web/cssembed/CSSEmbed.java
Expand Up @@ -49,6 +49,9 @@ public static void main(String[] args) {
String cssOutputFilename = null;
ByteArrayOutputStream cssBytes = new ByteArrayOutputStream();
Writer cssOut = null;
String mhtmlFilename = null;
ByteArrayOutputStream mhtmlBytes = new ByteArrayOutputStream();
Writer mhtmlOut = null;
Reader in = null;
String root;
int options = CSSURLEmbedder.DATAURI_OPTION;
Expand All @@ -62,6 +65,7 @@ public static void main(String[] args) {
CmdLineParser.Option cssOutputFilenameOpt = parser.addStringOption('o', "output");
CmdLineParser.Option mhtmlOpt = parser.addBooleanOption("mhtml");
CmdLineParser.Option mhtmlRootOpt = parser.addStringOption("mhtmlroot");
CmdLineParser.Option mhtmlFilenameOpt = parser.addStringOption("mhtmlfile");
CmdLineParser.Option skipMissingOpt = parser.addBooleanOption("skip-missing");
CmdLineParser.Option uriLengthOpt = parser.addIntegerOption("max-uri-length");
CmdLineParser.Option imageSizeOpt = parser.addIntegerOption("max-image-size");
Expand Down Expand Up @@ -135,6 +139,10 @@ public static void main(String[] args) {
if (mhtml && mhtmlRoot == null){
throw new Exception("Must use --mhtmlroot when using --mhtml.");
}
mhtmlFilename = (String) parser.getOptionValue(mhtmlFilenameOpt);
if (null != mhtmlFilename){
options = options | CSSURLEmbedder.MHTML_FILE_OPTION;
}

//are missing files ok?
boolean skipMissingFiles = parser.getOptionValue(skipMissingOpt) != null;
Expand Down Expand Up @@ -169,13 +177,12 @@ public static void main(String[] args) {
System.err.println("[INFO] Using '" + root + "' as root for relative file paths.");
}

//get CSS output filename
//set CSS output
cssOutputFilename = (String) parser.getOptionValue(cssOutputFilenameOpt);
if (cssOutputFilename == null) {
if (verbose){
System.err.println("[INFO] No CSS output file specified, defaulting to stdout.");
}

cssOut = new OutputStreamWriter(System.out);
} else {
File cssOutputFile = new File(cssOutputFilename);
Expand All @@ -186,8 +193,19 @@ public static void main(String[] args) {
cssOut = new OutputStreamWriter(cssBytes, charset);
}

//set MHTML output
if (null != mhtmlFilename) {
File mhtmlOutputFile = new File(mhtmlFilename);
if (verbose){
System.err.println("[INFO] MHTML output file is '" + mhtmlOutputFile.getAbsolutePath() + "'");
}
mhtmlOut = new OutputStreamWriter(mhtmlBytes, charset);
// overwrite previous filename, if any
embedder.setFilename(mhtmlOutputFile.getName());
}

//set verbose option
embedder.embedImages(cssOut, root);
embedder.embedImages(cssOut, mhtmlOut, root);

} catch (CmdLineParser.OptionException e) {
usage();
Expand All @@ -202,7 +220,6 @@ public static void main(String[] args) {
if (cssOut != null) {
try {
cssOut.close();

if(cssBytes.size() > 0) {
cssBytes.writeTo(new FileOutputStream(cssOutputFilename));
}
Expand All @@ -213,6 +230,19 @@ public static void main(String[] args) {
}
}
}
if (mhtmlOut != null) {
try {
mhtmlOut.close();
if(mhtmlBytes.size() > 0) {
mhtmlBytes.writeTo(new FileOutputStream(mhtmlFilename));
}
} catch (IOException e) {
System.err.println("[ERROR] " + e.getMessage());
if (verbose){
e.printStackTrace();
}
}
}
}

}
Expand All @@ -229,6 +259,7 @@ private static void usage() {
+ " --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"
+ " --mhtmlfile <file> Place the MHTML output into <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"
Expand Down
27 changes: 22 additions & 5 deletions src/net/nczonline/web/cssembed/CSSURLEmbedder.java
Expand Up @@ -46,6 +46,7 @@ public class CSSURLEmbedder {
public static final int DATAURI_OPTION = 1;
public static final int MHTML_OPTION = 2;
public static final int SKIP_MISSING_OPTION = 4;
public static final int MHTML_FILE_OPTION = 8;

public static final int DEFAULT_MAX_URI_LENGTH = 32768;

Expand Down Expand Up @@ -153,7 +154,7 @@ public void setFilename(String filename){
* @throws java.io.IOException
*/
public void embedImages(Writer cssOut) throws IOException {
embedImages(cssOut, null);
embedImages(cssOut, null, null);
}

/**
Expand All @@ -163,6 +164,17 @@ public void embedImages(Writer cssOut) throws IOException {
* @throws java.io.IOException
*/
public void embedImages(Writer cssOut, String root) throws IOException {
embedImages(cssOut, null, root);
}

/**
* Embeds data URI images into a CSS file.
* @param cssOut The place to write out the CSS source code.
* @param mhtmlOut The place to write out the MHTML source code.
* @param root The root to prepend to any relative paths.
* @throws java.io.IOException
*/
public void embedImages(Writer cssOut, Writer mhtmlOut, String root) throws IOException {
BufferedReader reader = new BufferedReader(new StringReader(code));
StringBuilder cssBuilder = new StringBuilder();
StringBuilder mhtmlBuilder = new StringBuilder();
Expand All @@ -173,7 +185,6 @@ public void embedImages(Writer cssOut, String root) throws IOException {

//create initial MHTML code
if (hasOption(MHTML_OPTION)){
mhtmlBuilder.append("/*\n");
mhtmlBuilder.append("Content-Type: multipart/related; boundary=\"");
mhtmlBuilder.append(MHTML_SEPARATOR);
mhtmlBuilder.append("\"\n\n");
Expand Down Expand Up @@ -305,9 +316,15 @@ public void embedImages(Writer cssOut, String root) throws IOException {
mhtmlBuilder.append(MHTML_SEPARATOR);
mhtmlBuilder.append("--\n");

//close comment
mhtmlBuilder.append("*/\n");
cssOut.write(mhtmlBuilder.toString());
if (hasOption(MHTML_FILE_OPTION)) {
// write MHTML output to MHTML file
mhtmlOut.write(mhtmlBuilder.toString());
} else {
// wrap MHTML output in comments, write to CSS file
cssOut.write("/*\n");
cssOut.write(mhtmlBuilder.toString());
cssOut.write("*/\n");
}
}

if (verbose){
Expand Down

0 comments on commit 2e1f829

Please sign in to comment.