com.hazelcast.org.snakeyaml.engine.v1.api.Load Maven / Gradle / Ivy
Show all versions of hazelcast-all Show documentation
/**
* Copyright (c) 2018, http://www.snakeyaml.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.org.snakeyaml.engine.v1.api;
import java.io.InputStream;
import java.io.Reader;
import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
import com.hazelcast.org.snakeyaml.engine.v1.composer.Composer;
import com.hazelcast.org.snakeyaml.engine.v1.constructor.BaseConstructor;
import com.hazelcast.org.snakeyaml.engine.v1.constructor.StandardConstructor;
import com.hazelcast.org.snakeyaml.engine.v1.nodes.Node;
import com.hazelcast.org.snakeyaml.engine.v1.parser.ParserImpl;
import com.hazelcast.org.snakeyaml.engine.v1.scanner.StreamReader;
/**
* Common way to load Java instance(s)
*/
public class Load {
private LoadSettings settings;
private BaseConstructor constructor;
/**
* Create instance to parse the incoming YAML data and create Java instances
*
* @param settings - configuration
*/
public Load(LoadSettings settings) {
this(settings, new StandardConstructor(settings));
}
/**
* Create instance to parse the incoming YAML data and create Java instances
* @param settings - configuration
* @param constructor - custom YAML constructor
*/
public Load(LoadSettings settings, BaseConstructor constructor) {
Objects.requireNonNull(settings, "LoadSettings cannot be null");
Objects.requireNonNull(constructor, "BaseConstructor cannot be null");
this.settings = settings;
this.constructor = constructor;
}
protected Composer createComposer(LoadSettings settings, StreamReader streamReader) {
return new Composer(new ParserImpl(streamReader, settings), settings.getScalarResolver());
}
// Load a single document
private Object loadOne(Composer composer) {
Optional nodeOptional = composer.getSingleNode();
return constructor.constructSingleDocument(nodeOptional);
}
/**
* Parse the only YAML document in a stream and produce the corresponding
* Java object.
*
* @param yamlStream - data to load from (BOM is respected to detect encoding and removed from the data)
* @return parsed Java instance
*/
public Object loadFromInputStream(InputStream yamlStream) {
Objects.requireNonNull(yamlStream, "InputStream cannot be null");
return loadOne(createComposer(settings, new StreamReader(new YamlUnicodeReader(yamlStream), settings)));
}
/**
* Parse a YAML document and create a Java instance
*
* @param yamlReader - data to load from (BOM must not be present)
* @return parsed Java instance
*/
public Object loadFromReader(Reader yamlReader) {
Objects.requireNonNull(yamlReader, "Reader cannot be null");
return loadOne(createComposer(settings, new StreamReader(yamlReader, settings)));
}
/**
* Parse a YAML document and create a Java instance
*
* @param yaml - YAML data to load from (BOM must not be present)
* @return parsed Java instance
* @throws com.hazelcast.org.snakeyaml.engine.v1.exceptions.YamlEngineException if the YAML is not valid
*/
public Object loadFromString(String yaml) {
Objects.requireNonNull(yaml, "String cannot be null");
return loadOne(createComposer(settings, new StreamReader(yaml, settings)));
}
// Load all the documents
private Iterable