all files / montage/core/serialization/ serialization.js

90.07% Statements 263/292
84.28% Branches 134/159
83.33% Functions 35/42
90.07% Lines 263/292
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897                                        12× 12× 12×   12×           146× 146× 146×   146×                         216× 12×     216×           12× 12×     12×           42×   42× 34× 34×       42×                                         12×           36×   36× 36×                                                                         72×                                                     23× 11×   11×     23×                                                                                                                               13×               13× 13×     13×   13×       11×         11× 11×   11×     14× 12×       11×   11×                                       11× 11×   11×                                                                 11×     11×                                                                           23×               23× 24×             24× 24×   24× 24×     23×       34× 34×                       27× 12× 12×   15×       23×                       53×           41×   41× 41×           15×   15× 15× 15×                 11×     11× 11× 11×           41× 66×             81×   81× 26×   55×                           132×         132×     132× 26×   106×   132× 106×       132× 27× 27× 27×   105×   103× 11× 11× 11×   92× 37× 37× 37×   55×       52× 52× 52× 52×   52× 58×         132×             55×     55×           55× 55× 55×     55× 39×   55×   55× 11×   55×             11×     11×           11× 11×   11× 11×             11×     11×             11× 11×   11×           13×       13× 13× 13× 14×       14× 14×       13×           13×                                                                                                     39×   39× 14×     39× 13× 26×                                           11×                   11×         11× 11×   11× 14×   14× 56×   56×                                           11×                 11×     11×          
/**
 * @module montage/serialization/serialization
 * @requires montage/core
 */
 
var Montage = require("../core").Montage,
    MontageLabeler = require("./serializer/montage-labeler").MontageLabeler,
    MontageReviver = require("./deserializer/montage-reviver").MontageReviver,
    parse = require("frb/parse"),
    stringify = require("frb/stringify");
 
/**
 * @class Serialization
 * @extends Montage
 */
