public SafeConstructor() { this.yamlConstructors.put(Tag.NULL, new ConstructYamlNull()); this.yamlConstructors.put(Tag.BOOL, new ConstructYamlBool()); this.yamlConstructors.put(Tag.INT, new ConstructYamlInt()); this.yamlConstructors.put(Tag.FLOAT, new ConstructYamlFloat()); this.yamlConstructors.put(Tag.BINARY, new ConstructYamlBinary()); this.yamlConstructors.put(Tag.TIMESTAMP, new ConstructYamlTimestamp()); this.yamlConstructors.put(Tag.OMAP, new ConstructYamlOmap()); this.yamlConstructors.put(Tag.PAIRS, new ConstructYamlPairs()); this.yamlConstructors.put(Tag.SET, new ConstructYamlSet()); this.yamlConstructors.put(Tag.STR, new ConstructYamlStr()); this.yamlConstructors.put(Tag.SEQ, new ConstructYamlSeq()); this.yamlConstructors.put(Tag.MAP, new ConstructYamlMap()); this.yamlConstructors.put(null, undefinedConstructor); this.yamlClassConstructors.put(NodeId.scalar, undefinedConstructor); this.yamlClassConstructors.put(NodeId.sequence, undefinedConstructor); this.yamlClassConstructors.put(NodeId.mapping, undefinedConstructor); }
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; }
protected Node composeSequenceNode(String anchor) { SequenceStartEvent startEvent = (SequenceStartEvent) parser.getEvent(); String tag = startEvent.getTag(); Tag nodeTag; boolean resolved = false; if (tag == null || tag.equals("!")) { nodeTag = resolver.resolve(NodeId.sequence, null, startEvent.getImplicit()); resolved = true; } else { nodeTag = new Tag(tag); } final ArrayList<Node> children = new ArrayList<Node>(); SequenceNode node = new SequenceNode(nodeTag, resolved, children, startEvent.getStartMark(), null, startEvent.getFlowStyle()); if (anchor != null) { anchors.put(anchor, node); } while (!parser.checkEvent(Event.ID.SequenceEnd)) { children.add(composeNode(node)); } Event endEvent = parser.getEvent(); node.setEndMark(endEvent.getEndMark()); return node; }
protected Node composeMappingNode(String anchor) { MappingStartEvent startEvent = (MappingStartEvent) parser.getEvent(); String tag = startEvent.getTag(); Tag nodeTag; boolean resolved = false; if (tag == null || tag.equals("!")) { nodeTag = resolver.resolve(NodeId.mapping, null, startEvent.getImplicit()); resolved = true; } else { nodeTag = new Tag(tag); } final List<NodeTuple> children = new ArrayList<NodeTuple>(); MappingNode node = new MappingNode(nodeTag, resolved, children, startEvent.getStartMark(), null, startEvent.getFlowStyle()); if (anchor != null) { anchors.put(anchor, node); } while (!parser.checkEvent(Event.ID.MappingEnd)) { composeMappingChildren(children, node); } Event endEvent = parser.getEvent(); node.setEndMark(endEvent.getEndMark()); return node; }
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; }
private static Yaml newYaml() { return new Yaml(new Constructor(), new Representer(), new DumperOptions(), new Resolver() { @Override public Tag resolve(NodeId kind, String value, boolean implicit) { if (value != null) { if (value.equalsIgnoreCase("on") || value.equalsIgnoreCase("off") || value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("no")) { return Tag.STR; } } return super.resolve(kind, value, implicit); } }); }
@Override protected void lookAhead(Lexer baseLexer) { super.lookAhead(baseLexer); if (getTokenType() == YamlTokenTypes.YAML_Scalar) { Tag tag = resolver.resolve(NodeId.scalar, getTokenText().trim(), true); if (tag.equals(Tag.BOOL)) replaceCachedType(0, YamlTokenTypes.YAML_Tag_BOOL); else if (tag.equals(Tag.INT)) replaceCachedType(0, YamlTokenTypes.YAML_Tag_INT); else if (tag.equals(Tag.FLOAT)) replaceCachedType(0, YamlTokenTypes.YAML_Tag_FLOAT); else if (tag.equals(Tag.NULL)) replaceCachedType(0, YamlTokenTypes.YAML_Tag_NULL); else if (tag.equals(Tag.TIMESTAMP)) replaceCachedType(0, YamlTokenTypes.YAML_Tag_TIMESTAMP); if (baseLexer.getTokenType() == YamlTokenTypes.YAML_Value) { replaceCachedType(0, YamlTokenTypes.YAML_Key); } } }
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; }
private Node composeSequenceNode(String anchor) { SequenceStartEvent startEvent = (SequenceStartEvent) parser.getEvent(); String tag = startEvent.getTag(); Tag nodeTag; boolean resolved = false; if (tag == null || tag.equals("!")) { nodeTag = resolver.resolve(NodeId.sequence, null, startEvent.getImplicit()); resolved = true; } else { nodeTag = new Tag(tag); } final ArrayList<Node> children = new ArrayList<Node>(); SequenceNode node = new SequenceNode(nodeTag, resolved, children, startEvent.getStartMark(), null, startEvent.getFlowStyle()); if (anchor != null) { anchors.put(anchor, node); } while (!parser.checkEvent(Event.ID.SequenceEnd)) { children.add(composeNode(node)); } Event endEvent = parser.getEvent(); node.setEndMark(endEvent.getEndMark()); return node; }
@Override // Skip null values for configuration generating protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object value, Tag customTag) { if (value != null) { NodeTuple tuple = super.representJavaBeanProperty(javaBean, property, value, customTag); Node valueNode = tuple.getValueNode(); // Avoid using tags for enums if (customTag == null && valueNode.getNodeId() == NodeId.scalar && value instanceof Enum<?>) { valueNode.setTag(Tag.STR); } return tuple; } else { return 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 Node composeSequenceNode(final String anchor) { SequenceStartEvent startEvent = (SequenceStartEvent) parser.getEvent(); String tag = startEvent.getTag(); Tag nodeTag; boolean resolved = false; if (tag == null || tag.equals("!")) { nodeTag = resolver.resolve(NodeId.sequence, null, startEvent.getImplicit()); resolved = true; } else { nodeTag = new Tag(tag); } final ArrayList<Node> children = new ArrayList<Node>(); SequenceNode node = new SequenceNode(nodeTag, resolved, children, startEvent.getStartMark(), null, startEvent.getFlowStyle()); if (anchor != null) { anchors.put(anchor, node); } int index = 0; while (!parser.checkEvent(Event.ID.SequenceEnd)) { children.add(composeNode(node)); index++; } Event endEvent = parser.getEvent(); node.setEndMark(endEvent.getEndMark()); return node; }
/** * Represent one JavaBean property. * * @param javaBean * - the instance to be represented * @param property * - the property of the instance * @param propertyValue * - value to be represented * @param customTag * - user defined Tag * @return NodeTuple to be used in a MappingNode. Return null to skip the * property */ protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue, Tag customTag) { ScalarNode nodeKey = (ScalarNode) representData(property.getName()); // the first occurrence of the node must keep the tag boolean hasAlias = this.representedObjects.containsKey(propertyValue); Node nodeValue = representData(propertyValue); if (propertyValue != null && !hasAlias) { NodeId nodeId = nodeValue.getNodeId(); if (customTag == null) { if (nodeId == NodeId.scalar) { if (propertyValue instanceof Enum<?>) { nodeValue.setTag(Tag.STR); } } else { if (nodeId == NodeId.mapping) { if (property.getType() == propertyValue.getClass()) { if (!(propertyValue instanceof Map<?, ?>)) { if (!nodeValue.getTag().equals(Tag.SET)) { nodeValue.setTag(Tag.MAP); } } } } checkGlobalTag(property, nodeValue, propertyValue); } } } return new NodeTuple(nodeKey, nodeValue); }
public Constructor(TypeDescription theRoot) { if (theRoot == null) { throw new NullPointerException("Root type must be provided."); } this.yamlConstructors.put(null, new ConstructYamlObject()); if (!Object.class.equals(theRoot.getType())) { rootTag = new Tag(theRoot.getType()); } typeTags = new HashMap<Tag, Class<? extends Object>>(); typeDefinitions = new HashMap<Class<? extends Object>, TypeDescription>(); yamlClassConstructors.put(NodeId.scalar, new ConstructScalar()); yamlClassConstructors.put(NodeId.mapping, new ConstructMapping()); yamlClassConstructors.put(NodeId.sequence, new ConstructSequence()); addTypeDescription(theRoot); }
public JodaTimeConstructor() { javaDateConstruct = new ConstructYamlTimestamp(); jodaDateConstruct = new ConstructJodaTimestamp(); // Whenever we see an explicit timestamp tag, make a Joda Date // instead yamlConstructors.put(Tag.TIMESTAMP, jodaDateConstruct); // See // We need this to work around implicit construction. yamlClassConstructors.put(NodeId.scalar, new TimeStampConstruct()); }
public void testComposeAllFromReader() { Yaml yaml = new Yaml(); boolean first = true; for (Node node : yaml.composeAll(new StringReader("abc: 56\n---\n123\n---\n456"))) { if (first) { assertEquals(NodeId.mapping, node.getNodeId()); } else { assertEquals(NodeId.scalar, node.getNodeId()); } first = false; } }
@Override public Object construct(Node node) { if (node.getNodeId() == NodeId.scalar) { ScalarNode n = (ScalarNode) node; String value = n.getValue(); int i = 3; if (value.length() != 0) { i = Integer.parseInt(value); } return new BeanHolder(new Bean1(i)); } else { return new BeanHolder(); } }
public void testComposeBean() { String data = "!!org.yaml.snakeyaml.composer.ComposerImplTest$BeanToCompose {name: Bill, age: 18}"; Yaml yaml = new Yaml(); Node node = yaml.compose(new StringReader(data)); assertNotNull(node); assertTrue(node instanceof MappingNode); assertEquals( "tag:yaml.org,2002:org.yaml.snakeyaml.composer.ComposerImplTest$BeanToCompose", node.getTag().getValue()); assertEquals(NodeId.mapping, node.getNodeId()); assertEquals(Object.class, node.getType()); }
public SrcdepsConstructor() { super(); this.yamlClassConstructors.put(NodeId.scalar, new PathConstruct()); this.setPropertyUtils(new BuilderPropertyUtils(Configuration.Builder.class, BuilderIo.Builder.class, Maven.Builder.class, MavenAssertions.FailWithoutBuilder.class, MavenAssertions.FailWithBuilder.class, ScmRepository.Builder.class, ScmRepositoryMaven.Builder.class, ScmRepositoryGradle.Builder.class)); }
private String validateScalar(Node node) { if (node.isTwoStepsConstruction()) { throw new TagException("'"+node.getTag()+"' cannot be recursive.", node.getStartMark()); } if (!node.getNodeId().equals(NodeId.scalar)) { throw new TagException("'"+node.getTag()+"' must be a string.", node.getStartMark()); } return ((ScalarNode) node).getValue().toString(); }
/** * Returns true if the argument can be identified as a JSON reference node. * * @param tuple * @return true if a reference node */ public static boolean isReference(NodeTuple tuple) { if (tuple.getKeyNode().getNodeId() == NodeId.scalar) { String value = ((ScalarNode) tuple.getKeyNode()).getValue(); return JsonReference.PROPERTY.equals(value) && tuple.getValueNode().getNodeId() == NodeId.scalar; } return false; }
private Node composeMappingNode(String anchor) { MappingStartEvent startEvent = (MappingStartEvent) parser.getEvent(); String tag = startEvent.getTag(); Tag nodeTag; boolean resolved = false; if (tag == null || tag.equals("!")) { nodeTag = resolver.resolve(NodeId.mapping, null, startEvent.getImplicit()); resolved = true; } else { nodeTag = new Tag(tag); } final List<NodeTuple> children = new ArrayList<NodeTuple>(); MappingNode node = new MappingNode(nodeTag, resolved, children, startEvent.getStartMark(), null, startEvent.getFlowStyle()); if (anchor != null) { anchors.put(anchor, node); } while (!parser.checkEvent(Event.ID.MappingEnd)) { Node itemKey = composeNode(node); if (itemKey.getTag().equals(Tag.MERGE)) { node.setMerged(true); } Node itemValue = composeNode(node); children.add(new NodeTuple(itemKey, itemValue)); } Event endEvent = parser.getEvent(); node.setEndMark(endEvent.getEndMark()); return node; }