X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fcom%2Fdogcows%2FUtil.java;h=ceb26a93f42c4940099a0dfec1c6a9360238902b;hb=76bb87cdcb7d155f97e02e3fb461c57da18f5724;hp=bc5bdb891acb4ed1867d52b71fd1aeb284e88f54;hpb=c981f9811ab15c2a521ab6e40b7b5dd7b3f3d88a;p=chaz%2Fvimcoder diff --git a/src/com/dogcows/Util.java b/src/com/dogcows/Util.java index bc5bdb8..ceb26a9 100644 --- a/src/com/dogcows/Util.java +++ b/src/com/dogcows/Util.java @@ -26,7 +26,7 @@ public abstract class Util System.arraycopy(b, 0, result, a.length, b.length); return result; } - + /** * Combined string elements from two arrays into a single array, gluing * together elements of the same index with a delimiter string. @@ -59,20 +59,20 @@ public abstract class Util for (int i = 1; i < a.length; ++i) result.append(glue).append(a[i]); return result.toString(); } - + /** - * Quote a string by replacing prepending backslashes and double + * Escape a string by replacing prepending backslashes and double * quotation characters with an extra backslash. - * @param The string to be quoted. - * @return The quoted string. + * @param The string to be escaped. + * @return The escaped string. */ - public static String quote(String a) + public static String escape(String a) { a = a.replaceAll("\\\\", "\\\\\\\\"); a = a.replaceAll("\"", "\\\\\\\""); return a; } - + /** * Simply read a file's contents into a string object. * @param file The file to read. @@ -82,12 +82,12 @@ public abstract class Util public static String readFile(File file) throws IOException { StringBuilder text = new StringBuilder(); - + BufferedReader reader = new BufferedReader(new FileReader(file.getPath())); try { String line = null; - + while ((line = reader.readLine()) != null) { text.append(line + System.getProperty("line.separator")); @@ -97,14 +97,15 @@ public abstract class Util { reader.close(); } - + return text.toString(); } - + /** - * Read a resource file into a string object. The resources should be - * placed in the directory `resources' underneath the parent directory of - * this class. Reading resources packaged in a jar is allowable. + * Read a resource file into a string object. + * The resources should be placed in the directory `resources' + * underneath the parent directory of this class. Reading resources + * packaged in a jar is allowable. * @param path Relative path to the resource. * @return The contents of the resource. * @throws IOException If the resource is not readable. @@ -112,7 +113,7 @@ public abstract class Util public static String readResource(String path) throws IOException { StringBuilder text = new StringBuilder(); - + InputStream stream = Util.class.getResourceAsStream("resources/" + path); if (stream != null) { @@ -130,16 +131,17 @@ public abstract class Util stream.close(); } } - - return text.toString(); + + return text.toString().replaceAll("\n", System.getProperty("line.separator")); } - + /** - * The poor man's template package. Provide a template and a map of terms - * to build the result with the terms expanded into the template. Terms - * in the template should appear surrounded with dollar signs. For example, - * if $MYTERM$ appears in the template, it will be replaced by the value - * into the terms map with the key MYTERM (if it exists in the map). + * The poor man's template package. + * Provide a template and a map of terms to build the result with the + * terms expanded into the template. Terms in the template should + * appear surrounded with dollar signs. For example, if $MYTERM$ + * appears in the template, it will be replaced by the value into the + * terms map with the key MYTERM (if it exists in the map). * @param template The template string. * @param terms A map of key/value terms. * @return The string expanded from the template and terms. @@ -149,10 +151,10 @@ public abstract class Util String text = template; for (String key : terms.keySet()) { - text = text.replaceAll("\\$" + key + "\\$", - Util.quote(terms.get(key))); + text = text.replaceAll("\\$" + key + "\\$", Util.escape(terms.get(key))); } return text; } } +// vim:et:ts=8:sts=4:sw=4