var Serialization = Montage.specialize( /** @lends Serialization.prototype # */ {
    _serializationString: {value: null},
    _serializationObject: {value: null},
    _serializationLabels: {value: null},
 
    initWithString: {
        value: function (string) {
            this._serializationString = string;
            this._serializationObject = null;
            this._serializationLabels = null;
 
            return this;
        }
    },
 
    initWithObject: {
        value: function (object) {
            this._serializationString = null;
            this._serializationObject = object;
            this._serializationLabels = null;
 
            return this;
        }
    },
 
    clone: {
        value: function () {
            var serialization = new Serialization();
 
            serialization.initWithString(this.getSerializationString());
 
            return serialization;
        }
    },
 
    getSerializationObject: {
        value: function () {
            if (!this._serializationObject) {
                this._serializationObject = JSON.parse(this._serializationString);
            }
 
            return this._serializationObject;
        }
    },
 
    getSerializationString: {
        value: function () {
            Eif (!this._serializationString) {
                this._serializationString = JSON.stringify(this._serializationObject);
            }
 
            return this._serializationString;
        }
    },
 
    getSerializationLabels: {
        value: function () {
            var serializationObject;
 
            if (!this._serializationLabels) {
                Eif (serializationObject = this.getSerializationObject()) {
                    this._serializationLabels = Object.keys(serializationObject);
                }
            }
 
            return this._serializationLabels;
        }
    },
 
    getExternalObjectLabels: {
        value: function () {
            var serializationObject = this.getSerializationObject(),
                labels = [];
 
            for (var label in serializationObject) {
                if (Object.keys(serializationObject[label]).length === 0) {
                    labels.push(label);
                }
            }
 
            return labels;
        }
    },
 
    hasSerializationLabel: {
        value: function (label) {
            return label in this.getSerializationObject();
        }
    },
 
    isExternalObject: {
        value: function (label) {
            var serializationObject = this.getSerializationObject();
 
            Eif (serializationObject && label in serializationObject) {
                return Object.keys(serializationObject[label]).length === 0;
            } else {
                return false;
            }
        }
    },
 
    isAlias: {
        value: function (label) {
            var serializationObject = this.getSerializationObject();
 
            if (serializationObject && label in serializationObject) {
                return "alias" in serializationObject[label];
            } else {
                return false;
            }
        }
    },
 
    getElementId: {
        value: function (label) {
            var object = this.getSerializationObject();
 
            // TODO: much faster than using the visitor, need to make the visitor
            // faster.
            var element = Montage.getPath.call(object, label + ".properties.element");
            if (element) {
                return element["#"];
            }
        }
    },
 
    getSerializationLabelsWithElements: {
        value: function (elementIds) {
            var inspector = new SerializationInspector(),
                labels = [];
 
            inspector.initWithSerialization(this);
            inspector.visitSerialization(function (node) {
                // Check if this is one of the elements we're looking for
                if (node.type === "Element" && elementIds.indexOf(node.data) >= 0) {
                    // Check if it's inside a "properties" block
                    node = node.parent;
                    Eif (node && node.name === "properties") {
                        // Check if it's in a montage object
                        node = node.parent;
                        Eif (node && node.type === "montageObject") {
                            labels.push(node.label);
                        }
                    }
                }
            });
 
            return labels;
        }
    },
 
    renameElementReferences: {
        value: function (elementsTable) {
            var inspector = new SerializationInspector();
 
            inspector.initWithSerialization(this);
            inspector.visitSerialization(function (node) {
                if (node.type === "Element" && node.data in elementsTable) {
                    node.data = elementsTable[node.data];
                }
            });
        }
    },
 
    renameSerializationLabels: {
        value: function (labelsTable) {
            var inspector = new SerializationInspector();
 
            inspector.initWithSerialization(this);
            inspector.visitSerialization(function (node) {
                if (node.label) {
                    var label = node.label;
 
                    if (label in labelsTable) {
                        node.label = labelsTable[label];
                    }
                }
                if (node.type === "reference") {
                    var reference = node.data;
 
                    Eif (reference in labelsTable) {
                        node.data = labelsTable[reference];
                    }
                }
            });
        }
    },
 
    mergeSerialization: {
        value: function (serialization, delegate) {
            return SerializationMerger.mergeSerializations(this, serialization, delegate);
        }
    },
 
    extractSerialization: {
        value: function (labels, externalLabels) {
            var extractor = new SerializationExtractor();
 
            extractor.initWithSerialization(this);
            return extractor.extractSerialization(labels, externalLabels);
        }
    }
});
 
/**
 * @class SerializationMerger
 * @extends Montage
 */
