Archive for the 'actionscript' Category

making mx.rpc.xml.XMLDecoder play nice with xsi:type

Sunday, August 23rd, 2009

In previous posts I have outlined how to use the inbuilt (and well hidden) XML Schema classes that are available in the mx.rpc.xml package in Flex.  Utilising SchemaManager and SchemaTypeRegistry one can quickly register specific ActionScript classes with specific types defined within an xml schema (XSD). Once set up it is simply a matter of using XMLDecoder and XMLEncoder to automatically serialise between XML and ActionScript and voila! the work is done.

That is unless you happen to have elements with your xml file that use an xsi:type attribute to indicate that their contents is actually that of a derivative element (such as an extension, restriction, etc). In these cases Flex just ignores the xsi:type and decodes against the original element type – which aint much help. What’s suprisising is that the Flex classes have been built with xsi:type in mind (storing the type in custom classes implementing IXMLSchemaInstance), but just seems to have been missed when implementing XMLDecoder – always saves the element type in the xsitype property of IXMLSchemaInstance

Whether you love it, hate it or are just indifferent,  xsi:type is part of the XSD standard and all it takes is a few tweaks for Flex to handle it correctly.

I’ve included a CustomXMLDecoder class below, but firstly:

A quick primer on xsi:type

If you are unfamiliar with xsi:type here is  a quick example of it in action:

In an XSD file we have a complex type called user:

<xs:complexType name="User">
 <xs:sequence>
 <xs:element name="username" type="xs:string" />
 <xs:element name="password" type="xs:string" />
 </xs:sequence>
 </xs:complexType> 

And we have another complex type that is an extension of User called InternalUser :

<xs:complexType name="InternalUser">
 <xs:complexContent>
 <xs:extension base="User" >
 <xs:sequence>
 <xs:element name="staffId" type="xs:string" />
 </xs:sequence>
 </xs:extension>
 </xs:complexContent>
 </xs:complexType>

In an XML file implementing this schema we have a set of User elements. As you can see below one of which includes contains an xsi:type attribute that indicates that the contents is actually that of an InternalUser.

<user >
 <username>joeCitizen</username>
 <password>1234</password>
 </user>
 <user xsi:type="InternalUser" >
 <username>jamesBond</username>
 <password>5678</password>
 <staffId>007</staffId>
 </user>

CustomXMLDecoder

After hours of trawling through  mx.rpc.xml.XMLDecoder, it was surprisingly straight forward to fix the shortcoming by overriding a couple of methods within XMLDecoder.

As an added tweak i’ve included a property to the class that determines the behaviour when attemptying to decode an xsi:type that hasn’t been registered with the SchemaTypeRegistry.

ignoreXSITypeIfNotRegistered:Boolean = false;

If this property is set to true then the decoder will ignore an xsi:type that is not registered against an ActionScript class, and try to decode it against the original element type instead (esentially replicating the functionality of XMLDecoder). This ensures that unexpected xsi:type extensions to a element  are decoded as the element type rather than generic object proxies.

If the xsi:type is registered then it will always use it.

Here is the class:

