org.apache.commons.configuration2.YAMLConfiguration类的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(5.1k)|赞(0)|评价(0)|浏览(275)

本文整理了Java中org.apache.commons.configuration2.YAMLConfiguration类的一些代码示例,展示了YAMLConfiguration类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。YAMLConfiguration类的具体详情如下:
包路径:org.apache.commons.configuration2.YAMLConfiguration
类名称:YAMLConfiguration

YAMLConfiguration介绍

[英]A specialized hierarchical configuration class that is able to parse YAML documents.
[中]一个专门的层次结构配置类,能够解析YAML文档。

代码示例

代码示例来源:origin: org.apache.commons/commons-configuration2

public void dump(final Writer out, final DumperOptions options)
    throws ConfigurationException, IOException
{
  final Yaml yaml = new Yaml(options);
  yaml.dump(constructMap(getNodeModel().getNodeHandler().getRootNode()),
      out);
}

代码示例来源:origin: org.apache.commons/commons-configuration2

@Override
public void read(final Reader in) throws ConfigurationException
{
  try
  {
    final Yaml yaml = new Yaml();
    final Map<String, Object> map = (Map) yaml.load(in);
    load(map);
  }
  catch (final Exception e)
  {
    rethrowException(e);
  }
}

代码示例来源:origin: linqs/psl

private static Set<StandardPredicate> parsePredicates(YAMLConfiguration yaml, boolean useIntIds, DataStore dataStore) {
  Set<StandardPredicate> closedPredicates = new HashSet<StandardPredicate>();
  boolean foundPredicate = false;
  Iterator<String> keyIterator = yaml.getKeys(KEY_PREDICATE);
  while (keyIterator.hasNext()) {
    foundPredicate = true;
    String key = keyIterator.next();
    Object rawValue = yaml.getProperty(key);
    String predicateName = key.replaceFirst("^" + KEY_PREDICATE + ".", "");
    List<Object> values = null;
    if (rawValue instanceof String) {
      values = new ArrayList<Object>();
      values.add((String)rawValue);
    } else if (rawValue instanceof List) {
      @SuppressWarnings("unchecked")
      List<Object> ignoreCompilerWarning = (List<Object>)rawValue;
      values = ignoreCompilerWarning;
    } else {
      throw new IllegalStateException(String.format("Predicate, %s, has an unknown value type: %s.", predicateName, rawValue.getClass().getName()));
    }
    parsePredicate(predicateName, values, useIntIds, dataStore, closedPredicates);
  }
  if (!foundPredicate) {
    throw new IllegalStateException(String.format("Found no predicates. Predicates must be defined under the '%s' key.", KEY_PREDICATE));
  }
  return closedPredicates;
}

代码示例来源:origin: linqs/psl

public static Set<StandardPredicate> load(DataStore dataStore, String path, boolean useIntIds)
    throws ConfigurationException, FileNotFoundException {
  InputStream inputStream = new FileInputStream(path);
  YAMLConfiguration yaml = new YAMLConfiguration();
  yaml.read(inputStream);
  // Make sure to get the absolute path so we can take the parent.
  path = (new File(path)).getAbsolutePath();
  // All non-absolute paths should be relative to the data file.
  String relativeDir = (new File(path)).getParentFile().getAbsolutePath();
  // Fetch the predicates.
  Set<StandardPredicate> closedPredicates = parsePredicates(yaml, useIntIds, dataStore);
  // Load the partitions.
  loadPartitions(yaml, dataStore, relativeDir);
  return closedPredicates;
}

代码示例来源:origin: org.apache.commons/commons-configuration2

@Override
public void write(final Writer out) throws ConfigurationException, IOException
{
  final DumperOptions options = new DumperOptions();
  options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
  dump(out, options);
}

代码示例来源:origin: linqs/psl

private static void loadPartitions(YAMLConfiguration yaml, DataStore dataStore, String relativeDir) {
  String[] partitions = new String[]{
    KEY_PARTITION_OBS,
    KEY_PARTITION_TARGETS,
    KEY_PARTITION_TRUTH
  };
  for (String partition : partitions) {
    Iterator<String> keyIterator = yaml.getKeys(partition);
    while (keyIterator.hasNext()) {
      String key = keyIterator.next();
      Object value = yaml.getProperty(key);
      String predicate = key.replaceFirst("^" + partition + ".", "");
      if (value instanceof String) {
        loadData(partition, predicate, dataStore, (String)value, relativeDir);
      } else if (value instanceof List) {
        @SuppressWarnings("unchecked")
        List<Object> listValues = (List<Object>)value;
        for (Object listValue : listValues) {
          if (!(listValue instanceof String)) {
            throw new IllegalArgumentException(String.format("Expected a string property to key, %s, found: '%s'.", key, listValue.getClass().getName()));
          }
          loadData(partition, predicate, dataStore, (String)listValue, relativeDir);
        }
      } else {
        throw new IllegalArgumentException(String.format("Key, %s, has an unrecognized type: '%s'.", key, value.getClass().getName()));
      }
    }
  }
}

代码示例来源:origin: org.apache.commons/commons-configuration2

public void read(final InputStream in, final LoaderOptions options)
    throws ConfigurationException
{
  try
  {
    final Yaml yaml = new Yaml(options);
    final Map<String, Object> map = (Map) yaml.load(in);
    load(map);
  }
  catch (final Exception e)
  {
    rethrowException(e);
  }
}

代码示例来源:origin: org.apache.commons/commons-configuration2

public void read(final Reader in, final LoaderOptions options)
    throws ConfigurationException
{
  try
  {
    final Yaml yaml = new Yaml(options);
    final Map<String, Object> map = (Map) yaml.load(in);
    load(map);
  }
  catch (final Exception e)
  {
    rethrowException(e);
  }
}

代码示例来源:origin: org.apache.commons/commons-configuration2

/**
 * Loads the configuration from the given input stream.
 *
 * @param in the input stream
 * @throws ConfigurationException if an error occurs
 */
@Override
public void read(final InputStream in) throws ConfigurationException
{
  try
  {
    final Yaml yaml = new Yaml();
    final Map<String, Object> map = (Map) yaml.load(in);
    load(map);
  }
  catch (final Exception e)
  {
    rethrowException(e);
  }
}

相关文章