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