Java Code Examples for org.yaml.snakeyaml.nodes.Node#getType()

The following examples show how to use org.yaml.snakeyaml.nodes.Node#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseConstructor.java    From onedev with MIT License 5 votes vote down vote up
protected Object finalizeConstruction(Node node, Object data) {
    final Class<? extends Object> type = node.getType();
    if (typeDefinitions.containsKey(type)) {
        return typeDefinitions.get(type).finalizeConstruction(data);
    }
    return data;
}
 
Example 2
Source File: VersionedYamlDoc.java    From onedev with MIT License 5 votes vote down vote up
@Override
protected Class<?> getClassForNode(Node node) {
	Class<?> type = node.getType();
	if (type.getAnnotation(Editable.class) != null && !ClassUtils.isConcrete(type)) {
		ImplementationRegistry registry = OneDev.getInstance(ImplementationRegistry.class);
		for (Class<?> implementationClass: registry.getImplementations(node.getType())) {
			String implementationTag = new Tag("!" + implementationClass.getSimpleName()).getValue();
			if (implementationTag.equals(node.getTag().getValue()))
				return implementationClass;
		}
	}
	
	return super.getClassForNode(node);
}
 
Example 3
Source File: Yaml.java    From java with Apache License 2.0 5 votes vote down vote up
@Override
protected Object constructObject(Node node) {
  if (node.getType() == IntOrString.class) {
    return constructIntOrString((ScalarNode) node);
  }
  if (node.getType() == byte[].class) {
    return constructByteArray((ScalarNode) node);
  }

  if (node.getType() == org.joda.time.DateTime.class) {
    return constructDateTime((ScalarNode) node);
  }

  return super.constructObject(node);
}
 
Example 4
Source File: BaseConstructor.java    From onedev with MIT License 4 votes vote down vote up
protected Object constructArrayStep2(SequenceNode node, Object array) {
    final Class<?> componentType = node.getType().getComponentType();

    int index = 0;
    for (Node child : node.getValue()) {
        // Handle multi-dimensional arrays...
        if (child.getType() == Object.class) {
            child.setType(componentType);
        }

        final Object value = constructObject(child);

        if (componentType.isPrimitive()) {
            // Null values are disallowed for primitives
            if (value == null) {
                throw new NullPointerException(
                        "Unable to construct element value for " + child);
            }

            // Primitive arrays require quite a lot of work.
            if (byte.class.equals(componentType)) {
                Array.setByte(array, index, ((Number) value).byteValue());

            } else if (short.class.equals(componentType)) {
                Array.setShort(array, index, ((Number) value).shortValue());

            } else if (int.class.equals(componentType)) {
                Array.setInt(array, index, ((Number) value).intValue());

            } else if (long.class.equals(componentType)) {
                Array.setLong(array, index, ((Number) value).longValue());

            } else if (float.class.equals(componentType)) {
                Array.setFloat(array, index, ((Number) value).floatValue());

            } else if (double.class.equals(componentType)) {
                Array.setDouble(array, index, ((Number) value).doubleValue());

            } else if (char.class.equals(componentType)) {
                Array.setChar(array, index, ((Character) value).charValue());

            } else if (boolean.class.equals(componentType)) {
                Array.setBoolean(array, index, ((Boolean) value).booleanValue());

            } else {
                throw new YAMLException("unexpected primitive type");
            }

        } else {
            // Non-primitive arrays can simply be assigned:
            Array.set(array, index, value);
        }

        ++index;
    }
    return array;
}
 
Example 5
Source File: BaseConstructor.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
protected Object constructArrayStep2(SequenceNode node, Object array) {
    final Class<?> componentType = node.getType().getComponentType();

    int index = 0;
    for (Node child : node.getValue()) {
        // Handle multi-dimensional arrays...
        if ( child.getType() == Object.class ) {
            child.setType(componentType);
        }
        
        final Object value = constructObject(child);

        if (componentType.isPrimitive()) {
            // Null values are disallowed for primitives
            if ( value == null ) {
                throw new NullPointerException ( "Unable to construct element value for " + child );
            }
            
            // Primitive arrays require quite a lot of work.
            if (byte.class.equals(componentType)) {
                Array.setByte(array, index, ((Number) value).byteValue());

            } else if (short.class.equals(componentType)) {
                Array.setShort(array, index, ((Number) value).shortValue());

            } else if (int.class.equals(componentType)) {
                Array.setInt(array, index, ((Number) value).intValue());

            } else if (long.class.equals(componentType)) {
                Array.setLong(array, index, ((Number) value).longValue());

            } else if (float.class.equals(componentType)) {
                Array.setFloat(array, index, ((Number) value).floatValue());

            } else if (double.class.equals(componentType)) {
                Array.setDouble(array, index, ((Number) value).doubleValue());

            } else if (char.class.equals(componentType)) {
                Array.setChar(array, index, ((Character) value).charValue());

            } else if (boolean.class.equals(componentType)) {
                Array.setBoolean(array, index, ((Boolean) value).booleanValue());

            } else {
                throw new YAMLException("unexpected primitive type");
            }

        } else {
            // Non-primitive arrays can simply be assigned:
            Array.set(array, index, value);
        }

        ++index;
    }
    return array;
}
 
