Search in sources :

Example 6 with Yaml

use of org.yaml.snakeyaml.Yaml in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class YamlConfiguration method expand.

@SuppressWarnings("unchecked")
private void expand(InputStream input) {
    Map<String, Object> source = new HashMap<>();
    for (Object data : new Yaml().loadAll(input)) {
        source.putAll((Map<String, Object>) data);
    }
    expand(source);
}
Also used : Yaml(org.yaml.snakeyaml.Yaml)

Example 7 with Yaml

use of org.yaml.snakeyaml.Yaml in project spring-boot by spring-projects.

the class OriginTrackedYamlLoader method createYaml.

@Override
protected Yaml createYaml() {
    BaseConstructor constructor = new OriginTrackingConstructor();
    Representer representer = new Representer();
    DumperOptions dumperOptions = new DumperOptions();
    LimitedResolver resolver = new LimitedResolver();
    return new Yaml(constructor, representer, dumperOptions, resolver);
}
Also used : BaseConstructor(org.yaml.snakeyaml.constructor.BaseConstructor) Representer(org.yaml.snakeyaml.representer.Representer) DumperOptions(org.yaml.snakeyaml.DumperOptions) Yaml(org.yaml.snakeyaml.Yaml)

Example 8 with Yaml

use of org.yaml.snakeyaml.Yaml in project spring-framework by spring-projects.

the class YamlPropertiesFactoryBeanTests method testYaml.

@SuppressWarnings("unchecked")
@Test
public void testYaml() {
    Yaml yaml = new Yaml();
    Map<String, ?> map = yaml.loadAs("foo: bar\nspam:\n  foo: baz", Map.class);
    assertThat(map.get("foo"), equalTo((Object) "bar"));
    assertThat(((Map<String, Object>) map.get("spam")).get("foo"), equalTo((Object) "baz"));
}
Also used : Yaml(org.yaml.snakeyaml.Yaml) Test(org.junit.Test)

Example 9 with Yaml

use of org.yaml.snakeyaml.Yaml in project es6draft by anba.

the class Test262Info method readYaml.

private void readYaml(String descriptor, boolean lenient) throws MalformedDataException {
    assert descriptor != null && !descriptor.isEmpty();
    Yaml yaml = null;
    if (lenient) {
        yaml = yamlQueue.poll();
    }
    if (yaml == null) {
        Constructor constructor = new Constructor(TestDescriptor.class);
        if (lenient) {
            PropertyUtils utils = new PropertyUtils();
            utils.setSkipMissingProperties(true);
            constructor.setPropertyUtils(utils);
        }
        yaml = new Yaml(constructor);
    }
    TestDescriptor desc;
    try {
        desc = yaml.loadAs(descriptor, TestDescriptor.class);
    } catch (YAMLException e) {
        throw new MalformedDataException(e.getMessage(), e);
    }
    if (lenient) {
        yamlQueue.offer(yaml);
    }
    this.description = desc.getDescription();
    this.includes = desc.getIncludes();
    this.features = desc.getFeatures();
    this.errorType = desc.getNegative();
    this.negative = desc.getNegative() != null;
    if (!desc.getFlags().isEmpty()) {
        if (!lenient) {
            for (String flag : desc.getFlags()) {
                if (!allowedFlags.contains(flag)) {
                    throw new MalformedDataException(String.format("Unknown flag '%s'", flag));
                }
            }
        }
        this.negative |= desc.getFlags().contains("negative");
        this.noStrict = desc.getFlags().contains("noStrict");
        this.onlyStrict = desc.getFlags().contains("onlyStrict");
        this.module = desc.getFlags().contains("module");
        this.raw = desc.getFlags().contains("raw");
    }
}
Also used : PropertyUtils(org.yaml.snakeyaml.introspector.PropertyUtils) Constructor(org.yaml.snakeyaml.constructor.Constructor) YAMLException(org.yaml.snakeyaml.error.YAMLException) Yaml(org.yaml.snakeyaml.Yaml)

Example 10 with Yaml

use of org.yaml.snakeyaml.Yaml in project siddhi by wso2.

the class DocumentationUtils method updateAPIPagesInMkdocsConfig.

/**
 * This add a new page to the list of pages in the mkdocs configuration
 *
 * @param mkdocsConfigFile           The mkdocs configuration file
 * @param documentationBaseDirectory The base directory of the documentation
 * @throws FileNotFoundException If mkdocs configuration file is not found
 */