package
{
import mx.rpc.xml.*;
public class CustomXMLDecoder extends XMLDecoder
 {

 public function CustomXMLDecoder()
 {
 super();
 }

 /**
 * Set this flag if you want unregistered xsi types to be ignored (and objects to be decoded against the
 * base element type instread).
 * If the xsi type is registered then it will always be used over the base element type
 */
 public var ignoreXSITypeIfNotRegistered:Boolean = false;

 /**
 * Decodes a element based on the xsi:type. If the type isn't registered then it
 * checks the ignoreXSITypeIfNotRegistered property of the class.
 *
 * If true - decodes against the element type (and ignores the xsi:type)
 * If false - uses object proxy.
 */
 public override function decodeElementTopLevel(definition:XML, elementQName:QName, value:*):*
 {
 var content:*;

 if(value is XML)
 {
 var xsiType:QName = getXSIType(value);
 var isXSITypeRegistered:Boolean = typeRegistry.getClass(xsiType) != null

 if(xsiType != null && (isXSITypeRegistered || ignoreXSITypeIfNotRegistered == false))
 {
 content = createContent(xsiType);
 decodeType(xsiType, content, elementQName, value);
 return content;
 }
 }
 return super.decodeElementTopLevel(definition, elementQName, value);
 }

 /**
 * Decodes agains the xsi:type (if available) rather than the defined element type
 */
 public override function decode(xml:*, name:QName=null, type:QName=null, definition:XML=null):*
 {
 if(xml is XML)
 {
 var xsiType:QName = getXSIType(xml);

 if(xsiType != null)
 type = xsiType;
 }
 return super.decode(xml, name, type, definition);
 }
}

Hope that helps other people out there  – as this was previously the only real show stopper for using the Schema classes in some of our projects.

———

One last thing,

Can anyone point out to how override the decoding of nested arrays of elements?

Ususally XML will be structured so that an array of items is wrapped within a parent element

e.g the child ‘item’ elements are located within an <items/> element

<item>
 <name>Parent</name>
 <items>
   <item >
     <name>Child A</name>
   </item>
   <item >
     <name>Child B</name>
   </item>
 </items>
</item>

In Flex this data may be registered to a  class such as this one:

package
{
 [Bindable]
 public class ItemVO
{
 public var name:String;
 public var items:ArrayCollection;

 public function ItemVO():void
 {
 }
}
}

Unfortunately the decoder always includes the extra level when decoding – resulting in an ArrayCollection containing a single item (an ArrayCollection) that contains the array of items.

Does anyone know of a way to map this relationship better so that the items.item in the xml maps directly to the items ArrayCollection in the Class?

Ta,

Dom

Adobe reopens discussion around the Flex 4 namespaces and the ‘Fx’ prefix on components

Tuesday, February 10th, 2009

[UPDATE] A decision has been made and the Fx prefix is no more – http://www.adobeforums.com/webx/.59b7e849

——-

I don’t like reposting Flex related news; however I’m please to see that Adobe have decided to revisit their original naming strategy for the new Flex 4 components.

There is a good discussion happening on their forums on Flex 3/4 component compatibility – the distinction between flex 3 and flex 4 components, namespace/naming conventions, and IDE/documentation/CSS implications.

It is a response to the community’s criticism of the whole “Fx” thing – naming the new Flex 4 components with a class prefix to distinguish them – such as “FxButton, “FxList”, etc.

Most developers seem to agree that separate namespaces for the halo and spark (gumbo) components would be most consistent and future proof and doesn’t further complicate usage considering that most developers already utilise additional namespaces for custom components and 3rd party libraries.

The general consensus seems to support three namespaces – the default for language specific elements – e.g. <Style>, <Script>), and two separate namespaces for the Flex 3 and Flex 4 components ( e.g. – <mx:Button> and <fx:Button>)

I have to agree that this solution is preferable over the original “Fx” class name prefix strategy. A key concern/priority for Adobe seems to be minimizing the complexity of the initial learning curve for new Flex developers (part of their reasoning behind choosing class prefixes over additional namespaces).

I agree with this concern, but believe that due to the overlapping functionality within the two component sets, any proposed solution is going to increase this complexity but at least the latter provides a consistent usage of xml namespace that existing flex developers will understand immediately, and new developers will appreciate in time.
Adobe is due to finalise their decision soon, and the discussion is ongoing.

Forum discussion here:
http://www.adobeforums.com/webx?128@@.59b7cdf0

Original Post here:
http://davidzuckerman.com/adobe/2009/02/10/help-us-decide-where-to-go-with-flex-4/

Flex 3, XML Schemas & automatic mapping of AS classes to XSD element definitions (Part 2)

Sunday, November 30th, 2008

This is a set of posts documenting my experiences with utilising functionality within the Flex SDK that provides the automatic mapping of custom ActionScript classes to element definitions within an XML Schema (XSD).

1.  Part 1 – Introduction and background
2.  Part 2 – Overview of Flex’s XML Schema classes (inc example code)

