本文整理汇总了Java中org.yaml.snakeyaml.nodes.ScalarNode类的典型用法代码示例。如果您正苦于以下问题:Java ScalarNode类的具体用法?Java ScalarNode怎么用?Java ScalarNode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ScalarNode类属于org.yaml.snakeyaml.nodes包,在下文中一共展示了ScalarNode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addSerializer
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
public static final <T> void addSerializer(ExtensibleRepresenter representer, Class<T> type,
Function<T, String> function) {
representer.addRepresenter(type, new Represent() {
@SuppressWarnings("unchecked")
@Override
public Node representData(Object data) {
String txt = function.apply((T) data);
if (txt == null) {
return new ScalarNode(Tag.NULL, "null", null, null, null);
}
return new ScalarNode(Tag.STR, txt, null, null, null);
}
});
}
示例2: representSequence
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
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;
}
示例3: representMapping
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
protected Node representMapping(Tag tag, Map<?, ?> mapping, Boolean flowStyle) {
List<NodeTuple> value = new ArrayList<NodeTuple>(mapping.size());
MappingNode node = new MappingNode(tag, value, flowStyle);
representedObjects.put(objectToRepresent, node);
boolean bestStyle = true;
for (Map.Entry<?, ?> entry : mapping.entrySet()) {
Node nodeKey = representData(entry.getKey());
Node nodeValue = representData(entry.getValue());
if (!(nodeKey instanceof ScalarNode && ((ScalarNode) nodeKey).getStyle() == null)) {
bestStyle = false;
}
if (!(nodeValue instanceof ScalarNode && ((ScalarNode) nodeValue).getStyle() == null)) {
bestStyle = false;
}
value.add(new NodeTuple(nodeKey, nodeValue));
}
if (flowStyle == null) {
if (defaultFlowStyle != FlowStyle.AUTO) {
node.setFlowStyle(defaultFlowStyle.getStyleBoolean());
} else {
node.setFlowStyle(bestStyle);
}
}
return node;
}
示例4: construct
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
public Object construct(Node node) {
ScalarNode scalar = (ScalarNode) node;
try {
return nf.parse(scalar.getValue());
} catch (ParseException e) {
String lowerCaseValue = scalar.getValue().toLowerCase();
if (lowerCaseValue.contains("inf") || lowerCaseValue.contains("nan")) {
/*
* Non-finites such as (+/-)infinity and NaN are not
* parseable by NumberFormat when these `Double` values are
* dumped by snakeyaml. Delegate to the `Tag.FLOAT`
* constructor when for this expected failure cause.
*/
return (Number) yamlConstructors.get(Tag.FLOAT).construct(node);
} else {
throw new IllegalArgumentException("Unable to parse as Number: "
+ scalar.getValue());
}
}
}
示例5: notNullMapProperty
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
@Test
public void notNullMapProperty() {
bean.setMapProperty(ImmutableMap.<String, Long> builder().put("first", 1L).put("second", 2L).build());
Property property = new MethodProperty(getPropertyDescriptor("mapProperty"));
NodeTuple nodeTuple = representer.representJavaBeanProperty(bean, property, bean.getMapProperty(), null);
assertThat(nodeTuple, is(notNullValue()));
assertThat(nodeTuple.getKeyNode(), is(instanceOf(ScalarNode.class)));
assertThat(((ScalarNode) nodeTuple.getKeyNode()).getValue(), is("map-property"));
assertThat(nodeTuple.getValueNode(), is(instanceOf(MappingNode.class)));
assertThat(((MappingNode) nodeTuple.getValueNode()).getValue().size(), is(2));
assertThat(((MappingNode) nodeTuple.getValueNode()).getValue().get(0), is(instanceOf(NodeTuple.class)));
assertThat(((ScalarNode) ((MappingNode) nodeTuple.getValueNode()).getValue().get(0).getKeyNode()).getValue(),
is("first"));
assertThat(((ScalarNode) ((MappingNode) nodeTuple.getValueNode()).getValue().get(0).getValueNode()).getValue(),
is("1"));
assertThat(((ScalarNode) ((MappingNode) nodeTuple.getValueNode()).getValue().get(1).getKeyNode()).getValue(),
is("second"));
assertThat(((ScalarNode) ((MappingNode) nodeTuple.getValueNode()).getValue().get(1).getValueNode()).getValue(),
is("2"));
}
示例6: getKeys
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
@Override
public Set<String> getKeys()
{
if (this.node instanceof MappingNode)
{
List<NodeTuple> tuples = ((MappingNode) this.node).getValue();
Set<String> result = new LinkedHashSet<>(tuples.size());
for (NodeTuple tuple : tuples)
{
Node keyNode = tuple.getKeyNode();
if (keyNode instanceof ScalarNode)
{
result.add(((ScalarNode) keyNode).getValue());
}
}
return result;
}
return Collections.emptySet();
}
示例7: getNode
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
@Nullable
private Node getNode(MappingNode node, String key, boolean remove)
{
for (Iterator<NodeTuple> iterator = node.getValue().iterator(); iterator.hasNext(); )
{
NodeTuple tuple = iterator.next();
Node keyNode = tuple.getKeyNode();
if (! (keyNode instanceof ScalarNode))
{
return null;
}
if (key.equals(((ScalarNode) keyNode).getValue()))
{
if (remove)
{
iterator.remove();
}
return tuple.getValueNode();
}
}
return null;
}
示例8: composeScalarNode
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
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;
}
示例9: testLongURIEscape
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
/**
* Try loading a tag with a very long escaped URI section (over 256 bytes'
* worth).
*/
public void testLongURIEscape() {
Yaml loader = new Yaml();
// Create a long escaped string by exponential growth...
String longEscURI = "%41"; // capital A...
for (int i = 0; i < 10; ++i) {
longEscURI = longEscURI + longEscURI;
}
assertEquals(1024 * 3, longEscURI.length());
String yaml = "!" + longEscURI + " www";
Node node = loader.compose(new StringReader(yaml));
ScalarNode scalar = (ScalarNode) node;
String etalon = "!";
for (int i = 0; i < 1024; i++) {
etalon += "A";
}
assertEquals(1025, etalon.length());
assertEquals(etalon, scalar.getTag().toString());
}
示例10: getSingleNode
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
@Override
public Node getSingleNode() {
Node node = super.getSingleNode();
if (!MappingNode.class.isAssignableFrom(node.getClass())) {
throw new RuntimeException(
"Document is not structured as expected. Root element should be a map!");
}
MappingNode root = (MappingNode) node;
for (NodeTuple tuple : root.getValue()) {
Node keyNode = tuple.getKeyNode();
if (ScalarNode.class.isAssignableFrom(keyNode.getClass())) {
if (((ScalarNode) keyNode).getValue().equals(nodeName)) {
return tuple.getValueNode();
}
}
}
throw new RuntimeException("Did not find key \"" + nodeName + "\" in document-level map");
}
示例11: getNode
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
private static Node getNode(final MappingNode node, final String key, final boolean remove)
{
for (final Iterator<NodeTuple> it = node.getValue().iterator(); it.hasNext(); )
{
final NodeTuple nodeTuple = it.next();
final Node keyNode = nodeTuple.getKeyNode();
if (keyNode instanceof ScalarNode)
{
if (key.equals(((ScalarNode) keyNode).getValue()))
{
if (remove)
{
it.remove();
}
return nodeTuple.getValueNode();
}
}
}
return null;
}
示例12: composeScalarNode
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
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;
}
示例13: construct
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
public Object construct(Node node) {
ScalarNode scalar = (ScalarNode) node;
try {
return nf.parse(scalar.getValue());
} catch (ParseException e) {
String lowerCaseValue = scalar.getValue().toLowerCase();
if (lowerCaseValue.contains("inf") || lowerCaseValue.contains("nan")) {
/*
* Non-finites such as (+/-)infinity and NaN are not
* parseable by NumberFormat when these `Double` values are
* dumped by snakeyaml. Delegate to the `Tag.FLOAT`
* constructor when for this expected failure cause.
*/
return yamlConstructors.get(Tag.FLOAT).construct(node);
} else {
throw new IllegalArgumentException(
"Unable to parse as Number: " + scalar.getValue());
}
}
}
示例14: construct
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
@Override
public Object construct(Node node) {
if(!(node instanceof MappingNode)) {
fail("The value of the !select-engine tag must be a map" + "\nGot: " + node);
}
String matchingKey = null;
Object result = null;
for(NodeTuple tuple : ((MappingNode)node).getValue()) {
Node keyNode = tuple.getKeyNode();
if(!(keyNode instanceof ScalarNode)) {
fail("The key in a !select-engine map must be a scalar" + "\nGot: " + constructObject(keyNode));
}
String key = ((ScalarNode)keyNode).getValue();
if(IT_ENGINE.equals(key) || (matchingKey == null && ALL_ENGINE.equals(key))) {
matchingKey = key;
result = constructObject(tuple.getValueNode());
}
}
if(matchingKey != null) {
LOG.debug("Select engine: '{}' => '{}'", matchingKey, result);
return result;
} else {
LOG.debug("Select engine: no match");
return null;
}
}
示例15: validateKeys
import org.yaml.snakeyaml.nodes.ScalarNode; //导入依赖的package包/类
private void validateKeys(MappingNode node)
{
Map<String, Long> keyCounts = node.getValue().stream()
.map(NodeTuple::getKeyNode)
.filter(n -> n instanceof ScalarNode)
.map(ScalarNode.class::cast)
.filter(n -> !"!include".equals(n.getTag().getValue())) // exclude !include tag
.collect(Collectors.groupingBy(ScalarNode::getValue, Collectors.counting()));
List<String> duplicatedKeys = keyCounts.entrySet().stream()
.filter(it -> it.getValue() > 1)
.map(Map.Entry<String, Long>::getKey)
.collect(Collectors.toList());
if (!duplicatedKeys.isEmpty()) {
throw new DuplicateKeyYAMLException(duplicatedKeys);
}
}