| OLD | NEW | 
|---|
|  | (Empty) | 
| 1 // Copyright (c) 2011, the Dart project authors.  Please see the AUTHORS file |  | 
| 2 // for details. All rights reserved. Use of this source code is governed by a |  | 
| 3 // BSD-style license that can be found in the LICENSE file. |  | 
| 4 |  | 
| 5 #library('file_system_dom'); |  | 
| 6 |  | 
| 7 #import('dart:dom_deprecated'); |  | 
| 8 #import('file_system.dart'); |  | 
| 9 |  | 
| 10 /** |  | 
| 11  * [FileSystem] implementation using XHRs for reading files and an in memory |  | 
| 12  * cache for writing them. |  | 
| 13  */ |  | 
| 14 class DomFileSystem implements FileSystem { |  | 
| 15   Map<String, String> _fileCache; |  | 
| 16   String _path; |  | 
| 17 |  | 
| 18   DomFileSystem([this._path = null]) : _fileCache = {}; |  | 
| 19 |  | 
| 20   // TODO(vsm): Move this to FileSystem. |  | 
| 21   String absPath(String filename) { |  | 
| 22     if (_path != null && !filename.startsWith('/') |  | 
| 23         && !filename.startsWith('file:///') && !filename.startsWith('http://') |  | 
| 24         && !filename.startsWith('dart:')) { |  | 
| 25       filename = joinPaths(_path, filename); |  | 
| 26     } |  | 
| 27     return filename; |  | 
| 28   } |  | 
| 29 |  | 
| 30   String readAll(String filename) { |  | 
| 31     filename = absPath(filename); |  | 
| 32     var result = _fileCache[filename]; |  | 
| 33     if (result == null) { |  | 
| 34       final xhr = new XMLHttpRequest(); |  | 
| 35       // TODO(jimhug): Fix API so we can get multiple files at once. |  | 
| 36       // Use a sychronous XHR to match the current API. |  | 
| 37       xhr.open('GET', filename, false); |  | 
| 38       try { |  | 
| 39         xhr.send(null); |  | 
| 40       } catch (var e) { |  | 
| 41         // TODO(vsm): This XHR appears to fail if the URL is a |  | 
| 42         // directory.  Return something to make fileExists work. |  | 
| 43         // Handle this better. |  | 
| 44         return "_directory($filename)_"; |  | 
| 45       } |  | 
| 46 |  | 
| 47       if (xhr.status == 0 || xhr.status == 200) { |  | 
| 48         result = xhr.responseText; |  | 
| 49         if (result.isEmpty()) { |  | 
| 50           // TODO(vsm): Figure out why a non-existent file is not giving |  | 
| 51           // an error code. |  | 
| 52           print('Error: $filename is not found or empty'); |  | 
| 53           return null; |  | 
| 54         } |  | 
| 55       } else { |  | 
| 56         // TODO(jimhug): Better error handling. |  | 
| 57         print("Error: ${xhr.statusText}"); |  | 
| 58       } |  | 
| 59       _fileCache[filename] = result; |  | 
| 60     } |  | 
| 61     return result; |  | 
| 62   } |  | 
| 63 |  | 
| 64   void writeString(String outfile, String text) { |  | 
| 65     outfile = absPath(outfile); |  | 
| 66     _fileCache[outfile] = text; |  | 
| 67   } |  | 
| 68 |  | 
| 69   // Note: this is not a perf nightmare only because of caching. |  | 
| 70   bool fileExists(String filename) => readAll(filename) != null; |  | 
| 71 |  | 
| 72   void createDirectory(String path, [bool recursive = false]) { |  | 
| 73     // TODO(rnystrom): Implement. |  | 
| 74     throw 'createDirectory() is not implemented by DomFileSystem yet.'; |  | 
| 75   } |  | 
| 76 |  | 
| 77   void removeDirectory(String path, [bool recursive = false]) { |  | 
| 78     // TODO(rnystrom): Implement. |  | 
| 79     throw 'removeDirectory() is not implemented by DomFileSystem yet.'; |  | 
| 80   } |  | 
| 81 } |  | 
| OLD | NEW | 
|---|