blob: 48149450f4e2a233c1009ecc070ed158442b49aa [file] [log] [blame]
Simon Kaegi87360e22010-08-18 20:50:00 +00001orion.Import = function(header) {
Simon Kaegifd60a1e2010-08-09 05:56:11 +00002 if (header === null)
3 throw "header cannot be null";
4
5 this._name = null;
Simon Kaegid043f1c2010-08-16 21:08:30 +00006 this._versionRange = orion.VersionRange.EMPTY_RANGE;
Simon Kaegi96e69922010-08-31 19:41:17 +00007 this._bundleName = null;
Simon Kaegid043f1c2010-08-16 21:08:30 +00008 this._bundleVersionRange = orion.VersionRange.EMPTY_RANGE;
Simon Kaegifd60a1e2010-08-09 05:56:11 +00009 this._optional = false;
10 this._attributes = {};
Simon Kaegifd60a1e2010-08-09 05:56:11 +000011 this._wiredExport = null;
12
13 this._parseImport(header);
Simon Kaegid043f1c2010-08-16 21:08:30 +000014};
Simon Kaegifd60a1e2010-08-09 05:56:11 +000015
Simon Kaegi87360e22010-08-18 20:50:00 +000016orion.Import.prototype = {
Simon Kaegifd60a1e2010-08-09 05:56:11 +000017 _parseImport : function(header) {
Simon Kaegi96e69922010-08-31 19:41:17 +000018 if (typeof header == "string") {
19 this._name = header;
20 return;
Simon Kaegifd60a1e2010-08-09 05:56:11 +000021 }
Simon Kaegi96e69922010-08-31 19:41:17 +000022
23 this._name = header.name;
24 if (header.version)
25 this._versionRange = orion.VersionRange.parseVersionRange(header.version);
26
27 if (header.bundleName)
28 this._bundleName = orion.VersionRange.parseVersionRange(header.bundleName);
29
30 if (header.bundleVersion)
31 this._bundleVersionRange = orion.VersionRange.parseVersionRange(header.bundleVersion);
32
33 if (header.resolution)
34 this._optional = (header.resolution === "optional");
35
36 var attributeName;
37 for (attributeName in header) {
38 if (header.hasOwnProperty(attributeName))
39 this._attributes[attributeName] = header[attributeName];
40 }
Simon Kaegifd60a1e2010-08-09 05:56:11 +000041 },
42 getName : function() {
43 return this._name;
44 },
45 getVersionRange : function() {
46 return this._versionRange;
47 },
Simon Kaegi96e69922010-08-31 19:41:17 +000048 getBundleName : function() {
49 return this._bundleName;
Simon Kaegifd60a1e2010-08-09 05:56:11 +000050 },
51 getBundleVersionRange : function() {
52 return this._bundleVersionRange;
53 },
54 isOptional : function() {
55 return this._optional;
56 },
57 getAttributes : function() {
58 this._attributes;
59 },
Simon Kaegifd60a1e2010-08-09 05:56:11 +000060 wire : function(candidate) {
61 if (this._name !== candidate.getName())
62 return false;
63
64 if (!this._checkAttributes(candidate))
65 return false;
66
67 if (!this._checkMandatory(candidate))
68 return false;
69
70 this._wiredExport = candidate;
71 return true;
72 },
73 _checkAttributes : function(candidate) {
74 for ( var key in this._attributes) {
Simon Kaegi96e69922010-08-31 19:41:17 +000075 if (key === "version") {
Simon Kaegifd60a1e2010-08-09 05:56:11 +000076 if (!this._versionRange.isIncluded(candidate.getVersion()))
77 return false;
Simon Kaegi96e69922010-08-31 19:41:17 +000078 } else if (key === "bundleName") {
79 if (this._bundleName !== candidate.getBundleName())
Simon Kaegifd60a1e2010-08-09 05:56:11 +000080 return false;
Simon Kaegi96e69922010-08-31 19:41:17 +000081 } else if (key === "bundleVersion") {
Simon Kaegifd60a1e2010-08-09 05:56:11 +000082 if (!this._bundleVersionRange.isIncluded(candidate.getBundleVersion()))
83 return false;
84 } else {
85 var value = this._attributes[key];
86 var attributeValue = candidate.getAttributes()[key];
87 if (attributeValue !== value)
88 return false;
89 }
90 }
91 return true;
92 },
93 _checkMandatory : function(candidate) {
94 for ( var i = 0; i < candidate.getMandatory().length; i++) {
95 var key = candidate.getMandatory()[i];
96 var mandatoryValue = candidate.getAttributes()[key];
97 var value = this._attributes[key];
98 if (value !== mandatoryValue)
99 return false;
100 }
101 return true;
102 },
103 getWiredExport : function() {
104 return this._wiredExport;
105 },
106 unwire : function() {
107 this._wiredExport = null;
108 }
109};