views:

426

answers:

2

I've been struggling with how I can build mxml from the displaylist created within an application. For example, create a number of components on a canvas, then click a button to get a formatted xml document, in mxml, of the canvas and its properties.

Is there a simple method for this?

Many thanks for any help...

+1  A: 

There is no simple method for this for sure.

Edit: The reasonong behind this is that mxml is actually translated into actionscript, and then compiled. So, flash player know absolutely nothing about mxml and it's existance.

Hrundik
A: 

It would help to know a little more background on this, like why you would need to do this. As far as I know there isnt an easy or built-in way to do this.

heres a direction that might help:

  • Embed PlainText mxml template files for each type of DisplayObject your app is going to support. ( template files where each property has a variable to swap with string interpolation ex:mx:HRule height="{$hRuleHeight}" width="${hRuleWidth}"/

  • Everytime an Object is edited/created save the imformation---when it comes time to generate the mxml sort each saved item and take the properties and parse the templates with them. AS3 doesnt support string interpolation, below is an implementation here if you dont have one yourself.

usage:

  var example:StringInterpolation=new StringInterpolation();

  example.setKeyAndValue('${id}','foo');
  example.setKeyAndValue('${baseurl}','http://testing123');

  trace(example.eval('<table width="480" border="0" cellpadding="0" cellspacing="0"><tr><td colspan="3"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="480" height="270"><param name="movie" value="http://testing123/player/Take180Player.swf?xmlLocation=/s/bx/http://testing123}&amp;links=true" />'));


 /*outputs:<table width="480" border="0" cellpadding="0" cellspacing="0"><tr><td colspan="3"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="480" height="270"><param name="movie" value="http://testing123/player/Take180Player.swf?xmlLocation=/s/bx/http://testing123&amp;links=true" /> */

src:

//

package com.philipbroadway.utils
{
    public class StringInterpolation extends Object
    {
        internal var _keys:Array;
        internal var _values:Array;
        internal var _result:String;
        internal var i:uint;

        /**
         * 
         * 
         */        
        public function StringInterpolation()
        {
            _keys=[];
            _values=[];    
        }





        /**
         * 
         * @param key:String a variable name
         * @param value:String a value to replace when the key is found during eval
         * 
         */        
        public function setKeyAndValue(key:String,value:String):void
        {
            var metacharacters:Array=[];
            metacharacters.push(new RegExp(/\$/));
            metacharacters.push(new RegExp(/\{/));
            metacharacters.push(new RegExp(/\}/));
            metacharacters.push(new RegExp(/\^/));
            metacharacters.push(new RegExp(/\./));
            metacharacters.push(new RegExp(/\*/));
            metacharacters.push(new RegExp(/\+/));

            var replacements:Array=[];
            replacements.push(new String('\\$'));
            replacements.push(new String('\\{'));
            replacements.push(new String('\\}'));
            replacements.push(new String('\\^'));
            replacements.push(new String('\\.'));
            replacements.push(new String('\\*'));
            replacements.push(new String('\\+'));

            for(i=0;i<metacharacters.length;i++)
            {
                key=key.replace(metacharacters[i],replacements[i]);
            }
            _keys.push(key);
            _values.push(value);
        }





        /**
         * 
         * @param value:String to be interpolated
         * @return String interpolated
         * 
         */        
        public function eval(value:String):String
        {
            _result=value;
            for(i=0;i<_keys.length;i++)
            {
                var regEx:RegExp=new RegExp(_keys[i],'g');
                _result=_result.replace(regEx,_values[i]);
            }

            return _result;
        }




        /**
         * 
         * 
         */        
        public  function reset():void
        {
            _keys=[];
            _values=[];                
        }
    }
}
philip
i don't fully understand how this works, but let me add a note, if i may: your setKeyAndValue produces the same two arrays again and again, although it does not have to. furthermore, you don't need regex either ... this would do the trick: for each (var char:String in '${}^.*+'.split('')) key.replace(char,'\\'+char) ... caching '${}^.*+'.split('') would also help a little ...
back2dos