home *** CD-ROM | disk | FTP | other *** search
/ Minami 81 / MINAMI81.ISO / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / wireformat.js < prev    next >
Text File  |  2006-05-15  |  4KB  |  115 lines

  1. function PROT_VersionParser(type, opt_major, opt_minor, opt_requireMac) {
  2. this.debugZone = "versionparser";
  3. this.type = type;
  4. this.major = 0;
  5. this.minor = 0;
  6. this.badHeader = false;
  7. this.mac = false;
  8. this.macval = "";
  9. this.macFailed = false;
  10. this.requireMac = !!opt_requireMac;
  11. this.update = false;
  12. this.needsUpdate = false;  // used by ListManager to determine update policy
  13. this.didRead = false;
  14. if (opt_major)
  15. this.major = parseInt(opt_major);
  16. if (opt_minor)
  17. this.minor = parseInt(opt_minor);
  18. }
  19. PROT_VersionParser.prototype.ImportVersion = function(version) {
  20. this.major = version.major;
  21. this.minor = version.minor;
  22. this.mac = version.mac;
  23. this.macFailed = version.macFailed;
  24. this.macval = version.macval;
  25. }
  26. PROT_VersionParser.prototype.toString = function() {
  27. var s = "[" + this.type + " " + this.major + "." + this.minor + "]";
  28. return s;
  29. }
  30. PROT_VersionParser.prototype.toUrl = function() {
  31. return this.major + ":" + this.minor;
  32. }
  33. PROT_VersionParser.prototype.processOldFormat_ = function(line) {
  34. if (line[0] != '[' || line.slice(-1) != ']')
  35. return false;
  36. var description = line.slice(1, -1);
  37. var tokens = description.split(" ");
  38. this.type = tokens[0];
  39. var majorminor = tokens[1].split(".");
  40. this.major = parseInt(majorminor[0]);
  41. this.minor = parseInt(majorminor[1]);
  42. if (isNaN(this.major) || isNaN(this.minor))
  43. return false;
  44. if (tokens.length >= 3) {
  45. this.update = tokens[2] == "update";
  46. }
  47. return true;
  48. }
  49. PROT_VersionParser.prototype.fromString = function(line) {
  50. G_Debug(this, "Calling fromString with line: " + line);
  51. if (line[0] != '[' || line.slice(-1) != ']')
  52. return false;
  53. var secondBracket = line.indexOf('[', 1);
  54. var firstPart = null;
  55. var secondPart = null;
  56. if (secondBracket != -1) {
  57. firstPart = line.substring(0, secondBracket);
  58. secondPart = line.substring(secondBracket);
  59. G_Debug(this, "First part: " + firstPart + " Second part: " + secondPart);
  60. } else {
  61. firstPart = line;
  62. G_Debug(this, "Old format: " + firstPart);
  63. }
  64. if (!this.processOldFormat_(firstPart))
  65. return false;
  66. if (secondPart && !this.processOptTokens_(secondPart))
  67. return false;
  68. return true;
  69. }
  70. PROT_VersionParser.prototype.processOptTokens_ = function(line) {
  71. if (line[0] != '[' || line.slice(-1) != ']')
  72. return false;
  73. var description = line.slice(1, -1);
  74. var tokens = description.split(" ");
  75. for (var i = 0; i < tokens.length; i++) {
  76. G_Debug(this, "Processing optional token: " + tokens[i]);
  77. var tokenparts = tokens[i].split("=");
  78. switch(tokenparts[0]){
  79. case "mac":
  80. this.mac = true;
  81. if (tokenparts.length < 2) {
  82. G_Debug(this, "Found mac flag but not mac value!");
  83. return false;
  84. }
  85. this.macval = tokens[i].substr(tokens[i].indexOf("=")+1);
  86. break;
  87. default:
  88. G_Debug(this, "Found unrecognized token: " + tokenparts[0]);
  89. break;
  90. }
  91. }
  92. return true;
  93. }
  94. function TEST_PROT_WireFormat() {
  95. if (G_GDEBUG) {
  96. var z = "versionparser UNITTEST";
  97. G_Debug(z, "Starting");
  98. var vp = new PROT_VersionParser("dummy");
  99. G_Assert(z, vp.fromString("[foo-bar-url 1.234]"),
  100. "failed to parse old format");
  101. G_Assert(z, "foo-bar-url" == vp.type, "failed to parse type");
  102. G_Assert(z, "1" == vp.major, "failed to parse major");
  103. G_Assert(z, "234" == vp.minor, "failed to parse minor");
  104. vp = new PROT_VersionParser("dummy");
  105. G_Assert(z, vp.fromString("[foo-bar-url 1.234][mac=567]"),
  106. "failed to parse new format");
  107. G_Assert(z, "foo-bar-url" == vp.type, "failed to parse type");
  108. G_Assert(z, "1" == vp.major, "failed to parse major");
  109. G_Assert(z, "234" == vp.minor, "failed to parse minor");
  110. G_Assert(z, true == vp.mac, "failed to parse mac");
  111. G_Assert(z, "567" == vp.macval, "failed to parse macval");
  112. G_Debug(z, "PASSED");
  113. }
  114. }
  115.