2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.uuid._base"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.uuid._base"] = true;
|
|
|
3 |
dojo.provide("dojox.uuid._base");
|
|
|
4 |
|
|
|
5 |
// Public constants:
|
|
|
6 |
dojox.uuid.NIL_UUID = "00000000-0000-0000-0000-000000000000";
|
|
|
7 |
dojox.uuid.version = {
|
|
|
8 |
// Enumeration for the different UUID versions.
|
|
|
9 |
UNKNOWN: 0,
|
|
|
10 |
TIME_BASED: 1,
|
|
|
11 |
DCE_SECURITY: 2,
|
|
|
12 |
NAME_BASED_MD5: 3,
|
|
|
13 |
RANDOM: 4,
|
|
|
14 |
NAME_BASED_SHA1: 5 };
|
|
|
15 |
dojox.uuid.variant = {
|
|
|
16 |
// Enumeration for the different UUID variants.
|
|
|
17 |
NCS: "0",
|
|
|
18 |
DCE: "10",
|
|
|
19 |
MICROSOFT: "110",
|
|
|
20 |
UNKNOWN: "111" };
|
|
|
21 |
|
|
|
22 |
dojox.uuid.assert = function(/*Boolean*/ booleanValue, /*String?*/ message){
|
|
|
23 |
// summary:
|
|
|
24 |
// Throws an exception if the assertion fails.
|
|
|
25 |
// description:
|
|
|
26 |
// If the asserted condition is true, this method does nothing. If the
|
|
|
27 |
// condition is false, we throw an error with a error message.
|
|
|
28 |
// booleanValue: Must be true for the assertion to succeed.
|
|
|
29 |
// message: A string describing the assertion.
|
|
|
30 |
// throws: Throws an Error if 'booleanValue' is false.
|
|
|
31 |
if(!booleanValue){
|
|
|
32 |
if(!message){
|
|
|
33 |
message = "An assert statement failed.\n" +
|
|
|
34 |
"The method dojox.uuid.assert() was called with a 'false' value.\n";
|
|
|
35 |
}
|
|
|
36 |
throw new Error(message);
|
|
|
37 |
}
|
|
|
38 |
};
|
|
|
39 |
|
|
|
40 |
dojox.uuid.generateNilUuid = function(){
|
|
|
41 |
// summary:
|
|
|
42 |
// This function returns the Nil UUID: "00000000-0000-0000-0000-000000000000".
|
|
|
43 |
// description:
|
|
|
44 |
// The Nil UUID is described in section 4.1.7 of
|
|
|
45 |
// RFC 4122: http://tools.ietf.org/html/rfc4122#section-4.1.7
|
|
|
46 |
// examples:
|
|
|
47 |
// var string = dojox.uuid.generateNilUuid();
|
|
|
48 |
return dojox.uuid.NIL_UUID;; // String
|
|
|
49 |
};
|
|
|
50 |
|
|
|
51 |
dojox.uuid.isValid = function(/*String*/ uuidString){
|
|
|
52 |
// summary:
|
|
|
53 |
// Returns true if the UUID was initialized with a valid value.
|
|
|
54 |
uuidString = uuidString.toString();
|
|
|
55 |
var valid = (dojo.isString(uuidString) &&
|
|
|
56 |
(uuidString.length == 36) &&
|
|
|
57 |
(uuidString == uuidString.toLowerCase()));
|
|
|
58 |
if(valid){
|
|
|
59 |
var arrayOfParts = uuidString.split("-");
|
|
|
60 |
valid = ((arrayOfParts.length == 5) &&
|
|
|
61 |
(arrayOfParts[0].length == 8) &&
|
|
|
62 |
(arrayOfParts[1].length == 4) &&
|
|
|
63 |
(arrayOfParts[2].length == 4) &&
|
|
|
64 |
(arrayOfParts[3].length == 4) &&
|
|
|
65 |
(arrayOfParts[4].length == 12));
|
|
|
66 |
var HEX_RADIX = 16;
|
|
|
67 |
for (var i in arrayOfParts) {
|
|
|
68 |
var part = arrayOfParts[i];
|
|
|
69 |
var integer = parseInt(part, HEX_RADIX);
|
|
|
70 |
valid = valid && isFinite(integer);
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
return valid; // boolean
|
|
|
74 |
};
|
|
|
75 |
|
|
|
76 |
dojox.uuid.getVariant = function(/*String*/ uuidString){
|
|
|
77 |
// summary:
|
|
|
78 |
// Returns a variant code that indicates what type of UUID this is.
|
|
|
79 |
// Returns one of the enumerated dojox.uuid.variant values.
|
|
|
80 |
// example:
|
|
|
81 |
// var variant = dojox.uuid.getVariant("3b12f1df-5232-4804-897e-917bf397618a");
|
|
|
82 |
// dojox.uuid.assert(variant == dojox.uuid.variant.DCE);
|
|
|
83 |
// example:
|
|
|
84 |
// "3b12f1df-5232-4804-897e-917bf397618a"
|
|
|
85 |
// ^
|
|
|
86 |
// |
|
|
|
87 |
// (variant "10__" == DCE)
|
|
|
88 |
if(!dojox.uuid._ourVariantLookupTable){
|
|
|
89 |
var variant = dojox.uuid.variant;
|
|
|
90 |
var lookupTable = [];
|
|
|
91 |
|
|
|
92 |
lookupTable[0x0] = variant.NCS; // 0000
|
|
|
93 |
lookupTable[0x1] = variant.NCS; // 0001
|
|
|
94 |
lookupTable[0x2] = variant.NCS; // 0010
|
|
|
95 |
lookupTable[0x3] = variant.NCS; // 0011
|
|
|
96 |
|
|
|
97 |
lookupTable[0x4] = variant.NCS; // 0100
|
|
|
98 |
lookupTable[0x5] = variant.NCS; // 0101
|
|
|
99 |
lookupTable[0x6] = variant.NCS; // 0110
|
|
|
100 |
lookupTable[0x7] = variant.NCS; // 0111
|
|
|
101 |
|
|
|
102 |
lookupTable[0x8] = variant.DCE; // 1000
|
|
|
103 |
lookupTable[0x9] = variant.DCE; // 1001
|
|
|
104 |
lookupTable[0xA] = variant.DCE; // 1010
|
|
|
105 |
lookupTable[0xB] = variant.DCE; // 1011
|
|
|
106 |
|
|
|
107 |
lookupTable[0xC] = variant.MICROSOFT; // 1100
|
|
|
108 |
lookupTable[0xD] = variant.MICROSOFT; // 1101
|
|
|
109 |
lookupTable[0xE] = variant.UNKNOWN; // 1110
|
|
|
110 |
lookupTable[0xF] = variant.UNKNOWN; // 1111
|
|
|
111 |
|
|
|
112 |
dojox.uuid._ourVariantLookupTable = lookupTable;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
uuidString = uuidString.toString();
|
|
|
116 |
var variantCharacter = uuidString.charAt(19);
|
|
|
117 |
var HEX_RADIX = 16;
|
|
|
118 |
var variantNumber = parseInt(variantCharacter, HEX_RADIX);
|
|
|
119 |
dojox.uuid.assert((variantNumber >= 0) && (variantNumber <= 16));
|
|
|
120 |
return dojox.uuid._ourVariantLookupTable[variantNumber]; // dojox.uuid.variant
|
|
|
121 |
};
|
|
|
122 |
|
|
|
123 |
dojox.uuid.getVersion = function(/*String*/ uuidString){
|
|
|
124 |
// summary:
|
|
|
125 |
// Returns a version number that indicates what type of UUID this is.
|
|
|
126 |
// Returns one of the enumerated dojox.uuid.version values.
|
|
|
127 |
// example:
|
|
|
128 |
// var version = dojox.uuid.getVersion("b4308fb0-86cd-11da-a72b-0800200c9a66");
|
|
|
129 |
// dojox.uuid.assert(version == dojox.uuid.version.TIME_BASED);
|
|
|
130 |
// exceptions:
|
|
|
131 |
// Throws an Error if this is not a DCE Variant UUID.
|
|
|
132 |
var errorMessage = "dojox.uuid.getVersion() was not passed a DCE Variant UUID.";
|
|
|
133 |
dojox.uuid.assert(dojox.uuid.getVariant(uuidString) == dojox.uuid.variant.DCE, errorMessage);
|
|
|
134 |
uuidString = uuidString.toString();
|
|
|
135 |
|
|
|
136 |
// "b4308fb0-86cd-11da-a72b-0800200c9a66"
|
|
|
137 |
// ^
|
|
|
138 |
// |
|
|
|
139 |
// (version 1 == TIME_BASED)
|
|
|
140 |
var versionCharacter = uuidString.charAt(14);
|
|
|
141 |
var HEX_RADIX = 16;
|
|
|
142 |
var versionNumber = parseInt(versionCharacter, HEX_RADIX);
|
|
|
143 |
return versionNumber; // dojox.uuid.version
|
|
|
144 |
};
|
|
|
145 |
|
|
|
146 |
dojox.uuid.getNode = function(/*String*/ uuidString){
|
|
|
147 |
// summary:
|
|
|
148 |
// If this is a version 1 UUID (a time-based UUID), getNode() returns a
|
|
|
149 |
// 12-character string with the "node" or "pseudonode" portion of the UUID,
|
|
|
150 |
// which is the rightmost 12 characters.
|
|
|
151 |
// exceptions:
|
|
|
152 |
// Throws an Error if this is not a version 1 UUID.
|
|
|
153 |
var errorMessage = "dojox.uuid.getNode() was not passed a TIME_BASED UUID.";
|
|
|
154 |
dojox.uuid.assert(dojox.uuid.getVersion(uuidString) == dojox.uuid.version.TIME_BASED, errorMessage);
|
|
|
155 |
|
|
|
156 |
uuidString = uuidString.toString();
|
|
|
157 |
var arrayOfStrings = uuidString.split('-');
|
|
|
158 |
var nodeString = arrayOfStrings[4];
|
|
|
159 |
return nodeString; // String (a 12-character string, which will look something like "917bf397618a")
|
|
|
160 |
};
|
|
|
161 |
|
|
|
162 |
dojox.uuid.getTimestamp = function(/*String*/ uuidString, /*String?*/ returnType){
|
|
|
163 |
// summary:
|
|
|
164 |
// If this is a version 1 UUID (a time-based UUID), this method returns
|
|
|
165 |
// the timestamp value encoded in the UUID. The caller can ask for the
|
|
|
166 |
// timestamp to be returned either as a JavaScript Date object or as a
|
|
|
167 |
// 15-character string of hex digits.
|
|
|
168 |
// returnType: Any of these five values: "string", String, "hex", "date", Date
|
|
|
169 |
// returns:
|
|
|
170 |
// Returns the timestamp value as a JavaScript Date object or a 15-character string of hex digits.
|
|
|
171 |
// examples:
|
|
|
172 |
// var uuidString = "b4308fb0-86cd-11da-a72b-0800200c9a66";
|
|
|
173 |
// var date, string, hexString;
|
|
|
174 |
// date = dojox.uuid.getTimestamp(uuidString); // returns a JavaScript Date
|
|
|
175 |
// date = dojox.uuid.getTimestamp(uuidString, Date); //
|
|
|
176 |
// string = dojox.uuid.getTimestamp(uuidString, String); // "Mon, 16 Jan 2006 20:21:41 GMT"
|
|
|
177 |
// hexString = dojox.uuid.getTimestamp(uuidString, "hex"); // "1da86cdb4308fb0"
|
|
|
178 |
// exceptions:
|
|
|
179 |
// Throws an Error if this is not a version 1 UUID.
|
|
|
180 |
var errorMessage = "dojox.uuid.getTimestamp() was not passed a TIME_BASED UUID.";
|
|
|
181 |
dojox.uuid.assert(dojox.uuid.getVersion(uuidString) == dojox.uuid.version.TIME_BASED, errorMessage);
|
|
|
182 |
|
|
|
183 |
uuidString = uuidString.toString();
|
|
|
184 |
if(!returnType){returnType = null};
|
|
|
185 |
switch(returnType){
|
|
|
186 |
case "string":
|
|
|
187 |
case String:
|
|
|
188 |
return dojox.uuid.getTimestamp(uuidString, Date).toUTCString(); // String (e.g. "Mon, 16 Jan 2006 20:21:41 GMT")
|
|
|
189 |
break;
|
|
|
190 |
case "hex":
|
|
|
191 |
// Return a 15-character string of hex digits containing the
|
|
|
192 |
// timestamp for this UUID, with the high-order bits first.
|
|
|
193 |
var arrayOfStrings = uuidString.split('-');
|
|
|
194 |
var hexTimeLow = arrayOfStrings[0];
|
|
|
195 |
var hexTimeMid = arrayOfStrings[1];
|
|
|
196 |
var hexTimeHigh = arrayOfStrings[2];
|
|
|
197 |
|
|
|
198 |
// Chop off the leading "1" character, which is the UUID
|
|
|
199 |
// version number for time-based UUIDs.
|
|
|
200 |
hexTimeHigh = hexTimeHigh.slice(1);
|
|
|
201 |
|
|
|
202 |
var timestampAsHexString = hexTimeHigh + hexTimeMid + hexTimeLow;
|
|
|
203 |
dojox.uuid.assert(timestampAsHexString.length == 15);
|
|
|
204 |
return timestampAsHexString; // String (e.g. "1da86cdb4308fb0")
|
|
|
205 |
break;
|
|
|
206 |
case null: // no returnType was specified, so default to Date
|
|
|
207 |
case "date":
|
|
|
208 |
case Date:
|
|
|
209 |
// Return a JavaScript Date object.
|
|
|
210 |
var GREGORIAN_CHANGE_OFFSET_IN_HOURS = 3394248;
|
|
|
211 |
var HEX_RADIX = 16;
|
|
|
212 |
|
|
|
213 |
var arrayOfParts = uuidString.split('-');
|
|
|
214 |
var timeLow = parseInt(arrayOfParts[0], HEX_RADIX);
|
|
|
215 |
var timeMid = parseInt(arrayOfParts[1], HEX_RADIX);
|
|
|
216 |
var timeHigh = parseInt(arrayOfParts[2], HEX_RADIX);
|
|
|
217 |
var hundredNanosecondIntervalsSince1582 = timeHigh & 0x0FFF;
|
|
|
218 |
hundredNanosecondIntervalsSince1582 <<= 16;
|
|
|
219 |
hundredNanosecondIntervalsSince1582 += timeMid;
|
|
|
220 |
// What we really want to do next is shift left 32 bits, but the
|
|
|
221 |
// result will be too big to fit in an int, so we'll multiply by 2^32,
|
|
|
222 |
// and the result will be a floating point approximation.
|
|
|
223 |
hundredNanosecondIntervalsSince1582 *= 0x100000000;
|
|
|
224 |
hundredNanosecondIntervalsSince1582 += timeLow;
|
|
|
225 |
var millisecondsSince1582 = hundredNanosecondIntervalsSince1582 / 10000;
|
|
|
226 |
|
|
|
227 |
// Again, this will be a floating point approximation.
|
|
|
228 |
// We can make things exact later if we need to.
|
|
|
229 |
var secondsPerHour = 60 * 60;
|
|
|
230 |
var hoursBetween1582and1970 = GREGORIAN_CHANGE_OFFSET_IN_HOURS;
|
|
|
231 |
var secondsBetween1582and1970 = hoursBetween1582and1970 * secondsPerHour;
|
|
|
232 |
var millisecondsBetween1582and1970 = secondsBetween1582and1970 * 1000;
|
|
|
233 |
var millisecondsSince1970 = millisecondsSince1582 - millisecondsBetween1582and1970;
|
|
|
234 |
|
|
|
235 |
var timestampAsDate = new Date(millisecondsSince1970);
|
|
|
236 |
return timestampAsDate; // Date
|
|
|
237 |
break;
|
|
|
238 |
default:
|
|
|
239 |
// we got passed something other than a valid returnType
|
|
|
240 |
dojox.uuid.assert(false, "dojox.uuid.getTimestamp was not passed a valid returnType: " + returnType);
|
|
|
241 |
break;
|
|
|
242 |
}
|
|
|
243 |
};
|
|
|
244 |
|
|
|
245 |
}
|