Java Code Examples for org.yaml.snakeyaml.Yaml#loadAll()

The following examples show how to use org.yaml.snakeyaml.Yaml#loadAll() . 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: PerlTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testMaps() {
    Yaml yaml = new Yaml(new CustomConstructor());
    String input = Util.getLocalResource("issues/issue56-1.yaml");
    int counter = 0;
    for (Object obj : yaml.loadAll(input)) {
        // System.out.println(obj);
        Map<String, Object> map = (Map<String, Object>) obj;
        Integer oid = (Integer) map.get("oid");
        if (oid == 123058) {
            ArrayList a = (ArrayList) map.get("sequences");
            LinkedHashMap b = (LinkedHashMap) a.get(0);
            LinkedHashMap c = (LinkedHashMap) b.get("atc");
            LinkedHashMap d = (LinkedHashMap) c.get("name");
            LinkedHashMap e = (LinkedHashMap) d.get("canonical");
            String acidNameDe = e.entrySet().toArray()[1].toString();
            assertEquals("Unicode escaped sequence must be decoded.",
                    ":de=Acetylsalicylsäure", acidNameDe);
        }
        assertTrue(oid > 10000);
        counter++;
    }
    assertEquals(4, counter);
    assertEquals(0, CodeBean.counter);
}
 
Example 2
Source File: ThesaurusLoader.java    From dice-heroes with GNU General Public License v3.0 6 votes vote down vote up
@Override public void loadAsync(AssetManager manager, String fileName, FileHandle file, ThesaurusLoader.ThesaurusParameter parameter) {
    Constructor constructor = new Constructor(ThesaurusData.class);
    Yaml yaml = new Yaml(constructor);
    ObjectMap<String, ThesaurusData> data = new ObjectMap<String, ThesaurusData>();
    for (Object o : yaml.loadAll(resolve(fileName).read())) {
        ThesaurusData description = (ThesaurusData) o;
        data.put(description.key, description);
    }
    if (parameter != null && parameter.other.length > 0) {
        for (String depName : parameter.other) {
            Thesaurus dep = manager.get(depName);
            data.putAll(dep.data);
        }
    }
    thesaurus = new Thesaurus(data);
}
 
Example 3
Source File: Coordinator.java    From cloudml with GNU Lesser General Public License v3.0 6 votes vote down vote up
public String process(String cmdLiteral, PeerStub from) {

        if (cmdLiteral.startsWith(ADDITIONAL_PREFIX)) {
            String additional = cmdLiteral.substring(ADDITIONAL_PREFIX.length());
            lastInstruction.addAdditional(additional);
            return (String) process(lastInstruction, from);
        }


        Yaml yaml = CloudMLCmds.INSTANCE.getYaml();

        String ret = "";
        for (Object cmd : yaml.loadAll(cmdLiteral)) {
            Object obj = null;
            if (cmd instanceof Instruction)
                obj = process((Instruction) cmd, from);
            else if (cmd instanceof Listener)
                obj = process((Listener) cmd, from);
            if (obj != null) {
                ret += String.format("###return of %s###\n%s\n", cmd.getClass().getSimpleName(), codec(obj));
            }
        }
        return ret;
    }
 
Example 4
Source File: PerlTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void testJavaBean() {
    Constructor c = new CustomBeanConstructor();
    Yaml yaml = new Yaml(c);
    String input = Util.getLocalResource("issues/issue56-1.yaml");
    int counter = 0;
    for (Object obj : yaml.loadAll(input)) {
        // System.out.println(obj);
        Map<String, Object> map = (Map<String, Object>) obj;
        Integer oid = (Integer) map.get("oid");
        assertTrue(oid > 10000);
        counter++;
    }
    assertEquals(4, counter);
    assertEquals(55, CodeBean.counter);
}
 
Example 5
Source File: ManifestUtils.java    From spring-cloud-skipper with Apache License 2.0 6 votes vote down vote up
/**
 * Resolve a kind from a raw manifest yaml.
 *
 * @param manifest the raw yaml
 * @return the kind or {@code null} if not found
 */