Part 2 – Overview of Flex’s XML Schema classes (including example code)

This post covers how to take advantage of these classes (including example code) – as at the time of writing there doesn’t seem to be any discussion of these features by Adobe or the community at large. I’ll also list a few caveats we have come across in using these classes.

Read the rest of this entry »

Flex 3, XML Schemas & automatic mapping of AS classes to XSD element definitions (Part 1)

Sunday, November 30th, 2008

This is a set of posts documenting my experiences with utilising functionality within the Flex SDK that provides the automatic mapping of custom ActionScript classes to element definitions within an XML Schema (XSD).

1.  Part 1 – Introduction and background
2.  Part 2 – Overview of Flex’s XML Schema classes (inc example code)

Part 1 – Introduction and background

This post explains some reasons on why the automated mapping of ActionScript classes to XML Schema definitions can be a better alternative to other data integration options available to Flex (in certain circumstances).

Read the rest of this entry »

Flex: ObjectUtil.copy() throws errors for classes with custom namespace properties.

Tuesday, February 26th, 2008

The easiest way to clone an instance of a class in Flex is to use the inbuilt ObjectUtil.copy() . This function serialises the entire object into an array of bytes and then deserialises it back into a completely new instance of that class. All that needs to be added to a class to ensure that it is correctly deserialised is to include the [RemoteClass] metadata tag above the class definition.

Rather than explaining it all here, I suggest you have a read of Darron Schall’s explanation of it here.

I’ve used this method for cloning custom objects in several projects without issue, but when I implemented this functionality in a current project, everytime I called ObjectUtil.copy() I received the following error:

ArgumentError: Error #2004: One of the parameters is invalid.
    at flash.utils::ByteArray/writeObject()

There is only one parameter for ObjectUtil.copy() – the object to be copied, so obviously there was something in one of my classes that didn’t like being deserialised. Unfortunately by this time there was already a reasonable inheritance chain going in my classes, and eventually I had to start stripping bits out of my classes (and superclasses) until the error went away.

It turns out that the culprit was a custom namespace property in my superclass. Whenever the namespace was defined, ByteArray.writeObject() fails to deserialise the byte array, and the copy fails.

package
{
    [RemoteClass]
    public class Example
    {
        public var ns:Namespace = new Namespace("http://www.misprintt.net");

        public function Example()
        {

        }
    }
}

Cloning an instance of this class will always throw an error:

var example:Example();= new Example();
var o:Object = ObjectUtil.copy(example);

Luckily the solution is also describe in Darron’s post. While he doesn’t indentify what problems he was having, he explains how he used the [Transient] metadata tag to avoid issues with ObjectUtil.copy().

So now that the culprit has been identified, it is just a matter of adding the [Transient] metadata tag to avoid the namespace being serialised in the first place.

package
{
    [RemoteClass]
    public class Example
    {
	[Transient]
        public var ns:Namespace = new Namespace("http://www.misprintt.net");

        public function Example()
        {

        }
    }
}

Anyway, hopefully this saves some time for those trying to decipher yet another one of those vague flash player errors :)

Nasty FDT bug – renaming a project with “refactoring” selected

Sunday, April 29th, 2007

The FDT plugin for Eclipse is a great piece of software (way way better than other flash plugins for Eclipse such as ASDT and FlexBuilder). I use it for all my AS2 flash development. However it has always had some issues with the location of the intrinsic core ActionScript classes in Flash 8 because they reside in a FP7 or FP8 directory (see forum post here and solution here).

While this solution solves the problem of FDT not recognising intrinsic classes such as Object and String, It doesn’t resolve a very nasty potential bug.

DON’T rename a project if you have selected “Refactoring” – “Flash Explorer File Operations” in the FDT preferences.

FDT preferences

What happens is it refactors all the intrinsic classes in the core library and renames all the classes in the FP7 and FP8 folders as if the folders were package names. E.g. Object becomes FP8.Object, String becomes FP8.String.

This causes FDT all kinds of problems, and Eclipse hangs while trying to refresh the workspace (while consuming 100% CPU).

