<commit_before>/*var mynumber = 0x2
console.log(Number(mynumber));
var othernumber = 1115;
console.log('0x'+othernumber.toString(16));
console.log(Number('0x'+othernumber.toString(16)));
console.log((new Date()));
var fs = require('fs');
var hugeString = "";
var item = "abcdefghijklmnopqrstuvwxyz";
for(var i=0;i<10000000;i++){
    hugeString+=item+"\n";
}
console.log((new Date()));
fs.appendFileSync('mytest.txt', hugeString);
console.log((new Date()));

console.log('big loading test');
console.log((new Date()));
var file1 = fs.readFileSync('IFV.xme','utf8');
console.log((new Date()));
var file2 = fs.readFileSync('IFV.xme','utf8');
console.log((new Date()));
var file3 = fs.readFileSync('IFV.xme','utf8');
console.log((new Date()));

var ASSERT = function(value){};*/
/*
 * Copyright (C) 2013 Vanderbilt University, All rights reserved.
 *
 * Author: <NAME>
 */

if (typeof define !== "function") {
    var requirejs = require("requirejs");

    requirejs.config({
        nodeRequire: require,
        baseUrl: __dirname + "/.."
    });

    requirejs([ "util/common", "util/assert", "core/tasync", "bin/serialize_to_xml" ], function (COMMON, ASSERT, TASYNC, serializer) {
        "use strict";

        TASYNC.trycatch(main, function (error) {
            console.log(error.trace || error.stack);

            COMMON.setProgress(null);
            COMMON.closeProject();
            COMMON.closeDatabase();
        });
        var _commit = null,
            _startHash = null,
            _branch = null,
            _projectName = null,
            _outFile = null;

        function main () {
            var args = COMMON.getParameters(null);

            if (args.length < 1 || COMMON.getParameters("help") !== null) {
                console.log("Usage: node parse_xme.js <xmlfile> [options]");
                console.log("");
                console.log("Parses a GME xme file and stores it int a WEBGME database. Possible options:");
                console.log("");
                console.log("  -mongo [database [host [port]]]\topens a mongo database");
                console.log("  -proj <project>\t\t\tselects the given project");
                console.log("  -branch <branch>\t\t\tthe branch to serialize");
                console.log("  -hash <hash>\t\t\t\tthe root hash to serialize");
                console.log("  -commit <hash>\t\t\tthe commit hash to serialize");
                console.log("  -help\t\t\t\t\tprints out this help message");
                console.log("");
                return;
            }

            _outFile = args[0];

            _branch = COMMON.getParameters("branch");
            if (_branch) {
                _branch = _branch[0] || "parsed";
            }
            _startHash = COMMON.getParameters("hash");
            if(_startHash){
                _startHash = _startHash[0];
            }
            _projectName = COMMON.getParameters("proj");
            if(_projectName){
                _projectName = _projectName[0];
            }
            _commit = COMMON.getParameters('commit');
            if(_commit){
                _commit = _commit[0];
            }

            var done = TASYNC.call(COMMON.openDatabase);
            done = TASYNC.call(COMMON.openProject, done);
            var core = TASYNC.call(COMMON.getCore, done);
            console.log('kecso',_branch);
            if(!_startHash){
                if(_branch){
                    _startHash = TASYNC.call(getRootHashOfBranch,_branch,done);
                    TASYNC.call(settingCommitHashOfBranch,_branch,done);
                } else if(_commit){
                    _startHash = TASYNC.call(getRootHashOfCommit,_commit,done);
                } else {
                    done = TASYNC.call(COMMON.closeProject, done);
                    done = TASYNC.call(COMMON.closeDatabase, done);
                    return done;
                }
            }
            done = TASYNC.call(serializer,core,_startHash,_outFile);
            done = TASYNC.call(function(object){
                console.log('the root after serializing and normalizing:',object);
                console.log('the commit hash what we modified:',_commit);
                if(_commit){
                    //we should make a commit
                    console.log('kecso1',_branch);
                    var newCommit = makeCommit(_outFile,object,_commit);
                    if(_branch){
                        console.log('kecso2');
                        //we also should update the branch
                        return TASYNC.call(writeBranch,newCommit);
                    } else {
                        return null;
                    }
                } else {
                    return null;
                }
            },done);
            done = TASYNC.call(COMMON.closeProject, done);
            done = TASYNC.call(COMMON.closeDatabase, done);
            return done;
        }

        function makeCommit (xmlfile, rootHash, commitHash) {
            var project = COMMON.getProject();
            return project.makeCommit([commitHash], rootHash, "normalizing during serialization to " + xmlfile);
        }
        function getRootHashOfBranch (branch){
            var project = COMMON.getProject();
            var commitHash = project.getBranchHash(branch,null);
            var done = TASYNC.call(project.loadObject,commitHash);
            done = TASYNC.call(function(object){
                console.log('getRootHashOfBranch',branch,object.root);
                _branch = branch;
                return object.root;
            },done);
            return done;
        }

        function settingCommitHashOfBranch (branch){
            var project = COMMON.getProject();
            var cHash = project.getBranchHash(branch,null);
            return TASYNC.call(function(hash){
                _commit = hash;
                return hash;
            },cHash);
        }

        function getRootHashOfCommit (commit){
            var project = COMMON.getProject();
            return TASYNC.call(function(object){
                console.log('getRootHashOfCommit',commit,object.root);
                return object.root;
            },TASYNC.call(project.loadObject,commit));
        }

        function writeBranch (hash) {
            var project = COMMON.getProject();
            var done = project.setBranchHash(_branch, _commit, hash);
            return TASYNC.call(function () {
                console.log("Commit " + hash + " written to branch " + _branch);
            }, done);
        }

    });

    return;
}