public static void updateAPIPagesInMkdocsConfig(File mkdocsConfigFile, String documentationBaseDirectory) throws FileNotFoundException {
    // Retrieving the documentation file names
    File documentationDirectory = new File(documentationBaseDirectory + File.separator + Constants.API_SUB_DIRECTORY);
    String[] documentationFiles = documentationDirectory.list((directory, fileName) -> fileName.endsWith(Constants.MARKDOWN_FILE_EXTENSION));
    List<String> apiDirectoryContent;
    if (documentationFiles == null) {
        apiDirectoryContent = new ArrayList<>();
    } else {
        apiDirectoryContent = Arrays.asList(documentationFiles);
        apiDirectoryContent.sort(new Comparator<String>() {

            @Override
            public int compare(String s1, String s2) {
                String[] s1s = s1.split("\\D+");
                String[] s2s = s2.split("\\D+");
                int i = 0;
                while (s1s.length > i || s2s.length > i) {
                    String s1a = "0";
                    String s2a = "0";
                    if (s1s.length > i) {
                        s1a = s1s[i];
                    }
                    if (s2s.length > i) {
                        s2a = s2s[i];
                    }
                    int s1aInt = Integer.parseInt(s1a);
                    int s2aInt = Integer.parseInt(s2a);
                    if (s2aInt > s1aInt) {
                        return 1;
                    } else if (s2aInt < s1aInt) {
                        return -1;
                    }
                    i++;
                }
                return 0;
            }
        });
    }
    String latestVersionFile = null;
    if (apiDirectoryContent.size() > 1) {
        String first = apiDirectoryContent.get(0);
        String second = apiDirectoryContent.get(1);
        if (first.equals(Constants.LATEST_FILE_NAME + Constants.MARKDOWN_FILE_EXTENSION)) {
            latestVersionFile = second;
        }
    }
    // Creating yaml parser
    DumperOptions dumperOptions = new DumperOptions();
    dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    Yaml yaml = new Yaml(dumperOptions);
    // Reading the mkdocs configuration
    Map<String, Object> yamlConfig = (Map<String, Object>) yaml.load(new InputStreamReader(new FileInputStream(mkdocsConfigFile), Constants.DEFAULT_CHARSET));
    // Getting the pages list
    List<Map<String, Object>> yamlConfigPagesList = (List<Map<String, Object>>) yamlConfig.get(Constants.MKDOCS_CONFIG_PAGES_KEY);
    // Creating the new api pages list
    List<Map<String, Object>> apiPagesList = new ArrayList<>();
    for (String apiFile : apiDirectoryContent) {
        String pageName = apiFile.substring(0, apiFile.length() - Constants.MARKDOWN_FILE_EXTENSION.length());
        Map<String, Object> newPage = new HashMap<>();
        if (latestVersionFile != null && pageName.equals(Constants.LATEST_FILE_NAME)) {
            pageName = "Latest (" + latestVersionFile.substring(0, latestVersionFile.length() - Constants.MARKDOWN_FILE_EXTENSION.length()) + ")";
        }
        newPage.put(pageName, Constants.API_SUB_DIRECTORY + Constants.MKDOCS_FILE_SEPARATOR + apiFile);
        apiPagesList.add(newPage);
    }
    // Setting the new api pages
    Map<String, Object> yamlConfigAPIPage = null;
    for (Map<String, Object> yamlConfigPage : yamlConfigPagesList) {
        if (yamlConfigPage.get(Constants.MKDOCS_CONFIG_PAGES_API_KEY) != null) {
            yamlConfigAPIPage = yamlConfigPage;
            break;
        }
    }
    if (yamlConfigAPIPage == null) {
        yamlConfigAPIPage = new HashMap<>();
        yamlConfigPagesList.add(yamlConfigAPIPage);
    }
    yamlConfigAPIPage.put(Constants.MKDOCS_CONFIG_PAGES_API_KEY, apiPagesList);
    // Saving the updated configuration
    yaml.dump(yamlConfig, new OutputStreamWriter(new FileOutputStream(mkdocsConfigFile), Constants.DEFAULT_CHARSET));
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Yaml(org.yaml.snakeyaml.Yaml) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) DumperOptions(org.yaml.snakeyaml.DumperOptions) List(java.util.List) ArrayList(java.util.ArrayList) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Aggregations

Yaml (org.yaml.snakeyaml.Yaml)253 Map (java.util.Map)92 HashMap (java.util.HashMap)73 IOException (java.io.IOException)55 FileInputStream (java.io.FileInputStream)47 InputStream (java.io.InputStream)47 File (java.io.File)43 DumperOptions (org.yaml.snakeyaml.DumperOptions)39 Constructor (org.yaml.snakeyaml.constructor.Constructor)27 ArrayList (java.util.ArrayList)25 FileNotFoundException (java.io.FileNotFoundException)21 List (java.util.List)21 Test (org.junit.Test)21 LinkedHashMap (java.util.LinkedHashMap)17 Writer (java.io.Writer)13 FileWriter (java.io.FileWriter)12 InputStreamReader (java.io.InputStreamReader)12 Path (java.nio.file.Path)12 FileReader (java.io.FileReader)10 Reader (java.io.Reader)9