Search in sources :

Example 71 with Yaml

use of org.yaml.snakeyaml.Yaml in project pom-manipulation-ext by release-engineering.

the class YamlTest method readYamlViaMap.

@Test
public void readYamlViaMap() throws ManipulationException, IOException {
    Representer representer = new Representer();
    representer.getPropertyUtils().setSkipMissingProperties(true);
    Yaml y = new Yaml(representer);
    Map config = (Map) y.load(new FileInputStream(yamlFile));
    Map usersConfig = (Map) config.get("pme");
    assertTrue(usersConfig.size() > 0);
}
Also used : Representer(org.yaml.snakeyaml.representer.Representer) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 72 with Yaml

use of org.yaml.snakeyaml.Yaml in project google-cloud-intellij by GoogleCloudPlatform.

the class DefaultAppEngineProjectService method getValueFromAppYaml.

/**
 * Returns the value of a key-value pair for a given {@code key}, on the file located at {@code
 * appYamlPathString}.
 *
 * @return a String with the value, or an empty Optional if app.yaml isn't a regular file, or if
 *     there is any error getting the value
 * @throws MalformedYamlFileException when an app.yaml isn't syntactically well formed
 */
private Optional<String> getValueFromAppYaml(@NotNull String appYamlPathString, @NotNull String key) throws MalformedYamlFileException {
    Yaml yamlParser = new Yaml();
    Path appYamlPath = Paths.get(appYamlPathString);
    if (!Files.isRegularFile(appYamlPath)) {
        return Optional.empty();
    }
    try (BufferedReader reader = Files.newBufferedReader(appYamlPath, Charset.defaultCharset())) {
        Object parseResult = yamlParser.load(reader);
        if (!(parseResult instanceof Map)) {
            return Optional.empty();
        }
        // It's possible to get rid of this unchecked cast using a loadAs(file,
        // AppEngineYamlWebApp.class) sort of approach.
        Map<String, String> yamlMap = (Map<String, String>) parseResult;
        return yamlMap.containsKey(key) ? Optional.of(yamlMap.get(key)) : Optional.empty();
    } catch (ScannerException se) {
        throw new MalformedYamlFileException(se);
    } catch (InvalidPathException | IOException ioe) {
        return Optional.empty();
    }
}
Also used : Path(java.nio.file.Path) ScannerException(org.yaml.snakeyaml.scanner.ScannerException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml) InvalidPathException(java.nio.file.InvalidPathException)

Example 73 with Yaml

use of org.yaml.snakeyaml.Yaml in project pmd by pmd.

the class SidebarGeneratorTest method testSidebar.

@Test
public void testSidebar() throws IOException {
    Map<Language, List<RuleSet>> rulesets = new HashMap<>();
    RuleSet ruleSet1 = new RuleSetFactory().createNewRuleSet("test", "test", "bestpractices.xml", Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
    RuleSet ruleSet2 = new RuleSetFactory().createNewRuleSet("test2", "test", "codestyle.xml", Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
    rulesets.put(LanguageRegistry.findLanguageByTerseName("java"), Arrays.asList(ruleSet1, ruleSet2));
    rulesets.put(LanguageRegistry.findLanguageByTerseName("ecmascript"), Arrays.asList(ruleSet1));
    SidebarGenerator generator = new SidebarGenerator(writer, FileSystems.getDefault().getPath(".."));
    List<Map<String, Object>> result = generator.generateRuleReferenceSection(rulesets);
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(FlowStyle.BLOCK);
    if (SystemUtils.IS_OS_WINDOWS) {
        options.setLineBreak(LineBreak.WIN);
    }
    String yaml = new Yaml(options).dump(result);
    assertEquals(IOUtils.toString(SidebarGeneratorTest.class.getResourceAsStream("sidebar.yml")), yaml);
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSet(net.sourceforge.pmd.RuleSet) Language(net.sourceforge.pmd.lang.Language) HashMap(java.util.HashMap) DumperOptions(org.yaml.snakeyaml.DumperOptions) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml) Test(org.junit.Test)

Example 74 with Yaml

use of org.yaml.snakeyaml.Yaml in project Nukkit by Nukkit.

the class PluginBase method reloadConfig.

@Override
public void reloadConfig() {
    this.config = new Config(this.configFile);
    InputStream configStream = this.getResource("config.yml");
    if (configStream != null) {
        DumperOptions dumperOptions = new DumperOptions();
        dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        Yaml yaml = new Yaml(dumperOptions);
        try {
            this.config.setDefault(yaml.loadAs(Utils.readFile(this.configFile), LinkedHashMap.class));
        } catch (IOException e) {
            Server.getInstance().getLogger().logException(e);
        }
    }
}
Also used : Config(cn.nukkit.utils.Config) InputStream(java.io.InputStream) DumperOptions(org.yaml.snakeyaml.DumperOptions) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml) LinkedHashMap(java.util.LinkedHashMap)

Example 75 with Yaml

use of org.yaml.snakeyaml.Yaml in project Nukkit by Nukkit.

the class Config method parseContent.

private void parseContent(String content) {
    switch(this.type) {
        case Config.PROPERTIES:
            this.parseProperties(content);
            break;
        case Config.JSON:
            GsonBuilder builder = new GsonBuilder();
            Gson gson = builder.create();
            this.config = new ConfigSection(gson.fromJson(content, new TypeToken<LinkedHashMap<String, Object>>() {
            }.getType()));
            break;
        case Config.YAML:
            DumperOptions dumperOptions = new DumperOptions();
            dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
            Yaml yaml = new Yaml(dumperOptions);
            this.config = new ConfigSection(yaml.loadAs(content, LinkedHashMap.class));
            break;
        // case Config.SERIALIZED
        case Config.ENUM:
            this.parseList(content);
            break;
        default:
            this.correct = false;
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) TypeToken(com.google.gson.reflect.TypeToken) Gson(com.google.gson.Gson) DumperOptions(org.yaml.snakeyaml.DumperOptions) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

Yaml (org.yaml.snakeyaml.Yaml)276 Map (java.util.Map)104 HashMap (java.util.HashMap)85 IOException (java.io.IOException)58 FileInputStream (java.io.FileInputStream)49 InputStream (java.io.InputStream)49 File (java.io.File)43 DumperOptions (org.yaml.snakeyaml.DumperOptions)42 Constructor (org.yaml.snakeyaml.constructor.Constructor)30 Test (org.junit.Test)26 ArrayList (java.util.ArrayList)25 FileNotFoundException (java.io.FileNotFoundException)22 SafeConstructor (org.yaml.snakeyaml.constructor.SafeConstructor)22 List (java.util.List)21 Writer (java.io.Writer)18 Path (java.nio.file.Path)17 LinkedHashMap (java.util.LinkedHashMap)17 Reader (java.io.Reader)16 Properties (java.util.Properties)14 InputStreamReader (java.io.InputStreamReader)13