Archive for the ‘ Flex ’ Category

PureASTemplate an AS3 port of PureJSTemplate engine.

In the last post I used javascript to dinamically parse a template and post back the results to AS3. Now I have ported the PureJSTemplate to AS3 with the help of D.eval API.

This means no Javascript/Browser need. Now you can integrate it directly to your AS3/Flex projects.

Download PureASTemplate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package es.xperiments.utils
{
    import r1.deval.D;

    /**
     * @author xperiments
     */

    public class PureASTemplate
    {
        private static var tplMap : Array = new Array();
        private static var left : String = "<#";
        private static var right : String = "#>";
        //Delimiters can have regex special characters in them; the following two variables will hold escaped versions of them
        private static var escapedLeft : String = "<#";
        private static var escapedRight : String = "#>";
        private static var escapeRegex : RegExp = new RegExp( '(\' + [ ', '^', '?', '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\' ].join( '|\' ) + ')', 'g' );
        private static var replaceLeft : RegExp = new RegExp( "<#", "g" );
        private static var regexLeftRight : RegExp = new RegExp( "<#" + "|" + "#>", "g" );
        private static var replaceSingleQuote : RegExp = new RegExp( "'
", "g" );
        private static var replaceLineBreak : RegExp = new RegExp( "
\\r|\\n", "g" );

        public static function parseTemplate( id : String, tpl : String, data : Object ):String
        {
            var unreplaceLineBreak : RegExp = new RegExp( "
{lineBreak}", "g" );
            var tplID : String = id;
            var tplObj : Object = tplMap[tplID];
            if (!tplObj)
            {
                var leftjs : String = String.fromCharCode( 21 );
                var leftjsout : String = leftjs + "
=";
                tpl = tpl.replace( replaceLeft, left + leftjs );
                var tplSplit : Array = tpl.split( regexLeftRight );
                var js:String = "
function "+id+"( data ) { ";
                    js += "
var output=''; var ld='" + left + "'; var rd='" + right + "'; ";
                for (var i : uint = 0; i < tplSplit.length ; i++)
                {
                    var line : String = tplSplit[i];
                    if (PureASTemplate.stringStartsWith( line, leftjsout ))
                    {
                        js += "
output+=" + line.substring( leftjsout.length ) + "; ";
                    }
                    else if (PureASTemplate.stringStartsWith( line, leftjs ))
                    {
                        js += "
" + line.substring( leftjs.length ) + " ";
                    }
                    else
                    {
                        js += "
output+='" + line.replace( replaceSingleQuote, "\'" ).replace( replaceLineBreak, '{lineBreak}' ) + "'; ";
                    }
                }
                js += "
return output; }";
                tplObj = D.parseFunctions( js );
                tplMap[tplID] = tplObj;
            }
            return D.evalToString( id+'( data )', { data:data }, tplObj ).replace( unreplaceLineBreak, '\n' );
        }

        public static function setDelimiters(l : String, r : String) : void
        {
            if (l != r)
            {
                left = l;
                escapedLeft = left.replace( escapeRegex, '\\$1' );
                replaceLeft = new RegExp( escapedLeft, "
g" );

                right = r;
                escapedRight = right.replace( escapeRegex, '\\$1' );

                regexLeftRight = new RegExp( escapedLeft + "
|" + escapedRight, "g" );
            }
        }

        private static function stringStartsWith(str : String, startsWith : String):Boolean
        {
            return str.substring( 0, startsWith.length ) == startsWith;
        }
    }
}

AS3 Implementing a simple Templating System II

After some investigation on as3 templates engines, I write a simple class to manage key value pairs, but for a project I am now developing I have to control more things in the Template layout…

I have find a simple Javascript template System and “Ported” it to work width AS3.

It is not a “Direct Port” because I adapted it to work as a single javascript class that anyone can use from AS3.

Example Code: ( assumes classPaths are correct and have a Textfield in stage named output )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import es.xperiments.utils.PureJSTemplate;
var exampleTemplate:XML =
<code>
<!--[CDATA[
<$=data.welcome$>
<$for(var k=0; k<data.number; k++){
    data.k = k+200;
$>
    <$=data.k$>
<$}$>
]]-->
</code>;

PureJSTemplate.addEventListener( Event.COMPLETE, onJSTemplate );
PureJSTemplate.initialize( );

function onJSTemplate( e:Event ):void
{
    PureJSTemplate.setDelimiters( "<$","$>");
    output.htmlText = PureJSTemplate.parseTemplate( "TestTemplate", exampleTemplate,{welcome:"PureJSTemplate is great",number:30} );
}

Download: Purejstemplate

Comparing Objects using ByteArrays

Other day I was thinking in this class I have seen.

Simple compare 2 objects by generating a ByteArray from themselves and compare with the other.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package {
              import flash.utils.ByteArray;
              public class ObjectTools {
                      public function ObjectTools () {
                      }
                      /**
                       * Compares two objects and checks if they are identical. Does not compare the objects by reference,
                       * but compares to see if the values of the properties are identical.
                       *
                       * @param obj1
                       * @param obj2
                       *
                       * @return
                       */

                      public static function compare (obj1:Object, obj2:Object):Boolean {
                              var b1:ByteArray = new ByteArray();
                              var b2:ByteArray = new ByteArray();
                              b1.writeObject(obj1);
                              b2.writeObject(obj2);

                              // compare the lengths first
                              var size:uint = b1.length;
                              if (b1.length == b2.length) {
                                      b1.position = 0;
                                      b2.position = 0;
                                      // then the bits
                                      while (b1.position &lt; size) {
                                              var v1:int = b1.readByte();
                                              if (v1 != b2.readByte()) {
                                                      return false;
                                              }
                                      }
                              }
                              if (b1.toString() == b2.toString()) {
                                      return true;
                              }
                              return false;
                      }
              }
      }

From: http://blog.joshbuhler.com/2008/02/11/comparing-objects-using-bytearrays/