Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 4b99a55efafd87ccc50f0fa5207023f2072f68be Nicholas C. Zakas committed Oct 30, 2009
@@ -0,0 +1,2 @@
+build/
+nbproject/
44 README
@@ -0,0 +1,44 @@
+DataURI
+Copyright (c) 2009 Nicholas C. Zakas. All rights reserved.
+
+-------------------------------------------------------------------------------
+ About
+-------------------------------------------------------------------------------
+
+DataURI is a small program/library to make creation of data URIs for web
+content quick and easy.
+
+-------------------------------------------------------------------------------
+ Usage
+-------------------------------------------------------------------------------
+
+Usage: java -jar datauri-x.y.z.jar [options] [input files]
+
+Global Options
+ -h, --help Displays this information.
+ --charset <charset> Character set to encode into the data URI.
+ -v, --verbose Display informational messages and warnings.
+ -m, --mime <type> Mime type to encode into the data URI.
+ -o <file> Place the output into <file>. Defaults to stdout.
+
+-------------------------------------------------------------------------------
+ License
+-------------------------------------------------------------------------------
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,8 @@
+src.dir = src
+lib.dir = lib
+doc.dir = doc
+build.dir = build
+product.name = cssembed
+version.number = 0.1.0
+jar.name = ${product.name}-${version.number}.jar
+dist.package.name = ${product.name}-${version.number}
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="iso-8859-1"?>
+<project name="CSSEmbed" default="build.jar" basedir=".">
+
+ <target name="clean" depends="-load.properties">
+ <delete dir="${build.dir}" quiet="true"/>
+ </target>
+
+ <target name="-load.properties">
+ <property file="ant.properties"/>
+ </target>
+
+ <target name="-init" depends="-load.properties">
+ <mkdir dir="${build.dir}"/>
+ </target>
+
+ <target name="build.classes" depends="-init">
+ <mkdir dir="${build.dir}/classes"/>
+ <javac srcdir="${src.dir}"
+ destdir="${build.dir}/classes"
+ includes="**/*.java"
+ deprecation="off"
+ debug="on"
+ source="1.5">
+ <classpath>
+ <pathelement location="${lib.dir}/datauri-0.1.0.jar"/>
+ </classpath>
+ </javac>
+ </target>
+
+ <target name="build.jar" depends="build.classes">
+ <mkdir dir="${build.dir}/jar"/>
+ <unjar src="${lib.dir}/datauri-0.1.0.jar" dest="${build.dir}/jar"/>
+ <copy todir="${build.dir}/jar">
+ <fileset dir="${build.dir}/classes" includes="**/*.class"/>
+ </copy>
+ <jar destfile="${build.dir}/${jar.name}" basedir="${build.dir}/jar">
+ <manifest>
+ <attribute name="Main-Class" value="net.nczonline.web.cssembed.CSSEmbed"/>
+ </manifest>
+ </jar>
+ </target>
+
+ <target name="build.dist.package" depends="build.jar">
+ <mkdir dir="${build.dir}/${dist.package.name}"/>
+ <mkdir dir="${build.dir}/${dist.package.name}/build"/>
+ <copy file="${build.dir}/${jar.name}" todir="${build.dir}/${dist.package.name}/build"/>
+ <copy todir="${build.dir}/${dist.package.name}">
+ <fileset dir=".">
+ <include name="ant.properties"/>
+ <include name="build.xml"/>
+ <include name="doc/**/*"/>
+ <include name="lib/**/*"/>
+ <include name="src/**/*"/>
+ <exclude name="**/.git"/>
+ </fileset>
+ </copy>
+ <zip destfile="${build.dir}/${dist.package.name}.zip"
+ basedir="${build.dir}"
+ includes="${dist.package.name}/**/*"/>
+ </target>
+
+</project>
Binary file not shown.
@@ -0,0 +1,151 @@
+/*
+ * 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
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package net.nczonline.web.cssembed;
+
+import jargs.gnu.CmdLineParser;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
+
+
+public class CSSEmbed {
+
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+
+ //default settings
+ boolean verbose = false;
+ String charset = null;
+ String outputFilename = null;
+ Writer out = null;
+ String mimeType = null;
+ Reader in = null;
+
+ //initialize command line parser
+ CmdLineParser parser = new CmdLineParser();
+ CmdLineParser.Option verboseOpt = parser.addBooleanOption('v', "verbose");
+ CmdLineParser.Option helpOpt = parser.addBooleanOption('h', "help");
+ CmdLineParser.Option charsetOpt = parser.addStringOption("charset");
+ CmdLineParser.Option outputFilenameOpt = parser.addStringOption('o', "output");
+
+ try {
+
+ //parse the arguments
+ parser.parse(args);
+
+ //figure out if the help option has been executed
+ Boolean help = (Boolean) parser.getOptionValue(helpOpt);
+ if (help != null && help.booleanValue()) {
+ usage();
+ System.exit(0);
+ }
+
+ //determine boolean options
+ verbose = parser.getOptionValue(verboseOpt) != null;
+
+ //check for charset
+ charset = (String) parser.getOptionValue(charsetOpt);
+
+ //get the file arguments
+ String[] fileArgs = parser.getRemainingArgs();
+
+ //need to have at least one file
+ if (fileArgs.length == 0){
+ System.err.println("[ERROR] No files specified.");
+ System.exit(1);
+ }
+
+ //only the first filename is used
+ String inputFilename = fileArgs[0];
+
+ in = new InputStreamReader(new FileInputStream(fileArgs[0]), charset);
+
+
+ //get output filename
+ outputFilename = (String) parser.getOptionValue(outputFilenameOpt);
+
+ if (outputFilename == null) {
+ if (verbose){
+ System.err.println("[INFO] No 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);
+ }
+
+ //set verbose option
+ CSSURLEmbedder.setVerbose(verbose);
+
+ CSSURLEmbedder.embedImages(in, out);
+ //determine if the filename is a local file or a URL
+// if (inputFilename.startsWith("http://")){
+// CSSURLEmbedder.generate(new URL(inputFilename), out, mimeType);
+// } else {
+// CSSURLEmbedder.generate(new File(inputFilename), out, mimeType);
+// }
+
+ } catch (CmdLineParser.OptionException e) {
+ usage();
+ System.exit(1);
+ } catch (Exception e) {
+ e.printStackTrace();
+ System.exit(1);
+ } finally {
+ if (out != null) {
+ try {
+ out.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ }
+
+ /**
+ * Outputs help information to the console.
+ */
+ private static void usage() {
+ System.out.println(
+ "\nUsage: java -jar datauri-x.y.z.jar [options] [input files]\n\n"
+
+ + "Global Options\n"
+ + " -h, --help Displays this information.\n"
+ + " --charset <charset> Character set of the input file.\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.");
+ }
+}
@@ -0,0 +1,93 @@
+/*
+ * 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
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+package net.nczonline.web.cssembed;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Writer;
+import java.io.Reader;
+
+/**
+ * Generator for Data URIs.
+ * @author Nicholas C. Zakas
+ */
+public class CSSURLEmbedder {
+
+ private static boolean verbose = false;
+
+
+ //--------------------------------------------------------------------------
+ // Get/Set verbose flag
+ //--------------------------------------------------------------------------
+
+ public static boolean getVerbose(){
+ return verbose;
+ }
+
+ public static void setVerbose(boolean newVerbose){
+ verbose = newVerbose;
+ }
+
+ //--------------------------------------------------------------------------
+ // Embed images
+ //--------------------------------------------------------------------------
+
+
+ public static void embedImages(Reader in, Writer out) throws IOException {
+ BufferedReader reader = new BufferedReader(in);
+ StringBuilder builder = new StringBuilder();
+ String line;
+ int lineNum = 1;
+
+
+ while((line = reader.readLine()) != null){
+
+ int start = 0;
+ int pos = line.indexOf("url(", start);
+
+ if (pos > -1){
+ while (pos > -1){
+ pos += 4;
+ builder.append(line.substring(start, pos));
+ String url = line.substring(pos, line.indexOf(")", pos));
+ if (verbose){
+ System.err.println("[INFO] Found URL '" + url + "' at line " + lineNum + ", col " + pos + ".");
+ }
+
+ start = pos + url.length();
+ pos = line.indexOf("url(", start);
+ }
+ } else {
+ builder.append(line);
+ }
+
+ builder.append("\n");
+ lineNum++;
+ }
+
+
+ }
+
+
+}

0 comments on commit 4b99a55

Please sign in to comment.