Browse files

Cleaned up interface and added Javadoc comments

  • Loading branch information...
1 parent 940d46f commit e9fffe94d38770ce90faa0f89e01beac0cb4ab33 Nicholas C. Zakas committed Oct 26, 2009
Showing with 136 additions and 81 deletions.
  1. +17 −33 src/net/nczonline/web/datauri/DataURI.java
  2. +119 −48 src/net/nczonline/web/datauri/DataURIGenerator.java
View
50 src/net/nczonline/web/datauri/DataURI.java
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2009 Nicholas C. Zakas. All rights reserved.
+ * http://www.nczonline.net/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -22,13 +23,13 @@
package net.nczonline.web.datauri;
import jargs.gnu.CmdLineParser;
-import java.io.*;
-import java.nio.charset.Charset;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
-/*
- * This file heavily inspired and based on YUI Compressor.
- * http://github.com/yui/yuicompressor
- */
public class DataURI {
@@ -43,10 +44,7 @@ public static void main(String[] args) {
String charset = null;
String outputFilename = null;
Writer out = null;
- Reader in = null;
String mimeType = null;
- String inputCharset = null;
-
//initialize command line parser
CmdLineParser parser = new CmdLineParser();
@@ -86,48 +84,35 @@ public static void main(String[] args) {
System.exit(1);
}
+ //only the first filename is used
String inputFilename = fileArgs[0];
- //inputCharset = DataURIGenerator.getCharset(inputFilename, charset);
-
-// if (DataURIGenerator.isImageFile(inputFilename)){
-// if (verbose){
-// System.err.println("[INFO] Image file detecting, not using specified charset.");
-// }
-// in = new InputStreamReader(new FileInputStream(inputFilename));
-// } else {
-// in = new InputStreamReader(new FileInputStream(inputFilename), inputCharset);
-// }
+
//get output filename
outputFilename = (String) parser.getOptionValue(outputFilenameOpt);
if (outputFilename == null) {
- if (charset == null){
- out = new OutputStreamWriter(System.out);
- } else {
- out = new OutputStreamWriter(System.out, charset);
- }
+ if (verbose){
+ System.err.println("[INFO] Not output file specified, defaulting to stdout.");
+ }
+
+ out = new OutputStreamWriter(System.out);
} else {
if (verbose){
System.err.println("[INFO] Output file is '" + (new File(outputFilename)).getAbsolutePath() + "'");
}
out = new OutputStreamWriter(new FileOutputStream(outputFilename), charset);
}
-
-
-
- //close just in case out is the same as in
- //in.close(); in = null;
//generate
DataURIGenerator.generate(new File(inputFilename), out, mimeType, charset, verbose);
+
} catch (CmdLineParser.OptionException e) {
usage();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
- System.err.println(e.getMessage());
System.exit(1);
} finally {
if (out != null) {
@@ -141,17 +126,16 @@ public static void main(String[] args) {
}
-
/**
* Outputs help information to the console.
*/
private static void usage() {
System.out.println(
- "\nUsage: java -jar combiner-x.y.z.jar [options] [input files]\n\n"
+ "\nUsage: java -jar datauri-x.y.z.jar [options] [input files]\n\n"
+ "Global Options\n"
+ " -h, --help Displays this information.\n"
- + " --charset <charset> Read the input file and write data URI using <charset>.\n"
+ + " --charset <charset> Character set to encode into the data URI.\n"
+ " -v, --verbose Display informational messages and warnings.\n"
+ " -m, --mime <type> Mime type to encode into the data URI.\n"
+ " -o <file> Place the output into <file>. Defaults to stdout.");
View
167 src/net/nczonline/web/datauri/DataURIGenerator.java
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2009 Nicholas C. Zakas. All rights reserved.
+ * http://www.nczonline.net/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -22,28 +23,26 @@
package net.nczonline.web.datauri;
-import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
-import java.nio.charset.UnsupportedCharsetException;
import java.util.HashMap;
import org.apache.commons.codec.binary.Base64;
-import java.awt.image. BufferedImage;
import java.io.FileInputStream;
import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
import java.nio.charset.Charset;
-import javax.imageio.ImageIO;
-
+/**
+ * Generator for Data URIs.
+ * @author Nicholas C. Zakas
+ */
public class DataURIGenerator {
private static HashMap imageTypes = new HashMap();
private static HashMap textTypes = new HashMap();
+ //initialize file types and MIME types
static {
imageTypes.put("gif", "image/gif");
imageTypes.put("jpg", "image/jpeg");
@@ -59,44 +58,100 @@
textTypes.put("txt", "text/plain");
}
-
- public static void generate(File file, Writer out, boolean verbose) throws IOException, UnsupportedCharsetException {
+ //--------------------------------------------------------------------------
+ // Generate data URIs from a file
+ //--------------------------------------------------------------------------
+
+ /**
+ * Generates a data URI from a file, outputting it to the given writer. The
+ * MIME type is determined from examining the filename.
+ * @param file The file from which to generate the data URI.
+ * @param out Where to output the data URI.
+ * @throws java.io.IOException
+ */
+ public static void generate(File file, Writer out) throws IOException {
+ generate(file, out, null, false);
+ }
+
+ /**
+ * Generates a data URI from a file, outputting it to the given writer. The
+ * MIME type is determined from examining the filename.
+ * @param file The file from which to generate the data URI.
+ * @param out Where to output the data URI.
+ * @param verbose Whether to display additional information.
+ * @throws java.io.IOException
+ */
+ public static void generate(File file, Writer out, boolean verbose) throws IOException {
generate(file, out, null, verbose);
}
- public static void generate(File file, Writer out, String mimeType, boolean verbose) throws IOException, UnsupportedCharsetException {
+ /**
+ * Generates a data URI from a file, outputting it to the given writer.
+ * @param file The file from which to generate the data URI.
+ * @param out Where to output the data URI.
+ * @param mimeType The MIME type to use for the data URI.
+ * @throws java.io.IOException
+ */
+ public static void generate(File file, Writer out, String mimeType) throws IOException {
+ generate(file, out, null, null, false);
+ }
+
+ /**
+ * Generates a data URI from a file, outputting it to the given writer.
+ * @param file The file from which to generate the data URI.
+ * @param out Where to output the data URI.
+ * @param mimeType The MIME type to use for the data URI.
+ * @param verbose Whether to display additional information.
+ * @throws java.io.IOException
+ */
+ public static void generate(File file, Writer out, String mimeType, boolean verbose) throws IOException {
generate(file, out, null, null, verbose);
}
- public static void generate(File file, Writer out, String mimeType, String charset, boolean verbose) throws IOException, UnsupportedCharsetException {
- //if (isImageFile(file.getName())){
- // generateImage(file, out, mimeType, verbose);
- //} else {
- // generateText(file, out, mimeType, charset, verbose);
- //}
- generateDataURI(file, out, getMimeType(file.getName(), mimeType, verbose), getCharset(file.getName(), charset, verbose), verbose);
+ /**
+ * Generates a data URI from a file, outputting it to the given writer.
+ * @param file The file from which to generate the data URI.
+ * @param out Where to output the data URI.
+ * @param mimeType The MIME type to use for the data URI.
+ * @param charset The charset to use for the data URI.
+ * @throws java.io.IOException
+ */
+ public static void generate(File file, Writer out, String mimeType, String charset) throws IOException {
+ generate(file, out, mimeType, charset, false);
}
- private static void generateImage(File file, Writer out, String mimeType, boolean verbose) throws IOException {
-
- //have to read images in using ImageIO to avoid encoding issues
- String imageType = getImageType(file.getName());
- BufferedImage image = ImageIO.read(file);
- ByteArrayOutputStream bytes = new ByteArrayOutputStream();
- ImageIO.write(image, imageType, bytes);
- bytes.flush();
- byte[] rawBytes = bytes.toByteArray();
- bytes.close();
-
- if (verbose){
- System.err.println("[INFO] Image is of type '" + imageType + "'.");
- }
-
- generateDataURI(rawBytes, out, getMimeType(file.getName(), mimeType, verbose), null, verbose);
+ /**
+ * Generates a data URI from a file, outputting it to the given writer.
+ * @param file The file from which to generate the data URI.
+ * @param out Where to output the data URI.
+ * @param mimeType The MIME type to use for the data URI.
+ * @param charset The charset to use for the data URI.
+ * @param verbose Whether to display additional information.
+ * @throws java.io.IOException
+ */
+ public static void generate(File file, Writer out, String mimeType, String charset, boolean verbose) throws IOException {
+ generateDataURI(file, out, getMimeType(file.getName(), mimeType, verbose), getCharset(file.getName(), charset, verbose));
}
+
+ //--------------------------------------------------------------------------
+ // Generate data URIs from a file
+ //--------------------------------------------------------------------------
+
+
+ //--------------------------------------------------------------------------
+ // Helper methods
+ //--------------------------------------------------------------------------
- private static void generateDataURI(File file, Writer out, String mimeType, String charset, boolean verbose) throws IOException{
+ /**
+ * Generates a data URI from the specified file and outputs to the given writer.
+ * @param file The file to from which to create a data URI.
+ * @param out Where to output the data URI.
+ * @param mimeType The MIME type to specify in the data URI.
+ * @param charset The character set to specify in the data URI.
+ * @throws java.io.IOException
+ */
+ private static void generateDataURI(File file, Writer out, String mimeType, String charset) throws IOException{
//read the bytes from the file
InputStream in = new FileInputStream(file);
@@ -105,10 +160,18 @@ private static void generateDataURI(File file, Writer out, String mimeType, Stri
in.close();
//actually write
- generateDataURI(bytes, out, mimeType, charset, verbose);
+ generateDataURI(bytes, out, mimeType, charset);
}
- private static void generateDataURI(byte[] bytes, Writer out, String mimeType, String charset, boolean verbose) throws IOException {
+ /**
+ * Generates a data URI from a byte array and outputs to the given writer.
+ * @param bytes The array of bytes to output to the data URI.
+ * @param out Where to output the data URI.
+ * @param mimeType The MIME type to specify in the data URI.
+ * @param charset The character set to specify in the data URI.
+ * @throws java.io.IOException
+ */
+ private static void generateDataURI(byte[] bytes, Writer out, String mimeType, String charset) throws IOException {
//create the output
StringBuffer buffer = new StringBuffer();
buffer.append("data:");
@@ -153,18 +216,17 @@ private static String getFileType(String filename){
return type;
}
-
- private static String getImageType(String filename){
- String type = getFileType(filename);
- if (type.equals("gif")){
- return "GIF";
- } else if (type.equals("jpeg") || type.equals("jpg")){
- return "JPEG";
- } else {
- return "PNG";
- }
- }
-
+
+ /**
+ * Determines the MIME type to use for the given filename. If a MIME type
+ * is passed in, then that is used by default. Otherwise, the filename
+ * is inspected to determine the appropriate MIME type.
+ * @param filename The filename to check.
+ * @param mimeType The provided MIME type or null if nothing was provided.
+ * @param verbose Whether or not to display additional information
+ * @return The MIME type string to use for the filename.
+ * @throws java.io.IOException When no MIME type can be determined.
+ */
private static String getMimeType(String filename, String mimeType, boolean verbose) throws IOException {
if (mimeType == null){
@@ -187,6 +249,15 @@ private static String getMimeType(String filename, String mimeType, boolean verb
return mimeType;
}
+ /**
+ * Determines the charset to use for the given filename. If a charset is
+ * passed in, then that is used by default. If the filename represents an
+ * image, this method always returns null because no charset should be used.
+ * @param filename The filename to check.
+ * @param charset The provided charset.
+ * @param verbose Whether or not to display additional information.
+ * @return The charset string or null if no charset should be used.
+ */
private static String getCharset(String filename, String charset, boolean verbose) {
if (charset != null){

0 comments on commit e9fffe9

Please sign in to comment.