Search in sources :

Example 1 with Mark

use of org.yaml.snakeyaml.error.Mark in project alien4cloud by alien4cloud.

the class ReferencedParserTest method referenceToMissingTypeShouldFail.

@Test
public void referenceToMissingTypeShouldFail() {
    ReferencedParser referencedParser = new ReferencedParser("missingParser");
    ParsingContextExecution contextExecution = new ParsingContextExecution();
    try {
        contextExecution.init();
        contextExecution.setRegistry(Maps.newHashMap());
        Node node = Mockito.mock(Node.class);
        Mockito.when(node.getStartMark()).thenReturn(new Mark("name", 0, 10, 10, "", 0));
        Mockito.when(node.getEndMark()).thenReturn(new Mark("name", 0, 10, 10, "", 0));
        referencedParser.parse(node, contextExecution);
        assertEquals(ParsingErrorLevel.ERROR, contextExecution.getParsingErrors().get(0).getErrorLevel());
        assertEquals(ErrorCode.ALIEN_MAPPING_ERROR, contextExecution.getParsingErrors().get(0).getErrorCode());
    } finally {
        ParsingContextExecution.destroy();
    }
}
Also used : ParsingContextExecution(alien4cloud.tosca.parser.ParsingContextExecution) Node(org.yaml.snakeyaml.nodes.Node) Mark(org.yaml.snakeyaml.error.Mark) Test(org.junit.Test)

Example 2 with Mark

use of org.yaml.snakeyaml.error.Mark in project alien4cloud by alien4cloud.

the class YamlParser method parseFile.

/**
 * Parse a yaml file into the given T instance.
 *
 * @param yamlStream Input stream that contains the yaml.
 * @param instance The instance to parse.
 * @return A parsing result that contains the parsing errors as well as the created instance.
 * @throws ParsingException In case there is a blocking issue while parsing the definition.
 */
public ParsingResult<T> parseFile(String filePath, String fileName, InputStream yamlStream, T instance) throws ParsingException {
    StreamReader sreader = new StreamReader(new UnicodeReader(yamlStream));
    Composer composer = new Composer(new ParserImpl(sreader), new Resolver());
    Node rootNode = null;
    try {
        rootNode = composer.getSingleNode();
        if (rootNode == null) {
            throw new ParsingException(fileName, new ParsingError(ErrorCode.SYNTAX_ERROR, "Empty file.", new Mark("root", 0, 0, 0, null, 0), "No yaml content found in file.", new Mark("root", 0, 0, 0, null, 0), filePath));
        }
    } catch (MarkedYAMLException exception) {
        throw new ParsingException(fileName, new ParsingError(ErrorCode.INVALID_YAML, exception));
    }
    try {
        return doParsing(fileName, rootNode, instance);
    } catch (ParsingException e) {
        e.setFileName(fileName);
        throw e;
    }
}
Also used : Composer(org.yaml.snakeyaml.composer.Composer) StreamReader(org.yaml.snakeyaml.reader.StreamReader) Resolver(org.yaml.snakeyaml.resolver.Resolver) Node(org.yaml.snakeyaml.nodes.Node) ParserImpl(org.yaml.snakeyaml.parser.ParserImpl) Mark(org.yaml.snakeyaml.error.Mark) UnicodeReader(org.yaml.snakeyaml.reader.UnicodeReader) MarkedYAMLException(org.yaml.snakeyaml.error.MarkedYAMLException)

Example 3 with Mark

use of org.yaml.snakeyaml.error.Mark in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class YamlSyntaxErrorPrettifier method apply.

public MarkedYAMLException apply(MarkedYAMLException e, String yaml) throws IOException {
    Mark problemMark = e.getProblemMark();
    Optional<YamlLine> prevLine = getLine(yaml, problemMark.getLine() - 1);
    Optional<YamlLine> line = getLine(problemMark.get_snippet(0, 100), 0);
    if (prevLine.isPresent() && line.isPresent()) {
        YamlLine prev = prevLine.get();
        YamlLine current = line.get();
        if (isIncorrectIndentation(prev, current)) {
            return incorrectIndentationException(prev, current, e);
        } else if (isItemOutsideSequence(prev, current)) {
            return itemOutsideSequence(e);
        }
    }
    return new YamlSyntaxErrorException(e.getMessage(), e);
}
Also used : Mark(org.yaml.snakeyaml.error.Mark)

Example 4 with Mark

use of org.yaml.snakeyaml.error.Mark in project alien4cloud by alien4cloud.

the class TopologyUtils method normalizeAllNodeTemplateName.

