Oops Null Pointer

Java programming related

Monthly Archives: November 2012

Quick guide to transforming XML with XSLT

As a reminder to myself for a quick pattern for transforming XML via XSLT:

Some setup:


public static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
public static final String XSL_HEADER = "<xsl:stylesheet version=\"1.0\" " +
    "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">";
public static final String ID_XFORM = "<xsl:template match=\"@*|node()\"><xsl:copy>" +
    "<xsl:apply-templates select=\"@*|node()\"/></xsl:copy></xsl:template>";

public static final String TRANS_ATTR = "<xsl:template match=\"ATTR_PATH@ATTR_NAME\">" +
    "<xsl:attribute name=\"ATTR_NAME\">ATTR_VALUE</xsl:attribute></xsl:template>";
public static final String XSL_END = "</xsl:stylesheet>";
public static final String XFORM_ATTR_XSL = XML_HEADER + XSL_HEADER + ID_XFORM + TRANS_ATTR + XSL_END;

And a usage sample transforming an attribute’s value:

String xsl = XFORM_ATTR_XSL.replace("ATTR_PATH", "");
xsl = xsl.replace("ATTR_NAME", "myAttribute");
xsl = xsl.replace("ATTR_VALUE", "newValue");

ByteArrayInputStream xslInputStream = new ByteArrayInputStream(xsl.getBytes("UTF-8"));
FileInputStream xml = new FileInputStream(new File("myXmlFile");
InputStream transformedXml = null;

try {
    transformedXml = transformXml(xml, xslInputStream);
}
finally {
    if (xml != null) xml.close();
}

and the transform method:

private InputStream transformXml(InputStream xml, InputStream xsl) throws TransformerConfigurationException, TransformerException {

    TransformerFactory factory = new org.apache.xalan.processor.TransformerFactoryImpl();
    Transformer transformer = factory.newTransformer(new StreamSource(xsl));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transformer.transform(new StreamSource(xml), new StreamResult(baos));
    return new ByteArrayInputStream(baos.toByteArray());
}

ExtGWT Cookbook Review in progress

I’m in the process of reviewing a GXT book called ExtGWT Rich Internet Application Cookbook.

Hopefully I’ll pick up a few tips on the way. It will be interesting to compare it to a book a did a technical review for around a year ago: Ext GWT 2.0 Beginners Guide.

XStream 1.3.1 to 1.4.3 ReflectionConverter changes

Recently while upgrading to Java 7 I had to upgrade XStream due to (I think) Oracle changing the name of the JVM or reflection providers. My converters that subclass the ReflectionConverter class started to fail. I had been omitting fields and then manually marshalling them in by overriding the marshall and marshallFields methods.

The 1.3.1 version of the RelectionConverter (in AbstractConverter) passed all non transient fields to the marshallField method. But in 1.4.3 it now checks if the fields should be omitted and will not pass omitted fields through to the marshallField method.

While mine was a relatively unusual case I hope this post can help others out stuck on similar issues.