var SerializationMerger = Montage.specialize(null, /** @lends SerializationMerger.prototype */ {
    /**
     * This delegate method is called when merging an object from serialization2
     * into serialization1. It allows the delegate to change how the object is
     * going to be merged by saying that the object already exists in
     * serialization1 under a different or the same label.
     *
     * When the delegate method doesn't return a string then the default
     * behavior is to add a new object to serialization1. If the object's label
     * collides with another label in serialization1 then a new label is
     * generated and used.
     *
     * By returning a label that exists in serialization1 from the delegate
     * method all references to the object being merged will change to point
     * to the object from serialization1 instead and the object will not be
     * merged.
     * By returning a label that does not exist in both serializations the
     * object will be merged into serialization2 with this new label instead.
     *
     * @callback delegateWillMergeObjectWithLabel
     * @param {string} label The object label.
     * @param {string} newLabel The new label generated by the collision resolver in case of collision.
     * @returns {string|undefined} the new label for this object.
     */
 
    /**
     * Merges serialization2 into serialization1.
     *
     * @param {Serialization} serialization1 - The serialization to be merged and end result of the merge operation.
     * @param {Serialization} serialization2 - The serialization to be merged.
     * @param {{willMergeObjectWithLabel: delegateWillMergeObjectWithLabel}} delegate
     * - The delegate to override the default behavior.
     *
     * @returns {Object} The collision table with the new labels generated for label clashes
     */
    mergeSerializations: {
        value: function (serialization1, serialization2, delegate) {
            var serializationObject1,
                serializationObject2,
                labels1,
                labels2,
                collisionTable = {},
                foundCollisions,
                hasCollisionTableChanged;
 
            labels1 = serialization1.getSerializationLabels();
            labels2 = serialization2.getSerializationLabels();
 
            // Check for name collisions and generate new labels
            foundCollisions = this._createCollisionTable(labels1, labels2, collisionTable, delegate && delegate.labeler);
 
            if (delegate && delegate.willMergeObjectWithLabel) {
                hasCollisionTableChanged = this._willMergeObjectWithLabel(delegate, serialization1, serialization2, collisionTable);
                foundCollisions = foundCollisions || hasCollisionTableChanged;
            }
 
            // Replace the labels with the new, non-colliding, ones
            if (foundCollisions) {
                // Clone serialization2 because we don't want to modify it.
                serialization2 = serialization2.clone();
                serialization2.renameSerializationLabels(collisionTable);
                labels2 = serialization2.getSerializationLabels();
            }
 
            // Merge the two serializations without the fear of name clashing
            serializationObject1 = serialization1.getSerializationObject();
            serializationObject2 = serialization2.getSerializationObject();
 
            for (var i = 0, label; (label = labels2[i]); i++) {
                // If this label already exists in serialization1 don't merge
                // it as it means they are the same object.
                if (labels1.indexOf(label) === -1) {
                    serializationObject1[label] = serializationObject2[label];
                }
            }
 
            serialization1.initWithObject(serializationObject1);
 
            return collisionTable;
        }
    },
 
    /**
     * @private
     * @function
     */
    _willMergeObjectWithLabel: {
        value: function (delegate, serialization1, serialization2, collisionTable) {
            var newLabel,
                collisionLabel,
                collisionLabels,
                inDestination,
                renameLabel,
                hasCollisionTableChanged = false,
                labels2 = serialization2.getSerializationLabels();
 
            Eif (collisionTable) {
                collisionLabels = [];
                Object.keys(collisionTable).forEach(function (label) {
                    collisionLabels.push(collisionTable[label]);
                });
            }
 
            for (var i = 0, label; (label = labels2[i]); i++) {
                collisionLabel = collisionTable && collisionTable[label];
                newLabel = delegate.willMergeObjectWithLabel(label, collisionLabel);
 
                if (typeof newLabel === "string") {
                    // If the delegate returns a new label there are two
                    // possible interpretations:
                    // 1) The label is on the destination serialization so it
                    //    means that we don't need to move this object there
                    //    because it is already there under a different label.
                    //    We just need to update the references to point to the
                    //    right name.
                    // 2) The label doesn't exist anywhere so it means we just
                    //    need to move the object under this new different
                    //    label.
                    inDestination = this._isLabelValidInSerialization(
                        newLabel, serialization1);
                    if (!inDestination) {
                        renameLabel = !this._isLabelValidInSerialization(
                            newLabel, serialization2)
                            && collisionLabels.indexOf(newLabel) === -1;
                    }
 
                    if (inDestination || renameLabel) {
                        hasCollisionTableChanged = true;
                        collisionTable[label] = newLabel;
                    } else {
                        throw new Error("willMergeObjectWithLabel either needs to return a label that exists in the destination serialization to indicate it's the same object or return a completely new label to rename the object being merged. \"" + newLabel + "\"  destination: " + serialization1.getSerializationString() + "\n source: " + serialization2.getSerializationString() + "\n collision table: " + JSON.stringify(collisionTable, null, 4));
                    }
                }
            }
 
            return hasCollisionTableChanged;
        }
    },
 
    /**
     * This function returns true when the label is part of the serialization,
     * or, if a template property, it refers to a component label that is part
     * of the serialization.
     *
     * @private
     */
    _isLabelValidInSerialization: {
        value: function (label, serialization) {
            var componentLabel,
                ix;
 
            if (serialization.hasSerializationLabel(label)) {
                return true;
            } else {
                ix = label.indexOf(":");
                // It's a template property, if the component part is
                // in the serialization then it's a valid label too.
                if (ix > 0) {
                    componentLabel = label.slice(0, ix);
                    Eif (serialization.hasSerializationLabel(componentLabel)) {
                        return true;
                    }
                }
            }
 
            return false;
        }
    },
 
    /**
     * This function creates a collision table between labels1 and labels2.
     * The collision table offers renames for the labels in the labels2 array
     * that already exist in the labels1 array.
     *
     * This function knows how to deal with labels that refer to template
     * properties. A label for a template property has the following syntax:
     * <component label>:<label>.
     * The collision table guarantees that template properties' labels will
     * always be in sync with their corresponding component label.
     *
     * When a collision exist with a label for a template property the
     * collision is solved by creating a new label for the component and
     * adopting that new label: <new component label>:<label>. In this
     * case the component label will also be part of the resulting collision
     * table even if there was no original collision in labels1.
     *
     * @example
     * labels1: ["repetition:iteration"]
     * labels2: ["repetition", "repetition:iteration"]
     * collisionTable: {"repetition:iteration": "object:iteration",
     *                  "repetition": "object"}
     *
     * @private
     */
    _createCollisionTable: {
        value: function (labels1, labels2, collisionTable, labeler) {
            var labeler = labeler || new MontageLabeler(),
                foundCollisions = false,
                componentLabel,
                labels1Index = Object.create(null),
                newLabel,
                label,
                ix;
 
            for (var i = 0; i < labels1.length; i++) {
                label = labels1[i];
 
                // If this label is a property template then we need to register
                // the component name too, it could be that it's not present
                // on labels1. We want to avoid the possibility of generating
                // a label that conflicts with the component part of the template
                // property.
                ix = label.indexOf(":");
                if (ix > 0) {
                    componentLabel = label.slice(0, ix);
                    labeler.addLabel(componentLabel);
                    labels1Index[componentLabel] = 1;
                }
                labeler.addLabel(label);
                labels1Index[label] = 1;
            }
 
            for (var i = 0; (label = labels2[i]); i++) {
                // If the label is a template property then check to see if
                // the component label has been renamed already or if the entire
                // label or component label have a collision to solve.
                ix = label.indexOf(":");
                if (ix > 0) {
                    componentLabel = label.slice(0, ix);
                    newLabel = collisionTable[componentLabel];
 
                    if (newLabel) {
                        collisionTable[label] = newLabel + ":" + label.slice(ix+1);
                        foundCollisions = true;
                    } else if (componentLabel in labels1Index) {
                        // Renaming a label that is a property template is
                        // the same as renaming the component part of the
                        // label.
                        newLabel = labeler.generateLabel(labeler.getLabelBaseName(componentLabel));
                        // Rename the component label too if it exists.
                        if (labels2.indexOf(componentLabel) >= 0) {
                            collisionTable[componentLabel] = newLabel;
                        }
                        collisionTable[label] = newLabel + label.slice(ix);
                        foundCollisions = true;
                    } else {
                        labeler.addLabel(componentLabel);
                    }
                }
                // Also check if the label already has a new label, this can
                // happen if a template property on that component was renamed.
                else if (label in labels1Index && !(label in collisionTable)) {
                    collisionTable[label] = labeler.generateLabel(labeler.getLabelBaseName(label));
                    foundCollisions = true;
                } else {
                    labeler.addLabel(label);
                }
            }
 
            return foundCollisions;
        }
    }
});
 
 
/**
 * @class SerializationInspector
 * @extends Montage
 */