/**
 * In alien 4 Cloud we try
 * Rename the node template with an invalid name on the topology.
 *
 * @param topology
 * @param parsingErrors
 * @param objectToNodeMap
 */
public static void normalizeAllNodeTemplateName(Topology topology, List<ParsingError> parsingErrors, Map<Object, Node> objectToNodeMap) {
    if (topology.getNodeTemplates() != null && !topology.getNodeTemplates().isEmpty()) {
        Map<String, NodeTemplate> nodeTemplates = Maps.newHashMap(topology.getNodeTemplates());
        for (Map.Entry<String, NodeTemplate> nodeEntry : nodeTemplates.entrySet()) {
            String nodeName = nodeEntry.getKey();
            if (!NameValidationUtils.isValid(nodeName)) {
                String newName = StringUtils.stripAccents(nodeName);
                newName = NameValidationUtils.DEFAULT_NAME_REPLACE_PATTERN.matcher(newName).replaceAll("_");
                if (topology.getNodeTemplates().containsKey(newName)) {
                    int i = 1;
                    while (topology.getNodeTemplates().containsKey(newName + i)) {
                        i++;
                    }
                    newName = newName + i;
                }
                renameNodeTemplate(topology, nodeName, newName);
                if (parsingErrors != null) {
                    Node node = (Node) MapUtil.get(objectToNodeMap, nodeName);
                    Mark startMark = null;
                    Mark endMark = null;
                    if (node != null) {
                        startMark = node.getStartMark();
                        endMark = node.getEndMark();
                    }
                    parsingErrors.add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.INVALID_NAME, "NodeTemplate", startMark, nodeName, endMark, newName));
                }
            }
        }
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) ParsingError(alien4cloud.tosca.parser.ParsingError) Node(org.yaml.snakeyaml.nodes.Node) Mark(org.yaml.snakeyaml.error.Mark) Map(java.util.Map)

Example 5 with Mark

use of org.yaml.snakeyaml.error.Mark in project alien4cloud by alien4cloud.

the class ToscaParser method getToscaDefinitionVersion.

private DefinitionVersionInfo getToscaDefinitionVersion(List<NodeTuple> topLevelNodes, ParsingContextExecution context) throws ParsingException {
    boolean first = true;
    for (NodeTuple node : topLevelNodes) {
        Node key = node.getKeyNode();
        if (key instanceof ScalarNode) {
            ScalarNode scalarKey = (ScalarNode) key;
            if (scalarKey.getValue().equals("tosca_definitions_version")) {
                if (!first) {
                    // TOSCA definition version must be the first yaml element
                    context.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.TOSCA_VERSION_NOT_FIRST, "File is not a valid tosca definition file.", node.getKeyNode().getStartMark(), "tosca_definitions_version must be the first element of the document.", node.getValueNode().getEndMark(), null));
                }
                return new DefinitionVersionInfo(ParserUtils.getScalar(node.getValueNode(), context), node);
            }
        }
        first = false;
    }
    throw new ParsingException(null, new ParsingError(ErrorCode.MISSING_TOSCA_VERSION, "File is not a valid tosca definition file.", new Mark("root", 0, 0, 0, null, 0), "Unable to find the mandatory tosca_definitions_version.", new Mark("root", 0, 0, 0, null, 0), null));
}
Also used : ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) Node(org.yaml.snakeyaml.nodes.Node) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) Mark(org.yaml.snakeyaml.error.Mark) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple)

Aggregations

Mark (org.yaml.snakeyaml.error.Mark)6 Node (org.yaml.snakeyaml.nodes.Node)4 ParsingContextExecution (alien4cloud.tosca.parser.ParsingContextExecution)1 ParsingError (alien4cloud.tosca.parser.ParsingError)1 Map (java.util.Map)1 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)1 Test (org.junit.Test)1 YamlFileAST (org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST)1 Composer (org.yaml.snakeyaml.composer.Composer)1 MarkedYAMLException (org.yaml.snakeyaml.error.MarkedYAMLException)1 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)1 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)1 ScalarNode (org.yaml.snakeyaml.nodes.ScalarNode)1 ParserException (org.yaml.snakeyaml.parser.ParserException)1 ParserImpl (org.yaml.snakeyaml.parser.ParserImpl)1 StreamReader (org.yaml.snakeyaml.reader.StreamReader)1 UnicodeReader (org.yaml.snakeyaml.reader.UnicodeReader)1 Resolver (org.yaml.snakeyaml.resolver.Resolver)1 ScannerException (org.yaml.snakeyaml.scanner.ScannerException)1