May 2016
Mo Tu We Th Fr Sa Su
<< >>


About haxe.xml.Proxy

Posted on Sep 13 2007

In Haxe 1.14 was introduced a new class called haxe.xml.Proxy.

At Motion-Twin, we are using it when we need to have some data structures stored in external XML files and that we want to reference in a program and still be sure that they exists.

Let's take a practical example. When some action occurs on our website, we can display either a confirm or an error text message. In order to ease translation of our website, we don't put any text in the source code itself, so all these messages are stored in an XML file and referenced by their 'id'.

   <t id="works">Great ! It works !</t>

Before haxe.xml.Proxy, we were then building an Hash'table of these texts and simply having in our code :

class Texts {
   public static var all = new Hash<String>();
   // on initialization, fill 'all' with the XML content

It works well of course, but we found several times that we mistake the spelling of the identifier, or that we duplicate messages that should be reused, and that we never remember which identifier to use to display a particular message (so having to look at the XML file everytime).

So I came up with the idea of haxe.xml.Proxy. Basically, you can declare a Proxy this way :

class AllTexts extends haxe.xml.Proxy<"texts.xml",String> {

The first type argument in the name of the XML file (must be in the classpath, you can add one -cp if it's in specific path) and the second one is the data structure content, String in our case, but this can be used with more complex data structures.

The AllTexts can then be instantiated with a method that will take an identifier and return a String. Our hashtable 'get' methods does just that. This way, our Texts class becomes :

class Texts {
    public static var all = new Hash<String>();
    public static var list = new AllTexts(all.get);

And all texts can then be replaced by using dot access :

This will do in practice exactly the same has before since at runtime it will call the hashtable get method, but in will ensure at compile time that there is an node in the XML with the identifier 'works'.

The best is that that completion works very well so as soon as you write Texts.list. you will get the list of all available identifiers.

Since then, we've been using this technique for many different kind of resources.

0 comment
Name : Email : Website : Message :