![JAR search and dependency download from the Maven repository](/logo.png)
bt.bencoding.model.YamlBEObjectModelLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bt-bencoding Show documentation
Show all versions of bt-bencoding Show documentation
Library for parsing, encoding and validating bencoded documents in Java
package bt.bencoding.model;
import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
/**
* Loads model definitions from YAML documents.
*
* @since 1.0
*/
public class YamlBEObjectModelLoader implements BEObjectModelLoader {
private static final String MODEL_DEFINITION_KEY = "model";
private BEObjectModelBuilderFactory modelBuilderFactory;
private Yaml yaml;
/**
* @since 1.0
*/
public YamlBEObjectModelLoader() {
yaml = new Yaml();
modelBuilderFactory = new DefaultModelBuilderFactory();
}
@Override
public BEObjectModel load(InputStream source) {
if (source == null) {
throw new NullPointerException("Missing source -- null");
}
return fromYaml(yaml.load(source));
}
@SuppressWarnings("raw")
private BEObjectModel fromYaml(Object yamlObject) {
if (!List.class.isAssignableFrom(yamlObject.getClass())) {
throw new IllegalArgumentException("Invalid model -- root document must be a list");
}
List entries = (List) yamlObject;
for (Object entry : entries) {
if (entry instanceof Map && ((Map) entry).containsKey(MODEL_DEFINITION_KEY)) {
return buildObjectModel(((Map) entry).get(MODEL_DEFINITION_KEY));
}
}
throw new IllegalArgumentException("Invalid model -- missing model definition");
}
private BEObjectModel buildObjectModel(Object modelDefinition) {
return modelBuilderFactory.getOrCreateBuilder(modelDefinition.getClass())
.apply(modelDefinition);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy