/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is ChatZilla.
 *
 * The Initial Developer of the Original Code is James Ross.
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   James Ross <silver@warwickcompsoc.co.uk>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

/* The serialized file format is pretty generic... each line (using any line
 * separator, so we don't mind being moved between platforms) consists of
 * a command name, and some parameters (optionally). The commands 'start'
 * and 'end' mark the chunks of properties for each object - in this case
 * motifs. Every command inside a start/end block is considered a property
 * for the object. There are some rules, but we are generally pretty flexible.
 *
 * Example file:
 *   START <Array>
 *     START 0
 *       "message" "Food%3a%20Mmm...%20food..."
 *     END
 *     START 1
 *       "message" "Busy%3a%20Working."
 *     END
 *     START 2
 *       "message" "Not%20here."
 *     END
 *   END
 *
 * The whitespace at the start of the inner lines is generated by the
 * serialisation process, but is ignored when parsing - it is only to make
 * the file more readable.
 *
 * The START command may be followed by one or both of a class name (enclosed
 * in angle brackets, as above) and a property name (the first non-<>-enclosed
 * word). Top-level START commands must not have a property name, although a
 * class name is fine. Only the following class names are supported:
 *   - Object (the default)
 *   - Array
 *
 * For arrays, there are some limitations; saving an array cannot save any
 * properties that are not numerics, due to limitations in JS' for...in
 * enumeration. Thus, for loading, only items with numeric property names are
 * allowed. If an item is STARTed inside an array, and specifies no property
 * name, it will be push()ed into the array instead.
 */

/* This file has been taken from the ChatZilla source
 * and had it's file handling stuff removed,
 * so it instead deals with strings
 */


function TextSerializer()
{
    this._initialized = false;
    this._open = false;
    this._lines = [];
    this.lineEnd = "\n";
    this._initialized = true;
}

TextSerializer.prototype.getData =
function ts_get()
{
    return this._lines.join(this.lineEnd) + this.lineEnd;
}
TextSerializer.prototype.setData =
function ts_set(data)
{
    return this._lines = data.split(this.lineEnd);
}

/* serialize(object)
 *
 * Serializes a single object into the file stream. All properties of the object
 * are stored in the stream, including properties that contain other objects.
 */
TextSerializer.prototype.serialize =
function ts_serialize(obj)
{
    var me = this;

    function writeObjProps(o, indent)
    {
        function writeProp(name, val)
        {
            me._lines.push(indent + "\"" + ecmaEscape(name) + "\" " + val);
        };

        for (var p in o)
        {
            switch (typeof o[p])
            {
                case "string":
                    writeProp(p, '"' + ecmaEscape(o[p]) + '"');
                    break;

                case "number":
                case "boolean":
                case "null": // (just in case)
                case "undefined":
                    // These all serialise to what we want.
                    writeProp(p, o[p]);
                    break;

                case "function":
                    if (o[p] instanceof RegExp)
                        writeProp(p, ecmaEscape("" + o[p]));
                    // Can't serialize non-RegExp functions (yet).
                    break;

                case "object":
                    if (o[p] == null)
                    {
                        // typeof null == "object", just to catch us out.
                        writeProp(p, "null");
                    }
                    else
                    {
                        var className = "";
                        if (o[p] instanceof Array)
                            className = "<Array> ";

                        me._lines.push(indent + "START " + className + ecmaEscape(p));
                        writeObjProps(o[p], indent + "  ");
                        me._lines.push(indent + "END");
                    }
                    break;

                default:
                    // Can't handle anything else!
            }
        }
    };

    if (obj instanceof Array)
        this._lines.push("START <Array>");
    else
        this._lines.push("START");
    writeObjProps(obj, "  ");
    this._lines.push("END");
}

/* deserialize()
 *
 * Reads in enough of the file to deserialize (realize) a single object. The
 * object deserialized is returned; all sub-properties of the object are
 * deserialized with it.
 */
