Java Code Examples for org.yaml.snakeyaml.nodes.SequenceNode#getValue()

The following examples show how to use org.yaml.snakeyaml.nodes.SequenceNode#getValue() . 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: YamlDeserializationData.java    From Diorite with MIT License 6 votes vote down vote up
@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;
}
 
Example 2
Source File: BuildSpec.java    From onedev with MIT License 5 votes vote down vote up
@SuppressWarnings("unused")
private void migrate1(VersionedYamlDoc doc, Stack<Integer> versions) {
	for (NodeTuple specTuple: doc.getValue()) {
		if (((ScalarNode)specTuple.getKeyNode()).getValue().equals("jobs")) {
			SequenceNode jobsNode = (SequenceNode) specTuple.getValueNode();
			for (Node jobsNodeItem: jobsNode.getValue()) {
				MappingNode jobNode = (MappingNode) jobsNodeItem;
				for (Iterator<NodeTuple> itJobTuple = jobNode.getValue().iterator(); itJobTuple.hasNext();) {
					NodeTuple jobTuple = itJobTuple.next();
					String jobTupleKey = ((ScalarNode)jobTuple.getKeyNode()).getValue();
					if (jobTupleKey.equals("submoduleCredentials")) {
						itJobTuple.remove();
					} else if (jobTupleKey.equals("projectDependencies")) {
						SequenceNode projectDependenciesNode = (SequenceNode) jobTuple.getValueNode();
						for (Node projectDependenciesItem: projectDependenciesNode.getValue()) {
							MappingNode projectDependencyNode = (MappingNode) projectDependenciesItem;
							for (Iterator<NodeTuple> itProjectDependencyTuple = projectDependencyNode.getValue().iterator(); 
									itProjectDependencyTuple.hasNext();) {
								NodeTuple projectDependencyTuple = itProjectDependencyTuple.next();
								if (((ScalarNode)projectDependencyTuple.getKeyNode()).getValue().equals("authentication"))
									itProjectDependencyTuple.remove();
							}								
						}
					}
				}
				NodeTuple cloneCredentialTuple = new NodeTuple(
						new ScalarNode(Tag.STR, "cloneCredential"), 
						new MappingNode(new Tag("!DefaultCredential"), Lists.newArrayList(), FlowStyle.BLOCK));
				jobNode.getValue().add(cloneCredentialTuple);
			}
		}
	}
}
 
Example 3
Source File: OpenShiftClusterConstructor.java    From launchpad-missioncontrol with Apache License 2.0 5 votes vote down vote up
@Override
public List<OpenShiftCluster> construct(Node node) {
    List<OpenShiftCluster> clusters = new ArrayList<>();
    SequenceNode sequenceNode = (SequenceNode) node;
    for (Node n : sequenceNode.getValue()) {
        MappingNode mapNode = (MappingNode) n;
        Map<Object, Object> valueMap = constructMapping(mapNode);
        String id = (String) valueMap.get("id");
        String apiUrl = (String) valueMap.get("apiUrl");
        String consoleUrl = (String) valueMap.get("consoleUrl");
        clusters.add(new OpenShiftCluster(id, apiUrl, consoleUrl));
    }
    return clusters;
}
 
Example 4
Source File: BundleCacher.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Object construct(Node node)
{
	node.setType(SmartTypingPairsElement.class);
	String path = getPath(node);
	SmartTypingPairsElement be = new SmartTypingPairsElement(path);
	MappingNode mapNode = (MappingNode) node;
	List<NodeTuple> tuples = mapNode.getValue();
	for (NodeTuple tuple : tuples)
	{
		ScalarNode keyNode = (ScalarNode) tuple.getKeyNode();
		String key = keyNode.getValue();
		// "pairs", "scope", "displayName" are required
		if ("pairs".equals(key)) //$NON-NLS-1$
		{
			SequenceNode pairsValueNode = (SequenceNode) tuple.getValueNode();
			List<Character> pairs = new ArrayList<Character>();
			List<Node> pairsValues = pairsValueNode.getValue();
			for (Node pairValue : pairsValues)
			{
				ScalarNode blah = (ScalarNode) pairValue;
				String pairCharacter = blah.getValue();
				pairs.add(Character.valueOf(pairCharacter.charAt(0)));
			}
			be.setPairs(pairs);
		}
		else if ("scope".equals(key)) //$NON-NLS-1$
		{
			ScalarNode scopeValueNode = (ScalarNode) tuple.getValueNode();
			be.setScope(scopeValueNode.getValue());
		}
		else if ("displayName".equals(key)) //$NON-NLS-1$
		{
			ScalarNode displayNameNode = (ScalarNode) tuple.getValueNode();
			be.setDisplayName(displayNameNode.getValue());
		}
	}
	return be;
}
 
