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();
}
}
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;
}
}
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);
}
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));
}
}
}
}
}
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));
}
Aggregations