Example 6
Source File: BaseConstructor.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
protected Object constructArrayStep2(SequenceNode node, Object array) {
    final Class<?> componentType = node.getType().getComponentType();

    int index = 0;
    for (Node child : node.getValue()) {
        // Handle multi-dimensional arrays...
        if (child.getType() == Object.class) {
            child.setType(componentType);
        }

        final Object value = constructObject(child);

        if (componentType.isPrimitive()) {
            // Null values are disallowed for primitives
            if (value == null) {
                throw new NullPointerException("Unable to construct element value for " + child);
            }

            // Primitive arrays require quite a lot of work.
            if (byte.class.equals(componentType)) {
                Array.setByte(array, index, ((Number) value).byteValue());

            } else if (short.class.equals(componentType)) {
                Array.setShort(array, index, ((Number) value).shortValue());

            } else if (int.class.equals(componentType)) {
                Array.setInt(array, index, ((Number) value).intValue());

            } else if (long.class.equals(componentType)) {
                Array.setLong(array, index, ((Number) value).longValue());

            } else if (float.class.equals(componentType)) {
                Array.setFloat(array, index, ((Number) value).floatValue());

            } else if (double.class.equals(componentType)) {
                Array.setDouble(array, index, ((Number) value).doubleValue());

            } else if (char.class.equals(componentType)) {
                Array.setChar(array, index, ((Character) value).charValue());

            } else if (boolean.class.equals(componentType)) {
                Array.setBoolean(array, index, ((Boolean) value).booleanValue());

            } else {
                throw new YAMLException("unexpected primitive type");
            }

        } else {
            // Non-primitive arrays can simply be assigned:
            Array.set(array, index, value);
        }

        ++index;
    }
    return array;
}
 
Example 7
Source File: YamlConstructMapping.java    From Diorite with MIT License 4 votes vote down vote up
/**
 * Construct JavaBean. If type safe collections are used please look at
 * <code>TypeDescription</code>.
 *
 * @param node
 *         node where the keys are property names (they can only be <code>String</code>s) and values are objects to be created
 *
 * @return constructed JavaBean
 */
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public Object construct(Node node)
{
    MappingNode mnode = (MappingNode) node;
    Class<?> nodeType = node.getType();
    if (Map.class.isAssignableFrom(nodeType) || Collection.class.isAssignableFrom(nodeType))
    {
        if (Config.class.isAssignableFrom(nodeType))
        {
            ConfigManager configManager = ConfigManager.get();
            ConfigTemplate configTemplate = configManager.getConfigFile((Class) nodeType);
            Config config = configTemplate.create();
            this.yamlConstructor.constructMapping2ndStep(mnode, (Map) config);
            return config;
        }
        Object created = YamlCollectionCreator.createCollection(nodeType, mnode.getValue().size());
        if (Properties.class.isAssignableFrom(nodeType))
        {
            if (node.isTwoStepsConstruction())
            {
                throw new YAMLException("Properties must not be recursive.");
            }
            this.yamlConstructor.constructMapping2ndStep(mnode, (Map<Object, Object>) created);
            return created;
        }
        if (Map.class.isAssignableFrom(nodeType))
        {
            if (node.isTwoStepsConstruction())
            {
                return created;
            }
            this.yamlConstructor.constructMapping2ndStep(mnode, (Map<Object, Object>) created);
            return created;
        }
        if (Set.class.isAssignableFrom(nodeType))
        {
            if (node.isTwoStepsConstruction())
            {
                return created;
            }
            this.yamlConstructor.constructSet2ndStep(mnode, (Set<Object>) created);
            return created;
        }
        if (Collection.class.isAssignableFrom(nodeType))
        {
            Set<Object> collection = new LinkedHashSet<>((Collection<?>) created);
            if (node.isTwoStepsConstruction())
            {
                return collection;
            }
            this.yamlConstructor.constructSet2ndStep(mnode, collection);
            return collection;
        }
        else
        {
            throw new YAMLException("Unknown type: " + nodeType);
        }
    }
    else
    {
        if (node.isTwoStepsConstruction())
        {
            return this.createEmptyJavaBean(mnode);
        }
        else
        {
            return this.constructJavaBean2ndStep(mnode, this.createEmptyJavaBean(mnode));
        }
    }
}