OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 path.style.windows; |
| 6 |
| 7 import '../parsed_path.dart'; |
| 8 import '../style.dart'; |
| 9 |
| 10 /// The style for Windows paths. |
| 11 class WindowsStyle extends Style { |
| 12 WindowsStyle(); |
| 13 |
| 14 final name = 'windows'; |
| 15 final separator = '\\'; |
| 16 final separatorPattern = new RegExp(r'[/\\]'); |
| 17 final needsSeparatorPattern = new RegExp(r'[^/\\]$'); |
| 18 final rootPattern = new RegExp(r'^(\\\\[^\\]+\\[^\\/]+|[a-zA-Z]:[/\\])'); |
| 19 final relativeRootPattern = new RegExp(r"^[/\\](?![/\\])"); |
| 20 |
| 21 String pathFromUri(Uri uri) { |
| 22 if (uri.scheme != '' && uri.scheme != 'file') { |
| 23 throw new ArgumentError("Uri $uri must have scheme 'file:'."); |
| 24 } |
| 25 |
| 26 var path = uri.path; |
| 27 if (uri.host == '') { |
| 28 // Drive-letter paths look like "file:///C:/path/to/file". The |
| 29 // replaceFirst removes the extra initial slash. |
| 30 if (path.startsWith('/')) path = path.replaceFirst("/", ""); |
| 31 } else { |
| 32 // Network paths look like "file://hostname/path/to/file". |
| 33 path = '\\\\${uri.host}$path'; |
| 34 } |
| 35 return Uri.decodeComponent(path.replaceAll("/", "\\")); |
| 36 } |
| 37 |
| 38 Uri absolutePathToUri(String path) { |
| 39 var parsed = new ParsedPath.parse(path, this); |
| 40 if (parsed.root.startsWith(r'\\')) { |
| 41 // Network paths become "file://server/share/path/to/file". |
| 42 |
| 43 // The root is of the form "\\server\share". We want "server" to be the |
| 44 // URI host, and "share" to be the first element of the path. |
| 45 var rootParts = parsed.root.split('\\').where((part) => part != ''); |
| 46 parsed.parts.insert(0, rootParts.last); |
| 47 |
| 48 if (parsed.hasTrailingSeparator) { |
| 49 // If the path has a trailing slash, add a single empty component so the |
| 50 // URI has a trailing slash as well. |
| 51 parsed.parts.add(""); |
| 52 } |
| 53 |
| 54 return new Uri(scheme: 'file', host: rootParts.first, |
| 55 pathSegments: parsed.parts); |
| 56 } else { |
| 57 // Drive-letter paths become "file:///C:/path/to/file". |
| 58 |
| 59 // If the path is a bare root (e.g. "C:\"), [parsed.parts] will currently |
| 60 // be empty. We add an empty component so the URL constructor produces |
| 61 // "file:///C:/", with a trailing slash. We also add an empty component if |
| 62 // the URL otherwise has a trailing slash. |
| 63 if (parsed.parts.length == 0 || parsed.hasTrailingSeparator) { |
| 64 parsed.parts.add(""); |
| 65 } |
| 66 |
| 67 // Get rid of the trailing "\" in "C:\" because the URI constructor will |
| 68 // add a separator on its own. |
| 69 parsed.parts.insert(0, parsed.root.replaceAll(separatorPattern, "")); |
| 70 |
| 71 return new Uri(scheme: 'file', pathSegments: parsed.parts); |
| 72 } |
| 73 } |
| 74 } |
OLD | NEW |