public static String resolveKind(String manifest) {
	if (!StringUtils.hasText(manifest)) {
		return null;
	}
	Yaml yaml = new Yaml();
	Iterable<Object> object = yaml.loadAll(manifest);
	for (Object o : object) {
		if (o != null && o instanceof Map) {
			Object kind = ((Map<?, ?>) o).get("kind");
			if (kind instanceof String) {
				return (String) kind;
			}
		}
	}
	return null;
}
 
Example 6
Source File: DefaultYamlConfigParse.java    From nacos-spring-project with Apache License 2.0 6 votes vote down vote up
protected static boolean process(MatchCallback callback, Yaml yaml, String content) {
	int count = 0;
	if (logger.isDebugEnabled()) {
		logger.debug("Loading from YAML: " + content);
	}
	for (Object object : yaml.loadAll(content)) {
		if (object != null && process(asMap(object), callback)) {
			count++;
		}
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Loaded " + count + " document" + (count > 1 ? "s" : "")
				+ " from YAML resource: " + content);
	}
	return (count > 0);
}
 
Example 7
Source File: LevelsLoader.java    From dice-heroes with GNU General Public License v3.0 6 votes vote down vote up
@Override @SuppressWarnings("unchecked") public void loadAsync(AssetManager manager, String fileName, FileHandle file, AssetLoaderParameters<Levels> parameter) {
    Yaml yaml = new Yaml();
    ObjectMap<String, BaseLevelDescription> data = new ObjectMap<String, BaseLevelDescription>();
    for (Object o : yaml.loadAll(resolve(fileName).read())) {
        HashMap<String, Object> value = (HashMap<String, Object>) o;
        String type = MapHelper.get(value, "type", "level");
        try {
            BaseLevelDescription desc = types.get(type).getConstructor(Map.class).newInstance(value);
            data.put(desc.name, desc);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
    levels = new Levels(data);
    Config.levels = levels;
}
 
Example 8
Source File: YamlProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private boolean process(MatchCallback callback, Yaml yaml, Resource resource) {
	int count = 0;
	try {
		if (logger.isDebugEnabled()) {
			logger.debug("Loading from YAML: " + resource);
		}
		try (Reader reader = new UnicodeReader(resource.getInputStream())) {
			for (Object object : yaml.loadAll(reader)) {
				if (object != null && process(asMap(object), callback)) {
					count++;
					if (this.resolutionMethod == ResolutionMethod.FIRST_FOUND) {
						break;
					}
				}
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Loaded " + count + " document" + (count > 1 ? "s" : "") +
						" from YAML resource: " + resource);
			}
		}
	}
	catch (IOException ex) {
		handleProcessError(resource, ex);
	}
	return (count > 0);
}
 
Example 9
Source File: SpringCloudDeployerApplicationManifestReader.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
public boolean canSupport(String manifest) {
	Yaml yaml = new Yaml();
	Iterable<Object> object = yaml.loadAll(manifest);
	for (Object o : object) {
		boolean supportKind = assertSupportedKind(o);
		if (!supportKind) {
			return false;
		}
	}
	return true;
}
 
Example 10
Source File: CloudFoundryApplicationManifestReader.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
public boolean canSupport(String manifest) {
	Yaml yaml = new Yaml();
	Iterable<Object> object = yaml.loadAll(manifest);
	for (Object o : object) {
		boolean supportKind = assertSupportedKind(o);
		if (!supportKind) {
			return false;
		}
	}
	return true;
}
 
Example 11
Source File: Denominator.java    From denominator with Apache License 2.0 5 votes vote down vote up
Map<?, ?> getConfigFromYaml(String yamlAsString) {
  Yaml yaml = new Yaml();
  Iterable<Object> configs = yaml.loadAll(yamlAsString);
  Object providerConf = FluentIterable.from(configs).firstMatch(new Predicate<Object>() {
    @Override
    public boolean apply(Object input) {
      return providerConfigurationName.equals(Map.class.cast(input).get("name"));
    }
  }).get();
  return Map.class.cast(providerConf);
}
 
Example 12
Source File: LoadExampleTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testLoadManyDocuments() throws IOException {
    InputStream input = new FileInputStream(new File(
            "src/test/resources/specification/example2_28.yaml"));
    Yaml yaml = new Yaml();
    int counter = 0;
    for (Object data : yaml.loadAll(input)) {
        assertNotNull(data);
        assertTrue(data.toString().length() > 1);
        counter++;
    }
    assertEquals(3, counter);
    input.close();
}
 
Example 13
Source File: DockerComposeConfigHandler.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private Iterable<Object> getComposeConfigurations(File composePath) {
    try {
        Yaml yaml = new Yaml();
        return yaml.loadAll(new FileReader(composePath));
    }
    catch (FileNotFoundException e) {
        throw new ExternalConfigHandlerException("failed to load external configuration: " + composePath, e);
    }
}
 
Example 14
Source File: RulesFactory.java    From nifi with Apache License 2.0 5 votes vote down vote up
private static List<Rule> yamlToRules(InputStream rulesInputStream) throws FileNotFoundException {
    List<Rule> rules = new ArrayList<>();
    Yaml yaml = new Yaml(new Constructor(Rule.class));
    for (Object object : yaml.loadAll(rulesInputStream)) {
        if (object instanceof Rule) {
            rules.add((Rule) object);
        }
    }
    return rules;
}
 
Example 15
Source File: YamlProcessor.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
private boolean process(MatchCallback callback, Yaml yaml, Resource resource) {
    int count = 0;
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Loading from YAML: " + resource);
        }
        Reader reader = new UnicodeReader(resource.getInputStream());
        try {
            for (Object object : yaml.loadAll(reader)) {
                if (object != null && process(asMap(object), callback)) {
                    count++;
                    if (this.resolutionMethod == ResolutionMethod.FIRST_FOUND) {
                        break;
                    }
                }
            }
            if (logger.isDebugEnabled()) {
                logger.debug(
                    "Loaded " + count + " document" + (count > 1 ? "s" : "") + " from YAML resource: " + resource);
            }
        } finally {
            reader.close();
        }
    } catch (IOException ex) {
        handleProcessError(resource, ex);
    }
    return (count > 0);
}
 