Example 5
Source File: YauaaVersion.java    From yauaa with Apache License 2.0 5 votes vote down vote up
public static void assertSameVersion(NodeTuple versionNodeTuple, String filename) {
    // Check the version information from the Yaml files
    SequenceNode versionNode = getValueAsSequenceNode(versionNodeTuple, filename);
    String gitCommitIdDescribeShort = null;
    String buildTimestamp = null;
    String projectVersion = null;

    List<Node> versionList = versionNode.getValue();
    for (Node versionEntry : versionList) {
        requireNodeInstanceOf(MappingNode.class, versionEntry, filename, "The entry MUST be a mapping");
        NodeTuple entry = getExactlyOneNodeTuple((MappingNode) versionEntry, filename);
        String key = getKeyAsString(entry, filename);
        String value = getValueAsString(entry, filename);
        switch (key) {
            case "git_commit_id_describe_short":
                gitCommitIdDescribeShort = value;
                break;
            case "build_timestamp":
                buildTimestamp = value;
                break;
            case "project_version":
                projectVersion = value;
                break;
            case "copyright":
            case "license":
            case "url":
                // Ignore those two when comparing.
                break;
            default:
                throw new InvalidParserConfigurationException(
                    "Yaml config.(" + filename + ":" + versionNode.getStartMark().getLine() + "): " +
                        "Found unexpected config entry: " + key + ", allowed are " +
                        "'git_commit_id_describe_short', 'build_timestamp' and 'project_version'");
        }
    }
    assertSameVersion(gitCommitIdDescribeShort, buildTimestamp, projectVersion);
}
 
Example 6
Source File: SafeConstructor.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public Object construct(Node node) {
    // Note: we do not check for duplicate keys, because it's too
    // CPU-expensive.
    Map<Object, Object> omap = new LinkedHashMap<Object, Object>();
    if (!(node instanceof SequenceNode)) {
        throw new ConstructorException("while constructing an ordered map",
                node.getStartMark(), "expected a sequence, but found " + node.getNodeId(),
                node.getStartMark());
    }
    SequenceNode snode = (SequenceNode) node;
    for (Node subnode : snode.getValue()) {
        if (!(subnode instanceof MappingNode)) {
            throw new ConstructorException("while constructing an ordered map",
                    node.getStartMark(), "expected a mapping of length 1, but found "
                            + subnode.getNodeId(), subnode.getStartMark());
        }
        MappingNode mnode = (MappingNode) subnode;
        if (mnode.getValue().size() != 1) {
            throw new ConstructorException("while constructing an ordered map",
                    node.getStartMark(), "expected a single mapping item, but found "
                            + mnode.getValue().size() + " items", mnode.getStartMark());
        }
        Node keyNode = mnode.getValue().get(0).getKeyNode();
        Node valueNode = mnode.getValue().get(0).getValueNode();
        Object key = constructObject(keyNode);
        Object value = constructObject(valueNode);
        omap.put(key, value);
    }
    return omap;
}
 
Example 7
Source File: SafeConstructor.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public Object construct(Node node) {
    // Note: we do not check for duplicate keys, because it's too
    // CPU-expensive.
    if (!(node instanceof SequenceNode)) {
        throw new ConstructorException("while constructing pairs", node.getStartMark(),
                "expected a sequence, but found " + node.getNodeId(), node.getStartMark());
    }
    SequenceNode snode = (SequenceNode) node;
    List<Object[]> pairs = new ArrayList<Object[]>(snode.getValue().size());
    for (Node subnode : snode.getValue()) {
        if (!(subnode instanceof MappingNode)) {
            throw new ConstructorException("while constructingpairs", node.getStartMark(),
                    "expected a mapping of length 1, but found " + subnode.getNodeId(),
                    subnode.getStartMark());
        }
        MappingNode mnode = (MappingNode) subnode;
        if (mnode.getValue().size() != 1) {
            throw new ConstructorException("while constructing pairs", node.getStartMark(),
                    "expected a single mapping item, but found " + mnode.getValue().size()
                            + " items", mnode.getStartMark());
        }
        Node keyNode = mnode.getValue().get(0).getKeyNode();
        Node valueNode = mnode.getValue().get(0).getValueNode();
        Object key = constructObject(keyNode);
        Object value = constructObject(valueNode);
        pairs.add(new Object[] { key, value });
    }
    return pairs;
}
 
