protected Node representSequence(Tag tag, Iterable<?> sequence, Boolean flowStyle) { int size = 10;// default for ArrayList if (sequence instanceof List<?>) { size = ((List<?>) sequence).size(); } List<Node> value = new ArrayList<Node>(size); SequenceNode node = new SequenceNode(tag, value, flowStyle); representedObjects.put(objectToRepresent, node); boolean bestStyle = true; for (Object item : sequence) { Node nodeItem = representData(item); if (!(nodeItem instanceof ScalarNode && ((ScalarNode) nodeItem).getStyle() == null)) { bestStyle = false; } value.add(nodeItem); } if (flowStyle == null) { if (defaultFlowStyle != FlowStyle.AUTO) { node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); } else { node.setFlowStyle(bestStyle); } } return node; }
@Override public void construct2ndStep(Node node, Object object) { // Compact Object Notation may contain only one entry MappingNode mnode = (MappingNode) node; NodeTuple nodeTuple = mnode.getValue().iterator().next(); Node valueNode = nodeTuple.getValueNode(); if (valueNode instanceof MappingNode) { valueNode.setType(object.getClass()); constructJavaBean2ndStep((MappingNode) valueNode, object); } else { // value is a list applySequence(object, constructSequence((SequenceNode) valueNode)); } }
@SuppressWarnings("unchecked") protected List<? extends Object> constructSequence(SequenceNode node) { List<Object> result; if (List.class.isAssignableFrom(node.getType()) && !node.getType().isInterface()) { // the root class may be defined (Vector for instance) try { result = (List<Object>) node.getType().newInstance(); } catch (Exception e) { throw new YAMLException(e); } } else { result = createDefaultList(node.getValue().size()); } constructSequenceStep2(node, result); return result; }
@SuppressWarnings("unchecked") protected Set<? extends Object> constructSet(SequenceNode node) { Set<Object> result; if (!node.getType().isInterface()) { // the root class may be defined try { result = (Set<Object>) node.getType().newInstance(); } catch (Exception e) { throw new YAMLException(e); } } else { result = createDefaultSet(node.getValue().size()); } constructSequenceStep2(node, result); return result; }
@Override @SuppressWarnings("unchecked") public void construct2ndStep(Node node, Object object) { SequenceNode snode = (SequenceNode) node; if (List.class.isAssignableFrom(node.getType())) { List<Object> list = (List<Object>) object; this.yamlConstructor.constructSequenceStep2(snode, list); } else if (node.getType().isArray()) { this.yamlConstructor.constructArrayStep2(snode, object); } else { throw new YAMLException("Immutable objects cannot be recursive."); } }
@Override public boolean containsKey(String key) { if (this.node instanceof MappingNode) { return this.getNode((MappingNode) this.node, key) != null; } if (this.node instanceof SequenceNode) { int i = DioriteMathUtils.asInt(key, - 1); if (i == - 1) { return false; } return i < ((SequenceNode) this.node).getValue().size(); } return false; }
@Nullable private Node getNode(Node node, String key) { if (key.isEmpty()) { return node; } if (node instanceof SequenceNode) { SequenceNode sequenceNode = (SequenceNode) node; List<Node> sequenceNodeValue = sequenceNode.getValue(); int i = DioriteMathUtils.asInt(key, - 1); if ((i == - 1) || (i < sequenceNodeValue.size())) { return null; } return sequenceNodeValue.get(i); } if (node instanceof MappingNode) { return this.getNode((MappingNode) node, key); } return null; }
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; }
@SuppressWarnings("unchecked") public Car construct(Node node) { Car car = new Car(); MappingNode mapping = (MappingNode) node; List<NodeTuple> list = mapping.getValue(); for (NodeTuple tuple : list) { String field = toScalarString(tuple.getKeyNode()); if ("plate".equals(field)) { car.setPlate(toScalarString(tuple.getValueNode())); } if ("wheels".equals(field)) { SequenceNode snode = (SequenceNode) tuple.getValueNode(); List<Wheel> wheels = (List<Wheel>) constructSequence(snode); car.setWheels(wheels); } } return car; }
private void loadYamlLookupSets(MappingNode entry, String filename) { // LOG.info("Loading lookupSet.({}:{})", filename, entry.getStartMark().getLine()); String name = null; Set<String> lookupSet = new HashSet<>(); for (NodeTuple tuple : entry.getValue()) { switch (getKeyAsString(tuple, filename)) { case "name": name = getValueAsString(tuple, filename); break; case "values": SequenceNode node = getValueAsSequenceNode(tuple, filename); List<String> values = getStringValues(node, filename); for (String value: values) { lookupSet.add(value.toLowerCase(Locale.ENGLISH)); } break; default: break; } } lookupSets.put(name, lookupSet); }
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; }
protected Node representSequence(Tag tag, Iterable<? extends Object> sequence, Boolean flowStyle) { int size = 10;// default for ArrayList if (sequence instanceof List<?>) { size = ((List<?>) sequence).size(); } List<Node> value = new ArrayList<Node>(size); SequenceNode node = new SequenceNode(tag, value, flowStyle); representedObjects.put(objectToRepresent, node); boolean bestStyle = true; for (Object item : sequence) { Node nodeItem = representData(item); if (!((nodeItem instanceof ScalarNode && ((ScalarNode) nodeItem).getStyle() == null))) { bestStyle = false; } value.add(nodeItem); } if (flowStyle == null) { if (defaultFlowStyle != FlowStyle.AUTO) { node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); } else { node.setFlowStyle(bestStyle); } } return node; }
@Override public void typeCheck(Node node) throws ConfigTypeErrorException { if (YamlUtils.isNull(node)) { return; } if (!(node instanceof SequenceNode)) { throw new ConfigTypeErrorException(node, format("Expected a %s, found %s", this.toString(), node.getTag())); } else { SequenceNode listNode = (SequenceNode)node; Set<Object> results = new HashSet<>(); for (Node innerNode : listNode.getValue()) { innerType.typeCheck(innerNode); Object obj = innerType.coerce(innerNode); if (results.contains(obj)) { throw new ConfigTypeErrorException(innerNode, "Duplicate entry in set"); } results.add(obj); } } }
protected Node representSequence(final Tag tag, final Iterable<?> sequence, final Boolean flowStyle) { int size = 10;// default for ArrayList if (sequence instanceof List<?>) { size = ((List<?>) sequence).size(); } List<Node> value = new ArrayList<Node>(size); SequenceNode node = new SequenceNode(tag, value, flowStyle); representedObjects.put(objectToRepresent, node); boolean bestStyle = true; for (Object item : sequence) { Node nodeItem = representData(item); if (!((nodeItem instanceof ScalarNode && ((ScalarNode) nodeItem).getStyle() == null))) { bestStyle = false; } value.add(nodeItem); } if (flowStyle == null) { if (defaultFlowStyle != FlowStyle.AUTO) { node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); } else { node.setFlowStyle(bestStyle); } } 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; }
@Override public void construct2ndStep(final Node node, final Object object) { // Compact Object Notation may contain only one entry MappingNode mnode = (MappingNode) node; NodeTuple nodeTuple = mnode.getValue().iterator().next(); Node valueNode = nodeTuple.getValueNode(); if (valueNode instanceof MappingNode) { valueNode.setType(object.getClass()); constructJavaBean2ndStep((MappingNode) valueNode, object); } else { // value is a list applySequence(object, constructSequence((SequenceNode) valueNode)); } }