Porting XMLHighlighter class to ActionScript 3.

XMLHighlighter generates color highlighted pretty printed HTML code for the given XML document, I have ported it from ActionScript 2 to ActionScript 3.

Here is what I learned during the process

  • XML object in ActionScript 3 is different (It is ECMAScript for XML (E4X)).
  • For backward compatibility we have XMLDocument class which is equalent to XML in Actionscript 3.
  • In ActionScript 3 we need to keep the class inside Package container.

I replaced all “XML” to “XMLDocument” from the source, Moved my XMLHighlighter class inside blank Package, renamed it as XMLDocumentHighlighter. That’s all.

Here is the resulting class with usage example

/*
************************************************************
Developed by R.Arul Kumaran [[email protected]]     *
for more code keep visiting [www.shockwave-india.com/blog] *
************************************************************
version 1.0 Last updated on 4 July, 2006
*/

/*
XMLHighlighter for ActionScript3 generates color highlighted pretty printed HTML code
*/

package
{
    import flash.xml.*
    class XMLDocumentHighlighter
    {
        public static var useCDATA : Boolean = true;
        //_c: color string list
        private static var _c : Object = {
            tag : '0000FF', att : 'FF0000', txt : '000000', tgt : '990000'
        };
        //_fTag: get font begin tag
        private static function _fTag (clr : String, content : String)
        {
            return "<font color='#" + clr + "'>" + content;
        }
        // _tf: font tag close string
        private static var _tf : String = "</font>";
        // public function
        public static function highlight (x : XMLDocument) : String
        {
            //var x:XML;
            var ignoreWhite = x.ignoreWhite;
            var s : String = _fTag (_c.tag, '');
            x.ignoreWhite = true;
            if (x.nodeName == undefined)
            {
                if (x.xmlDecl != undefined)
                {
                    s += x.createTextNode (x.xmlDecl) + 'r';
                }
                if (x.docTypeDecl != undefined)
                {
                    s += x.createTextNode (x.docTypeDecl) + 'r';
                }
                s += _getHStr (x.firstChild, '', '');
            } else
            {
                s += _getHStr (x, '', '');
            }
            x.ignoreWhite = ignoreWhite;
            return "<pre>" + s + _tf + "</pre>";
        }
        private static function _getHStr (x, tab : String, r : String) : String
        {
            var s : String = '';
            switch (x.nodeType)
            {
                //TEXT_NODE
                case 3 :
                //Is it a CDATA Node?
                var xt : String = x.toString ();
                if (useCDATA && (x.nodeValue.indexOf ('<') != - 1 || x.nodeValue.indexOf ('>') != - 1))
                {
                    s = "&lt;![CDATA[" + _fTag (_c.txt, '<b>' + xt + '</b>') + _tf + "]]&gt;";
                } else
                {
                    s = _fTag (_c.txt, '<b>' + xt.split ('&').join ('&amp;') + '</b>') + _tf;
                }
                break;
                //ELEMENT_NODE
                case 1 :
                default :
                s = r + tab + '&lt;' + _fTag (_c.tgt, x.nodeName) + _tf;
                for (var v in x.attributes)
                {
                    s += _fTag (_c.tgt, " " + v) + _tf + "=&quot;" + _fTag (_c.att, x.attributes [v]) + _tf + "&quot;";
                }
                if (x.firstChild == null)
                {
                    s += "/&gt;";
                } else
                {
                    s += "&gt;" + _getHStr (x.firstChild, tab + "t", "r");
                    if (x.lastChild.nodeType == 3)
                    {
                        s += "&lt;/";
                    } else
                    {
                        s += 'r' + tab + "&lt;/";
                    }
                    s += _fTag (_c.tgt, x.nodeName) + _tf + "&gt;";
                }
            }
            if (x.nextSibling != null)
            {
                s += _getHStr (x.nextSibling, tab, r);
            }
            return s;
        }
    }
}
/*
//Usage:-
//copy this as file to the same folder as your FLA
//Usage Example:
var my_xml:XMLDocument=new XMLDocument('<a><b><c/></b></a>');
trace(XMLDocumentHighlighter.highlight(my_xml));
 
//trace output :
 
<pre><font color='#0000FF'>&lt;<font color='#990000'>a</font>&gt;
    &lt;<font color='#990000'>b</font>&gt;
        &lt;<font color='#990000'>c</font>/&gt;
    &lt;/<font color='#990000'>b</font>&gt;
&lt;/<font color='#990000'>a</font>&gt;</font></pre>
 
 
*/

Leave a Reply