Posted By


alejandromb on 07/29/10

Tagged


Statistics


Viewed 605 times
Favorited by 1 user(s)

AS3 | Resolve Dependency


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function ResolveDependency( value:String, data:XMLList ):String {
  2.  
  3. var pattern:RegExp = /\{([a-zA-Z]([\w\_\-])*)(([\.][a-zA-Z]([\w\_\-])*)*([\.](@?)[a-zA-Z]([\w\_\-])*))?\}/g;
  4. var patternNodes:RegExp = /([a-zA-Z]([\w\_\-])*)(([\.][a-zA-Z]([\w\_\-])*)*([\.](@?)[a-zA-Z]([\w\_\-])*))?/g;
  5. var patternAttribute:RegExp = /\@/;
  6.  
  7. var list:Array;
  8. var node:XMLList;
  9. var vnode:String;
  10.  
  11. var attribute:String = null;
  12. var lnode:Array = value.match( pattern );
  13.  
  14. if ( lnode != null && lnode.length > 0 ) {
  15.  
  16. for ( var a:uint = 0; a < lnode.length; a++ ) {
  17.  
  18. node = data;
  19. vnode = String( lnode[a] );
  20. list = String( vnode.match( patternNodes )[0] ).split( "." );
  21.  
  22. if ( String( list[ list.length - 1 ] ).search( patternAttribute ) > -1 ) {
  23. attribute = String( list.pop() ).replace( patternAttribute, "" );
  24. }
  25.  
  26. for ( var b:uint = 0; b < list.length; b++ ) {
  27. if ( node.hasOwnProperty( list[b] ) ) {
  28. node = node.child( list[b] );
  29. } else if ( b > 0 && ! node.hasOwnProperty( list[b] ) ) {
  30. return null;
  31. }
  32. }
  33.  
  34. if( attribute != null ) {
  35. value = value.replace( vnode, node.attribute( attribute ) );
  36. }else{
  37. value = value.replace( vnode, node.text() );
  38. }
  39.  
  40. }
  41.  
  42. return value;
  43.  
  44. }
  45.  
  46. return value;
  47.  
  48. }
  49.  
  50. //Example:
  51.  
  52. var _data:XMLList = new XMLList("<xml><node><value id=\"url\">www.kirikacode.com</value></node></xml>");
  53. trace( ResolveDependency( "http://{node.value}", _data ) );
  54. trace( ResolveDependency( "Type: {node.value.@id}", _data ) );

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.