OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 yaml; | 5 library yaml; |
6 | 6 |
7 import 'src/composer.dart'; | 7 import 'src/composer.dart'; |
8 import 'src/constructor.dart'; | 8 import 'src/constructor.dart'; |
9 import 'src/parser.dart'; | 9 import 'src/parser.dart'; |
10 import 'src/yaml_exception.dart'; | 10 import 'src/yaml_exception.dart'; |
11 | 11 |
12 export 'src/yaml_exception.dart'; | 12 export 'src/yaml_exception.dart'; |
13 export 'src/yaml_map.dart'; | 13 export 'src/yaml_map.dart'; |
14 | 14 |
15 /// Loads a single document from a YAML string. | 15 /// Loads a single document from a YAML string. |
16 /// | 16 /// |
17 /// If the string contains more than one document, this throws a | 17 /// If the string contains more than one document, this throws a |
18 /// [YamlException]. In future releases, this will become an [ArgumentError]. | 18 /// [YamlException]. In future releases, this will become an [ArgumentError]. |
19 /// | 19 /// |
20 /// The return value is mostly normal Dart objects. However, since YAML mappings | 20 /// The return value is mostly normal Dart objects. However, since YAML mappings |
21 /// support some key types that the default Dart map implementation doesn't | 21 /// support some key types that the default Dart map implementation doesn't |
22 /// (NaN, lists, and maps), all maps in the returned document are [YamlMap]s. | 22 /// (NaN, lists, and maps), all maps in the returned document are [YamlMap]s. |
23 /// These have a few small behavioral differences from the default Map | 23 /// These have a few small behavioral differences from the default Map |
24 /// implementation; for details, see the [YamlMap] class. | 24 /// implementation; for details, see the [YamlMap] class. |
25 /// | 25 /// |
26 /// In future versions, maps will instead be [HashMap]s with a custom equality | 26 /// In future versions, maps will instead be [HashMap]s with a custom equality |
27 /// operation. | 27 /// operation. |
28 loadYaml(String yaml) { | 28 /// |
29 var stream = loadYamlStream(yaml); | 29 /// If [sourceName] is passed, it's used as the name of the file or URL from |
| 30 /// which the YAML originated for error reporting. |
| 31 loadYaml(String yaml, {String sourceName}) { |
| 32 var stream = loadYamlStream(yaml, sourceName: sourceName); |
30 if (stream.length != 1) { | 33 if (stream.length != 1) { |
31 throw new YamlException("Expected 1 document, were ${stream.length}."); | 34 throw new YamlException("Expected 1 document, were ${stream.length}."); |
32 } | 35 } |
33 return stream[0]; | 36 return stream[0]; |
34 } | 37 } |
35 | 38 |
36 /// Loads a stream of documents from a YAML string. | 39 /// Loads a stream of documents from a YAML string. |
37 /// | 40 /// |
38 /// The return value is mostly normal Dart objects. However, since YAML mappings | 41 /// The return value is mostly normal Dart objects. However, since YAML mappings |
39 /// support some key types that the default Dart map implementation doesn't | 42 /// support some key types that the default Dart map implementation doesn't |
40 /// (NaN, lists, and maps), all maps in the returned document are [YamlMap]s. | 43 /// (NaN, lists, and maps), all maps in the returned document are [YamlMap]s. |
41 /// These have a few small behavioral differences from the default Map | 44 /// These have a few small behavioral differences from the default Map |
42 /// implementation; for details, see the [YamlMap] class. | 45 /// implementation; for details, see the [YamlMap] class. |
43 /// | 46 /// |
44 /// In future versions, maps will instead be [HashMap]s with a custom equality | 47 /// In future versions, maps will instead be [HashMap]s with a custom equality |
45 /// operation. | 48 /// operation. |
46 List loadYamlStream(String yaml) { | 49 /// |
47 return new Parser(yaml).l_yamlStream() | 50 /// If [sourceName] is passed, it's used as the name of the file or URL from |
| 51 /// which the YAML originated for error reporting. |
| 52 List loadYamlStream(String yaml, {String sourceName}) { |
| 53 var stream; |
| 54 try { |
| 55 stream = new Parser(yaml, sourceName).l_yamlStream(); |
| 56 } on FormatException catch (error) { |
| 57 throw new YamlException(error.toString()); |
| 58 } |
| 59 |
| 60 return stream |
48 .map((doc) => new Constructor(new Composer(doc).compose()).construct()) | 61 .map((doc) => new Constructor(new Composer(doc).compose()).construct()) |
49 .toList(); | 62 .toList(); |
50 } | 63 } |
OLD | NEW |