Example 8
Source File: YamlDeserializationData.java    From Diorite with MIT License 5 votes vote down vote up
@Override
public <T, C extends Collection<T>> void getAsCollection(String key, Class<T> type, C collection)
{
    Node node = this.getNode(this.node, key);
    if (node == null)
    {
        throw new DeserializationException(type, this, "Can't find valid value for key: " + key);
    }
    if (node instanceof SequenceNode)
    {
        SequenceNode sequenceNode = (SequenceNode) node;
        for (Node nodeValue : sequenceNode.getValue())
        {
            collection.add(this.deserializeSpecial(type, nodeValue, null));
        }
    }
    else if (node instanceof MappingNode)
    {
        MappingNode mappingNode = (MappingNode) node;
        for (NodeTuple tuple : mappingNode.getValue())
        {
            collection.add(this.deserializeSpecial(type, tuple.getValueNode(), null));
        }
    }
    else
    {
        throw new DeserializationException(type, this, "Can't find valid value for key: " + key);
    }
}
 
Example 9
Source File: SafeConstructor.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public Object construct(Node node) {
    // Note: we do not check for duplicate keys, because it's too
    // CPU-expensive.
    if (!(node instanceof SequenceNode)) {
        throw new ConstructorException("while constructing pairs", node.getStartMark(),
                "expected a sequence, but found " + node.getNodeId(), node.getStartMark());
    }
    SequenceNode snode = (SequenceNode) node;
    List<Object[]> pairs = new ArrayList<Object[]>(snode.getValue().size());
    for (Node subnode : snode.getValue()) {
        if (!(subnode instanceof MappingNode)) {
            throw new ConstructorException("while constructingpairs", node.getStartMark(),
                    "expected a mapping of length 1, but found " + subnode.getNodeId(),
                    subnode.getStartMark());
        }
        MappingNode mnode = (MappingNode) subnode;
        if (mnode.getValue().size() != 1) {
            throw new ConstructorException("while constructing pairs", node.getStartMark(),
                    "expected a single mapping item, but found " + mnode.getValue().size()
                            + " items", mnode.getStartMark());
        }
        Node keyNode = mnode.getValue().get(0).getKeyNode();
        Node valueNode = mnode.getValue().get(0).getValueNode();
        Object key = constructObject(keyNode);
        Object value = constructObject(valueNode);
        pairs.add(new Object[] { key, value });
    }
    return pairs;
}
 
Example 10
Source File: SafeConstructor.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public Object construct(Node node) {
    // Note: we do not check for duplicate keys, because it's too
    // CPU-expensive.
    Map<Object, Object> omap = new LinkedHashMap<Object, Object>();
    if (!(node instanceof SequenceNode)) {
        throw new ConstructorException("while constructing an ordered map",
                node.getStartMark(), "expected a sequence, but found " + node.getNodeId(),
                node.getStartMark());
    }
    SequenceNode snode = (SequenceNode) node;
    for (Node subnode : snode.getValue()) {
        if (!(subnode instanceof MappingNode)) {
            throw new ConstructorException("while constructing an ordered map",
                    node.getStartMark(), "expected a mapping of length 1, but found "
                            + subnode.getNodeId(), subnode.getStartMark());
        }
        MappingNode mnode = (MappingNode) subnode;
        if (mnode.getValue().size() != 1) {
            throw new ConstructorException("while constructing an ordered map",
                    node.getStartMark(), "expected a single mapping item, but found "
                            + mnode.getValue().size() + " items", mnode.getStartMark());
        }
        Node keyNode = mnode.getValue().get(0).getKeyNode();
        Node valueNode = mnode.getValue().get(0).getValueNode();
        Object key = constructObject(keyNode);
        Object value = constructObject(valueNode);
        omap.put(key, value);
    }
    return omap;
}
 
