org.yaml.snakeyaml.parser.ParserException Java Examples

The following examples show how to use org.yaml.snakeyaml.parser.ParserException. 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: YamlFileResourceProviderTest.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldThrowForInvalidFile() {
    // given
    File yamlFile = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "service/yaml/invalidYaml.yml");

    // when
    try {
        YamlFileResourceProvider.loadFromFile(yamlFile).createReader();

        // then
        fail("Expected exception to be thrown");
    } catch (YamlParseException e) {
        assertThat(e.getFile(), equalTo(yamlFile.getPath()));
        assertThat(e.getCause(), instanceOf(ParserException.class));
    }
}
 
Example #2
Source File: YamlConfigLoader.java    From mxisd with GNU Affero General Public License v3.0 6 votes vote down vote up
public static MxisdConfig loadFromFile(String path) throws IOException {
    File f = new File(path).getAbsoluteFile();
    log.info("Reading config from {}", f.toString());
    Representer rep = new Representer();
    rep.getPropertyUtils().setBeanAccess(BeanAccess.FIELD);
    rep.getPropertyUtils().setAllowReadOnlyProperties(true);
    rep.getPropertyUtils().setSkipMissingProperties(true);
    Yaml yaml = new Yaml(new Constructor(MxisdConfig.class), rep);
    try (FileInputStream is = new FileInputStream(f)) {
        MxisdConfig raw = yaml.load(is);
        log.debug("Read config in memory from {}", path);

        // SnakeYaml set objects to null when there is no value set in the config, even a full sub-tree.
        // This is problematic for default config values and objects, to avoid NPEs.
        // Therefore, we'll use Gson to re-parse the data in a way that avoids us checking the whole config for nulls.
        MxisdConfig cfg = GsonUtil.get().fromJson(GsonUtil.get().toJson(raw), MxisdConfig.class);

        log.info("Loaded config from {}", path);
        return cfg;
    } catch (ParserException t) {
        throw new ConfigurationException(t.getMessage(), "Could not parse YAML config file - Please check indentation and that the configuration options exist");
    }
}
 
Example #3
Source File: YamlProcessor.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
@Override
protected Map<Object, Object> constructMapping(MappingNode node) {
    try {
        return super.constructMapping(node);
    } catch (IllegalStateException ex) {
        throw new ParserException("while parsing MappingNode",
            node.getStartMark(),
            ex.getMessage(),
            node.getEndMark());
    }
}
 
Example #4
Source File: YamlProcessor.java    From canal with Apache License 2.0 5 votes vote down vote up
@Override
protected Map<Object, Object> constructMapping(MappingNode node) {
    try {
        return super.constructMapping(node);
    } catch (IllegalStateException ex) {
        throw new ParserException("while parsing MappingNode",
            node.getStartMark(),
            ex.getMessage(),
            node.getEndMark());
    }
}
 
Example #5
Source File: YamlErrorProcessor.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 5 votes vote down vote up
public String rewriteMessage(YAMLException exception) {
    if (exception instanceof ParserException) {
        if ("while parsing a block mapping".equals(((ParserException) exception).getContext())) {
            return Messages.error_yaml_parser_indentation;
        }
    }
    return exception != null ? exception.getLocalizedMessage() : null;
}
 
Example #6
Source File: ParseConfigException.java    From halyard with Apache License 2.0 5 votes vote down vote up
public ParseConfigException(ParserException e) {
  Problem problem =
      new ConfigProblemBuilder(
              Problem.Severity.FATAL, "Could not parse your halconfig: " + e.getMessage())
          .build();
  getProblems().add(problem);
}
 
Example #7
Source File: YamlPropertiesFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadResourcesWithInternalOverride() throws Exception {
	YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
	factory.setResources(new ByteArrayResource(
			"foo: bar\nspam:\n  foo: baz\nfoo: bucket".getBytes()));
	exception.expect(ParserException.class);
	factory.getObject();
}
 
Example #8
Source File: YamlProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testBadDocumentStart() throws Exception {
	this.processor.setResources(new ByteArrayResource(
			"foo # a document\nbar: baz".getBytes()));
	this.exception.expect(ParserException.class);
	this.exception.expectMessage("line 2, column 1");
	this.processor.process(new MatchCallback() {
		@Override
		public void process(Properties properties, Map<String, Object> map) {
		}
	});
}
 
Example #9
Source File: YamlProcessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Map<Object, Object> constructMapping(MappingNode node) {
	try {
		return super.constructMapping(node);
	}
	catch (IllegalStateException ex) {
		throw new ParserException("while parsing MappingNode",
				node.getStartMark(), ex.getMessage(), node.getEndMark());
	}
}
 
Example #10
Source File: YamlParser.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Override
protected Map<Object, Object> constructMapping(MappingNode node) {
  try {
    return super.constructMapping(node);
  } catch (IllegalStateException ex) {
    throw new ParserException("while parsing MappingNode", node.getStartMark(), ex.getMessage(), node.getEndMark());
  }
}
 
Example #11
Source File: YamlProcessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Map<Object, Object> constructMapping(MappingNode node) {
	try {
		return super.constructMapping(node);
	}
	catch (IllegalStateException ex) {
		throw new ParserException("while parsing MappingNode",
				node.getStartMark(), ex.getMessage(), node.getEndMark());
	}
}
 
Example #12
Source File: DefaultYamlConfigParse.java    From nacos-spring-project with Apache License 2.0 5 votes vote down vote up
@Override
protected Map<Object, Object> constructMapping(MappingNode node) {
	try {
		return super.constructMapping(node);
	}
	catch (IllegalStateException ex) {
		throw new ParserException("while parsing MappingNode",
				node.getStartMark(), ex.getMessage(), node.getEndMark());
	}
}
 
Example #13
Source File: YamlProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testBadDocumentStart() {
	this.processor.setResources(new ByteArrayResource("foo # a document\nbar: baz".getBytes()));
	this.exception.expect(ParserException.class);
	this.exception.expectMessage("line 2, column 1");
	this.processor.process((properties, map) -> {});
}
 
Example #14
Source File: YamlProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testBadDocumentStart() {
	this.processor.setResources(new ByteArrayResource("foo # a document\nbar: baz".getBytes()));
	assertThatExceptionOfType(ParserException.class).isThrownBy(() ->
			this.processor.process((properties, map) -> {}))
		.withMessageContaining("line 2, column 1");
}
 
Example #15
Source File: YamlParserTest.java    From apollo with Apache License 2.0 4 votes vote down vote up
@Test(expected = ParserException.class)
public void testcase8() throws Exception {
  testInvalid("case8.yaml");
}
 
Example #16
Source File: YamlParserTest.java    From apollo with Apache License 2.0 4 votes vote down vote up
@Test(expected = ParserException.class)
public void testcase2() throws Exception {
  testInvalid("case2.yaml");
}
 
Example #17
Source File: YamlMapFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test(expected = ParserException.class)
public void testDuplicateKey() throws Exception {
	this.factory.setResources(new ByteArrayResource[] {new ByteArrayResource(
			"mymap:\n  foo: bar\nmymap:\n  bar: foo".getBytes())});
	this.factory.getObject().get("mymap");
}