OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library path.context; | 5 library path.context; |
6 | 6 |
| 7 import 'internal_style.dart'; |
7 import 'style.dart'; | 8 import 'style.dart'; |
8 import 'parsed_path.dart'; | 9 import 'parsed_path.dart'; |
9 import 'path_exception.dart'; | 10 import 'path_exception.dart'; |
10 import '../path.dart' as p; | 11 import '../path.dart' as p; |
11 | 12 |
12 /// An instantiable class for manipulating paths. Unlike the top-level | 13 /// An instantiable class for manipulating paths. Unlike the top-level |
13 /// functions, this lets you explicitly select what platform the paths will use. | 14 /// functions, this lets you explicitly select what platform the paths will use. |
14 class Context { | 15 class Context { |
15 /// Creates a new path context for the given style and current directory. | 16 /// Creates a new path context for the given style and current directory. |
16 /// | 17 /// |
17 /// If [style] is omitted, it uses the host operating system's path style. If | 18 /// If [style] is omitted, it uses the host operating system's path style. If |
18 /// only [current] is omitted, it defaults ".". If *both* [style] and | 19 /// only [current] is omitted, it defaults ".". If *both* [style] and |
19 /// [current] are omitted, [current] defaults to the real current working | 20 /// [current] are omitted, [current] defaults to the real current working |
20 /// directory. | 21 /// directory. |
21 /// | 22 /// |
22 /// On the browser, [style] defaults to [Style.url] and [current] defaults to | 23 /// On the browser, [style] defaults to [Style.url] and [current] defaults to |
23 /// the current URL. | 24 /// the current URL. |
24 factory Context({Style style, String current}) { | 25 factory Context({Style style, String current}) { |
25 if (current == null) { | 26 if (current == null) { |
26 if (style == null) { | 27 if (style == null) { |
27 current = p.current; | 28 current = p.current; |
28 } else { | 29 } else { |
29 current = "."; | 30 current = "."; |
30 } | 31 } |
31 } | 32 } |
32 | 33 |
33 if (style == null) style = Style.platform; | 34 if (style == null) { |
| 35 style = Style.platform; |
| 36 } else if (style is! InternalStyle) { |
| 37 throw new ArgumentError("Only styles defined by the path package are " |
| 38 "allowed."); |
| 39 } |
34 | 40 |
35 return new Context._(style, current); | 41 return new Context._(style, current); |
36 } | 42 } |
37 | 43 |
38 Context._(this.style, this.current); | 44 Context._(this.style, this.current); |
39 | 45 |
40 /// The style of path that this context works with. | 46 /// The style of path that this context works with. |
41 final Style style; | 47 final InternalStyle style; |
42 | 48 |
43 /// The current directory that relative paths will be relative to. | 49 /// The current directory that relative paths will be relative to. |
44 final String current; | 50 final String current; |
45 | 51 |
46 /// Gets the path separator for the context's [style]. On Mac and Linux, | 52 /// Gets the path separator for the context's [style]. On Mac and Linux, |
47 /// this is `/`. On Windows, it's `\`. | 53 /// this is `/`. On Windows, it's `\`. |
48 String get separator => style.separator; | 54 String get separator => style.separator; |
49 | 55 |
50 /// Creates a new path by appending the given path parts to [current]. | 56 /// Creates a new path by appending the given path parts to [current]. |
51 /// Equivalent to [join()] with [current] as the first argument. Example: | 57 /// Equivalent to [join()] with [current] as the first argument. Example: |
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 // Show the arguments. | 538 // Show the arguments. |
533 var message = new StringBuffer(); | 539 var message = new StringBuffer(); |
534 message.write("$method("); | 540 message.write("$method("); |
535 message.write(args.take(numArgs) | 541 message.write(args.take(numArgs) |
536 .map((arg) => arg == null ? "null" : '"$arg"') | 542 .map((arg) => arg == null ? "null" : '"$arg"') |
537 .join(", ")); | 543 .join(", ")); |
538 message.write("): part ${i - 1} was null, but part $i was not."); | 544 message.write("): part ${i - 1} was null, but part $i was not."); |
539 throw new ArgumentError(message.toString()); | 545 throw new ArgumentError(message.toString()); |
540 } | 546 } |
541 } | 547 } |
OLD | NEW |