org.yaml.snakeyaml.reader.UnicodeReader Java Examples

The following examples show how to use org.yaml.snakeyaml.reader.UnicodeReader. 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: YamlBasedPropertiesProvider.java    From cfg4j with Apache License 2.0 6 votes vote down vote up
/**
 * Get {@link Properties} for a given {@code inputStream} treating it as a YAML file.
 *
 * @param inputStream input stream representing YAML file
 * @return properties representing values from {@code inputStream}
 * @throws IllegalStateException when unable to read properties
 */
@Override
public Properties getProperties(InputStream inputStream) {
  requireNonNull(inputStream);

  Yaml yaml = new Yaml();

  Properties properties = new Properties();

  try (Reader reader = new UnicodeReader(inputStream)) {

    Object object = yaml.load(reader);

    if (object != null) {
      Map<String, Object> yamlAsMap = convertToMap(object);
      properties.putAll(flatten(yamlAsMap));
    }

    return properties;

  } catch (IOException | ScannerException e) {
    throw new IllegalStateException("Unable to load yaml configuration from provided stream", e);
  }
}
 
Example #2
Source File: YamlParser.java    From nexus-repository-helm with Eclipse Public License 1.0 6 votes vote down vote up
public Map<String, Object> load(InputStream is) throws IOException {
  checkNotNull(is);
  String data = IOUtils.toString(new UnicodeReader(is));

  Map<String, Object> map;

  try {
    Yaml yaml = new Yaml(new Constructor(), new Representer(),
        new DumperOptions(), new Resolver());
    map = yaml.load(data);
  }
  catch (YAMLException e) {
    map = (Map<String, Object>) mapper.readValue(data, Map.class);
  }
  return map;
}
 
Example #3
Source File: YAMLProcessor.java    From skript-yaml with MIT License 6 votes vote down vote up
/**
 * Loads the configuration file.
 *
 * @throws java.io.IOException
 *             on load error
 */
public void oldLoad() throws IOException {
	InputStream stream = null;

	try {
		stream = getInputStream();
		if (stream == null)
			throw new IOException("Stream is null!");
		read(yaml.load(new UnicodeReader(stream)));
	} catch (YAMLProcessorException e) {
		root = new LinkedHashMap<String, Object>();
	} finally {
		try {
			if (stream != null) {
				stream.close();
			}
		} catch (IOException ignored) {
		}
	}
}
 
Example #4
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 #5
Source File: PyImportTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
protected List<Event> canonicalParse(InputStream input2) throws IOException {
    StreamReader reader = new StreamReader(new UnicodeReader(input2));
    StringBuilder buffer = new StringBuilder();
    while (reader.peek() != '\0') {
        buffer.append(reader.peek());
        reader.forward();
    }
    CanonicalParser parser = new CanonicalParser(buffer.toString());
    List<Event> result = new ArrayList<Event>();
    while (parser.peekEvent() != null) {
        result.add(parser.getEvent());
    }
    input2.close();
    return result;
}
 
Example #6
Source File: YamlProcessor.java    From canal 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 #7
Source File: YamlProcessor.java    From spring4-understanding 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 #8
Source File: PyStructureTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
private List<Node> canonical_compose_all(InputStream file) {
    StreamReader reader = new StreamReader(new UnicodeReader(file));
    StringBuilder buffer = new StringBuilder();
    while (reader.peek() != '\0') {
        buffer.append(reader.peek());
        reader.forward();
    }
    CanonicalParser parser = new CanonicalParser(buffer.toString());
    Composer composer = new Composer(parser, new Resolver());
    List<Node> documents = new ArrayList<Node>();
    while (composer.checkNode()) {
        documents.add(composer.getNode());
    }
    return documents;
}
 
Example #9
Source File: PyStructureTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
private List<Node> compose_all(InputStream file) {
    Composer composer = new Composer(new ParserImpl(new StreamReader(new UnicodeReader(file))),
            new Resolver());
    List<Node> documents = new ArrayList<Node>();
    while (composer.checkNode()) {
        documents.add(composer.getNode());
    }
    return documents;
}
 
Example #10
Source File: PyImportTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
protected List<Event> parse(InputStream input) throws IOException {
    StreamReader reader = new StreamReader(new UnicodeReader(input));
    Parser parser = new ParserImpl(reader);
    List<Event> result = new ArrayList<Event>();
    while (parser.peekEvent() != null) {
        result.add(parser.getEvent());
    }
    input.close();
    return result;
}
 
