Truthiness about Cauliflower

Last night at dinner I learned from my kids that eating cauliflower brings bad luck. They also believe that the more you say it the truer it gets. From what I could gather they did not develop this theory on their own – some other children believe it as well. It seems that they already get truthiness

Later that night Stephen Colbert, in a very funny segment, again pointed out that on wikipedia if enough people agree with something then it becomes the truth (“wikiality”). This was in the context of reports of Microsoft paying bloggers to edit wikipedia. I incorrectly read a lot into this news. They made it sound like it was something that happened a lot and was an open invitation to anyone who would write positive things about Microsoft. Also I somehow got confused and thought it was about Vista. Perhaps my mind was being reactive because the programming on Comedy Central last night seemed like one big Microsoft Vista commercial. I was surprised to find that it was a specific person, Rick Jelliffe, who I know of because I’m a big fan of Schematron. After reading Rick’s post I don’t think its that big a deal. Although I wouldn’t want to see it become a common practice.

Stephen offered to pay $5 to anyone who replaced the wikipedia entry on Reality with “Reality has become a commodity”. If one were to believe that wikipedia is always true then reality was a commodity for about one minute. I’m OK with wikipedia being out of sync with reality for one minute now and then.

Actually the truth is subjective and reality is a commodity that corporate marketing and government propaganda are paying to influence every day. The advice about reality went from “don’t believe everything you read” to “don’t believe everything you see on TV” to “don’t believe everything you see on the Internet”. On the Internet there are so many facts and they spread so quickly that it is hard to keep up with your fact checking. I only bother for things that are important to me. On the plus side I have such a bad memory for facts that I will likely forget most of the misinformation I read.

Now I’m off to check my facts on cauliflower…

Blog design change comming soon

I’m planning a redesign of this blog soon. I got some feedback on the overall layout and style. A few people commented that the picture on the left was out of place given the technical content of the blog. I tend to agree. One reason for the picture and the effect of the header text sliding over it was simply to play around and get more practice with CSS. The other thing I got comments on was the color scheme. I plan go with something much more plain and simple. Hopefully it will be ready in a few days.

I really like how customizable the presentation of WordPress is.

I18n With StringTemplate

Developers accustomed to internationalizing Java [web] applications are very familiar with ResourceBundle, MessageFormat and property files. The articles Localization and Internationalization with StringTemplate and Web Application Internationalization and Localization in Action give a good introduction to how StringTemplate can be used to do i18n but they don’t explain how to get the full set of benefits we derive from ResourceBundle, and MessageFormat. In this article I describe how StringTemplate can leverage ResourceBundle and remove the need for MessageFormat in the presentation layer.

Continue reading

Treadmill Workstation Experiment

Ever since I read the NY times article “The Lean and the Restless” (about a year and a half ago now) the idea to replace my desk and chair with a treadmill workstation has been in the back of my mind. The idea seems to be catching on although not main stream yet. There were a number of reports about making a commercially available treadmill workstation. I haven’t seen them in stores yet and I’m not sure we will anytime soon. Some people are calling these things treadputers – I hope this name doesn’t catch on!

I wanted to pass on my experience with creating a treadmill workstation.

Continue reading

Using JSON with StringTemplate

I looked at a few of the Java libraries for JSON and finally choose org.json. It’s small and does just what I need it to, no more, no less. Well a little less. See, I decided to replace some of my existing value objects with JSON objects. (They were going to need to be serialized to/from JSON format anyway.) I am using StringTemplate (ST) to generate the HTML presentation so now my templates will need to get data from the JSON objects. The trouble is that the org.json implementation is not ST friendly.

ST knows how to get data from scalar types like String and Integer, and from object properties, Maps, and Collections. JSONObject wraps a HashMap but does not implement Map or extend HashMap so ST cannot get at the items it holds. I could have modified ST to treat JSONObject and JSONArray special but there is no reason for ST to know anything about these specific classes. The simple thing to do is have JSONObject implement Map and this is what I did. Most of the methods are a trivial call to the underlying HashMap method. A few methods must go through existing JSONObject methods to maintain the intended semantics. For example, get(Object key) calls opt(String key) because get should return null if the key is not found.

A similar thing was done for JSONArray. I made it implement Collection and Iterable. Since JSONArray has a put(Collection) method and JSONObject has a put(String, Map) method this caused infinite recursion because JSONArray is now a Collection and JSONObject is a Map. The solution was to add more specific methods put(JSONArray) and put(String, JSONObject) so that JSONObjects and JSONArrays can be added to each other.

There is one more problem to solve. Sometimes I need to access the contents of the JSON object structure and sometimes I need to serialize it as a JSON string (after all, that’s what JSON is for). JSONObject has a toString method which ST will generally use but not if the object is a Map. Once ST sees an object is a Map it will not use it any other way. That means you can’t call toString on it or access any other properties. This is a good thing in general but I sill need the JSON string. I could have the controller call toString and stuff the result into another ST attribute but that puts more burden on the controller and requires that the controller know if the presentation is going to need the JSON string. I decided to create a pseudo key called “JSONString”. In the get method I check if the key is equal to “JSONString” and if it is I return the output of toString. Normally this would be a bad idea but in this case I think it is the best option. Note: ST already pollutes the name space of keys. In ST $mymap.keys$ returns a collection of all the key names and not the map item with key “keys”. It would be nice if $mymap.(“keys”)$ gave you the map item but it doesn’t. I think of both JSONString and keys as a hack but I don’t have a better way right now. ST has no problem with additional properties on Collections so I was able to add a getJSONString method to JSONArray.

Examples of use

this code

JSONObject jo = new JSONObject();
jo.put("long", 4);
jo.put("string", "some string");
JSONArray ja = new JSONArray();
ja.put("one");
ja.put("two");
ja.put("three");
jo.put("array", ja);

StringTemplate st = new StringTemplate(
"l=$jo.long$ s=$jo.string$n" +
"keys: $jo.keys : {[$it$] }$n" +
"array: $jo.array;separator=\", \"$n" +
"json=$jo.JSONString$n" +
"json array=$jo.array.JSONString$n");
st.setAttribute("jo", jo);
String text = st.toString();

produces this output:

l=4 s=some string
keys: [string] [array] [long]
array: one, two, three
json={"string":"some string","array":["one","two","three"],"long":4}
json array=["one","two","three"]

What I did with JSON is an example of a general principle of using ST: Wrap (or change) your existing object structures in a way that ST already deals with rather than change ST to understand your data.

I can make my changes to org.json available if anyone is interested.