public static YamlMap from(InputStream yaml, boolean canReset) { InputStream input = canReset ? yaml : inMemoryCopyOf(yaml); YamlMap result = new YamlMap(); try { for (Object data : newLoader().loadAll(input)) { result.putAll(new YamlMap(data)); } } catch (MarkedYAMLException e) { try { input.reset(); throw yamlSyntaxErrorPrettifier.apply(e, IOUtils.toString(input, StandardCharsets.UTF_8)); } catch (IOException ignored) { // NOTREACHED } } return result; }
public MarkedYAMLException apply(MarkedYAMLException e, String yaml) throws IOException { Optional<YamlLine> prevLine = getLine(yaml, e.getProblemMark().getLine() - 1); Optional<YamlLine> line = getLine(e.getProblemMark().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 e; }
private MarkedYAMLException incorrectIndentationException(YamlLine prev, YamlLine current, MarkedYAMLException e) { Collection<Integer> validIndentations = new HashSet<>(); if (prev.startsBlock()) { validIndentations.add(prev.indentation() + 2); } else { for (int indent = 0; indent <= prev.indentation(); indent += 2) { validIndentations.add(indent); } } String message = String.format("Incorrect indentation of %d %s; expected %s", current.indentation(), English.plural(SPACE, current.indentation()), oneOfSpaces(validIndentations)); return new YamlSyntaxErrorException(message, e); }
private void assertInvalidYaml(String expected, String format, Object... args) { try { String yaml = String.format(format, args); YamlMap map = YamlMap.from(yaml); fail("No exception for invalid YAML; " + yaml + " ->\n" + map); } catch (MarkedYAMLException e) { assertTrue("Problem doesn't contain '" + expected + "': " + e.getProblem(), e.getProblem().contains(expected)); } }
/** * Gets the parameters from the input string. * @param text The input string. * @param numbers The list of line numbers. * @return A map containing the directive parameters. * @throws NightingaleException Something wrong happened, to be caught in * the higher levels. */ private static Map<String, Object> getParameters(String text, List<Integer> numbers) throws NightingaleException { if (text == null) { return new HashMap<String, Object>(); } Yaml yaml = new Yaml( new Constructor(), new Representer(), new DumperOptions(), new DirectiveResolver() ); try { @SuppressWarnings("unchecked") HashMap<String, Object> map = yaml.loadAs(text, HashMap.class); return map; } catch (MarkedYAMLException exception) { throw new NightingaleException( messages.getMessage( Messages.ERROR_VALIDATE_YAML_EXCEPTION, CommonUtils.getCollectionElements( numbers, "(", ")", ", " ) ), exception ); } }
private MarkedYAMLException itemOutsideSequence(MarkedYAMLException e) { return new YamlSyntaxErrorException("Item outside of sequence", e); }
public YamlSyntaxErrorException(String message, MarkedYAMLException cause) { super(cause.getContext(), cause.getContextMark(), message, cause.getProblemMark()); }
public static SwaggerError newYamlError(YAMLException exception) { int line = (exception instanceof MarkedYAMLException) ? ((MarkedYAMLException) exception).getProblemMark().getLine() + 1 : 1; return new SwaggerError(line, IMarker.SEVERITY_ERROR, 0, processor.rewriteMessage(exception)); }