Skip to content

Commit

Permalink
Tag Directive code changes (EsotericSoftware#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr14huashao authored Jul 10, 2020
1 parent 53d156f commit 817df21
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/com/esotericsoftware/yamlbeans/YamlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public void setAllowDuplicates (boolean allowDuplicates) {
public void setClassTag (String tag, Class type) {
if (tag == null) throw new IllegalArgumentException("tag cannot be null.");
if (type == null) throw new IllegalArgumentException("type cannot be null.");
if (!tag.startsWith("!")) tag = "!" + tag;
classNameToTag.put(type.getName(), tag);
tagToClass.put(tag, type);
}
Expand Down
1 change: 1 addition & 0 deletions src/com/esotericsoftware/yamlbeans/YamlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ private Class<?> chooseType (String tag, Class<?> defaultType, Class<?> provided
ClassLoader classLoader = (config.readConfig.classLoader == null ? this.getClass().getClassLoader()
: config.readConfig.classLoader);

tag = tag.replace("!", "");
try {
Class<?> loadedFromTag = findTagClass(tag, classLoader);
if (loadedFromTag != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/com/esotericsoftware/yamlbeans/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public Event produce () {
if (tokenizer.peekNextTokenType() == ANCHOR) anchor = ((AnchorToken)tokenizer.getNextToken()).getInstanceName();
}
String tag = null;
if (tagHandle != null && !tagHandle.equals("!")) {
if (tagHandle != null) {
if (!tagHandles.containsKey(tagHandle)) throw new ParserException("Undefined tag handle: " + tagHandle);
tag = tagHandles.get(tagHandle) + tagSuffix;
} else
Expand Down
84 changes: 84 additions & 0 deletions test/com/esotericsoftware/yamlbeans/TagDirectiveTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.esotericsoftware.yamlbeans;

import static org.junit.Assert.assertEquals;

import java.util.Iterator;

import org.junit.Test;

import com.esotericsoftware.yamlbeans.document.YamlDocumentReader;
import com.esotericsoftware.yamlbeans.document.YamlElement;
import com.esotericsoftware.yamlbeans.document.YamlSequence;

public class TagDirectiveTest {

private static final String LINE_SEPARATOR = System.getProperty("line.separator");

@Test
public void testTagDirective() throws YamlException {
StringBuilder sb = new StringBuilder();
sb.append("%TAG !yaml! tag:yaml.org,2002:").append(LINE_SEPARATOR);
sb.append("---").append(LINE_SEPARATOR);
sb.append("!yaml!str \"foo\"");

YamlReader yamlReader = new YamlReader(sb.toString());
assertEquals("foo", yamlReader.read().toString());
}

@Test(expected = YamlException.class)
public void testRepeatedTagDirective() throws YamlException {
StringBuilder sb = new StringBuilder();
sb.append("%TAG %TAG ! !foo").append(LINE_SEPARATOR);
sb.append("%TAG %TAG ! !foo").append(LINE_SEPARATOR);
sb.append("---").append(LINE_SEPARATOR);
sb.append("bar");

new YamlReader(sb.toString()).read();
}

@Test
public void testTagHandles() throws YamlException {
StringBuilder sb = new StringBuilder();
sb.append("%TAG ! !").append(LINE_SEPARATOR);
sb.append("%TAG !yaml! tag:yaml.org,2002:").append(LINE_SEPARATOR);
sb.append("%TAG !o! tag:ben-kiki.org,2000:").append(LINE_SEPARATOR);
sb.append("---").append(LINE_SEPARATOR);
sb.append("- !foo bar").append(LINE_SEPARATOR);
sb.append("- !!str string").append(LINE_SEPARATOR);
sb.append("- !o!type baz").append(LINE_SEPARATOR);
YamlDocumentReader yamlDocumentReader = new YamlDocumentReader(sb.toString());
Iterator<YamlElement> iterator = yamlDocumentReader.read(YamlSequence.class).iterator();

YamlElement yamlElement = iterator.next();
assertEquals("!foo", yamlElement.getTag());

yamlElement = iterator.next();
assertEquals("tag:yaml.org,2002:str", yamlElement.getTag());

yamlElement = iterator.next();
assertEquals("tag:ben-kiki.org,2000:type", yamlElement.getTag());
}

@Test
public void testVersion() throws YamlException {
String yaml = "!str test";
YamlConfig yamlConfig = new YamlConfig();
yamlConfig.readConfig.setDefaultVersion(new Version(1, 0));
YamlReader yamlReader = new YamlReader(yaml, yamlConfig);
assertEquals("test", yamlReader.read());

yaml = "!!str test";
yamlConfig.readConfig.setDefaultVersion(new Version(1, 1));
yamlReader = new YamlReader(yaml, yamlConfig);
assertEquals("test", yamlReader.read());

yaml = "!!str test";
yamlConfig.readConfig.setDefaultVersion(new Version(1, 0));
yamlReader = new YamlReader(yaml, yamlConfig);
try {
yamlReader.read();
} catch (YamlException e) {
assertEquals("Error parsing YAML.", e.getMessage());
}
}
}
4 changes: 3 additions & 1 deletion test/com/esotericsoftware/yamlbeans/YamlConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ public void setup() throws Exception {
@Test
public void testSetClassTag() throws YamlException {
yamlConfig.setClassTag("String", String.class);
String yaml = "!String test";
yamlConfig.setClassTag("!Int", Integer.class);
String yaml = "!String test\n---\n!Int 1";
YamlReader yamlReader = new YamlReader(yaml, yamlConfig);
assertEquals("test", yamlReader.read());
assertEquals(1, yamlReader.read());

try {
yamlConfig.setClassTag(null, String.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.esotericsoftware.yamlbeans.issues.issue101;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.esotericsoftware.yamlbeans.YamlException;
import com.esotericsoftware.yamlbeans.YamlReader;

public class Issue101Test {

private static final String LINE_SEPARATOR = System.getProperty("line.separator");

@Test
public void test() throws YamlException {
StringBuilder sb = new StringBuilder();
sb.append("!com.esotericsoftware.yamlbeans.issues.issue101.Issue101Test$TestObject").append(LINE_SEPARATOR);
sb.append("test: test");
YamlReader yamlReader = new YamlReader(sb.toString());
TestObject testObject = (TestObject) yamlReader.read();
assertEquals("test", testObject.test);

sb = new StringBuilder();
sb.append("!<!com.esotericsoftware.yamlbeans.issues.issue101.Issue101Test$TestObject>").append(LINE_SEPARATOR);
sb.append("test: test");
yamlReader = new YamlReader(sb.toString());
testObject = (TestObject) yamlReader.read();
assertEquals("test", testObject.test);
}

static class TestObject {
public String test;
}
}

0 comments on commit 817df21

Please sign in to comment.