Example #11
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 #12
Source File: YamlProcessor.java    From java-technology-stack with MIT License 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 #13
Source File: YamlProcessor.java    From lams with GNU General Public License v2.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 #14
Source File: AbstractUserAgentAnalyzerDirect.java    From yauaa with Apache License 2.0 4 votes vote down vote up
private synchronized void loadYaml(Yaml yaml, InputStream yamlStream, String filename) {
    Node loadedYaml;
    try {
        loadedYaml = yaml.compose(new UnicodeReader(yamlStream));
    } catch (Exception e) {
        throw new InvalidParserConfigurationException("Parse error in the file " + filename + ": " + e.getMessage(), e);
    }

    if (loadedYaml == null) {
        LOG.warn("The file {} is empty", filename);
        return;
    }

    // Get and check top level config
    requireNodeInstanceOf(MappingNode.class, loadedYaml, filename, "File must be a Map");

    MappingNode rootNode = (MappingNode) loadedYaml;

    NodeTuple configNodeTuple = null;
    for (NodeTuple tuple : rootNode.getValue()) {
        String name = getKeyAsString(tuple, filename);
        if ("config".equals(name)) {
            configNodeTuple = tuple;
            break;
        }
        if ("version".equals(name)) {
            // Check the version information from the Yaml files
            assertSameVersion(tuple, filename);
            return;
        }
    }

    require(configNodeTuple != null, loadedYaml, filename, "The top level entry MUST be 'config'.");

    SequenceNode configNode = getValueAsSequenceNode(configNodeTuple, filename);
    List<Node> configList = configNode.getValue();

    for (Node configEntry : configList) {
        requireNodeInstanceOf(MappingNode.class, configEntry, filename, "The entry MUST be a mapping");
        NodeTuple entry = getExactlyOneNodeTuple((MappingNode) configEntry, filename);
        MappingNode actualEntry = getValueAsMappingNode(entry, filename);
        String entryType = getKeyAsString(entry, filename);
        switch (entryType) {
            case "lookup":
                loadYamlLookup(actualEntry, filename);
                break;
            case "set":
                loadYamlLookupSets(actualEntry, filename);
                break;
            case "matcher":
                loadYamlMatcher(actualEntry, filename);
                break;
            case "test":
                if (loadTests) {
                    loadYamlTestcase(actualEntry, filename);
                }
                break;
            default:
                throw new InvalidParserConfigurationException(
                    "Yaml config.(" + filename + ":" + actualEntry.getStartMark().getLine() + "): " +
                        "Found unexpected config entry: " + entryType + ", allowed are 'lookup', 'set', 'matcher' and 'test'");
        }
    }
}
 
Example #15
Source File: JavaBeanLoader.java    From orion.server with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Parse the first YAML document in a stream and produce the corresponding
 * JavaBean.
 * 
 * @param io
 *            data to load from (BOM is respected and removed)
 * @return parsed JavaBean
 */
@SuppressWarnings("unchecked")
public T load(InputStream io) {
    return (T) loader.load(new UnicodeReader(io));
}
 
Example #16
Source File: Yaml.java    From FastAsyncWorldedit with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Parse all YAML documents in a stream and produce corresponding Java
 * objects. The documents are parsed only when the iterator is invoked.
 *
 * @param yaml
 *            YAML data to load from (BOM is respected and ignored)
 * @return an iterator over the parsed Java objects in this stream in proper
 *         sequence
 */
public Iterable<Object> loadAll(InputStream yaml) {
    return loadAll(new UnicodeReader(yaml));
}
 
Example #17
Source File: Yaml.java    From FastAsyncWorldedit with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Parse the only YAML document in a stream and produce the corresponding
 * Java object.
 *
 * @param <T>
 *            Class is defined by the second argument
 * @param input
 *            data to load from (BOM is respected and removed)
 * @param type
 *            Class of the object to be created
 * @return parsed object
 */
@SuppressWarnings("unchecked")
public <T> T loadAs(InputStream input, Class<T> type) {
    return (T) loadFromReader(new StreamReader(new UnicodeReader(input)), type);
}
 
Example #18
Source File: Yaml.java    From FastAsyncWorldedit with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Parse the only YAML document in a stream and produce the corresponding
 * Java object.
 *
 * @param io
 *            data to load from (BOM is respected and removed)
 * @return parsed object
 */
public Object load(InputStream io) {
    return loadFromReader(new StreamReader(new UnicodeReader(io)), Object.class);
}
 