TextSerializer.prototype.deserialize =
function ts_deserialize()
{

    var obj = null;
    var rv = null;
    var objs = new Array();

    while (true)
    {
        if (this._lines.length == 0)
            break;
 
        // Split each line into "command params...".
        var parts = this._lines[0].match(/^\s*(\S+)(?:\s+(.*))?$/);
        var command = parts[1];
        var params = parts[2];

        // 'start' and 'end' commands are special.
        switch (command.toLowerCase())
        {
            case "start":
                var paramList = new Array();
                if (params)
                    paramList = params.split(/\s+/g);

                var className = "";
                if ((paramList.length > 0) && /^<\w+>$/i.test(paramList[0]))
                {
                    className = paramList[0].substr(1, paramList[0].length - 2);
                    paramList.shift();
                }

                if (!rv)
                {
                    // Construct the top-level object.
                    if (className)
                        rv = obj = new window[className]();
                    else
                        rv = obj = new Object();
                }
                else
                {
                    var n;
                    if (paramList.length == 0)
                    {
                        /* Create a new object level, but with no name. This is
                         * only valid if the parent level is an array.
                         */
                        if (!(obj instanceof Array))
                            return null;
                        if (className)
                            n = new window[className]();
                        else
                            n = new Object();
                        objs.push(obj);
                        obj.push(n);
                        obj = n;
                    }
                    else
                    {
                        /* Create a new object level, store the reference on the
                         * parent, and set the new object as the current.
                         */
                        if (className)
                            n = new window[className]();
                        else
                            n = new Object();
                        objs.push(obj);
                        obj[ecmaUnescape(paramList[0])] = n;
                        obj = n;
                    }
                }

                this._lines.shift();
                break;

            case "end":
                this._lines.shift();
                if (rv && (objs.length == 0))
                {
                    // We're done for the day.
                    return rv;
                }
                // Return to the previous object level.
                obj = objs.pop();
                if (!obj)
                    return rv;
                break;

            default:
                this._lines.shift();
                // The property name may be enclosed in quotes.
                if (command[0] == '"')
                    command = command.substr(1, command.length - 2);
                // But it is always escaped.
                command = ecmaUnescape(command);

                if (!obj)
                {
                    /* If we find a line that is NOT starting a new object, and
                     * we don't have a current object, we just assume the START
                     * command was missed.
                     */
                    rv = obj = new Object();
                }
                if (params[0] == '"') // String
                {
                    // Remove quotes, then ecmaUnescape.
                    params = params.substr(1, params.length - 2);
                    obj[command] = ecmaUnescape(params);
                }
                else if (params[0] == "/") // RegExp
                {
                    var p = params.match(/^\/(.*)\/(\w*)$/);
                    if (p)
                    {
                        var re = new RegExp(ecmaUnescape(p[1]), p[2]);
                        obj[command] = re;
                    }
                }
                else if (params == "null") // null
                {
                    obj[command] = null;
                }
                else if (params == "undefined") // undefined
                {
                    obj[command] = undefined;
                }
                else if ((params == "true") || (params == "false")) // boolean
                {
                    obj[command] = (params == "true");
                }
                else // Number
                {
                    obj[command] = Number(params);
                }
                break;
        }
    }
    return null;
}

/* ChatZilla js/lib/utils.js */
function ecmaEscape(str)
{
    function replaceNonPrintables(ch)
    {
        var rv = ch.charCodeAt().toString(16);
        if (rv.length == 1)
            rv = "0" + rv;
        else if (rv.length == 3)
            rv = "u0" + rv;
        else if (rv.length == 4)
            rv = "u" + rv;

        return "%" + rv;
    };

    // Replace any character that is not in the 69 character set
    // [ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./]
    // with an escape sequence.  Two digit sequences in the form %XX are used
    // for characters whose codepoint is less than 255, %uXXXX for all others.
    // See section B.2.1 of ECMA-262 rev3 for more information.
    return str.replace(/[^A-Za-z0-9@*_+.\-\/]/g, replaceNonPrintables);
}

function ecmaUnescape(str)
{
    function replaceEscapes(seq)
    {
        var ary = seq.match(/([\da-f]{1,2})(.*)|u([\da-f]{1,4})/i);
        if (!ary)
            return "<ERROR>";

        var rv;
        if (ary[1])
        {
            // two digit escape, possibly with cruft after
            rv = String.fromCharCode(parseInt(ary[1], 16)) + ary[2];
        }
        else
        {
            // four digits, no cruft
            rv = String.fromCharCode(parseInt(ary[3], 16));
        }

        return rv;
    };

    // Replace the escape sequences %X, %XX, %uX, %uXX, %uXXX, and %uXXXX with
    // the characters they represent, where X is a hexadecimal digit.
    // See section B.2.2 of ECMA-262 rev3 for more information.
    return str.replace(/%u?([\da-f]{1,4})/ig, replaceEscapes);
}