define([ "util/assert", "core/tasync", "util/common", 'fs', 'storage/commit', 'storage/cache', 'storage/mongo', 'core/core' ], function (ASSERT, TASYNC, COMMON,fs,Commit,Cache,Mongo,Core) {
// attributes reserved for easier JSON to XML conversion
// _type : show the type of the XML tag
// _attr : object which contain all the attributes of the XML node
// _empty : true/false show if the object has inner content or not (if not it will be written in <tag/> form
// _children : [] - ordered list of inner XML objects
// _string : if this tag exists then the inner part of the XML object will be this string
    var _core = null;
    var _nodes = [];
    var _prefix = "*&*";
    var _core = null;
    var _idCounter = {
        atom:0,
        model:0,
        connection:0,
        folder:0
    };
    //XML object attributes
    var registry = {
        guid: "guid",
        created: "cdate",
        modified: "mdate",
        version: "version",
        metaname: "metaname",
        metaguid: "metaguid",
        metaversion: "metaversion",
        id: "id",
        relid: "relid",
        kind: "kind",
        role: "role",
        isinstance: "isinstance",
        isprimary: "isprimary"
    };
    var webOnly = ['position','isPort','isConnection','metameta','author','comment'];
    function initJsonObject(gmeNode){
        var initJ = {};
        initJ._type = _core.getRegistry(gmeNode,'metameta');
        initJ._empty = false;
        initJ._children = [];
        initJ._attr = {};


        for(var i in registry){
            var value = _core.getRegistry(gmeNode,i);
            if(value !== undefined && value !== null){
                initJ._attr[registry[i]] = value;
            }
        }

        //now we should add the children to the node
        //first come the name
        //then the registry nodes
        //then the attributes

        //name
        initJ._children.push({
            _type:"name",
            _empty:false,
            _string:_core.getAttribute(gmeNode,'name')
        });

        //comment and author
        if(initJ._type === 'project'){
            initJ._children.push({
                _type:'comment',
                _empty:false,
                _string:_core.getRegistry(gmeNode,'comment') || ""
            });
            initJ._children.push({
                _type:'author',
                _empty:false,
                _string:_core.getRegistry(gmeNode,'author') || ""
            });
        }

        //regnodes
        initJ._children = initJ._children.concat(createRegistryObject(gmeNode));

        //attributes
        var attrNames = _core.getAttributeNames(gmeNode);
        for(i=0;i<attrNames.length;i++){
            if(attrNames[i] !== 'name'){
                initJ._children.push({
                    _type:"attribute",
                    _empty:false,
                    _attr:{
                        kind:attrNames[i]
                    },
                    _children:[
                        {
                            _type:"value",
                            _empty:false,
                            _string:_core.getAttribute(gmeNode,attrNames[i])
                        }
                    ]
                });
            }
        }

        return initJ;
    };
    function registryToChildrenArray_(registryObj){
        ASSERT(typeof registryObj === 'object' );
        var regJSON = [];
        for(var i in registryObj){
            regJSON.unshift({
                _type:"regnode",
                _empty:false,
                _attr:{
                    name:i
                },
                _children:[]
            });
            if(typeof registryObj[i] !== 'object'){
                regJSON[0]._children.push({
                    _type:"value",
                    _empty:false,
                    _string:""+registryObj[i]
                });
                regJSON[0]._attr.isopaque = 'yes';
            } else {
                regJSON[0]._children = registryToChildrenArray(registryObj[i]);
                regJSON[0]._children.unshift({
                    _type:"value",
                    _empty:false,
                    _string:""
                });
            }
        }
        return regJSON;
    }
    function createRegistryObject_(gmeNode){
        var regNames = _core.getRegistryNames(gmeNode);
        var regObject = {};
        for(i=0;i<regNames.length;i++){
            if((registry[regNames[i]] === undefined || registry[regNames[i]] === null)&& webOnly.indexOf(regNames[i]) === -1){
                var nameArray = regNames[i].split('/');
                var tObj = regObject;
                for(var j=0;j<nameArray.length;j++){
                    if(tObj[nameArray[j]]){
                        tObj = tObj[nameArray[j]];
                    } else {
                        if(j === nameArray.length-1){
                            tObj[nameArray[j]] = _core.getRegistry(gmeNode,regNames[i]);
                        } else {
                            tObj[nameArray[j]] = {};
                            tObj = tObj[nameArray[j]];
                        }
                    }
                }
            }
        }

        //no we should convert into the correct JSON form
        return registryToChildrenArray(regObject);
    }
    function registryToChildrenArray(registryObj){
        ASSERT(typeof registryObj === 'object' );
        var regJSON = [];
        for(var i in registryObj){
            if(i !== '_value'){
                regJSON.unshift({
                    _type:"regnode",
                    _empty:false,
                    _attr:{
                        name:i
                    },
                    _children:[]
                });

                regJSON[0]._children = registryToChildrenArray(registryObj[i]);
                regJSON[0]._children.unshift({
                    _type:"value",
                    _empty:false,
                    _string: (registryObj[i]._value === null || registryObj[i]._value === undefined) ? "" : registryObj[i]._value
                });
            }
        }
        return regJSON;
    }
    function createRegistryObject(gmeNode){
        var regNames = _core.getRegistryNames(gmeNode);
        var regObject = {};
        for(i=0;i<regNames.length;i++){
            if((registry[regNames[i]] === undefined || registry[regNames[i]] === null)&& webOnly.indexOf(regNames[i]) === -1){
                var nameArray = regNames[i].split('/');
                var tObj = regObject;
                for(var j=0;j<nameArray.length;j++){
                    if(tObj[nameArray[j]]){
                        tObj = tObj[nameArray[j]];
                    } else {
                        /*if(j === nameArray.length-1){
                            tObj[nameArray[j]] = _core.getRegistry(gmeNode,regNames[i]);
                        } else {
                            tObj[nameArray[j]] = {};
                            tObj = tObj[nameArray[j]];
                        }*/
                        tObj[nameArray[j]] = {};
                        tObj = tObj[nameArray[j]];
                    }
                }
                tObj._value = theCore.getRegistry(gmeNode,regNames[i]);
            }
        }

        //no we should convert into the correct JSON form
        return registryToChildrenArray(regObject);
    }
    function nodeToJsonSync (node){
        //console.log('node',_core.getPath(node));
        //inner functions - for shallow children creation and other stuff

        var createConnectionChild = function(connNode){
            //console.log('connection',_core.getPath(connNode));
            if(_core.getPath(connNode) === '/-1/-2/-2382/-2383/-2403'){
                console.log('ehune');
            }
            var jsonConnection = initJsonObject(connNode);
            //now we should add the two connection point tag
            jsonConnection._children.push({
                _type:'connpoint',
                _empty:true,
                _attr:{
                    role:"src",
                    target:_core.getRegistry(_nodes[_prefix+_core.getPointerPath(connNode,'source')],'id')
                }
            });
            jsonConnection._children.push({
                _type:'connpoint',
                _empty:true,
                _attr:{
                    role:"dst",
                    target:_core.getRegistry(_nodes[_prefix+_core.getPointerPath(connNode,'target')],'id')
                }
            });

            return jsonConnection;
        };

        //start
        var jsonObject = initJsonObject(node);

        //now we should add the children to the node
        //then the model/atom
        //then the connection

        var childrenPaths = _core.getChildrenPaths(node);
        var ordinaryChildren = [];
        var connections = [];
        for(var i=0;i<childrenPaths.length;i++){
            if(_core.getRegistry(_nodes[_prefix+childrenPaths[i]],'metameta') === 'connection'){
                connections.push(childrenPaths[i]);
            } else {
                ordinaryChildren.push(childrenPaths[i]);
            }
        }

        //ordinary children recursively
        for(var i=0;i<ordinaryChildren.length;i++){
            jsonObject._children.push(nodeToJsonSync(_nodes[_prefix+""+ordinaryChildren[i]]));
        }

        //connections
        for(var j=0;j<connections.length;j++){
            jsonObject._children.push(createConnectionChild(_nodes[ _prefix+""+connections[j]]));
        }

        return jsonObject;
    }
    function fullJsonToString(jsonObject,indent){
        if(jsonObject === null){
            return "";
        }
        ASSERT(jsonObject._type);
        var outString = indent;

        //starting with the type of the object
        outString += "<"+jsonObject._type;

        //adding XML attributes
        if(jsonObject._attr){
            for(var i in jsonObject._attr){
                outString += " "+i+"=\""+jsonObject._attr[i]+"\"";
            }
        }

        //checking _empty
        if(jsonObject._empty){
            outString += "/>\n";
            return outString;
        }

        //checking _string
        if(jsonObject._string !== undefined && jsonObject._string !== null){
            outString += ">"+jsonObject._string+"</"+jsonObject._type+">\n";
            return outString;
        }
        outString += ">\n";

        //creating children texts
        if(jsonObject._children){
            ASSERT(jsonObject._children.length);
            for(i=0;i<jsonObject._children.length;i++){
                outString+=fullJsonToString(jsonObject._children[i],indent+"  ");
            }
        }

        outString += indent+"</"+jsonObject._type+">\n";
        return outString;
    }
    function partialJsonToString(jsonObject,indent){
        ASSERT(jsonObject._type);
        var outString = indent;

        //starting with the type of the object
        outString += "<"+jsonObject._type;

        //adding XML attributes
        if(jsonObject._attr){
            for(var i in jsonObject._attr){
                outString += " "+i+"=\""+jsonObject._attr[i]+"\"";
            }
        }

        //checking _empty
        if(jsonObject._empty){
            outString += "/>\n";
            return outString;
        }

        //checking _string
        if(jsonObject._string !== undefined && jsonObject._string !== null){
            outString += ">"+jsonObject._string+"</"+jsonObject._type+">\n";
            return outString;
        }
        outString += ">\n";

        //creating children texts
        if(jsonObject._children){
            ASSERT(jsonObject._children.length);
            for(i=0;i<jsonObject._children.length;i++){
                outString+=fullJsonToString(jsonObject._children[i],indent+"  ");
            }
        }

        //in this version we are not closing the object
        //outString += indent+"</"+jsonObject._type+">\n";
        return outString;
    }
    function closeJsonToString(jsonObject,indent){
        return indent+"</"+jsonObject._type+">\n";
    }
    function createXMLStart(){
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE project SYSTEM \"mga.dtd\">\n"
    }

    function serialize(corr,rootHash,outPath,projectName,callback){
        //temporary hack
        console.log('serialize',(new Date()));
        var myStorage = new Commit(new Cache(new Mongo({
            database: 'test',
            host: 'localhost',
            port:27017
        }), {}));
        myStorage.openDatabase(function(err){
            if(!err){
                myStorage.openProject(projectName,function(err,project){
                    if(!err && project){
                        var mycore = new Core(project, {<commit_msg>bugfix on children order<commit_after>/*
 * Copyright (C) 2013 Vanderbilt University, All rights reserved.
 *
 * Author: <NAME>
 */

if (typeof define !== "function") {
    var requirejs = require("requirejs");

    requirejs.config({
        nodeRequire: require,
        baseUrl: __dirname + "/.."
    });

    requirejs([ "util/common", "util/assert", "core/tasync", "bin/serialize_to_xml" ], function (COMMON, ASSERT, TASYNC, serializer) {
        "use strict";

        TASYNC.trycatch(main, function (error) {
            console.log(error.trace || error.stack);

            COMMON.setProgress(null);
            COMMON.closeProject();
            COMMON.closeDatabase();
        });
        var _commit = null,
            _startHash = null,
            _branch = null,
            _projectName = null,
            _outFile = null;

        function main () {
            var args = COMMON.getParameters(null);

            if (args.length < 1 || COMMON.getParameters("help") !== null) {
                console.log("Usage: node parse_xme.js <xmlfile> [options]");
                console.log("");
                console.log("Parses a GME xme file and stores it int a WEBGME database. Possible options:");
                console.log("");
                console.log("  -mongo [database [host [port]]]\topens a mongo database");
                console.log("  -proj <project>\t\t\tselects the given project");
                console.log("  -branch <branch>\t\t\tthe branch to serialize");
                console.log("  -hash <hash>\t\t\t\tthe root hash to serialize");
                console.log("  -commit <hash>\t\t\tthe commit hash to serialize");
                console.log("  -help\t\t\t\t\tprints out this help message");
                console.log("");
                return;
            }

            _outFile = args[0];

            _branch = COMMON.getParameters("branch");
            if (_branch) {
                _branch = _branch[0] || "parsed";
            }
            _startHash = COMMON.getParameters("hash");
            if(_startHash){
                _startHash = _startHash[0];
            }
            _projectName = COMMON.getParameters("proj");
            if(_projectName){
                _projectName = _projectName[0];
            }
            _commit = COMMON.getParameters('commit');
            if(_commit){
                _commit = _commit[0];
            }

            var done = TASYNC.call(COMMON.openDatabase);
            done = TASYNC.call(COMMON.openProject, done);
            var core = TASYNC.call(COMMON.getCore, done);
            console.log('kecso',_branch);
            if(!_startHash){
                if(_branch){
                    _startHash = TASYNC.call(getRootHashOfBranch,_branch,done);
                    TASYNC.call(settingCommitHashOfBranch,_branch,done);
                } else if(_commit){
                    _startHash = TASYNC.call(getRootHashOfCommit,_commit,done);
                } else {
                    done = TASYNC.call(COMMON.closeProject, done);
                    done = TASYNC.call(COMMON.closeDatabase, done);
                    return done;
                }
            }
            done = TASYNC.call(serializer,core,_startHash,_outFile);
            done = TASYNC.call(function(object){
                console.log('the root after serializing and normalizing:',object);
                console.log('the commit hash what we modified:',_commit);
                if(_commit){
                    //we should make a commit
                    console.log('kecso1',_branch);
                    var newCommit = makeCommit(_outFile,object,_commit);
                    if(_branch){
                        console.log('kecso2');
                        //we also should update the branch
                        return TASYNC.call(writeBranch,newCommit);
                    } else {
                        return null;
                    }
                } else {
                    return null;
                }
            },done);
            done = TASYNC.call(COMMON.closeProject, done);
            done = TASYNC.call(COMMON.closeDatabase, done);
            return done;
        }

        function makeCommit (xmlfile, rootHash, commitHash) {
            var project = COMMON.getProject();
            return project.makeCommit([commitHash], rootHash, "normalizing during serialization to " + xmlfile);
        }
        function getRootHashOfBranch (branch){
            var project = COMMON.getProject();
            var commitHash = project.getBranchHash(branch,null);
            var done = TASYNC.call(project.loadObject,commitHash);
            done = TASYNC.call(function(object){
                console.log('getRootHashOfBranch',branch,object.root);
                _branch = branch;
                return object.root;
            },done);
            return done;
        }

        function settingCommitHashOfBranch (branch){
            var project = COMMON.getProject();
            var cHash = project.getBranchHash(branch,null);
            return TASYNC.call(function(hash){
                _commit = hash;
                return hash;
            },cHash);
        }

        function getRootHashOfCommit (commit){
            var project = COMMON.getProject();
            return TASYNC.call(function(object){
                console.log('getRootHashOfCommit',commit,object.root);
                return object.root;
            },TASYNC.call(project.loadObject,commit));
        }

        function writeBranch (hash) {
            var project = COMMON.getProject();
            var done = project.setBranchHash(_branch, _commit, hash);
            return TASYNC.call(function () {
                console.log("Commit " + hash + " written to branch " + _branch);
            }, done);
        }

    });

    return;
}

define([ "util/assert", "core/tasync", "util/common", 'fs', 'storage/commit', 'storage/cache', 'storage/mongo', 'core/core' ], function (ASSERT, TASYNC, COMMON,fs,Commit,Cache,Mongo,Core) {
// attributes reserved for easier JSON to XML conversion
// _type : show the type of the XML tag
// _attr : object which contain all the attributes of the XML node
// _empty : true/false show if the object has inner content or not (if not it will be written in <tag/> form
// _children : [] - ordered list of inner XML objects
// _string : if this tag exists then the inner part of the XML object will be this string
    var _core = null;
    var _nodes = [];
    var _prefix = "*&*";
    var _core = null;
    var _idCounter = {
        atom:0,
        model:0,
        connection:0,
        folder:0
    };
    //XML object attributes
    var registry = {
        guid: "guid",
        created: "cdate",
        modified: "mdate",
        version: "version",
        metaname: "metaname",
        metaguid: "metaguid",
        metaversion: "metaversion",
        id: "id",
        relid: "relid",
        kind: "kind",
        role: "role",
        isinstance: "isinstance",
        isprimary: "isprimary"
    };
    var webOnly = ['position','isPort','isConnection','metameta','author','comment'];
    function initJsonObject(gmeNode){
        var initJ = {};
        initJ._type = _core.getRegistry(gmeNode,'metameta');
        initJ._empty = false;
        initJ._children = [];
        initJ._attr = {};


        for(var i in registry){
            var value = _core.getRegistry(gmeNode,i);
            if(value !== undefined && value !== null){
                initJ._attr[registry[i]] = value;
            }
        }

        //now we should add the children to the node
        //first come the name
        //then the registry nodes
        //then the attributes

        //name
        initJ._children.push({
            _type:"name",
            _empty:false,
            _string:_core.getAttribute(gmeNode,'name')
        });

        //comment and author
        if(initJ._type === 'project'){
            initJ._children.push({
                _type:'comment',
                _empty:false,
                _string:_core.getRegistry(gmeNode,'comment') || ""
            });
            initJ._children.push({
                _type:'author',
                _empty:false,
                _string:_core.getRegistry(gmeNode,'author') || ""
            });
        }

        //regnodes
        initJ._children = initJ._children.concat(createRegistryObject(gmeNode));

        //attributes
        var attrNames = _core.getAttributeNames(gmeNode);
        for(i=0;i<attrNames.length;i++){
            if(attrNames[i] !== 'name'){
                initJ._children.push({
                    _type:"attribute",
                    _empty:false,
                    _attr:{
                        kind:attrNames[i]
                    },
                    _children:[
                        {
                            _type:"value",
                            _empty:false,
                            _string:_core.getAttribute(gmeNode,attrNames[i])
                        }
                    ]
                });
            }
        }

        return initJ;
    }
    function registryToChildrenArray(registryObj){
        ASSERT(typeof registryObj === 'object' );
        var regJSON = [];
        for(var i in registryObj){
            if(i !== '_value'){
                var length = regJSON.push({
                    _type:"regnode",
                    _empty:false,
                    _attr:{
                        name:i
                    },
                    _children:[]
                });

                regJSON[length-1]._children = registryToChildrenArray(registryObj[i]);
                regJSON[length-1]._children.unshift({
                    _type:"value",
                    _empty:false,
                    _string: (registryObj[i]._value === null || registryObj[i]._value === undefined) ? "" : registryObj[i]._value
                });
            }
        }
        return regJSON;
    }
    function createRegistryObject(gmeNode){
        var regNames = _core.getRegistryNames(gmeNode);
        var regObject = {};
        for(i=0;i<regNames.length;i++){
            if((registry[regNames[i]] === undefined || registry[regNames[i]] === null)&& webOnly.indexOf(regNames[i]) === -1){
                var nameArray = regNames[i].split('/');
                var tObj = regObject;
                for(var j=0;j<nameArray.length;j++){
                    if(tObj[nameArray[j]]){
                        tObj = tObj[nameArray[j]];
                    } else {
                        /*if(j === nameArray.length-1){
                            tObj[nameArray[j]] = _core.getRegistry(gmeNode,regNames[i]);
                        } else {
                            tObj[nameArray[j]] = {};
                            tObj = tObj[nameArray[j]];
                        }*/
                        tObj[nameArray[j]] = {};
                        tObj = tObj[nameArray[j]];
                    }
                }
                tObj._value = theCore.getRegistry(gmeNode,regNames[i]);
            }
        }

        //no we should convert into the correct JSON form
        var regChildren = registryToChildrenArray(regObject);
        return regChildren;
    }
    function nodeToJsonSync (node){
        //console.log('node',_core.getPath(node));
        //inner functions - for shallow children creation and other stuff

        var createConnectionChild = function(connNode){
            //console.log('connection',_core.getPath(connNode));
            if(_core.getPath(connNode) === '/-1/-2/-2382/-2383/-2403'){
                console.log('ehune');
            }
            var jsonConnection = initJsonObject(connNode);
            //now we should add the two connection point tag
            jsonConnection._children.push({
                _type:'connpoint',
                _empty:true,
                _attr:{
                    role:"src",
                    target:_core.getRegistry(_nodes[_prefix+_core.getPointerPath(connNode,'source')],'id')
                }
            });
            jsonConnection._children.push({
                _type:'connpoint',
                _empty:true,
                _attr:{
                    role:"dst",
                    target:_core.getRegistry(_nodes[_prefix+_core.getPointerPath(connNode,'target')],'id')
                }
            });

            return jsonConnection;
        };

        //start
        var jsonObject = initJsonObject(node);

        //now we should add the children to the node
        //then the model/atom
        //then the connection

        var childrenPaths = _core.getChildrenPaths(node);
        var ordinaryChildren = [];
        var connections = [];
        for(var i=0;i<childrenPaths.length;i++){
            if(_core.getRegistry(_nodes[_prefix+childrenPaths[i]],'metameta') === 'connection'){
                connections.push(childrenPaths[i]);
            } else {
                ordinaryChildren.push(childrenPaths[i]);
            }
        }

        //ordinary children recursively
        for(var i=0;i<ordinaryChildren.length;i++){
            jsonObject._children.push(nodeToJsonSync(_nodes[_prefix+""+ordinaryChildren[i]]));
        }

        //connections
        for(var j=0;j<connections.length;j++){
            jsonObject._children.push(createConnectionChild(_nodes[ _prefix+""+connections[j]]));
        }

        return jsonObject;
    }
    function fullJsonToString(jsonObject,indent){
        if(jsonObject === null){
            return "";
        }
        ASSERT(jsonObject._type);
        var outString = indent;

        //starting with the type of the object
        outString += "<"+jsonObject._type;

        //adding XML attributes
        if(jsonObject._attr){
            for(var i in jsonObject._attr){
                outString += " "+i+"=\""+jsonObject._attr[i]+"\"";
            }
        }

        //checking _empty
        if(jsonObject._empty){
            outString += "/>\n";
            return outString;
        }

        //checking _string
        if(jsonObject._string !== undefined && jsonObject._string !== null){
            outString += ">"+jsonObject._string+"</"+jsonObject._type+">\n";
            return outString;
        }
        outString += ">\n";

        //creating children texts
        if(jsonObject._children){
            ASSERT(jsonObject._children.length);
            for(i=0;i<jsonObject._children.length;i++){
                outString+=fullJsonToString(jsonObject._children[i],indent+"  ");
            }
        }

        outString += indent+"</"+jsonObject._type+">\n";
        return outString;
    }
    function partialJsonToString(jsonObject,indent){
        ASSERT(jsonObject._type);
        var outString = indent;

        //starting with the type of the object
        outString += "<"+jsonObject._type;

        //adding XML attributes
        if(jsonObject._attr){
            for(var i in jsonObject._attr){
                outString += " "+i+"=\""+jsonObject._attr[i]+"\"";
            }
        }

        //checking _empty
        if(jsonObject._empty){
            outString += "/>\n";
            return outString;
        }

        //checking _string
        if(jsonObject._string !== undefined && jsonObject._string !== null){
            outString += ">"+jsonObject._string+"</"+jsonObject._type+">\n";
            return outString;
        }
        outString += ">\n";

        //creating children texts
        if(jsonObject._children){
            ASSERT(jsonObject._children.length);
            for(i=0;i<jsonObject._children.length;i++){
                outString+=fullJsonToString(jsonObject._children[i],indent+"  ");
            }
        }

        //in this version we are not closing the object
        //outString += indent+"</"+jsonObject._type+">\n";
        return outString;
    }
    function closeJsonToString(jsonObject,indent){
        return indent+"</"+jsonObject._type+">\n";
    }
    function createXMLStart(){
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE project SYSTEM \"mga.dtd\">\n\n"
    }

    function serialize(corr,rootHash,outPath,projectName,callback){
        //temporary hack
        console.log('serialize',(new Date()));
        var myStorage = new Commit(new Cache(new Mongo({
            database: 'test',
            host: 'localhost',
            port:27017
        }), {}));
        myStorage.openDatabase(function(err){
            if(!err){
                myStorage.openProject(projectName,function(err,project){
                    if(!err && project){
                        var mycore = new Core(project, {
{'fraction_non_alphanumeric': 0.12593844363797546, 'fraction_numerical': 0.0037399229852896365, 'mean_word_length': 1.5910134941142693, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 2, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '735071', 'n_tokens_mistral': 9877, 'n_tokens_neox': 9471, 'n_words': 1876}