var SerializationInspector = Montage.specialize(/** @lends SerializationInspector.prototype # */ {
    initWithSerialization: {
        value: function (serialization) {
            this._serialization = serialization;
        }
    },
 
    visitSerialization: {
        value: function (visitor) {
            var serialization = this._serialization.getSerializationObject();
 
            this._walkRootObjects(visitor, serialization);
            this._serialization.initWithObject(serialization);
        }
    },
 
    visitSerializationObject: {
        value: function (label, visitor) {
            var serialization = this._serialization.getSerializationObject();
 
            Eif (label in serialization) {
                this._walkRootObject(visitor, serialization, label);
                this._serialization.initWithObject(serialization);
            } else {
                throw new Error('Object "' + label + '" does not exist in ' + this._serialization.getSerializationString());
            }
        }
    },
 
    changeLabel: {
        value: function (oldLabel, newLabel) {
            var serialization = this._serialization.getSerializationObject(),
                object;
 
            object = serialization[oldLabel];
            delete serialization[oldLabel];
            serialization[newLabel] = object;
        }
    },
 
    _walkRootObjects: {
        value: function (visitor, objects) {
            for (var label in objects) {
                this._walkRootObject(visitor, objects, label);
            }
        }
    },
 
    _walkRootObject: {
        value: function (visitor, objects, label) {
            var object = objects[label];
 
            if ("value" in object) {
                this._walkObject(visitor, object, "value", label);
            } else {
                this._walkCustomObject(visitor, objects, label);
            }
        }
    },
 
    /**
     * @private
     * @param parentObject {Object} The parent object of the object to walk
     * @param key {string} The key of the object in the parent object
     * @param label {string} Optional label for when the object has no parent
     * @param parent {Object} The representation of the object's parent
     */
    _walkObject: {
        value: function (visitor, parentObject, key, label, parent) {
            var object = parentObject[key],
                type = MontageReviver.getTypeOf(object),
                value;
 
            // Create the value representing this object in the serialization.
            value = {
                type: type
            };
            if (label) {
                value.label = label;
            } else {
                value.name = key;
            }
            if (parent) {
                value.parent = parent;
            }
 
            // Visit the value
            if (type === "number" || type === "string" || type === "null") {
                value.data = object;
                visitor(value);
                parentObject[key] = value.data;
 
            } else if (type === "regexp") {
                value.data = object["/"];
                visitor(value);
                object["/"] = value.data;
 
            } else if (type === "reference") {
                value.data = object["@"];
                visitor(value);
                object["@"] = value.data;
 
            } else if (type === "Element") {
                value.data = object["#"];
                visitor(value);
                object["#"] = value.data;
 
            } else if (type === "array") {
                value.data = object;
                visitor(value);
                parentObject[key] = object = value.data;
 
                for (var i = 0, ii = object.length; i < ii; i++) {
                    this._walkObject(visitor, object, ""+i, null, value);
                }
 
            } else Eif (type === "object") {
                value.data = object;
                visitor(value);
                parentObject[key] = object = value.data;
 
                for (var key in object) {
                    this._walkObject(visitor, object, key, null, value);
                }
            }
 
            // Update the label if it was changed.
            if (value.label != label) {
                this.changeLabel(label, value.label);
            }
        }
    },
 
    _walkCustomObject: {
        value: function (visitor, objects, label) {
            var object = objects[label],
                value;
 
            value = {
                type: "montageObject",
                label: label,
                data: object
            };
 
            visitor(value);
            objects[label] = object = value.data;
            if (value.label != label) {
                this.changeLabel(label, value.label);
            }
 
            if (object.properties) {
                this._walkObject(visitor, object, "properties", null, value);
            }
            if (object.listeners) {
                this._walkObject(visitor, object, "listeners", null, value);
            }
            if (object.bindings) {
                this._walkBindings(visitor, object, null, value);
            }
            if (object.localizations) {
                this._walkLocalizations(visitor, object, null, value);
            }
        }
    },
 
    _walkBindings: {
        value: function (visitor, parentObject, parent) {
            var object = parentObject.bindings,
                value;
 
            value = {
                type: "bindings",
                data: object,
                parent: parent
            };
 
            visitor(value);
            parentObject.bindings = object = value.data;
 
            for (var key in object) {
                this._walkBinding(visitor, object, key, value);
            }
        }
    },
 
    _walkBinding: {
        value: function (visitor, parentObject, key, parent) {
            var object = parentObject[key],
                value;
 
            value = {
                type: "binding",
                name: key,
                data: object,
                parent: parent
            };
 
            visitor(value);
            parentObject[key] = object = value.data;
 
            this._walkBindingData(visitor, object, value);
        }
    },
 
    _walkBindingData: {
        value: function (visitor, object, parent) {
            var sourcePath,
                parseTree,
                modified = false;
 
            sourcePath = object["<-"] || object["<->"];
            parseTree = Object.clone(parse(sourcePath));
            this._walksBindingReferences(parseTree, function (syntax) {
                var value = {
                    type: "reference",
                    data: syntax.label
                };
                visitor(value);
                if (syntax.label !== value.data) {
                    syntax.label = value.data;
                    modified = true;
                }
            });
 
            if (modified) {
                Eif ("<-" in object) {
                    object["<-"] = stringify(parseTree);
                } else {
                    object["<->"] = stringify(parseTree);
                }
            }
 
            if (object.converter) {
                this._walkObject(visitor, object, "converter", null, parent);
            }
        }
    },
 
    _walkLocalizations: {
        value: function (visitor, parentObject, parent) {
            var object = parentObject.localizations,
                value;
 
            value = {
                type: "localizations",
                data: object,
                parent: parent
            };
 
            visitor(value);
            parentObject.localizations = object = value.data;
 
            for (var key in object) {
                this._walkLocalization(visitor, object, key, value);
            }
        }
    },
 
    _walkLocalization: {
        value: function (visitor, parentObject, key, parent) {
            var object = parentObject[key],
                value,
                data;
 
            value = {
                type: "localization",
                name: key,
                data: object,
                parent: parent
            };
 
            visitor(value);
            parentObject[key] = object = value.data;
 
            if (typeof object.key === "object") {
                this._walkBindingData(visitor, object.key, value);
            }
 
            Iif (typeof object.default === "object") {
                this._walkBindingData(visitor, object.default, value);
            }
 
            Iif (typeof object.data === "object") {
                data = object.data;
 
                for (var key in data) {
                    this._walkBindingData(visitor, data[key], value);
                }
            }
        }
    },
 
    /**
     * Visits all object references made in the binding parsing tree
     * @private
     */
    _walksBindingReferences: {
        value: function (parseTree, visitor) {
            var args = parseTree.args;
 
            if (parseTree.type === "component") {
                visitor(parseTree);
            }
 
            if (args) {
                for (var i = 0, ii = args.length; i < ii; i++) {
                    this._walksBindingReferences(args[i], visitor);
                }
            }
        }
    }
});
 
 
/**
 * @class SerializationExtractor
 * @extends Montage
 */