Example 16
Source File: YAMLToJavaDeserialisationUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenLoadMultipleYAMLDocuments_thenLoadCorrectJavaObjects() {
    Yaml yaml = new Yaml(new Constructor(Customer.class));
    InputStream inputStream = this.getClass()
        .getClassLoader()
        .getResourceAsStream("yaml/customers.yaml");
    int count = 0;
    for (Object object : yaml.loadAll(inputStream)) {
        count++;
        assertTrue(object instanceof Customer);
    }
    assertEquals(2, count);
}
 
Example 17
Source File: DockerComposeConfigHandler.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
private Iterable<Object> getComposeConfigurations(File composePath, MavenProject project, MavenSession session) {
    try {
        Yaml yaml = new Yaml();
        return yaml.loadAll(getFilteredReader(composePath, project, session));
    }
    catch (FileNotFoundException | MavenFilteringException e) {
        throw new ExternalConfigHandlerException("failed to load external configuration: " + composePath, e);
    }
}
 
Example 18
Source File: PyImportTest.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
protected Iterable<Object> loadAll(InputStream data) {
    Yaml yaml = new Yaml();
    return yaml.loadAll(data);
}
 
Example 19
Source File: PyImportTest.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
protected Iterable<Object> loadAll(String data) {
    Yaml yaml = new Yaml();
    return yaml.loadAll(data);
}
 
Example 20
Source File: PyImportTest.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
protected Iterable<Object> loadAll(Constructor loader, String data) {
    Yaml yaml = new Yaml(loader);
    return yaml.loadAll(data);
}