Tag Archives: Java

Java: Simple XSLT transformation without external libraries

Here’s how to do a simple XSLT transformation using only classes in vanilla Java 1.5 (maybe even 1.4?), no external libraries or anything. The classes are found in the javax.xml.transform package.

// Create a factory
TransformerFactory tf = TransformerFactory.newInstance();
if (!tf.getFeature(SAXTransformerFactory.FEATURE))
    throw new RuntimeException("Did not find a SAX-compatible TransformerFactory.");
SAXTransformerFactory stf = (SAXTransformerFactory) tf;

// Create a reusable template for the XSLT
Templates xslt = stf.newTemplates(new SourceStream(inputStreamWithXslt));

// Use the template to transform some XML
        new StreamSource(inputStreamWithXml),
        new StreamResult(System.out));

Source: XML and XSLT Tips and Tricks for Java

Java: Readable password generator

Needed something to generate random passwords and found Java Password Generator. It generates passwords which are gibberish, but still sort of readable, which I think is a lot nicer than pure gibberish.

I just wanted a simple ‘get me a new random password method’ though, and not all the applet and ‘make several passwords’ stuff. Decided to extract the useful parts and clean it up a bit. In case I need it again, or others need it, and since he states “share your source with others freely”, here is the result 🙂

Continue reading Java: Readable password generator

Java: How to deal with the BOM in a Unicode InputStream

Ok, so I was happily reading CSV files from an SFTP server. The file content is returned as an InputStream and I I used a BufferedReader to read it line by line. Each line contained either a header or an order. The header lines started with the string “HDR”.

However, I suddenly discovered that my code was consistently skipping the first header (and as a result the orders belonging to it). The reason, I found, was simple. The first header, on the first line, didn’t start with “HDR”, it started with “â–¡HDR”! And that undisplayable square turned out to be a Unicode Byte Order Mark (BOM).

Continue reading Java: How to deal with the BOM in a Unicode InputStream