aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/yaml/snakeyaml
diff options
context:
space:
mode:
authorAndrey Somov <public.somov@gmail.com>2022-01-07 19:17:36 +0400
committerAndrey Somov <public.somov@gmail.com>2022-01-07 19:17:36 +0400
commit724b1193b88d67b2bea2b6061df1c69302f55dfc (patch)
tree9e15250fe637b5a71b8641a6c0558de81034dec8 /src/main/java/org/yaml/snakeyaml
parentb03bdc06cb894ff32cf79d31e8536d769859e7e1 (diff)
parente14de4c379873ee0238ce55cd3dfb8b877403766 (diff)
downloadsnakeyaml-724b1193b88d67b2bea2b6061df1c69302f55dfc.tar.gz
Merge branch 'fix-directives'
Diffstat (limited to 'src/main/java/org/yaml/snakeyaml')
-rw-r--r--src/main/java/org/yaml/snakeyaml/parser/ParserImpl.java37
1 files changed, 23 insertions, 14 deletions
diff --git a/src/main/java/org/yaml/snakeyaml/parser/ParserImpl.java b/src/main/java/org/yaml/snakeyaml/parser/ParserImpl.java
index 21491efa..a91146b9 100644
--- a/src/main/java/org/yaml/snakeyaml/parser/ParserImpl.java
+++ b/src/main/java/org/yaml/snakeyaml/parser/ParserImpl.java
@@ -143,7 +143,7 @@ public class ParserImpl implements Parser {
public ParserImpl(Scanner scanner) {
this.scanner = scanner;
currentEvent = null;
- directives = new VersionTagsTuple(null, new HashMap<String, String>(DEFAULT_TAGS));
+ directives = new VersionTagsTuple(null, new HashMap<String, String>());
states = new ArrayStack<Production>(100);
marks = new ArrayStack<Mark>(10);
state = new ParseStreamStart();
@@ -317,15 +317,23 @@ public class ParserImpl implements Parser {
}
}
+ /**
+ * https://yaml.org/spec/1.1/#id898785 says "If the document specifies no directives,
+ * it is parsed using the same settings as the previous document.
+ * If the document does specify any directives,
+ * all directives of previous documents, if any, are ignored."
+ * TODO the last statement is not respected (as in PyYAML, to work the same)
+ * @return directives to be applied for the current document
+ */
@SuppressWarnings("unchecked")
private VersionTagsTuple processDirectives() {
- Version yamlVersion = null;
HashMap<String, String> tagHandles = new HashMap<String, String>();
+ directives = new VersionTagsTuple(null, tagHandles);
while (scanner.checkToken(Token.ID.Directive)) {
@SuppressWarnings("rawtypes")
DirectiveToken token = (DirectiveToken) scanner.getToken();
if (token.getName().equals("YAML")) {
- if (yamlVersion != null) {
+ if (directives.getVersion() != null) {
throw new ParserException(null, null, "found duplicate YAML directive",
token.getStartMark());
}
@@ -339,11 +347,11 @@ public class ParserImpl implements Parser {
Integer minor = value.get(1);
switch (minor) {
case 0:
- yamlVersion = Version.V1_0;
+ directives = new VersionTagsTuple(Version.V1_0, tagHandles);
break;
default:
- yamlVersion = Version.V1_1;
+ directives = new VersionTagsTuple(Version.V1_1, tagHandles);
break;
}
} else if (token.getName().equals("TAG")) {
@@ -357,17 +365,18 @@ public class ParserImpl implements Parser {
tagHandles.put(handle, prefix);
}
}
- if (yamlVersion != null || !tagHandles.isEmpty()) {
- // directives in the document found - drop the previous
- for (String key : DEFAULT_TAGS.keySet()) {
- // do not overwrite re-defined tags
- if (!tagHandles.containsKey(key)) {
- tagHandles.put(key, DEFAULT_TAGS.get(key));
- }
+ HashMap<String, String> detectedTagHandles = new HashMap<String, String>();
+ if (!tagHandles.isEmpty()) {
+ // copy from tagHandles
+ detectedTagHandles = new HashMap<String, String>(tagHandles);
+ }
+ for (String key : DEFAULT_TAGS.keySet()) {
+ // do not overwrite re-defined tags
+ if (!tagHandles.containsKey(key)) {
+ tagHandles.put(key, DEFAULT_TAGS.get(key));
}
- directives = new VersionTagsTuple(yamlVersion, tagHandles);
}
- return directives;
+ return new VersionTagsTuple(directives.getVersion(), detectedTagHandles);
}
/**