public Mark getMark() { return new Mark(name, this.index, this.line, this.column, this.dataWindow, this.pointer); }
public Mark getMark() { return new Mark(name, this.line, this.column, this.dataWindow, this.pointer); }
public Mark getMark() { return new Mark(name, this.index, this.line, this.column, this.buffer, this.pointer); }
public Mark getMark() { return new Mark(name, this.index, this.line, this.column, this.buffer, this.pointer); }
private Mark getSafeNodeEndMark(Node node) { return node == null ? new Mark("", -1, -1, -1, "", -1) : node.getEndMark(); } }
private Mark getSafeNodeStartMark(Node node) { return node == null ? new Mark("", -1, -1, -1, "", -1) : node.getStartMark(); }
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)); }
@Override protected INodeParser<ArchiveRoot> getParser(Node rootNode, ParsingContextExecution context) throws ParsingException { if (rootNode instanceof MappingNode) { // try to find the tosca version DefinitionVersionInfo definitionVersionInfo = getToscaDefinitionVersion(((MappingNode) rootNode).getValue(), context); // call the parser for the given tosca version Map<String, INodeParser> registry = parserRegistriesByVersion.get(definitionVersionInfo.definitionVersion); if (registry == null) { throw new ParsingException(context.getFileName(), new ParsingError(ErrorCode.UNKNOWN_TOSCA_VERSION, "Definition version is not supported", definitionVersionInfo.definitionVersionTuple.getKeyNode().getStartMark(), "Version is not supported by Alien4Cloud", definitionVersionInfo.definitionVersionTuple.getValueNode().getStartMark(), definitionVersionInfo.definitionVersion)); } context.setRegistry(registry); context.setDefinitionVersion(definitionVersionInfo.definitionVersion); return registry.get(DEFINITION_TYPE); } else { throw new ParsingException(null, new ParsingError(ErrorCode.SYNTAX_ERROR, "File is not a valid tosca definition file.", new Mark("root", 0, 0, 0, null, 0), "The provided yaml file doesn't follow the Top-level key definitions of a valid TOSCA Simple profile file.", new Mark("root", 0, 0, 0, null, 0), "TOSCA Definitions")); } }
/** * 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; } }
@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(); } } }