This has happened to me on several occasions and it took me hours to work out the issue – i tried cleaning out all the cache and project metadata from eclipse, I tried reinstalling the FDT plugin, all with no success. I eventually set up a new workspace and imported a single project and eventually discovered what had happened.

The only solution is to copy over the core classes (C:\Program Files\Macromedia\Flash 8\en\First Run\Classes) with a fresh copy, or go through and re-edit each intrinsic class.

I’ve posted the bug here.

to Boolean || not to Boolean

Monday, April 23rd, 2007

I was looking for an simpler way of setting default values for function parameters in ActionScript2. The way I usually do this is something along the lines of:

function set value(s:String):Void
{
if(s == undefined) _value = s;
else _value = "default";
}

One option is using a logical OR operator to set default values. For example:

function set value(s:String):Void
{
_value = s || "default";
}

According to the flash LiveDocs a non-Boolean logical OR operation first converts the first operand (i.e. the expression on the left of the ||) to Boolean and if true returns the resolved value of the first operand. Otherwise it will return the resolved value of the second operand (expression on the right of the ||)

In the use case above it works fine, but after exploring the non-boolean implementation of the OR operator we found the following behaviour. In both the string literal (“hey”) and the string variable (s), the Boolean value equals true, but when placed in a logical OR operation they return different results. See below:

var s:String;
s = "hey";
trace(Boolean(s));//true
trace(Boolean("hey") //true
trace("hey" || "dude");//"dude"
trace(s || "dude");//"hey"

Can anyone explain why the String literal is treated differently than a variable containing a String literal?

Oh and word up to Ian on this one for his input :)

[UPDATE] I know I could have gone with “toBoolean() || !toBoolean()” for the title but I think that is taking it a little too far.

[UPDATE 2] The only gotcha with this approach is with numbers as a value of zero converts to false.

Taming the code library

Thursday, March 29th, 2007

I attended WEBDU the other week and had some thoughts after listening to one of the sessions – Geoff Bowers’ “taming the code”.

Amazingly over half the people in the audience weren’t using any form of version control. While we’ve been using version control for a few years now on our flash projects, It made me think about the development methodologies and processes that we have developed in the flash team at Massive over the last few years, specifically in relation to maintaining a common centralised code base.

The way i see it, there are two types of common code libraries – constant and evolving.

Constant code base
This comprises of functionality that rarely changes. Good examples of this are utility classes (e.g. trimming white space on a String or a generic XML parser). These classes are added to over time (in the case of utility classes as new conversions/formats are required), but the usage of existing methods remains the same across all projects – the returned format of a NumberUtil.toDollarsAndCents() method is always going to need to return a string with 2 decimal places.

Evolving code base
This is the common code base that evolves over time. At massive this is primarily the frameworks we use for our applications. While there is little change individually from project to project, over time our processes evolve and all the subtle improvements add up to a distinctly different framework. A project that is only 6-12 months old looks and behaves quite differently to a project started today.

Despite this, it is still necessary to version control the code base for our frameworks – because the alternative is to copy and paste core classes by hand every time we start a new project – yuck! At the same time we don’t want to have to maintain backwards compatibility in our common code base just in order to be able to recompile old projects.

Solution

Our solution to this problem has been to not version control the actual classes for our frameworks, but instead version control scripts that can automatically generate them. This way each project has its own version of the core classes that are fixed within that project, and every new project contains the most recent set of classes and structure.

There are heaps of benefits to this approach – not only do we not have to worry about breaking old projects – but we gain an extra level of standardisation across our projects no matter who initially creates them. Setting up a new project by hand can take hours and is often the most tedious part of the process. Automating this process saves time and heaps of monkey work.

Because we primarily use Eclipse for our actionscript development, we’ve been using Ant for all our build scripts (Eclipse has inbuilt support for it). Initially our scripts were responsible for creating a few folders, and some stub core classes. Over time we have extended this further to auto-generate project specific build scripts for both compiling a project and also creating specific class types on demand.