Return to Snippet

Revision: 29426
at July 29, 2010 03:42 by alejandromb


Initial Code
function ResolveDependency( value:String, data:XMLList ):String {

	var pattern:RegExp = /\{([a-zA-Z]([\w\_\-])*)(([\.][a-zA-Z]([\w\_\-])*)*([\.](@?)[a-zA-Z]([\w\_\-])*))?\}/g;
	var patternNodes:RegExp = /([a-zA-Z]([\w\_\-])*)(([\.][a-zA-Z]([\w\_\-])*)*([\.](@?)[a-zA-Z]([\w\_\-])*))?/g;
	var patternAttribute:RegExp = /\@/;
	
	var list:Array;
	var node:XMLList;
	var vnode:String;
	
	var attribute:String = null;
	var lnode:Array = value.match( pattern );
	
	if ( lnode != null && lnode.length > 0 ) {
		
		for ( var a:uint = 0; a < lnode.length; a++ ) {
			
			node = data;
			vnode = String( lnode[a] );
			list = String( vnode.match( patternNodes )[0] ).split( "." );
			
			if ( String( list[ list.length - 1 ] ).search( patternAttribute ) > -1 ) {
				attribute = String( list.pop() ).replace( patternAttribute, "" );
			}
			
			for ( var b:uint = 0; b < list.length; b++ ) {
				if ( node.hasOwnProperty( list[b] ) ) {
					node = node.child( list[b] );
				} else if ( b > 0 && ! node.hasOwnProperty( list[b] ) ) {
					return null;
				}
			}
			
			if( attribute != null ) {
				value = value.replace( vnode, node.attribute( attribute ) );
			}else{
				value = value.replace( vnode, node.text() );
			}
			
		}
		
		return value;
		
	}
	
	return value;
	
}

//Example:

var _data:XMLList = new XMLList("<xml><node><value id=\"url\">www.kirikacode.com</value></node></xml>");
trace( ResolveDependency( "http://{node.value}", _data ) );
trace( ResolveDependency( "Type: {node.value.@id}", _data ) );

Initial URL
http://www.kirikacode.com.ar

Initial Description

                                

Initial Title
AS3 | Resolve Dependency

Initial Tags

                                

Initial Language
ActionScript 3