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

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
************************************************************
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