Example #19
Source File: Yaml.java    From Diorite with MIT License 2 votes vote down vote up
/**
 * Parse all YAML documents in a stream and produce corresponding Java
 * objects. The documents are parsed only when the iterator is invoked.
 *
 * @param yaml
 *         YAML data to load from (BOM is respected and ignored)
 *
 * @return an iterator over the parsed Java objects in this stream in proper sequence
 */
public Iterable<Object> fromAllYaml(InputStream yaml)
{
    return this.fromAllYaml(new UnicodeReader(yaml));
}
 
Example #20
Source File: Yaml.java    From Diorite with MIT License 2 votes vote down vote up
/**
 * Parse the only YAML document in a stream and produce the corresponding
 * Java object.
 *
 * @param <T>
 *         Class is defined by the second argument
 * @param input
 *         data to load from (BOM is respected and removed)
 * @param type
 *         Class of the object to be created
 *
 * @return parsed object
 */
@SuppressWarnings("unchecked")
public <T> T fromYaml(InputStream input, Class<T> type)
{
    return (T) this.loadFromReader(new StreamReader(new UnicodeReader(input)), type);
}
 
Example #21
Source File: Yaml.java    From Diorite with MIT License 2 votes vote down vote up
/**
 * Parse the only YAML document in a stream and produce the corresponding
 * Java object.
 *
 * @param io
 *         data to load from (BOM is respected and removed)
 *
 * @return parsed object
 */
public Object fromYaml(InputStream io)
{
    return this.loadFromReader(new StreamReader(new UnicodeReader(io)), Object.class);
}
 
Example #22
Source File: Yaml.java    From snake-yaml with Apache License 2.0 2 votes vote down vote up
/**
 * Parse the only YAML document in a stream and produce the corresponding
 * Java object.
 * 
 * @param io
 *            data to load from (BOM is respected and removed)
 * @return parsed object
 */
public Object load(InputStream io) {
    return loadFromReader(new StreamReader(new UnicodeReader(io)), Object.class);
}
 
Example #23
Source File: Yaml.java    From orion.server with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Parse the only YAML document in a stream and produce the corresponding
 * Java object.
 * 
 * @param io
 *            data to load from (BOM is respected and removed)
 * @return parsed object
 */
public Object load(InputStream io) {
    return loadFromReader(new StreamReader(new UnicodeReader(io)), Object.class);
}
 
Example #24
Source File: Yaml.java    From orion.server with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Parse the only YAML document in a stream and produce the corresponding
 * Java object.
 * 
 * @param <T>
 *            Class is defined by the second argument
 * @param input
 *            data to load from (BOM is respected and removed)
 * @param type
 *            Class of the object to be created
 * @return parsed object
 */
@SuppressWarnings("unchecked")
public <T> T loadAs(InputStream input, Class<T> type) {
    return (T) loadFromReader(new StreamReader(new UnicodeReader(input)), type);
}
 
Example #25
Source File: Yaml.java    From orion.server with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Parse all YAML documents in a stream and produce corresponding Java
 * objects. The documents are parsed only when the iterator is invoked.
 * 
 * @param yaml
 *            YAML data to load from (BOM is respected and ignored)
 * @return an iterator over the parsed Java objects in this stream in proper
 *         sequence
 */
public Iterable<Object> loadAll(InputStream yaml) {
    return loadAll(new UnicodeReader(yaml));
}
 
Example #26
Source File: Yaml.java    From snake-yaml with Apache License 2.0 2 votes vote down vote up
/**
 * Parse all YAML documents in a stream and produce corresponding Java
 * objects. The documents are parsed only when the iterator is invoked.
 * 
 * @param yaml
 *            YAML data to load from (BOM is respected and ignored)
 * @return an iterator over the parsed Java objects in this stream in proper
 *         sequence
 */
public Iterable<Object> loadAll(InputStream yaml) {
    return loadAll(new UnicodeReader(yaml));
}
 
Example #27
Source File: Yaml.java    From snake-yaml with Apache License 2.0 2 votes vote down vote up
/**
 * Parse the only YAML document in a stream and produce the corresponding
 * Java object.
 * 
 * @param <T>
 *            Class is defined by the second argument
 * @param input
 *            data to load from (BOM is respected and removed)
 * @param type
 *            Class of the object to be created
 * @return parsed object
 */
@SuppressWarnings("unchecked")
public <T> T loadAs(InputStream input, Class<T> type) {
    return (T) loadFromReader(new StreamReader(new UnicodeReader(input)), type);
}