diff --git a/CHANGELOG b/CHANGELOG index a8faff2..2e6c0cd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,8 @@ +December 30, 2012 + +* Added option to enable pretty printing: [-p | --pretty] +* Added option to disable escaping of HTML entities like <>: [-e | --noescape] + December 15, 2011 - v1.0.0 * First public release diff --git a/ant.properties b/ant.properties index a8247c5..b04183b 100644 --- a/ant.properties +++ b/ant.properties @@ -15,6 +15,6 @@ class.version = 1.5 #props2js properties props2js.name = props2js -props2js.version = 0.1.0 +props2js.version = 0.1.2 props2js.jar = ${props2js.name}-${props2js.version}.jar props2js.main = net.nczonline.web.props2js.Props2Js diff --git a/src/net/nczonline/web/props2js/LinkedProperties.java b/src/net/nczonline/web/props2js/LinkedProperties.java new file mode 100644 index 0000000..3cadc63 --- /dev/null +++ b/src/net/nczonline/web/props2js/LinkedProperties.java @@ -0,0 +1,46 @@ +package net.nczonline.web.props2js; + +import java.util.*; + +/** + * Pulled from http://stackoverflow.com/questions/1312383/pulling-values-from-a-java-properties-file-in-order + */ +public class LinkedProperties extends Properties { + + private final LinkedHashSet keys = new LinkedHashSet(); + + @Override + public Enumeration propertyNames() { + return Collections.enumeration(keys); + } + + @Override + public synchronized Enumeration elements() { + return Collections.enumeration(keys); + } + + public Enumeration keys() { + return Collections.enumeration(keys); + } + + public Set keySet() { + return Collections.unmodifiableSet(keys); + } + + public Object put(Object key, Object value) { + keys.add(key); + return super.put(key, value); + } + + @Override + public synchronized Object remove(Object key) { + keys.remove(key); + return super.remove(key); + } + + @Override + public synchronized void clear() { + keys.clear(); + super.clear(); + } +} diff --git a/src/net/nczonline/web/props2js/PropertyConverter.java b/src/net/nczonline/web/props2js/PropertyConverter.java index 117cee5..1de3c2e 100644 --- a/src/net/nczonline/web/props2js/PropertyConverter.java +++ b/src/net/nczonline/web/props2js/PropertyConverter.java @@ -24,6 +24,7 @@ package net.nczonline.web.props2js; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import java.util.Properties; import java.util.regex.Matcher; @@ -36,28 +37,43 @@ public class PropertyConverter { private static Pattern floatPattern = Pattern.compile( "^[0-9]+(\\.[0-9]+)?$" ); private static Pattern intPattern = Pattern.compile( "^[0-9]+$" ); - + private static Pattern keyPattern = Pattern.compile( "\\." ); /** * Converts the properties object into a JSON string. * @param properties The properties object to convert. * @return A JSON string representing the object. */ - public static String convertToJson(Properties properties){ + public static String convertToJson(Properties properties, boolean pretty, boolean escape, boolean flattenKeys){ JsonObject json = new JsonObject(); for (Object key: properties.keySet()){ - String value = properties.getProperty(key.toString()); + String keyValue = key.toString(); + String value = properties.getProperty(keyValue); + if (flattenKeys){ + Matcher matcher = keyPattern.matcher(keyValue); + keyValue = matcher.replaceAll("_"); + } if (value.equals("true") || value.equals("false")){ - json.addProperty(key.toString(), Boolean.parseBoolean(value)); + json.addProperty(keyValue, Boolean.parseBoolean(value)); } else if (intPattern.matcher(value).matches()){ - json.addProperty(key.toString(), Integer.parseInt(value)); + json.addProperty(keyValue, Integer.parseInt(value)); } else if (floatPattern.matcher(value).matches()){ - json.addProperty(key.toString(), Float.parseFloat(value)); + json.addProperty(keyValue, Float.parseFloat(value)); } else { - json.addProperty(key.toString(), value); + json.addProperty(keyValue, value); } } - return new Gson().toJson(json); + if(!pretty && escape){ + return new Gson().toJson(json); + } + GsonBuilder builder = new GsonBuilder(); + if(pretty) { + builder.setPrettyPrinting(); + } + if(!escape) { + builder.disableHtmlEscaping(); + } + return builder.create().toJson(json); } /** @@ -67,8 +83,8 @@ public static String convertToJson(Properties properties){ * @param callback The name of the callback to call. * @return A JSONP string representing the object. */ - public static String convertToJsonP(Properties properties, String callback){ - return callback + "(" + convertToJson(properties) + ");"; + public static String convertToJsonP(Properties properties, String callback, boolean pretty, boolean escape, boolean flattenKeys){ + return callback + "(" + convertToJson(properties, pretty, escape, flattenKeys) + ");"; } /** @@ -78,8 +94,8 @@ public static String convertToJsonP(Properties properties, String callback){ * @param variable The name of the variable to store the object. * @return A JavaScript string representing the object. */ - public static String convertToJavaScript(Properties properties, String variable){ - String result = variable + "=" + convertToJson(properties) + ";"; + public static String convertToJavaScript(Properties properties, String variable, boolean pretty, boolean escape, boolean flattenKeys){ + String result = variable + "=" + convertToJson(properties, pretty, escape, flattenKeys) + ";"; if (!variable.contains(".")){ result = "var " + result; } diff --git a/src/net/nczonline/web/props2js/Props2Js.java b/src/net/nczonline/web/props2js/Props2Js.java index 8f1449d..8ee9c2d 100644 --- a/src/net/nczonline/web/props2js/Props2Js.java +++ b/src/net/nczonline/web/props2js/Props2Js.java @@ -45,6 +45,9 @@ public static void main(String[] args) { //default settings boolean verbose = false; + boolean pretty = false; + boolean escape = true; + boolean flattenKeys = true; String outputFilename = null; ByteArrayOutputStream bytes = new ByteArrayOutputStream(); Writer out = null; @@ -57,6 +60,9 @@ public static void main(String[] args) { CmdLineParser.Option outputFilenameOpt = parser.addStringOption('o', "output"); CmdLineParser.Option nameOpt = parser.addStringOption('n', "name"); CmdLineParser.Option outputTypeOpt = parser.addStringOption('t', "to"); + CmdLineParser.Option prettyOpt = parser.addBooleanOption('p', "pretty"); + CmdLineParser.Option noEscapeHtmlOpt = parser.addBooleanOption('e', "noescape"); + CmdLineParser.Option flattenKeysOpt = parser.addBooleanOption('f', "flattenKeys"); try { @@ -72,8 +78,15 @@ public static void main(String[] args) { //determine boolean options verbose = parser.getOptionValue(verboseOpt) != null; - - //get the file arguments + flattenKeys = parser.getOptionValue(flattenKeysOpt) != null; + + //determine if pretty printing is on + pretty = parser.getOptionValue(prettyOpt) != null; + + //determine if we should not escape HTML + escape = parser.getOptionValue(noEscapeHtmlOpt) == null; + + //get the file arguments String[] fileArgs = parser.getRemainingArgs(); String inputFilename = fileArgs[0]; @@ -82,7 +95,7 @@ public static void main(String[] args) { } in = new InputStreamReader(new FileInputStream(inputFilename), "UTF-8"); - Properties properties = new Properties(); + LinkedProperties properties = new LinkedProperties(); properties.load(in); //get output type @@ -121,11 +134,11 @@ public static void main(String[] args) { String result = ""; if (outputType.equalsIgnoreCase("js")){ - result = PropertyConverter.convertToJavaScript(properties, name); + result = PropertyConverter.convertToJavaScript(properties, name, pretty, escape, flattenKeys); } else if (outputType.equalsIgnoreCase("jsonp")){ - result = PropertyConverter.convertToJsonP(properties, name); + result = PropertyConverter.convertToJsonP(properties, name, pretty, escape, flattenKeys); } else { - result = PropertyConverter.convertToJson(properties); + result = PropertyConverter.convertToJson(properties, pretty, escape, flattenKeys); } out.write(result); @@ -177,8 +190,11 @@ private static void usage() { + "Global Options\n" + " -h, --help Displays this information.\n" + " -v, --verbose Display informational messages and warnings.\n" + + " -p, --pretty Print in multiple lines (More human readable).\n" + + " -e, --noescape Disable escaping of html charators (Default: escape).\n" + + " -f, --flattenKeys Replace dots in key strings with underscores.\n" + " --name The variable/callback name.\n" + " --to The output format: json (default), jsonp, or js.\n" + " -o Place the output into . Defaults to stdout."); } -} \ No newline at end of file +} diff --git a/tests/net/nczonline/web/props2js/PropertyConverterTest.java b/tests/net/nczonline/web/props2js/PropertyConverterTest.java index e463ce9..7092d19 100644 --- a/tests/net/nczonline/web/props2js/PropertyConverterTest.java +++ b/tests/net/nczonline/web/props2js/PropertyConverterTest.java @@ -3,6 +3,8 @@ package net.nczonline.web.props2js; import java.util.Properties; + +import net.nczonline.web.props2js.PropertyConverter; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -15,7 +17,43 @@ public class PropertyConverterTest { private Properties properties; - private final String resultJson = "{\"price\":12.3,\"count\":10,\"found\":true,\"baz\":\"boom\",\"foo\":\"bar\"}"; + private Properties propertiesHtml; + private final String resultJson = "{\"price\":12.3,\"count\":10,\"found\":true,\"under.score\":\"under_score\",\"baz\":\"boom\",\"foo\":\"bar\"}"; + private final String resultPretty = "{\n"+ + " \"price\": 12.3,\n"+ + " \"count\": 10,\n"+ + " \"found\": true,\n"+ + " \"under.score\": \"under_score\",\n"+ + " \"baz\": \"boom\",\n"+ + " \"foo\": \"bar\"\n"+ + "}"; + + private final String resultFlattenKeys = "{\n"+ + " \"price\": 12.3,\n"+ + " \"count\": 10,\n"+ + " \"found\": true,\n"+ + " \"under_score\": \"under_score\",\n"+ + " \"baz\": \"boom\",\n"+ + " \"foo\": \"bar\"\n"+ + "}"; + + private final String resultNoEscape = "{\n"+ + " \"html\": \"This is Bold text\",\n"+ + " \"price\": 12.3,\n"+ + " \"count\": 10,\n"+ + " \"found\": true,\n"+ + " \"under.score\": \"under_score\",\n"+ + " \"baz\": \"boom\"\n"+ + "}"; + + private final String resultEscape = "{\n"+ + " \"html\": \"This is \\u003cb\\u003eBold text\\u003c/b\\u003e\",\n"+ + " \"price\": 12.3,\n"+ + " \"count\": 10,\n"+ + " \"found\": true,\n"+ + " \"under.score\": \"under_score\",\n"+ + " \"baz\": \"boom\"\n"+ + "}"; @Before public void setUp(){ @@ -25,6 +63,15 @@ public void setUp(){ properties.setProperty("foo", "bar"); properties.setProperty("found", "true"); properties.setProperty("price", "12.3"); + properties.setProperty("under.score", "under_score"); + + propertiesHtml = new Properties(); + propertiesHtml.setProperty("baz", "boom"); + propertiesHtml.setProperty("count", "10"); + propertiesHtml.setProperty("html", "This is Bold text"); + propertiesHtml.setProperty("found", "true"); + propertiesHtml.setProperty("price", "12.3"); + propertiesHtml.setProperty("under.score", "under_score"); } @After @@ -34,25 +81,49 @@ public void tearDown(){ @Test public void testConvertToJSON(){ - String result = PropertyConverter.convertToJson(properties); + String result = PropertyConverter.convertToJson(properties, false, true, false); assertEquals(resultJson, result); } @Test public void testConvertToJSONP(){ - String result = PropertyConverter.convertToJsonP(properties, "func"); + String result = PropertyConverter.convertToJsonP(properties, "func", false, true, false); assertEquals("func(" + resultJson + ");", result); } @Test public void testConvertToJavaScript(){ - String result = PropertyConverter.convertToJavaScript(properties, "foo"); + String result = PropertyConverter.convertToJavaScript(properties, "foo", false, true, false); assertEquals("var foo=" + resultJson + ";", result); } @Test public void testConvertToJavaScriptProperty(){ - String result = PropertyConverter.convertToJavaScript(properties, "foo.bar"); + String result = PropertyConverter.convertToJavaScript(properties, "foo.bar", false, true, false); assertEquals("foo.bar=" + resultJson + ";", result); } + + @Test + public void testConvertToJavaScriptPretty(){ + String result = PropertyConverter.convertToJavaScript(properties, "foo", true, true, false); + assertEquals("var foo=" + resultPretty + ";", result); + } + + @Test + public void testConvertToJavaScriptNoEscapePretty(){ + String result = PropertyConverter.convertToJavaScript(propertiesHtml, "foo", true, false, false); + assertEquals("var foo=" + resultNoEscape + ";", result); + } + + @Test + public void testConvertToJavaScriptEscapePretty(){ + String result = PropertyConverter.convertToJavaScript(propertiesHtml, "foo", true, true, false); + assertEquals("var foo=" + resultEscape + ";", result); + } + + @Test + public void testConvertToJsonFlattenKeys(){ + String result = PropertyConverter.convertToJson(properties, true, true, true); + assertEquals(resultFlattenKeys, result); + } }