home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Shared / MM / Scripts / Class / FileClass.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  11.0 KB  |  385 lines

  1. // Copyright 1999 Macromedia, Inc. All rights reserved.
  2.  
  3. /* ----------------------------------------------------------------------*/
  4. //File Class
  5.  
  6. // This class represents a file in the file system.
  7. // The paths are represented by URL's for cross platform compatibility.
  8.  
  9. //INTERFACE
  10. //
  11. //File methods:
  12. //
  13. //  toString() returns string
  14. //    - returns the string representation of the file (value passed to constructor)
  15. //
  16. //  getName() return string
  17. //    - returns the name of the file
  18. //
  19. //  getSimpleName() return string
  20. //    - returns the name of the file without the extension
  21. //
  22. //  getExtension() return string
  23. //    - returns the file extension
  24. //
  25. //  getPath() return string
  26. //    - returns the full path of the file (value passed to constructor)
  27. //
  28. //  setPath() return boolean
  29. //    - sets the path for the file object  (returns true if successful)
  30. //
  31. //  isAbsolute() return boolean
  32. //    - returns true if the file path is absolute
  33. //
  34. //  getAbsolutePath() return string
  35. //    - returns the absolute path name of the file
  36. //
  37. //  getParent() return string
  38. //    - returns the path name of the enclosing folder
  39. //
  40. //  getAbsoluteParent() return string
  41. //    - returns the absolute path name of the enclosing folder
  42. //
  43. //  exists() return boolean
  44. //    - returns true if the file exists in the file system
  45. //
  46. //  getAttributes() return string
  47. //    - gets the attributes of the file (returns false if file doesn't exist)
  48. //      D = directory, R = read-only, H = hidden, S = system
  49. //
  50. //  canRead() return boolean
  51. //    - returns true if the file is readable
  52. //
  53. //  canWrite() return boolean
  54. //    - returns true if the file is writeable
  55. //
  56. //  isFile() return boolean
  57. //    - returns true if the file is a standard file
  58. //
  59. //  isFolder() return boolean
  60. //    - returns true if the file is a folder
  61. //
  62. //  listFolder(filterFunction) return Array
  63. //    - lists the contents of the file, if it is a folder
  64. //    - existing filter functions: File.selectFolders, File.selectFiles
  65. //
  66. //  createFolder() return boolean
  67. //    - creates a folder with the given path
  68. //
  69. //  getContents() return string
  70. //    - returns a string containing the contents of the file
  71. //
  72. //  setContents(contents, append) return boolean
  73. //    - sets the contents of the file
  74. //
  75. //  copyTo(newPath, noMetafile) return boolean
  76. //    - copies the file and its metafile to the new location (returns true if successful)
  77. //
  78. //  remove() return boolean
  79. //    - removes the file (returns true if successful)
  80. //
  81.  
  82.  
  83. //Constructor function
  84. //
  85. function File(theUrl, theDocUrl) {
  86.   // properties
  87.   this.url = '';
  88.   this.fullUrl = '';
  89.  
  90.   // initialization
  91.   this.setPath(theUrl, theDocUrl);
  92. }
  93.  
  94. // static properties
  95. File.separator = '/';
  96. File.absolutePrefix = 'file:///';
  97. File.parentDir = '/..';
  98. File.extensionSep = '.';
  99. File.metafileDir = '_notes';
  100. File.metafileExt = '.mno';
  101.  
  102. // static methods
  103. File.getFullUrl = File_getFullUrl;
  104. File.getNewlineFromString = File_getNewlineFromString;
  105.  
  106. // methods
  107. File.prototype.toString = File_toString;
  108. File.prototype.getName = File_getName;
  109. File.prototype.getSimpleName = File_getSimpleName;
  110. File.prototype.getExtension = File_getExtension;
  111. File.prototype.getPath = File_getPath;
  112. File.prototype.setPath = File_setPath;
  113. File.prototype.isAbsolute = File_isAbsolute;
  114. File.prototype.getAbsolutePath = File_getAbsolutePath;
  115. File.prototype.getParent = File_getParent;
  116. File.prototype.getAbsoluteParent = File_getAbsoluteParent;
  117. File.prototype.exists = File_exists;
  118. File.prototype.getAttributes = File_getAttributes;
  119. File.prototype.canRead = File_canRead;
  120. File.prototype.canWrite = File_canWrite;
  121. File.prototype.isFile = File_isFile;
  122. File.prototype.isFolder = File_isFolder;
  123. File.prototype.listFolder = File_listFolder;
  124. File.prototype.createFolder = File_createFolder;
  125. File.prototype.getContents = File_getContents;
  126. File.prototype.setContents = File_setContents;
  127. File.prototype.copyTo = File_copyTo;
  128. File.prototype.remove = File_remove;
  129.  
  130. // static filter functions for listFolder
  131. File.selectFolders = File_selectFolders;
  132. File.selectFiles = File_selectFiles;
  133.  
  134.  
  135.  
  136. function File_toString() {
  137.   return this.url;
  138. }
  139.  
  140. function File_getName() {
  141.   var retVal = '';
  142.   with (this) {
  143.     retVal = url;
  144.     var index = retVal.lastIndexOf(File.separator);
  145.     if (index != -1) retVal = retVal.substring(index + File.separator.length, retVal.length);
  146.     return retVal;
  147.   }
  148. }
  149.  
  150. function File_getSimpleName() {
  151.   var retVal = this.getName();
  152.   var index = retVal.lastIndexOf(File.extensionSep);
  153.   if (index != -1)
  154.     retVal = retVal.substring(0, index);
  155.   return retVal;
  156. }
  157.  
  158. function File_getExtension() {
  159.   var retVal = '';
  160.   var index = this.fullUrl.lastIndexOf(File.extensionSep);
  161.   if (index != -1)
  162.     retVal = this.fullUrl.substring(index+1);
  163.   return retVal;
  164. }
  165.  
  166. function File_getPath() {
  167.   return this.url;
  168. }
  169.  
  170. function File_setPath(theNewUrl, theDocUrl) {
  171.   this.url = (theNewUrl == null) ? "" : theNewUrl;
  172.   if (this.url.charAt(this.url.length-1) == File.separator)
  173.     this.url = this.url.substring(0,this.url.length-1);
  174.   this.fullUrl = File.getFullUrl(this.url, theDocUrl);
  175.   if (!this.fullUrl) this.url = '';
  176. }
  177.  
  178. function File_isAbsolute() {
  179.   return (this.url.indexOf(File.absolutePrefix) == 0);
  180. }
  181.  
  182. function File_getAbsolutePath(theDocUrl) {
  183.   return this.fullUrl;
  184. }
  185.  
  186. function File_getParent() {
  187.   var retVal = '';
  188.   with (this) {
  189.     var index = url.lastIndexOf(File.separator);
  190.     if (index != -1) retVal = url.substring(0, index);
  191.     return retVal;
  192.   }
  193. }
  194.  
  195. function File_getAbsoluteParent() {
  196.   var retVal = '';
  197.   with (this) {
  198.     var index = fullUrl.lastIndexOf(File.separator);
  199.     if (index != -1) retVal = fullUrl.substring(0, index);
  200.   }
  201.   return retVal;
  202. }
  203.  
  204. function File_exists() {
  205.   var retVal = false;
  206.   if (this.fullUrl)
  207.     retVal = (DWfile.exists(this.fullUrl)) ? true : false;
  208.   return retVal;
  209. }
  210.  
  211. function File_getAttributes() {
  212.   var retVal = false;
  213.   if (this.fullUrl && this.exists())
  214.     retVal = DWfile.getAttributes(this.fullUrl);
  215.   return retVal;
  216. }
  217.  
  218. function File_canRead() {
  219.   var retVal = false;
  220.   if (this.fullUrl && this.exists())
  221.     retVal = true;
  222.   return retVal;
  223. }
  224.  
  225. function File_canWrite() {
  226.   var retVal = false;
  227.   if (this.fullUrl && this.exists())
  228.     retVal = (DWfile.getAttributes(this.fullUrl).indexOf('R') == -1);
  229.   return retVal;
  230. }
  231.  
  232. function File_isFile() {
  233.   var attr, retVal = false;
  234.   if (this.fullUrl && this.exists()) {
  235.     attr = DWfile.getAttributes(this.fullUrl);
  236.     retVal = (attr == '' || attr == 'R');
  237.   }
  238.   return retVal;
  239. }
  240.  
  241. function File_isFolder() {
  242.   var retVal = false;
  243.   if (this.fullUrl && this.exists())
  244.     retVal = (DWfile.getAttributes(this.fullUrl).indexOf('D') != -1);
  245.   return retVal;
  246. }
  247.  
  248.  
  249. function File_listFolder(filterFunction) {
  250.   var retList = new Array();
  251.   var fileList, fileObj = new File();
  252.   with (this) {
  253.     if (fullUrl && exists()) {
  254.       fileList = DWfile.listFolder(fullUrl);
  255.       // now filter with the filter function
  256.       for (var i=0; i < fileList.length; i++) {
  257.         if (filterFunction != null) {
  258.           fileObj.setPath(fullUrl + File.separator + fileList[i]);
  259.           if (filterFunction(fileObj)) retList.push(fileList[i]);
  260.         } else {
  261.           retList.push(fileList[i]);
  262.   } } } }
  263.   return retList;
  264. }
  265.  
  266. function File_selectFolders(fileObj) {
  267.   return fileObj.isFolder();
  268. }
  269.  
  270. function File_selectFiles(fileObj) {
  271.   return fileObj.isFile();
  272. }
  273.  
  274.  
  275. function File_createFolder() {
  276.   var retVal = false;
  277.   if (this.fullUrl && !this.exists())
  278.     retVal = DWfile.createFolder(this.fullUrl);
  279.   return retVal;
  280. }
  281.  
  282.  
  283. function File_getContents() {
  284.   var strNewline, platformNewline, searchPatt, retVal = '';
  285.   if (this.fullUrl && this.exists() && this.isFile()) {
  286.     retVal = DWfile.read(this.fullUrl);
  287.     //replace file newlines with current page newlines
  288.     strNewline = File.getNewlineFromString(retVal);
  289.     platformNewline = (navigator.platform != "Win32") ? "\x0D":"\x0D\x0A";
  290.     if (strNewline != platformNewline) {
  291.       searchPatt = new RegExp(strNewline,"g");
  292.       retVal = retVal.replace(searchPatt, platformNewline);
  293.   } }
  294.   return retVal;
  295. }
  296.  
  297. function File_setContents(theContents, append) {
  298.   var strNewline, platformNewline, retVal = false;
  299.   if (this.fullUrl && ( !this.exists() || (this.exists() && this.isFile()) ) ) {
  300.     // set newlines
  301.     strNewline = File.getNewlineFromString(theContents);
  302.     platformNewline = (navigator.platform != "Win32") ? "\x0D":"\x0D\x0A";
  303.     if (strNewline != platformNewline) {
  304.       searchPatt = new RegExp(strNewline,"g");
  305.       theContents = theContents.replace(searchPatt, platformNewline);
  306.     }
  307.     // write the file
  308.     if (append)
  309.       retVal = DWfile.write(this.fullUrl, theContents, append);
  310.     else
  311.       retVal = DWfile.write(this.fullUrl, theContents);
  312.   }
  313.   return retVal;
  314. }
  315.  
  316.  
  317. function File_copyTo(theCopyName, noMetafile) {
  318.   var toObj, metaObj, toMetaObj, toMetaDir, retVal = false;
  319.   toObj = new File(theCopyName);
  320.   if (this.fullUrl) {
  321.     retVal = DWfile.copy(this.fullUrl, toObj.getAbsolutePath());
  322.     if (retVal && !noMetafile) {
  323.       metaObj = new File(this.getAbsoluteParent() + File.separator + File.metafileDir + File.separator + this.getName() + File.metafileExt);
  324.       if (metaObj.exists()) {
  325.         toMetaObj = new File(toObj.getAbsoluteParent() + File.separator + File.metafileDir + File.separator + toObj.getName() + File.metafileExt);
  326.         toMetaDir = new File(toMetaObj.getAbsoluteParent());
  327.         if (!toMetaDir.exists())
  328.           retVal = toMetaDir.createFolder();
  329.         if (retVal)
  330.           retVal = DWfile.copy(metaObj.getAbsolutePath(), toMetaObj.getAbsolutePath());
  331.   } } }
  332.   return retVal;
  333. }
  334.  
  335. function File_remove() {
  336.   var retVal = false;
  337.   if (this.fullUrl) retVal = DWfile.remove(this.fullUrl);
  338.   return retVal;
  339. }
  340.  
  341.  
  342. function File_getFullUrl(theUrl, theDocUrl) {
  343.   var retVal = '';
  344.   var siteUrl, docUrl, index, prevIndex;
  345.   retVal = theUrl;
  346.   if (theUrl.indexOf(File.absolutePrefix) == -1) {
  347.     if (theUrl.charAt(0) == File.separator) {  // site relative path
  348.       siteUrl = dreamweaver.getSiteRoot();
  349.       if (siteUrl) {
  350.         if (siteUrl.charAt(siteUrl.length-1) == File.separator)
  351.           retVal = siteUrl.substring(0, siteUrl.length-1) + theUrl;
  352.         else
  353.           retVal = siteUrl + theUrl;
  354.       } else {
  355.         //alert("Error: No site root defined. Cannot create File object (" + theUrl + ")");
  356.         retVal = '';
  357.       }
  358.     } else {  // doc relative path
  359.       docUrl = (theDocUrl) ? theDocUrl : dreamweaver.getDocumentPath("document");
  360.       if (docUrl) {
  361.         index = docUrl.lastIndexOf(File.separator);
  362.         if (index != -1) docUrl = docUrl.substring(0, index);
  363.         retVal = docUrl + File.separator + theUrl;
  364.       } else {
  365.         //alert("Error: Current document not saved. Cannot create File object (" + theUrl + ")");
  366.         retVal = '';
  367.   } } }
  368.   //remove relative references from the path
  369.   while (retVal.indexOf(File.parentDir) != -1) {
  370.     index = retVal.indexOf(File.parentDir);
  371.     prevIndex = retVal.lastIndexOf(File.separator, index-1);
  372.     if (prevIndex != -1)
  373.       retVal = retVal.substring(0, prevIndex) + retVal.substring(index+File.parentDir.length);
  374.     else
  375.       retVal = '';
  376.   }
  377.   return retVal;
  378. }
  379.  
  380. function File_getNewlineFromString(theStr) {
  381.   var retVal="";
  382.   if (theStr.search(/(\x0D\x0A)|(\x0D)|(\x0A)/) != -1) retVal = RegExp.lastMatch;
  383.   return retVal
  384. }
  385.