Pages

Twitter Updates

Sunday, August 09, 2009

xobj hosted on bitbucket

This post should have be written a few months ago. xobj has been made available as open source on bitbucket

The xobj project provides an object reflector between various dynamic languages and XML. Currently, python and ActionScript 3 implementations are available.

Motivations

xobj was motivated by a few specific goals:

  • Make the use of XML for object model interchange between client and server tiers less painful
  • Allow your code to use real typed instances of classes rather than simple generic data structures
  • Treat your code as the definition of schema rather than forcing an XMLSchema driven approach on your code.
  • Read and write XML documents that will pass most XMLSchema structural validation requirements with relatively little effort on your part
It is not required to provide an XML schema to use xobj. Custom objects may be used for parts or all of the document, whether or not an XML schema is provided. Schema validation is optional. If new elements show up, they will be preserved across read/write; schema additions will not normally require code changes.

Implementation Notes

During XML decoding, xobj preserves namespaces, element ordering and tracks attributes vs elements so that re-encoded object graphs can comply with most of the common XMLSchema validation requirements. This observed metadata is attached to your object graph rather than hidden out of view.

For "whole cloth" generation of XML from object graphs that were not themselves decoded in the first place, you can provide the namespace, elements ordering and attribute metadata declaratively to facilitate XMLSchema conformance.

However, most casual-use applications don't need to worry about namespaces, so your instance graphs will encode just fine as a usable XML document leveraging as much runtime class introspective data as is available. (AS3 in particular)

During decoding, xobj uses both the runtime type information available from your classes as well as a typemap you specify to determine the target type for every decoded XML element.

During encoding, xobj uses this same runtime information plus additional observed (or provided) metadata to inform the output XML.

For ActionScript, xobj decodes XML documents directly into typed instance objects rather than generic Object or ObjectProxy wrappers. This avoids the double-handling of generic objects required by the default SimpleXMLDecoder provided by Adobe Flex.

We've made a lot of use of this library in recent Flex/Python RESTful services at rPath.

Wednesday, August 05, 2009

Loading local file content in Flashplayer 10 with Flex

Recently, I was asked to improve a text entry UI for submitting certificates in support of Amazon EC2 credentials. We'd received feedback that copy/paste was proving problematic for certificate entry due to the finicky line-ending rules involved. The desired solution was to add a "Read from file..." button that would load the cert from a local file and paste that content into the UI. Note that we didn't want to upload the file itself, simply extract the content in the local client.

Flashplayer 9 won't let you do this. Flashplayer 10 will.

The solution revolves around the enhancements made to the FileReference class in Flash 10.

I'm posting this mainly because the examples I found on the net were either out of date (Flashplayer 10 changed in the final release) or were flat out wrong.

Firstly, make sure your dev environment is configured to use the playerglobals.swc from the Flash 10 player. Instructions for doing this can be found here

Second, crucial point, is that you must hold onto a reference to the FileReference instance across the whole browse, select, load and extract cycle. Failure to do this results in the instance being GC'd out from under you and subsequent "missing events".

This should be enough code to get you going:


// we need to keep this ref to prevent the fileRef getting GC'd
// cause the docs say that if it goes out of scope, you' won't get the
// completion events

private var fileRef:FileReference;

// call this from a button click or other control
public function handleReadFileContent(event:Event):void
{
fileRef = new FileReference();
fileRef.addEventListener( Event.SELECT, onFileSelect );
fileRef.addEventListener( Event.COMPLETE, onFileComplete );
fileRef.browse();
}

private function onFileSelect( event:Event ):void
{
fileRef.load();
}

private function onFileComplete( event:Event ):void
{
var content:ByteArray = fileRef.data as ByteArray;

myTextField.text = content;
}