The Office: US Edition Finally Makes It! Apples love of the mouse. Don’t forget the keyboard.
Feb 25

E4X: Is ECMAScript the glue language we have wanted? It powers the browser VM.

Java, JavaScript, Tech Add comments

JavaScript has a bit of a tough label. Many developers think of it as a hackers web scripting language which is good for alert("foo") and document.*.

The language is really growing up now though, and we have good implementations on the Java side such as Rhino.

Rhino even implements the latest and greatest of ECMAScript: ECMAScript for XML (E4X).

Now, XML is a first class citizen in the language which allows you to do some of the following:

Create a DOM from XML

var order = <order>
<customer>
<firstname>John</firstname>
<lastname>Doe</lastname>
</customer>
<item>
<description>Big Screen Television</description>
<price>1299.99</price>
<quantity>1</quantity>
</item>
</order>

Walk the XML tree a la XPath etc

// Construct the full customer name
var name = order.customer.firstname + " " + order.customer.lastname;

// Calculate the total price
var total = order.item.price * order.item.quantity;

Construct a new XML object using expando and super-expando properties

var order = <order/>;
order.customer.name = "Fred Jones";
order.customer.address.street = "123 Long Lang";
order.customer.address.city = "Underwood";
order.customer.address.state = "CA";
order.item[0] = "";
order.item[0].description = "Small Rodents";
order.item[0].quantity = 10;
order.item[0].price = 6.95;

Playing with SOAP

var message = <soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<soap:Body>
<m:GetLastTradePrice xmlns:m="http://mycompany.com/stocks">
<symbol>DIS</symbol>
</m:GetLastTradePrice>
</soap:Body>
</soap:Envelope>

// declare the SOAP and stocks namespaces
var soap = new Namespace("http://schemas.xmlsoap.org/soap/envelope/");
var stock = new Namespace ("http://mycompany.com/stocks");

// extract the soap encoding style and body from the soap message
var encodingStyle = message.@soap::encodingStyle;

print("The encoding style of the soap message is specified by:\n" + encodingStyle);

// change the stock symbol
message.soap::Body.stock::GetLastTradePrice.symbol = "MYCO";

var body = message.soap::Body;

Conclusion

It is interesting to see ECMAScript leading the way in some areas. It is an interesting idea to have XML as such as first class citizen. I don’t remember having “tab delimited data” at the same level, and it is a little worrying to think about XML abuse that could occur because of it.

However, maybe it is time to take ECMAScript more seriously. It is installed in all browser VMs so to speak, and with implementations like Rhino, allows you to script Java in a simple way :)

9 Responses to “E4X: Is ECMAScript the glue language we have wanted? It powers the browser VM.”

  1. Don Brown Says:

    Along that vein, Javascript also supports closures, Groovy-like JVM extensions (in Rhino at least), continuations (again, only in Rhino) and I just discovered, annotations. I think the Cocoon folks were on to something using Javascript on the server. Imagine a framework where your client-side javascript could call via XMLHTTPRequest your server side javascript passing JSON objects, using annotations to ensure only certain functions were accessible….

    JVM extensions – http://www.jroller.com/page/mrdon/20050216#making_rhino_javascript_more_groovy
    Annotations – http://www.jroller.com/page/mrdon/20050225#annotations_for_javascript

  2. Don Brown Says:

    Along that vein, Javascript also supports closures, Groovy-like JVM extensions (in Rhino at least), continuations (again, only in Rhino) and I just discovered, annotations. I think the Cocoon folks were on to something using Javascript on the server. Imagine a framework where your client-side javascript could call via XMLHTTPRequest your server side javascript passing JSON objects, using annotations to ensure only certain functions were accessible….

    JVM extensions – http://www.jroller.com/page/mrdon/20050216#making_rhino_javascript_more_groovy
    Annotations – http://www.jroller.com/page/mrdon/20050225#annotations_for_javascript

  3. Marcus Andersson Says:

    Actually, Javascript is a really cool language and you only need to go to the versions supported be the recent browsers to see it.

    You can almost do “real” OO (you only miss super but you can get around it using apply(), all other things you 1) don’t need or 2) you can easily simulate/implement yourself, both due to the extremely dynamic nature of js) and, as Don said, you do have full closure (functional as I prefer) support. Javascript is more Lisp than Java (and I like that).

    I started to learn Ruby the other day and Ruby has more power than javascript but I think that Javascript isn’t far behind (and I’m not including ECMAScript 2.0 or Rhino when I say that!)

    I would really love to have ECMAScript 2.0 with E4X in the browser. Then you could do more magic than you already can!

  4. Marcus Andersson Says:

    Actually, Javascript is a really cool language and you only need to go to the versions supported be the recent browsers to see it.

    You can almost do “real” OO (you only miss super but you can get around it using apply(), all other things you 1) don’t need or 2) you can easily simulate/implement yourself, both due to the extremely dynamic nature of js) and, as Don said, you do have full closure (functional as I prefer) support. Javascript is more Lisp than Java (and I like that).

    I started to learn Ruby the other day and Ruby has more power than javascript but I think that Javascript isn’t far behind (and I’m not including ECMAScript 2.0 or Rhino when I say that!)

    I would really love to have ECMAScript 2.0 with E4X in the browser. Then you could do more magic than you already can!

  5. anonymous Says:

    I just had a look at http://www.seppia.org.
    It is a java open source which uses rhino (javascript) to glue together the functionality embedded in the jars.
    E4X is available by default.

  6. anonymous Says:

    I just had a look at http://www.seppia.org.
    It is a java open source which uses rhino (javascript) to glue together the functionality embedded in the jars.
    E4X is available by default.

  7. alpha Says:

    got to play with seppia … rhino + e4x + java +
    jar class loader…

    real good.
    not sure if i need to pass objects as json
    when E4X is so good to create/set/get xml…

  8. alpha Says:

    got to play with seppia … rhino + e4x + java +
    jar class loader…

    real good.
    not sure if i need to pass objects as json
    when E4X is so good to create/set/get xml…

  9. Yaroukh Says:

    > I would really love to have ECMAScript 2.0 with E4X in the browser. Then you
    > could do more magic than you already can!

    U mean something u can’t do using XPath?

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: Type in the word 'cricket'