Recently by John Cato

Like Javascript, semicolons are optional in Groovy except for when they aren't optional. These examples are both pretty contrived, though I found both because they're actually something that I've written, and could both be written better. That's not really the point I'm making though. When something doesn't compile when it looks like it clearly should sometimes it's hard to track down why, and it's surprising to learn that it's because you need a semicolon.

Example the first: Generics at the end of a line:

def list = [1,2,3] as List<Integer>
println list

If you try to compile this in Groovy it will give you the error message: 'unexpected token: println', however this:

def list = [1,2,3] as List<Integer>;
println list
Gives the expected output.


Example the second: Ambiguous Closures

{-> assert GroovyClosureTest == owner.getClass() }()
{-> assert GroovyClosureTest == delegate.getClass() }()

I don't think you'd really ever need to do something like this, but a closure can be defined and called on a single line. Because of Groovy's special closure parameter syntax (e.g. list.each() {} being synonomous with list.each({})) the compiler thinks I'm passing the second closure into the first as an argument. Again a semicolon is needed to seperate the two lines:

{-> assert GroovyClosureTest == owner.getClass() }();
{-> assert GroovyClosureTest == delegate.getClass() }()

I've been interested in data visualization for a long time now, probably because I have backgrounds in both programming and art. After a quick look at the kind of output that the language Processing can create, I think that it's got to go on my lengthening to do list.

I hate blogs that are just links to other blogs, but here's an article about character encodings that everyone should take a look at if you don't have a good understanding of how they work.

http://www.joelonsoftware.com/articles/Unicode.html

XIncludes using Java

| | Comments (0) | TrackBacks (0)
OK, this isn't a lot of code, but seriously, it took two days to write. The documentation on how to do an XSL style XInclude in Java is pretty much non existent. I've added a maven plugin that wraps this to our maven plugins project as well to replace Ant targets that use <xcluder> tags.

public class XIncluder implements EntityResolver
{
    private File inFile;
    private File outFile;

    public XIncluder(File in, File out)
    {
        this.inFile = in;
        this.outFile = out;
    }
	
    public void processFiles() throws
        IOException, 
        ParserConfigurationException, 
        TransformerException, 
        SAXException
    {
        if (outFile.exists())
        {
            outFile.delete();
        }
        outFile.createNewFile();

        SAXParserFactory saxFactory = SAXParserFactory.newInstance();
        saxFactory.setNamespaceAware(true);
        saxFactory.setXIncludeAware(true);

        XMLReader reader = saxFactory.newSAXParser().getXMLReader();
        reader.setEntityResolver(this);

        SAXSource source = new SAXSource(reader,new InputSource(
                new FileInputStream(inFile)));

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(source, new StreamResult(outFile));
    }

    public InputSource resolveEntity(String publicId, String systemId)
    {
        try
        {
            return new InputSource(new FileInputStream(new File(systemId)));
        }
        catch (FileNotFoundException e)
        {
            return null;
        }
    }
	
}