78bb5fc22e3b4ca899019b080d1191d74436be33.svn-base 9.9 KB

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