var SerializationExtractor = Montage.specialize( /** @lends SerializationExtractor.prototype # */ {
    /**
     * @private
     */
    _serialization: {value: null},
 
    /**
     * @function
     */
    initWithSerialization: {
        value: function (serialization) {
            this._serialization = serialization;
        }
    },
 
    /**
     * Creates a new serialization with the labels given.
     * @function
     */
    extractSerialization: {
        value: function (labels, externalLabels) {
            var inspector = new SerializationInspector(),
                serializationObject,
                objects = {},
                references = [];
 
            serializationObject = this._serialization.getSerializationObject();
            inspector.initWithSerialization(this._serialization);
 
            for (var i = 0, label; (label = labels[i]); i++) {
                objects[label] = serializationObject[label];
 
                inspector.visitSerializationObject(label, function (node) {
                    var label;
 
                    if (node.type === "reference") {
                        label = node.data;
                        // We don't process template properties here, meaning
                        // that if we have "table" and a reference like
                        // "@table:cell" the latter will be considered an
                        // external reference even though the component is in
                        // scope.
                        // We do this on purpose because it allow us to process
                        // all template properties of the serialization without
                        // having to walk the entire serialization tree looking
                        // for them.
                        // If for some reason we need to "correct" this behavior
                        // then we also need to change the way we resolve
                        // template properties' alias in
                        // Template.expandParameters.
                        // Instead of relying on willMergeObjectWithLabel we
                        // need to walk the serialization looking for these.
                        if (references.indexOf(label) === -1 &&
                            labels.indexOf(label) === -1) {
                            references.push(label);
                        }
                    }
                });
            }
 
            if (externalLabels) {
                for (var i = 0, label; (label = externalLabels[i]); i++) {
                    // Make sure we don't add objects that are not part of the
                    // serialization we're extracting from.
                    // If the same label is defined in both labels and
                    // externalLabels then labels takes precedence.
                    if (label in serializationObject && !(label in objects)) {
                        objects[label] = {};
                    }
                }
            }
 
            for (var i = 0, label; (label = references[i]); i++) {
                objects[label] = {};
            }
 
            return new Serialization().initWithObject(objects);
        }
    }
});
 
exports.Serialization = Serialization;
exports.SerializationMerger = SerializationMerger;
exports.SerializationInspector = SerializationInspector;
exports.SerializationExtractor = SerializationExtractor;