Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Monday, 12 April 2010

Java XPath

I was pleasantly surprised when I needed to do some XPath in Java recently - it's been a while since I did any and I was expecting some potentially tedious 3rd party integration. Turns out it's now all included in the standard JDK and extracting the data we need from an online resource that looks like this ...








...

Was simply a case of doing this ...
// get connection to items service
URL itemsUrl = new URL(ITEMS_URI);
URLConnection itemsConnection = itemsUrl.openConnection();

// use xpath to extract the item ID's
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(itemsConnection.getInputStream());
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression xpathExpression = xpath.compile("/guides/r/a[@n=\"ItemId\"]/@v");

// create the list of item ID's
List<Long> itemIds = new ArrayList<Long>();
Object result = xpathExpression.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
String itemId = nodes.item(i).getNodeValue();
try {
itemIds.add(Long.parseLong(itemId));
} catch (NumberFormatException e) {
System.out.println("Could not parse item ID: " + itemId);
}
}

return itemIds;
I'm probably being more specific than I need to be there, but I'm not 100% sure what the finished format will look like.

Tuesday, 25 August 2009

Paying The Bills

A distinct lack of activity on the blogging front lately. This is because I've been (a) moving house and (b) actually enjoying myself at work! ;-)

As Tech Lead on a brand new project, I've been writing a web app using Spring MVC, Freemarker + Hibernate and I thought I would share a few thoughts. I should start by saying that it's been nowhere near as easy as Grails, GSP + GORM, but nonetheless, it's a big improvement on the 'bad old days' of Java web app development.

To be honest, although Spring MVC is both powerful and flexible, I've always found it a little fiddly in the past. However, this time I decided to use annotation-driven controllers and these were way easier to work with than the MultiActionController I've used before. All you need is something like this in your context:
<context:component-scan package="com.royale.sam.web.controller">
And then you can annotate your controller class like this:
@Controller
@RequestMapping(method = RequestMethod.GET)
public class PageController {

...

@RequestMapping(value={"/page.html", "/item.html"})
public ModelAndView pageHandler(HttpServletRequest request, HttpServletResponse response) {
...
}
}
And that's it! Using this technique you can group handler methods in one controller (much like you can in Grails) which makes it easier to reuse domain logic and leads to a well organised application. We found this was particularly useful when implementing handlers for our AJAX functionality.

Freemarker was also pretty good, although - possibly because it's application agnostic - we found that it didn't support a number of features that you take for granted when using JSP. (In fact, I would've used JSP, but requirements dictated that we used a templating language.) For example, we couldn't find an easy + reliable way to get the context path, so we ended up introducing a servlet filter that made this information (and more) available to every page in our app. We also steered clear of using taglibs in our Freemarker templates, preferring to write our own directives.

And Hibernate was Hibernate. Always trickier than you expect to set it up, but once it's done, you generally don't have to worry about it anymore.

Wednesday, 8 April 2009

Groovy comes to Google App Engine

It would seem that my reservations about Google App Engine may soon be no more. I haven't had chance to try it yet, but Google App Engine now supports Java!

Better still (for fans of Groovy like myself) work has been going on behind the scenes to ensure that Groovy is also supported - see here and here. In fact, Guillaume Laforge (creator/inventor of Groovy) has already written a Google Maps / Flickr mashup with it.

:-D

Monday, 2 February 2009

Level Editors

As we all know, every good game needs a level editor and mine is no exception. Here's what it looks like:


I knocked this up in Java / SWT in a few weeks and it already covers all the basic use cases. In fact, drawing the tiles was more time-consuming.

I was quite impressed by SWT. It's about a million times easier/quicker than Swing and I like the way it just looks like a native app. I did give the Eclipse Rich Client Platform a try to start with but that seemed to be one of the most impenetrably hard things I've ever looked at so I settled on SWT in the end. I also toyed with the idea of doing it in Python, but the sheer range of Windows Toolkits that are available for it put me off slightly.

Monday, 16 June 2008

Ubuntu + Juniper

I've now successfully set up my development environment on Ubuntu so I can work from home without needing my work laptop. I had a few issues with installing Juniper, but these have now been resolved ...

First issue: Firefox wouldn't install the Java Plugin. Solution: apt-get the plugin and then put a symlink in the Firefox plugins folder ...

> sudo apt-get install sun-java5-plugin sun-java5-fonts

> sudo ln -s /usr/lib/jvm/java-1.5.0-sun/jre
/plugin/i386/ns7/libjavaplugin_oji.so /usr/lib/firefox/plugins

Second issue: the juniper install needs to run as root and will prompt you for the root password. Solution: set a root password ...

> sudo -i
> passwd

To lock the root account again after you've finished ...

> sudo passwd -l root

And should you need to unlock (ie. set the password back to what it was before) ...

> sudo passwd -u root

Third issue: the juniper scripts don't work properly so it attempts to install them every time (note that this doesn't seem to prevent you from logging in!) Solution: You need to overwrite the default scripts with some provided in this rather excellent post on Ubuntu Forums.

Fourth issue: the lastest Subversive plugin for Eclipse doesn't seem to work on Ubuntu. Solution: use the old one:

> http://www.polarion.org/projects/subversive/download/1.1/update-site/

Fifth issue: Eclipse crashing randomly when it attempts to synchronise with SVN. Solution: this was either because I was running Eclipse under Java6 or because I had Java5 and Java6 both installed - it went away when I rolled back to Java5 only.

That should do it!