All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.internal.yaml.YamlLoader Maven / Gradle / Ivy

There is a newer version: 62
Show newest version
/*
 * Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
 *
 * 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.internal.yaml;

import java.io.InputStream;
import java.io.Reader;

import static com.hazelcast.internal.yaml.YamlUtil.ensureRunningOnJava8OrHigher;

/**
 * YAML loader that can load, parse YAML documents and can build a tree
 * of {@link YamlNode} instances.
 * 

* The possible sources of the YAML documents are: *

    *
  • {@link InputStream}
  • *
  • {@link Reader}
  • *
  • {@link String}
  • *
*/ public final class YamlLoader { private YamlLoader() { } /** * Loads a YAML document from an {@link InputStream} and builds a * {@link YamlNode} tree that is under the provided top-level * {@code rootName} key. This loading mode requires the topmost level * of the YAML document to be a mapping. * * @param inputStream The input stream to load the YAML from * @param rootName The name of the root's key * @return the tree built from the YAML document */ public static YamlNode load(InputStream inputStream, String rootName) { try { YamlDocumentLoader load = getLoad(); Object document = load.loadFromInputStream(inputStream); return buildDom(rootName, document); } catch (Exception ex) { throw new YamlException("An error occurred while loading and parsing the YAML stream", ex); } } /** * Loads a YAML document from an {@link InputStream} and builds a * {@link YamlNode} tree. * * @param inputStream The input stream to load the YAML from * @return the tree built from the YAML document */ public static YamlNode load(InputStream inputStream) { try { YamlDocumentLoader load = getLoad(); Object document = load.loadFromInputStream(inputStream); return buildDom(document); } catch (Exception ex) { throw new YamlException("An error occurred while loading and parsing the YAML stream", ex); } } /** * Loads a YAML document from an {@link Reader} and builds a * {@link YamlNode} tree that is under the provided top-level * {@code rootName} key. This loading mode requires the topmost level * of the YAML document to be a mapping. * * @param reader The reader to load the YAML from * @param rootName The name of the root's key * @return the tree built from the YAML document */ public static YamlNode load(Reader reader, String rootName) { try { YamlDocumentLoader load = getLoad(); Object document = load.loadFromReader(reader); return buildDom(rootName, document); } catch (Exception ex) { throw new YamlException("An error occurred while loading and parsing the YAML stream", ex); } } /** * Loads a YAML document from an {@link Reader} and builds a * {@link YamlNode} tree. * * @param reader The reader to load the YAML from * @return the tree built from the YAML document */ public static YamlNode load(Reader reader) { try { YamlDocumentLoader load = getLoad(); Object document = load.loadFromReader(reader); return buildDom(document); } catch (Exception ex) { throw new YamlException("An error occurred while loading and parsing the YAML stream", ex); } } /** * Loads a YAML document from an {@link String} and builds a * {@link YamlNode} tree that is under the provided top-level * {@code rootName} key. This loading mode requires the topmost level * of the YAML document to be a mapping. * * @param yaml The string to load the YAML from * @param rootName The name of the root's key * @return the tree built from the YAML document */ public static YamlNode load(String yaml, String rootName) { try { YamlDocumentLoader load = getLoad(); Object document = load.loadFromString(yaml); return buildDom(rootName, document); } catch (Exception ex) { throw new YamlException("An error occurred while loading and parsing the YAML string", ex); } } /** * Loads a YAML document from an {@link String} and builds a * {@link YamlNode} tree. * * @param yaml The string to load the YAML from * @return the tree built from the YAML string */ public static YamlNode load(String yaml) { try { YamlDocumentLoader loader = getLoad(); Object document = loader.loadFromString(yaml); return buildDom(document); } catch (Exception ex) { throw new YamlException("An error occurred while loading and parsing the YAML string", ex); } } private static YamlDocumentLoader getLoad() { ensureRunningOnJava8OrHigher(); return new ReflectiveYamlDocumentLoader(); } private static YamlNode buildDom(String rootName, Object document) { return YamlDomBuilder.build(document, rootName); } private static YamlNode buildDom(Object document) { return YamlDomBuilder.build(document); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy