package es.xperiments.utils { /** * @author xperiments */ public class TemplateParser { public static const backSlash : String = '\\'; public static const varFinder : RegExp = /\${\w*}/gixsm; public static function parse( template : *, templateValues : TemplateVariables ) : String { if ( !(template is String ) && !( template is XML) ) { throw new ArgumentError( "Template must be XML or String" ); } var result : Object; var outputTemplate : String = template; while ( result = varFinder.exec( template ) ) { var replaceVar : RegExp = new RegExp( backSlash + result, "gixsm" ); var key : String = result.toString( ).substring( 2, result.toString( ).length - 1 ); if ( templateValues.hasOwnProperty( key ) ) { outputTemplate = outputTemplate.replace( replaceVar, templateValues[ key ] ); } } return outputTemplate; } } }