Example 11
Source File: BaseConstructor.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
protected void constructSequenceStep2(SequenceNode node, Collection<Object> collection) {
    for (Node child : node.getValue()) {
        collection.add(constructObject(child));
    }
}
 
Example 12
Source File: AbstractUserAgentAnalyzerDirect.java    From yauaa with Apache License 2.0 4 votes vote down vote up
private synchronized void loadYaml(Yaml yaml, InputStream yamlStream, String filename) {
    Node loadedYaml;
    try {
        loadedYaml = yaml.compose(new UnicodeReader(yamlStream));
    } catch (Exception e) {
        throw new InvalidParserConfigurationException("Parse error in the file " + filename + ": " + e.getMessage(), e);
    }

    if (loadedYaml == null) {
        LOG.warn("The file {} is empty", filename);
        return;
    }

    // Get and check top level config
    requireNodeInstanceOf(MappingNode.class, loadedYaml, filename, "File must be a Map");

    MappingNode rootNode = (MappingNode) loadedYaml;

    NodeTuple configNodeTuple = null;
    for (NodeTuple tuple : rootNode.getValue()) {
        String name = getKeyAsString(tuple, filename);
        if ("config".equals(name)) {
            configNodeTuple = tuple;
            break;
        }
        if ("version".equals(name)) {
            // Check the version information from the Yaml files
            assertSameVersion(tuple, filename);
            return;
        }
    }

    require(configNodeTuple != null, loadedYaml, filename, "The top level entry MUST be 'config'.");

    SequenceNode configNode = getValueAsSequenceNode(configNodeTuple, filename);
    List<Node> configList = configNode.getValue();

    for (Node configEntry : configList) {
        requireNodeInstanceOf(MappingNode.class, configEntry, filename, "The entry MUST be a mapping");
        NodeTuple entry = getExactlyOneNodeTuple((MappingNode) configEntry, filename);
        MappingNode actualEntry = getValueAsMappingNode(entry, filename);
        String entryType = getKeyAsString(entry, filename);
        switch (entryType) {
            case "lookup":
                loadYamlLookup(actualEntry, filename);
                break;
            case "set":
                loadYamlLookupSets(actualEntry, filename);
                break;
            case "matcher":
                loadYamlMatcher(actualEntry, filename);
                break;
            case "test":
                if (loadTests) {
                    loadYamlTestcase(actualEntry, filename);
                }
                break;
            default:
                throw new InvalidParserConfigurationException(
                    "Yaml config.(" + filename + ":" + actualEntry.getStartMark().getLine() + "): " +
                        "Found unexpected config entry: " + entryType + ", allowed are 'lookup', 'set', 'matcher' and 'test'");
        }
    }
}
 
Example 13
Source File: SafeConstructor.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
/**
 * Does merge for supplied mapping node.
 * 
 * @param node
 *            where to merge
 * @param isPreffered
 *            true if keys of node should take precedence over others...
 * @param key2index
 *            maps already merged keys to index from values
 * @param values
 *            collects merged NodeTuple
 * @return list of the merged NodeTuple (to be set as value for the
 *         MappingNode)
 */
