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

New Pretty printing feature #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion ant.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
46 changes: 46 additions & 0 deletions src/net/nczonline/web/props2js/LinkedProperties.java
Original file line number Diff line number Diff line change
@@ -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<Object> keys = new LinkedHashSet<Object>();

@Override
public Enumeration<?> propertyNames() {
return Collections.enumeration(keys);
}

@Override
public synchronized Enumeration<Object> elements() {
return Collections.enumeration(keys);
}

public Enumeration<Object> keys() {
return Collections.enumeration(keys);
}

public Set<Object> 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();
}
}
40 changes: 28 additions & 12 deletions src/net/nczonline/web/props2js/PropertyConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -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) + ");";
}

/**
Expand All @@ -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;
}
Expand Down
30 changes: 23 additions & 7 deletions src/net/nczonline/web/props2js/Props2Js.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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];

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 <name> The variable/callback name.\n"
+ " --to <format> The output format: json (default), jsonp, or js.\n"
+ " -o <file> Place the output into <file>. Defaults to stdout.");
}
}
}
81 changes: 76 additions & 5 deletions tests/net/nczonline/web/props2js/PropertyConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <b>Bold text</b>\",\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(){
Expand All @@ -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 <b>Bold text</b>");
propertiesHtml.setProperty("found", "true");
propertiesHtml.setProperty("price", "12.3");
propertiesHtml.setProperty("under.score", "under_score");
}

@After
Expand All @@ -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);
}
}