Archive for July, 2005

Java Document to String

This is a very simple utility function to produce an indented XML document as a String given a Document. It throws a TransformerException if anything drastic goes wrong. [java] import java.io.*; import org.w3c.dom.Document; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; … public static String documentToString( Document document ) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(document); StringWriter sw = new StringWriter(); StreamResult result = new Read More...

Velocity if statement is type sensitive…

Problem I discovered that comparisons in Velocity if statements are type-sensitive the hard way. For example, I am trying to display the ‘selected’ option in a html <SELECT> object. The list of possible values is $list, each is a simple ‘NameValuePair’ value object { String name, String value }. The object I am editing in this form is $obj, and the field I am testing against is $obj.typeId defined as Read More...

Time Synchronisation With Windows 2000 or XP

If you’re running Windows 2000 or XP at home, it’s very unlikley that you have a ‘Primary Domain Controller’ to give you the time of day. ;) Windows 2000 & XP has a built in “Windows Time” service that is also compatible with public SNTP Time Servers, all you need to do is configure it. I chose a local SNTP server from the list: http://support.microsoft.com/kb/262680 [code] Microsoft Windows 2000 [Version Read More...

Fedora Core 4 and SELinux paranoia

The default configuration of Fedora Core 4 with SELinux enabled causes problems when you want to use the UserDir feature in Apache Httpd. Problem: SELinux is stopping the httpd processes from accessing your home directory. [code] Forbidden You don't have permission to access /~foo/ on this server. Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request. Apache/2.0.54 (Fedora) Server at localhost Port Read More...

Velocity Templates and Newlines

If you’re reading this article you’ve probably encounted the same problem as I have. I was trying to put a newline ‘\n’ character in the value of a variable in a Velocity template. Attempt 1: I assumed \n would work… [code] #if ( !$foo ) #set ( $foo = "There was no foo set in the Context.\nPlease do something about it." ) #end [/code] This produced There was no foo Read More...

Java Object Digester

I was working on a web-based application recently and wanted a lazy way to get data from a simple ‘value object’ out to the front end without having any hard-coded field names; I came up with this. Basically, this method will take any Object, iterate through its methods looking for any of the format “get<something>” then place the returned value in a new child element of the document. Obviously the Read More...

Unicode? Character Sets? UTF-what?

I was inspired to add this note after recent frustrations about the complete ignorance of character sets in both Commercial & Open software… I seriously recommend everyone reads the following, less ISO-blahblahblah and more UTF-8, no excuses! The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) http://www.joelonsoftware.com/articles/Unicode.html