private List<NodeTuple> mergeNode(MappingNode node, boolean isPreffered,
        Map<Object, Integer> key2index, List<NodeTuple> values) {
    List<NodeTuple> nodeValue = node.getValue();
    // reversed for http://code.google.com/p/snakeyaml/issues/detail?id=139
    Collections.reverse(nodeValue);
    for (Iterator<NodeTuple> iter = nodeValue.iterator(); iter.hasNext();) {
        final NodeTuple nodeTuple = iter.next();
        final Node keyNode = nodeTuple.getKeyNode();
        final Node valueNode = nodeTuple.getValueNode();
        if (keyNode.getTag().equals(Tag.MERGE)) {
            iter.remove();
            switch (valueNode.getNodeId()) {
            case mapping:
                MappingNode mn = (MappingNode) valueNode;
                mergeNode(mn, false, key2index, values);
                break;
            case sequence:
                SequenceNode sn = (SequenceNode) valueNode;
                List<Node> vals = sn.getValue();
                for (Node subnode : vals) {
                    if (!(subnode instanceof MappingNode)) {
                        throw new ConstructorException("while constructing a mapping",
                                node.getStartMark(),
                                "expected a mapping for merging, but found "
                                        + subnode.getNodeId(), subnode.getStartMark());
                    }
                    MappingNode mnode = (MappingNode) subnode;
                    mergeNode(mnode, false, key2index, values);
                }
                break;
            default:
                throw new ConstructorException("while constructing a mapping",
                        node.getStartMark(),
                        "expected a mapping or list of mappings for merging, but found "
                                + valueNode.getNodeId(), valueNode.getStartMark());
            }
        } else {
            // we need to construct keys to avoid duplications
            Object key = constructObject(keyNode);
            if (!key2index.containsKey(key)) { // 1st time merging key
                values.add(nodeTuple);
                // keep track where tuple for the key is
                key2index.put(key, values.size() - 1);
            } else if (isPreffered) { // there is value for the key, but we
                                      // need to override it
                // change value for the key using saved position
                values.set(key2index.get(key), nodeTuple);
            }
        }
    }
    return values;
}
 
Example 14
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 15
Source File: BaseConstructor.java    From onedev with MIT License 4 votes vote down vote up
protected void constructSequenceStep2(SequenceNode node, Collection<Object> collection) {
    for (Node child : node.getValue()) {
        collection.add(constructObject(child));
    }
}
 
Example 16
Source File: SafeConstructor.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Does merge for supplied mapping node.
 * 
 * @param node
 *            where to merge
 * @param isPreffered
 *            true if keys of node should take precedence over others...
 * @param key2index
 *            maps already merged keys to index from values
 * @param values
 *            collects merged NodeTuple
 * @return list of the merged NodeTuple (to be set as value for the
 *         MappingNode)
 */
private List<NodeTuple> mergeNode(MappingNode node, boolean isPreffered,
        Map<Object, Integer> key2index, List<NodeTuple> values) {
    List<NodeTuple> nodeValue = node.getValue();
    // reversed for http://code.google.com/p/snakeyaml/issues/detail?id=139
    Collections.reverse(nodeValue);
    for (Iterator<NodeTuple> iter = nodeValue.iterator(); iter.hasNext();) {
        final NodeTuple nodeTuple = iter.next();
        final Node keyNode = nodeTuple.getKeyNode();
        final Node valueNode = nodeTuple.getValueNode();
        if (keyNode.getTag().equals(Tag.MERGE)) {
            iter.remove();
            switch (valueNode.getNodeId()) {
            case mapping:
                MappingNode mn = (MappingNode) valueNode;
                mergeNode(mn, false, key2index, values);
                break;
            case sequence:
                SequenceNode sn = (SequenceNode) valueNode;
                List<Node> vals = sn.getValue();
                for (Node subnode : vals) {
                    if (!(subnode instanceof MappingNode)) {
                        throw new ConstructorException("while constructing a mapping",
                                node.getStartMark(),
                                "expected a mapping for merging, but found "
                                        + subnode.getNodeId(), subnode.getStartMark());
                    }
                    MappingNode mnode = (MappingNode) subnode;
                    mergeNode(mnode, false, key2index, values);
                }
                break;
            default:
                throw new ConstructorException("while constructing a mapping",
                        node.getStartMark(),
                        "expected a mapping or list of mappings for merging, but found "
                                + valueNode.getNodeId(), valueNode.getStartMark());
            }
        } else {
            // we need to construct keys to avoid duplications
            Object key = constructObject(keyNode);
            if (!key2index.containsKey(key)) { // 1st time merging key
                values.add(nodeTuple);
                // keep track where tuple for the key is
                key2index.put(key, values.size() - 1);
            } else if (isPreffered) { // there is value for the key, but we
                                      // need to override it
                // change value for the key using saved position
                values.set(key2index.get(key), nodeTuple);
            }
        }
    }
    return values;
}
 
