private Character chooseScalarStyle() { ScalarEvent ev = (ScalarEvent) event; if (analysis == null) { analysis = analyzeScalar(ev.getValue()); } if (ev.getStyle() != null && ev.getStyle() == '"' || this.canonical) { return '"'; } if (ev.getStyle() == null && ev.getImplicit().canOmitTagInPlainScalar()) { if (!(simpleKeyContext && (analysis.empty || analysis.multiline)) && ((flowLevel != 0 && analysis.allowFlowPlain) || (flowLevel == 0 && analysis.allowBlockPlain))) { return null; } } if (ev.getStyle() != null && (ev.getStyle() == '|' || ev.getStyle() == '>')) { if (flowLevel == 0 && !simpleKeyContext && analysis.allowBlock) { return ev.getStyle(); } } if (ev.getStyle() == null || ev.getStyle() == '\'') { if (analysis.allowSingleQuoted && !(simpleKeyContext && analysis.multiline)) { return '\''; } } return '"'; }
protected Node composeScalarNode(String anchor) { ScalarEvent ev = (ScalarEvent) parser.getEvent(); String tag = ev.getTag(); boolean resolved = false; Tag nodeTag; if (tag == null || tag.equals("!")) { nodeTag = resolver.resolve(NodeId.scalar, ev.getValue(), ev.getImplicit() .canOmitTagInPlainScalar()); resolved = true; } else { nodeTag = new Tag(tag); } Node node = new ScalarNode(nodeTag, resolved, ev.getValue(), ev.getStartMark(), ev.getEndMark(), ev.getStyle()); if (anchor != null) { anchors.put(anchor, node); } return node; }
public void testGetEvent() { String data = "string: abcd"; StreamReader reader = new StreamReader(data); Parser parser = new ParserImpl(reader); Mark dummyMark = new Mark("dummy", 0, 0, 0, "", 0); LinkedList<Event> etalonEvents = new LinkedList<Event>(); etalonEvents.add(new StreamStartEvent(dummyMark, dummyMark)); etalonEvents.add(new DocumentStartEvent(dummyMark, dummyMark, false, null, null)); etalonEvents.add(new MappingStartEvent(null, null, true, dummyMark, dummyMark, Boolean.FALSE)); etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false), "string", dummyMark, dummyMark, (char) 0)); etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false), "abcd", dummyMark, dummyMark, (char) 0)); etalonEvents.add(new MappingEndEvent(dummyMark, dummyMark)); etalonEvents.add(new DocumentEndEvent(dummyMark, dummyMark, false)); etalonEvents.add(new StreamEndEvent(dummyMark, dummyMark)); check(etalonEvents, parser); }
public void testGetEvent2() { String data = "american:\n - Boston Red Sox"; StreamReader reader = new StreamReader(data); Parser parser = new ParserImpl(reader); Mark dummyMark = new Mark("dummy", 0, 0, 0, "", 0); LinkedList<Event> etalonEvents = new LinkedList<Event>(); etalonEvents.add(new StreamStartEvent(dummyMark, dummyMark)); etalonEvents.add(new DocumentStartEvent(dummyMark, dummyMark, false, null, null)); etalonEvents .add(new MappingStartEvent(null, null, true, dummyMark, dummyMark, Boolean.TRUE)); etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false), "american", dummyMark, dummyMark, (char) 0)); etalonEvents.add(new SequenceStartEvent(null, null, true, dummyMark, dummyMark, Boolean.FALSE)); etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false), "Boston Red Sox", dummyMark, dummyMark, (char) 0)); etalonEvents.add(new SequenceEndEvent(dummyMark, dummyMark)); etalonEvents.add(new MappingEndEvent(dummyMark, dummyMark)); etalonEvents.add(new DocumentEndEvent(dummyMark, dummyMark, false)); etalonEvents.add(new StreamEndEvent(dummyMark, dummyMark)); check(etalonEvents, parser); }
protected Node composeScalarNode(String anchor) { ScalarEvent ev = (ScalarEvent) parser.getEvent(); String tag = ev.getTag(); boolean resolved = false; Tag nodeTag; if (tag == null || tag.equals("!")) { nodeTag = resolver.resolve(NodeId.scalar, ev.getValue(), ev.getImplicit().canOmitTagInPlainScalar()); resolved = true; } else { nodeTag = new Tag(tag); } Node node = new ScalarNode(nodeTag, resolved, ev.getValue(), ev.getStartMark(), ev.getEndMark(), ev.getStyle()); if (anchor != null) { anchors.put(anchor, node); } return node; }
@Override public Map<String, Properties> convert(Reader input) { Yaml y = new Yaml(); Map<String, Properties> propertiesMap = new HashMap<String, Properties>(); String currentKey = ""; String currentEnv = ""; for (Event event : y.parse(input)) { // mapping: scalar int level = event.getStartMark().getColumn(); if (event.is(ID.Scalar)) { ScalarEvent s = (ScalarEvent) event; if (level == 0) { currentKey = s.getValue(); } else if (level == 2) { currentEnv = s.getValue(); } else { Properties properties = propertiesMap.get(currentEnv); properties = createIfNoProperties(propertiesMap, currentEnv, properties); properties.put(currentKey, s.getValue()); } } } return propertiesMap; }
private Node composeScalarNode(String anchor) { ScalarEvent ev = (ScalarEvent) parser.getEvent(); String tag = ev.getTag(); boolean resolved = false; Tag nodeTag; if (tag == null || tag.equals("!")) { nodeTag = resolver.resolve(NodeId.scalar, ev.getValue(), ev.getImplicit() .canOmitTagInPlainScalar()); resolved = true; } else { nodeTag = new Tag(tag); } Node node = new ScalarNode(nodeTag, resolved, ev.getValue(), ev.getStartMark(), ev.getEndMark(), ev.getStyle()); if (anchor != null) { anchors.put(anchor, node); } return node; }
public void testGetEvent() { String data = "string: abcd"; StreamReader reader = new StreamReader(data); Parser parser = new ParserImpl(reader); Mark dummyMark = new Mark("dummy", 0, 0, 0, "", 0); LinkedList<Event> etalonEvents = new LinkedList<Event>(); etalonEvents.add(new StreamStartEvent(dummyMark, dummyMark)); etalonEvents.add(new DocumentStartEvent(dummyMark, dummyMark, false, null, null)); etalonEvents.add(new MappingStartEvent(null, null, true, dummyMark, dummyMark, Boolean.FALSE)); etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false), "string", dummyMark, dummyMark, (char) 0)); etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false), "abcd", dummyMark, dummyMark, (char) 0)); etalonEvents.add(new MappingEndEvent(dummyMark, dummyMark)); etalonEvents.add(new DocumentEndEvent(dummyMark, dummyMark, false)); etalonEvents.add(new StreamEndEvent(dummyMark, dummyMark)); while (parser.checkEvent(null)) { Event event = parser.getEvent(); if (etalonEvents.isEmpty()) { fail("unexpected event: " + event); } assertEquals(etalonEvents.removeFirst(), event); } assertFalse("Must contain no more events: " + parser.getEvent(), parser.checkEvent(null)); }
private Node composeScalarNode(final String anchor) { ScalarEvent ev = (ScalarEvent) parser.getEvent(); String tag = ev.getTag(); boolean resolved = false; Tag nodeTag; if (tag == null || tag.equals("!")) { nodeTag = resolver.resolve(NodeId.scalar, ev.getValue(), ev.getImplicit().canOmitTagInPlainScalar()); resolved = true; } else { nodeTag = new Tag(tag); } Node node = new ScalarNode(nodeTag, resolved, ev.getValue(), ev.getStartMark(), ev.getEndMark(), ev.getStyle()); if (anchor != null) { anchors.put(anchor, node); } return node; }
private void expectNode(boolean root, boolean mapping, boolean simpleKey) throws IOException { rootContext = root; mappingContext = mapping; simpleKeyContext = simpleKey; if (event instanceof AliasEvent) { expectAlias(); } else if (event instanceof ScalarEvent || event instanceof CollectionStartEvent) { processAnchor("&"); processTag(); if (event instanceof ScalarEvent) { expectScalar(); } else if (event instanceof SequenceStartEvent) { if (flowLevel != 0 || canonical || ((SequenceStartEvent) event).getFlowStyle() || checkEmptySequence()) { expectFlowSequence(); } else { expectBlockSequence(); } } else {// MappingStartEvent if (flowLevel != 0 || canonical || ((MappingStartEvent) event).getFlowStyle() || checkEmptyMapping()) { expectFlowMapping(); } else { expectBlockMapping(); } } } else { throw new EmitterException("需要 NodeEvent 事件, 但却得到了 " + event); } }
private boolean checkSimpleKey() { int length = 0; if (event instanceof NodeEvent && ((NodeEvent) event).getAnchor() != null) { if (preparedAnchor == null) { preparedAnchor = prepareAnchor(((NodeEvent) event).getAnchor()); } length += preparedAnchor.length(); } String tag = null; if (event instanceof ScalarEvent) { tag = ((ScalarEvent) event).getTag(); } else if (event instanceof CollectionStartEvent) { tag = ((CollectionStartEvent) event).getTag(); } if (tag != null) { if (preparedTag == null) { preparedTag = prepareTag(tag); } length += preparedTag.length(); } if (event instanceof ScalarEvent) { if (analysis == null) { analysis = analyzeScalar(((ScalarEvent) event).getValue()); } length += analysis.scalar.length(); } return length < 128 && (event instanceof AliasEvent || (event instanceof ScalarEvent && !analysis.empty && !analysis.multiline) || checkEmptySequence() || checkEmptyMapping()); }
private void processScalar() throws IOException { ScalarEvent ev = (ScalarEvent) event; if (analysis == null) { analysis = analyzeScalar(ev.getValue()); } if (style == null) { style = chooseScalarStyle(); } boolean split = !simpleKeyContext && splitLines; if (style == null) { writePlain(analysis.scalar, split); } else { switch (style) { case '"': writeDoubleQuoted(analysis.scalar, split); break; case '\'': writeSingleQuoted(analysis.scalar, split); break; case '>': writeFolded(analysis.scalar, split); break; case '|': writeLiteral(analysis.scalar); break; default: throw new YAMLException("未预期的主题: " + style); } } analysis = null; style = null; }
private void expectNode(boolean root, boolean mapping, boolean simpleKey) throws IOException { rootContext = root; mappingContext = mapping; simpleKeyContext = simpleKey; if (event instanceof AliasEvent) { expectAlias(); } else if (event instanceof ScalarEvent || event instanceof CollectionStartEvent) { processAnchor("&"); processTag(); if (event instanceof ScalarEvent) { expectScalar(); } else if (event instanceof SequenceStartEvent) { if (flowLevel != 0 || canonical || ((SequenceStartEvent) event).getFlowStyle() || checkEmptySequence()) { expectFlowSequence(); } else { expectBlockSequence(); } } else {// MappingStartEvent if (flowLevel != 0 || canonical || ((MappingStartEvent) event).getFlowStyle() || checkEmptyMapping()) { expectFlowMapping(); } else { expectBlockMapping(); } } } else { throw new EmitterException("expected NodeEvent, but got " + event); } }
private void processScalar() throws IOException { ScalarEvent ev = (ScalarEvent) event; if (analysis == null) { analysis = analyzeScalar(ev.getValue()); } if (style == null) { style = chooseScalarStyle(); } boolean split = !simpleKeyContext && splitLines; if (style == null) { writePlain(analysis.scalar, split); } else { switch (style) { case '"': writeDoubleQuoted(analysis.scalar, split); break; case '\'': writeSingleQuoted(analysis.scalar, split); break; case '>': writeFolded(analysis.scalar, split); break; case '|': writeLiteral(analysis.scalar); break; default: throw new YAMLException("Unexpected style: " + style); } } analysis = null; style = null; }
boolean checkSimpleKey() { int length = 0; if ((this.event instanceof NodeEvent) && (((NodeEvent) this.event).getAnchor() != null)) { if (this.preparedAnchor == null) { this.preparedAnchor = prepareAnchor(((NodeEvent) this.event).getAnchor()); } length += this.preparedAnchor.length(); } String tag = null; if (this.event instanceof ScalarEvent) { tag = ((ScalarEvent) this.event).getTag(); } else if (this.event instanceof CollectionStartEvent) { tag = ((CollectionStartEvent) this.event).getTag(); } if (tag != null) { if (this.preparedTag == null) { this.preparedTag = this.prepareTag(tag); } length += this.preparedTag.length(); } if (this.event instanceof ScalarEvent) { if (this.analysis == null) { this.analysis = this.analyzeScalar(((ScalarEvent) this.event).getValue()); } length += this.analysis.scalar.length(); } return (length < SMALL_LENGTH) && ((this.event instanceof AliasEvent) || ((this.event instanceof ScalarEvent) && ! ((this.analysis == null) || this.analysis.empty) && ! this.analysis.multiline) || this.checkEmptySequence() || this.checkEmptyMapping()); }
public void testLowLevel() { List<Object> list = new ArrayList<Object>(); list.add(1); list.add("abc"); Map<String, String> map = new HashMap<String, String>(); map.put("name", "Tolstoy"); map.put("book", "War and People"); list.add(map); Yaml yaml = new Yaml(); String etalon = yaml.dump(list); // System.out.println(etalon); // Node node = yaml.represent(list); // System.out.println(node); assertEquals( "Representation tree from an object and from its YAML document must be the same.", yaml.compose(new StringReader(etalon)).toString(), node.toString()); // List<Event> events = yaml.serialize(node); int i = 0; for (Event etalonEvent : yaml.parse(new StringReader(etalon))) { Event ev1 = events.get(i++); assertEquals(etalonEvent.getClass(), ev1.getClass()); if (etalonEvent instanceof ScalarEvent) { ScalarEvent scalar1 = (ScalarEvent) etalonEvent; ScalarEvent scalar2 = (ScalarEvent) ev1; assertEquals(scalar1.getAnchor(), scalar2.getAnchor()); assertEquals(scalar1.getValue(), scalar2.getValue()); } } assertEquals(i, events.size()); }
public void testLoad() { Yaml yaml = new Yaml(); Iterable<Event> parsed = yaml.parse(new StringReader("5")); List<Event> events = new ArrayList<Event>(5); for (Event event : parsed) { events.add(event); // System.out.println(event); } String tag = ((ScalarEvent) events.get(2)).getTag(); assertNull("The tag should not be specified: " + tag, tag); }
public void testDump() { Yaml yaml = new Yaml(); Node intNode = yaml.represent(7); assertEquals("tag:yaml.org,2002:int", intNode.getTag().toString()); // System.out.println(intNode); List<Event> intEvents = yaml.serialize(intNode); String tag = ((ScalarEvent) intEvents.get(2)).getTag(); assertEquals("Without the tag emitter would not know how to emit '7'", "tag:yaml.org,2002:int", tag); // Node strNode = yaml.represent("7"); assertEquals("tag:yaml.org,2002:str", strNode.getTag().toString()); // System.out.println(strNode); }
public void testWriteSupplementaryUnicode() throws IOException { DumperOptions options = new DumperOptions(); String burger = new String(Character.toChars(0x1f354)); String halfBurger = "\uD83C"; StringWriter output = new StringWriter(); Emitter emitter = new Emitter(output, options); emitter.emit(new StreamStartEvent(null, null)); emitter.emit(new DocumentStartEvent(null, null, false, null, null)); emitter.emit(new ScalarEvent(null, null, new ImplicitTuple(true, false), burger + halfBurger, null, null, '"')); String expected = "! \"\\U0001f354\\ud83c\""; assertEquals(expected, output.toString()); }
private void compareEvents(List<Event> events1, List<Event> events2, boolean full) { assertEquals(events1.size(), events2.size()); Iterator<Event> iter1 = events1.iterator(); Iterator<Event> iter2 = events2.iterator(); while (iter1.hasNext()) { Event event1 = iter1.next(); Event event2 = iter2.next(); assertEquals(event1.getClass(), event2.getClass()); if (event1 instanceof AliasEvent && full) { assertEquals(((AliasEvent) event1).getAnchor(), ((AliasEvent) event2).getAnchor()); } if (event1 instanceof CollectionStartEvent) { String tag1 = ((CollectionStartEvent) event1).getTag(); String tag2 = ((CollectionStartEvent) event1).getTag(); if (tag1 != null && !"!".equals(tag1) && tag2 != null && !"!".equals(tag1)) { assertEquals(tag1, tag2); } } if (event1 instanceof ScalarEvent) { ScalarEvent scalar1 = (ScalarEvent) event1; ScalarEvent scalar2 = (ScalarEvent) event2; if (scalar1.getImplicit().bothFalse() && scalar2.getImplicit().bothFalse()) { assertEquals(scalar1.getTag(), scalar2.getTag()); } assertEquals(scalar1.getValue(), scalar2.getValue()); } } }
public void testGetEvent2() { String data = "american:\n - Boston Red Sox"; StreamReader reader = new StreamReader(data); Parser parser = new ParserImpl(reader); Mark dummyMark = new Mark("dummy", 0, 0, 0, "", 0); LinkedList<Event> etalonEvents = new LinkedList<Event>(); etalonEvents.add(new StreamStartEvent(dummyMark, dummyMark)); etalonEvents.add(new DocumentStartEvent(dummyMark, dummyMark, false, null, null)); etalonEvents .add(new MappingStartEvent(null, null, true, dummyMark, dummyMark, Boolean.TRUE)); etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false), "american", dummyMark, dummyMark, (char) 0)); etalonEvents.add(new SequenceStartEvent(null, null, true, dummyMark, dummyMark, Boolean.FALSE)); etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false), "Boston Red Sox", dummyMark, dummyMark, (char) 0)); etalonEvents.add(new SequenceEndEvent(dummyMark, dummyMark)); etalonEvents.add(new MappingEndEvent(dummyMark, dummyMark)); etalonEvents.add(new DocumentEndEvent(dummyMark, dummyMark, false)); etalonEvents.add(new StreamEndEvent(dummyMark, dummyMark)); while (parser.checkEvent(null)) { Event event = parser.getEvent(); if (etalonEvents.isEmpty()) { fail("unexpected event: " + event); } assertEquals(etalonEvents.removeFirst(), event); } assertFalse("Must contain no more events: " + parser.getEvent(), parser.checkEvent(null)); }