Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.uuid.Uuid");
12
dojo.require("dojo.lang.common");
13
dojo.require("dojo.lang.assert");
14
dojo.uuid.Uuid = function (input) {
15
	this._uuidString = dojo.uuid.Uuid.NIL_UUID;
16
	if (input) {
17
		if (dojo.lang.isString(input)) {
18
			this._uuidString = input.toLowerCase();
19
			dojo.lang.assert(this.isValid());
20
		} else {
21
			if (dojo.lang.isObject(input) && input.generate) {
22
				var generator = input;
23
				this._uuidString = generator.generate();
24
				dojo.lang.assert(this.isValid());
25
			} else {
26
				dojo.lang.assert(false, "The dojo.uuid.Uuid() constructor must be initializated with a UUID string.");
27
			}
28
		}
29
	} else {
30
		var ourGenerator = dojo.uuid.Uuid.getGenerator();
31
		if (ourGenerator) {
32
			this._uuidString = ourGenerator.generate();
33
			dojo.lang.assert(this.isValid());
34
		}
35
	}
36
};
37
dojo.uuid.Uuid.NIL_UUID = "00000000-0000-0000-0000-000000000000";
38
dojo.uuid.Uuid.Version = {UNKNOWN:0, TIME_BASED:1, DCE_SECURITY:2, NAME_BASED_MD5:3, RANDOM:4, NAME_BASED_SHA1:5};
39
dojo.uuid.Uuid.Variant = {NCS:"0", DCE:"10", MICROSOFT:"110", UNKNOWN:"111"};
40
dojo.uuid.Uuid.HEX_RADIX = 16;
41
dojo.uuid.Uuid.compare = function (uuidOne, uuidTwo) {
42
	var uuidStringOne = uuidOne.toString();
43
	var uuidStringTwo = uuidTwo.toString();
44
	if (uuidStringOne > uuidStringTwo) {
45
		return 1;
46
	}
47
	if (uuidStringOne < uuidStringTwo) {
48
		return -1;
49
	}
50
	return 0;
51
};
52
dojo.uuid.Uuid.setGenerator = function (generator) {
53
	dojo.lang.assert(!generator || (dojo.lang.isObject(generator) && generator.generate));
54
	dojo.uuid.Uuid._ourGenerator = generator;
55
};
56
dojo.uuid.Uuid.getGenerator = function () {
57
	return dojo.uuid.Uuid._ourGenerator;
58
};
59
dojo.uuid.Uuid.prototype.toString = function (format) {
60
	if (format) {
61
		switch (format) {
62
		  case "{}":
63
			return "{" + this._uuidString + "}";
64
			break;
65
		  case "()":
66
			return "(" + this._uuidString + ")";
67
			break;
68
		  case "\"\"":
69
			return "\"" + this._uuidString + "\"";
70
			break;
71
		  case "''":
72
			return "'" + this._uuidString + "'";
73
			break;
74
		  case "urn":
75
			return "urn:uuid:" + this._uuidString;
76
			break;
77
		  case "!-":
78
			return this._uuidString.split("-").join("");
79
			break;
80
		  default:
81
			dojo.lang.assert(false, "The toString() method of dojo.uuid.Uuid was passed a bogus format.");
82
		}
83
	} else {
84
		return this._uuidString;
85
	}
86
};
87
dojo.uuid.Uuid.prototype.compare = function (otherUuid) {
88
	return dojo.uuid.Uuid.compare(this, otherUuid);
89
};
90
dojo.uuid.Uuid.prototype.isEqual = function (otherUuid) {
91
	return (this.compare(otherUuid) == 0);
92
};
93
dojo.uuid.Uuid.prototype.isValid = function () {
94
	try {
95
		dojo.lang.assertType(this._uuidString, String);
96
		dojo.lang.assert(this._uuidString.length == 36);
97
		dojo.lang.assert(this._uuidString == this._uuidString.toLowerCase());
98
		var arrayOfParts = this._uuidString.split("-");
99
		dojo.lang.assert(arrayOfParts.length == 5);
100
		dojo.lang.assert(arrayOfParts[0].length == 8);
101
		dojo.lang.assert(arrayOfParts[1].length == 4);
102
		dojo.lang.assert(arrayOfParts[2].length == 4);
103
		dojo.lang.assert(arrayOfParts[3].length == 4);
104
		dojo.lang.assert(arrayOfParts[4].length == 12);
105
		for (var i in arrayOfParts) {
106
			var part = arrayOfParts[i];
107
			var integer = parseInt(part, dojo.uuid.Uuid.HEX_RADIX);
108
			dojo.lang.assert(isFinite(integer));
109
		}
110
		return true;
111
	}
112
	catch (e) {
113
		return false;
114
	}
115
};
116
dojo.uuid.Uuid.prototype.getVariant = function () {
117
	var variantCharacter = this._uuidString.charAt(19);
118
	var variantNumber = parseInt(variantCharacter, dojo.uuid.Uuid.HEX_RADIX);
119
	dojo.lang.assert((variantNumber >= 0) && (variantNumber <= 16));
120
	if (!dojo.uuid.Uuid._ourVariantLookupTable) {
121
		var Variant = dojo.uuid.Uuid.Variant;
122
		var lookupTable = [];
123
		lookupTable[0] = Variant.NCS;
124
		lookupTable[1] = Variant.NCS;
125
		lookupTable[2] = Variant.NCS;
126
		lookupTable[3] = Variant.NCS;
127
		lookupTable[4] = Variant.NCS;
128
		lookupTable[5] = Variant.NCS;
129
		lookupTable[6] = Variant.NCS;
130
		lookupTable[7] = Variant.NCS;
131
		lookupTable[8] = Variant.DCE;
132
		lookupTable[9] = Variant.DCE;
133
		lookupTable[10] = Variant.DCE;
134
		lookupTable[11] = Variant.DCE;
135
		lookupTable[12] = Variant.MICROSOFT;
136
		lookupTable[13] = Variant.MICROSOFT;
137
		lookupTable[14] = Variant.UNKNOWN;
138
		lookupTable[15] = Variant.UNKNOWN;
139
		dojo.uuid.Uuid._ourVariantLookupTable = lookupTable;
140
	}
141
	return dojo.uuid.Uuid._ourVariantLookupTable[variantNumber];
142
};
143
dojo.uuid.Uuid.prototype.getVersion = function () {
144
	if (!this._versionNumber) {
145
		var errorMessage = "Called getVersion() on a dojo.uuid.Uuid that was not a DCE Variant UUID.";
146
		dojo.lang.assert(this.getVariant() == dojo.uuid.Uuid.Variant.DCE, errorMessage);
147
		var versionCharacter = this._uuidString.charAt(14);
148
		this._versionNumber = parseInt(versionCharacter, dojo.uuid.Uuid.HEX_RADIX);
149
	}
150
	return this._versionNumber;
151
};
152
dojo.uuid.Uuid.prototype.getNode = function () {
153
	if (!this._nodeString) {
154
		var errorMessage = "Called getNode() on a dojo.uuid.Uuid that was not a TIME_BASED UUID.";
155
		dojo.lang.assert(this.getVersion() == dojo.uuid.Uuid.Version.TIME_BASED, errorMessage);
156
		var arrayOfStrings = this._uuidString.split("-");
157
		this._nodeString = arrayOfStrings[4];
158
	}
159
	return this._nodeString;
160
};
161
dojo.uuid.Uuid.prototype.getTimestamp = function (returnType) {
162
	var errorMessage = "Called getTimestamp() on a dojo.uuid.Uuid that was not a TIME_BASED UUID.";
163
	dojo.lang.assert(this.getVersion() == dojo.uuid.Uuid.Version.TIME_BASED, errorMessage);
164
	if (!returnType) {
165
		returnType = null;
166
	}
167
	switch (returnType) {
168
	  case "string":
169
	  case String:
170
		return this.getTimestamp(Date).toUTCString();
171
		break;
172
	  case "hex":
173
		if (!this._timestampAsHexString) {
174
			var arrayOfStrings = this._uuidString.split("-");
175
			var hexTimeLow = arrayOfStrings[0];
176
			var hexTimeMid = arrayOfStrings[1];
177
			var hexTimeHigh = arrayOfStrings[2];
178
			hexTimeHigh = hexTimeHigh.slice(1);
179
			this._timestampAsHexString = hexTimeHigh + hexTimeMid + hexTimeLow;
180
			dojo.lang.assert(this._timestampAsHexString.length == 15);
181
		}
182
		return this._timestampAsHexString;
183
		break;
184
	  case null:
185
	  case "date":
186
	  case Date:
187
		if (!this._timestampAsDate) {
188
			var GREGORIAN_CHANGE_OFFSET_IN_HOURS = 3394248;
189
			var arrayOfParts = this._uuidString.split("-");
190
			var timeLow = parseInt(arrayOfParts[0], dojo.uuid.Uuid.HEX_RADIX);
191
			var timeMid = parseInt(arrayOfParts[1], dojo.uuid.Uuid.HEX_RADIX);
192
			var timeHigh = parseInt(arrayOfParts[2], dojo.uuid.Uuid.HEX_RADIX);
193
			var hundredNanosecondIntervalsSince1582 = timeHigh & 4095;
194
			hundredNanosecondIntervalsSince1582 <<= 16;
195
			hundredNanosecondIntervalsSince1582 += timeMid;
196
			hundredNanosecondIntervalsSince1582 *= 4294967296;
197
			hundredNanosecondIntervalsSince1582 += timeLow;
198
			var millisecondsSince1582 = hundredNanosecondIntervalsSince1582 / 10000;
199
			var secondsPerHour = 60 * 60;
200
			var hoursBetween1582and1970 = GREGORIAN_CHANGE_OFFSET_IN_HOURS;
201
			var secondsBetween1582and1970 = hoursBetween1582and1970 * secondsPerHour;
202
			var millisecondsBetween1582and1970 = secondsBetween1582and1970 * 1000;
203
			var millisecondsSince1970 = millisecondsSince1582 - millisecondsBetween1582and1970;
204
			this._timestampAsDate = new Date(millisecondsSince1970);
205
		}
206
		return this._timestampAsDate;
207
		break;
208
	  default:
209
		dojo.lang.assert(false, "The getTimestamp() method dojo.uuid.Uuid was passed a bogus returnType: " + returnType);
210
		break;
211
	}
212
};
213