package com.ericfeminella.annotations
{
import flash.utils.describeType;
import flash.utils.getQualifiedClassName;
import flash.utils.getDefinitionByName;
import flash.utils.Dictionary;
import mx.collections.ArrayCollection;
/**
*
* <code>MetadataUtils</code> is an all static class which provides
* utility operations from which class annotations can be located
* and inspected.
*
* <p>
* <code>MetadataUtils</code> provides a runtime introspective API
* which can be used to work with custom ActionScript and MXML metadata
* annotations
* </p>
*
*/
public final class MetadataUtils
{
/**
*
* Retrieves all defined custom metadata in an object and returns
* as a <code>Dictionary</code> of name / value pairs
*
* @param object by which to inspect for custom metadata
* @return <code>Dictionary</code> of attribute name and values
*
*/
public static function getMetadata(object:Object) : ArrayCollection
{
var metadataNodes:XMLList = getMetadataXML( object );
var n:int = metadataNodes.length();
var annotations:ArrayCollection = new ArrayCollection();
var map:Dictionary;
var metadata:XML;
var key:String;
var value:String;
for (var i:int = 0; i < n; i++)
{
metadata = metadataNodes[i];
map = new Dictionary( true );
for each (var arg:XML in metadata..arg)
{
key = arg.@key;
value = arg.@value;
map[ key ] = value;
}
annotations.addItem( new Metadata( metadata.@name, map ) );
}
return annotations;
}
/**
*
* Retrieves the specific named custom metadata defined in an object
* and returns as a <code>Dictionary</code> of name / value pairs
*
* @param object by which to inspect for custom metadata
* @param the specific metadata type to locate
* @return <code>Dictionary</code> of attribute name and values
*
*/
public static function getMetadataByName(object:Object, name:String) : Metadata
{
var metadataNodes:XMLList = getMetadataXML( object );
var n:int = metadataNodes.length();
var metadata:Metadata;
var map:Dictionary = new Dictionary( true );
var key:String;
var value:String;
for (var i:int = 0; i < n; i++)
{
var metadataXML:XML = metadataNodes[i];
if ( metadataXML.@name == name)
{
for each (var arg:XML in metadataXML..arg)
{
key = arg.@key;
value = arg.@value;
map[ key ] = value;
}
metadata = new Metadata( metadataXML.@name, map );
break;
}
}
return metadata;
}
/**
*
* Retrieves all custom metadata defined in an object and returns
* as a <code>XMLList</code>
*
* @param object by which to inspect for custom metadata
* @return <code>XML</code> of attribute name and value nodes
*
*/
public static function getMetadataXML(object:Object) : XMLList
{
var className:String = getQualifiedClassName( object );
var Type:Class = Class( getDefinitionByName( className ) );
var xml:XML = describeType( new Type() );
return xml.metadata;
}
}
}