Example 17
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 18
Source File: BundleCacher.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Fix for https://aptana.lighthouseapp.com/projects/35272/tickets/1658 Sets the prefix triggers for
 * CommandElements and subclasses. Fixes an issue where "[def]" isn't treated as an array of strings with
 * "def" as an item in it (and would instead think it's a string of "[def]").
 */
protected void setPrefixTriggers(Node node, CommandElement be)
{
	MappingNode mapNode = (MappingNode) node;
	List<NodeTuple> tuples = mapNode.getValue();
	for (NodeTuple tuple : tuples)
	{
		ScalarNode keyNode = (ScalarNode) tuple.getKeyNode();
		String key = keyNode.getValue();
		if ("customProperties".equals(key)) //$NON-NLS-1$
		{
			Node customPropertiesValueNode = tuple.getValueNode();
			if (customPropertiesValueNode instanceof MappingNode)
			{
				MappingNode custompropertiesNode = (MappingNode) customPropertiesValueNode;
				for (NodeTuple propTuple : custompropertiesNode.getValue())
				{
					ScalarNode propKeyNode = (ScalarNode) propTuple.getKeyNode();
					if ("prefix_values".equals(propKeyNode.getValue())) //$NON-NLS-1$
					{
						SequenceNode prefixValuesNode = (SequenceNode) propTuple.getValueNode();
						List<String> values = new ArrayList<String>();
						for (Node prefixValue : prefixValuesNode.getValue())
						{
							if (prefixValue instanceof ScalarNode)
							{
								ScalarNode blah = (ScalarNode) prefixValue;
								values.add(blah.getValue());
							}
							else
							{
								IdeLog.logWarning(ScriptingActivator.getDefault(),
										"Expected a flattened array for trigger, but got nested arrays."); //$NON-NLS-1$
							}
						}
						be.setTrigger(TriggerType.PREFIX.getName(),
								values.toArray(new String[values.size()]));
					}
				}
			}
		}
	}
}
 
Example 19
Source File: Serializer.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
private void serializeNode(Node node, Node parent) throws IOException {
    if (node.getNodeId() == NodeId.anchor) {
        node = ((AnchorNode) node).getRealNode();
    }
    String tAlias = this.anchors.get(node);
    if (this.serializedNodes.contains(node)) {
        this.emitter.emit(new AliasEvent(tAlias, null, null));
    } else {
        this.serializedNodes.add(node);
        switch (node.getNodeId()) {
        case scalar:
            ScalarNode scalarNode = (ScalarNode) node;
            Tag detectedTag = this.resolver.resolve(NodeId.scalar, scalarNode.getValue(), true);
            Tag defaultTag = this.resolver.resolve(NodeId.scalar, scalarNode.getValue(), false);
            ImplicitTuple tuple = new ImplicitTuple(node.getTag().equals(detectedTag), node
                    .getTag().equals(defaultTag));
            ScalarEvent event = new ScalarEvent(tAlias, node.getTag().getValue(), tuple,
                    scalarNode.getValue(), null, null, scalarNode.getStyle());
            this.emitter.emit(event);
            break;
        case sequence:
            SequenceNode seqNode = (SequenceNode) node;
            boolean implicitS = (node.getTag().equals(this.resolver.resolve(NodeId.sequence,
                    null, true)));
            this.emitter.emit(new SequenceStartEvent(tAlias, node.getTag().getValue(),
                    implicitS, null, null, seqNode.getFlowStyle()));
            int indexCounter = 0;
            List<Node> list = seqNode.getValue();
            for (Node item : list) {
                serializeNode(item, node);
                indexCounter++;
            }
            this.emitter.emit(new SequenceEndEvent(null, null));
            break;
        default:// instance of MappingNode
            Tag implicitTag = this.resolver.resolve(NodeId.mapping, null, true);
            boolean implicitM = (node.getTag().equals(implicitTag));
            this.emitter.emit(new MappingStartEvent(tAlias, node.getTag().getValue(),
                    implicitM, null, null, ((CollectionNode) node).getFlowStyle()));
            MappingNode mnode = (MappingNode) node;
            List<NodeTuple> map = mnode.getValue();
            for (NodeTuple row : map) {
                Node key = row.getKeyNode();
                Node value = row.getValueNode();
                serializeNode(key, mnode);
                serializeNode(value, mnode);
            }
            this.emitter.emit(new MappingEndEvent(null, null));
        }
    }
}
 
Example 20
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;
}