Search in sources :

Example 66 with Yaml

use of org.yaml.snakeyaml.Yaml in project mule by mulesoft.

the class DefaultConfigurationPropertiesProvider method createAttributesFromYamlObject.

protected void createAttributesFromYamlObject(String parentPath, Object parentYamlObject, Object yamlObject) {
    if (yamlObject instanceof List) {
        List list = (List) yamlObject;
        if (list.get(0) instanceof Map) {
            list.forEach(value -> createAttributesFromYamlObject(parentPath, yamlObject, value));
        } else {
            if (!(list.get(0) instanceof String)) {
                throw new ConfigurationPropertiesException(createStaticMessage("List of complex objects are not supported as property values. Offending key is " + parentPath), this);
            }
            String[] values = new String[list.size()];
            list.toArray(values);
            String value = join(",", list);
            configurationAttributes.put(parentPath, new DefaultConfigurationProperty(this, parentPath, value));
        }
    } else if (yamlObject instanceof Map) {
        if (parentYamlObject instanceof List) {
            throw new ConfigurationPropertiesException(createStaticMessage("Configuration properties does not support type a list of complex types. Complex type keys are: " + join(",", ((Map) yamlObject).keySet())), this);
        }
        Map<String, Object> map = (Map) yamlObject;
        map.entrySet().stream().forEach(entry -> createAttributesFromYamlObject(createKey(parentPath, entry.getKey()), yamlObject, entry.getValue()));
    } else {
        if (!(yamlObject instanceof String)) {
            throw new ConfigurationPropertiesException(createStaticMessage(format("YAML configuration properties only supports string values, make sure to wrap the value with \" so you force the value to be an string. Offending property is %s with value %s", parentPath, yamlObject)), this);
        }
        String resultObject = createValue(parentPath, (String) yamlObject);
        configurationAttributes.put(parentPath, new DefaultConfigurationProperty(this, parentPath, resultObject));
    }
}
Also used : ResourceProvider(org.mule.runtime.config.api.dsl.model.ResourceProvider) Properties(java.util.Properties) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) Optional.of(java.util.Optional.of) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) IOException(java.io.IOException) HashMap(java.util.HashMap) NoExtend(org.mule.api.annotation.NoExtend) FileInputStream(java.io.FileInputStream) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) ConfigurationPropertiesException(org.mule.runtime.config.internal.dsl.model.config.ConfigurationPropertiesException) String.format(java.lang.String.format) File(java.io.File) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Yaml(org.yaml.snakeyaml.Yaml) List(java.util.List) String.join(java.lang.String.join) Map(java.util.Map) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) Optional(java.util.Optional) DefaultConfigurationProperty(org.mule.runtime.config.internal.dsl.model.config.DefaultConfigurationProperty) InputStream(java.io.InputStream) ConfigurationPropertiesException(org.mule.runtime.config.internal.dsl.model.config.ConfigurationPropertiesException) DefaultConfigurationProperty(org.mule.runtime.config.internal.dsl.model.config.DefaultConfigurationProperty) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 67 with Yaml

use of org.yaml.snakeyaml.Yaml in project Essentials by EssentialsX.

the class Commandcreatekit method run.

// /createkit <name> <delay>
@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
    if (args.length != 2) {
        throw new NotEnoughArgumentsException();
    }
    // Command handler will auto fail if this fails.
    long delay = Long.valueOf(args[1]);
    String kitname = args[0];
    ItemStack[] items = user.getBase().getInventory().getContents();
    List<String> list = new ArrayList<>();
    for (ItemStack is : items) {
        if (is != null && is.getType() != null && is.getType() != Material.AIR) {
            String serialized = ess.getItemDb().serialize(is);
            list.add(serialized);
        }
    }
    // Some users might want to directly write to config knowing the consequences. *shrug*
    if (!ess.getSettings().isPastebinCreateKit()) {
        ess.getKits().addKit(kitname, list, delay);
        user.sendMessage(tl("createdKit", kitname, list.size(), delay));
    } else {
        ConfigurationSection config = new MemoryConfiguration();
        config.set("kits." + kitname + ".delay", delay);
        config.set("kits." + kitname + ".items", list);
        final Yaml yaml = new Yaml(yamlConstructor, yamlRepresenter, yamlOptions);
        String fileContents = "# Copy the kit code below into the kits section in your config.yml file\n";
        fileContents += yaml.dump(config.getValues(false));
        uploadPaste(user.getSource(), kitname, delay, fileContents);
    }
}
Also used : ArrayList(java.util.ArrayList) MemoryConfiguration(org.bukkit.configuration.MemoryConfiguration) ItemStack(org.bukkit.inventory.ItemStack) Yaml(org.yaml.snakeyaml.Yaml) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 68 with Yaml

use of org.yaml.snakeyaml.Yaml in project bboxdb by jnidzwetzki.

the class DistributionGroupMetadataHelper method writeMedatadataForGroup.

/**
 * Write new metadata for a distribution group
 * @param distributionGroupName
 * @param distributionGroupMetadata
 * @throws IOException
 */
public static void writeMedatadataForGroup(final String basedir, final String distributionGroupName, final DistributionGroupMetadata distributionGroupMetadata) throws IOException {
    final Map<String, Object> data = new HashMap<>();
    data.put("version", distributionGroupMetadata.getVersion());
    final String filename = getFilename(basedir, distributionGroupName);
    final File metadataFile = new File(filename);
    logger.info("Writing metadata to {} ", metadataFile);
    final FileWriter writer = new FileWriter(metadataFile);
    final Yaml yaml = new Yaml();
    yaml.dump(data, writer);
    writer.close();
}
Also used : HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) File(java.io.File) Yaml(org.yaml.snakeyaml.Yaml)

Example 69 with Yaml

use of org.yaml.snakeyaml.Yaml in project bboxdb by jnidzwetzki.

the class BBoxDBConfigurationManager method loadConfiguration.

/**
 * Load the configuration of the BBoxDB from the classpath
 */
public static void loadConfiguration() {
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader == null) {
        throw new IllegalArgumentException("Got null classloader");
    }
    final URL inputFile = classLoader.getResource(Const.CONFIG_FILE);
    if (inputFile == null) {
        configuration = new BBoxDBConfiguration();
        logger.warn("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        logger.warn("!! No configuration file found, using default values !!");
        logger.warn("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        return;
    }
    try (final InputStream inputStream = inputFile.openStream()) {
        logger.info("Loading configuration from: " + inputFile);
        final Yaml yaml = new Yaml();
        configuration = yaml.loadAs(inputStream, BBoxDBConfiguration.class);
    } catch (IOException e) {
        logger.error("Exception while loading configuration", e);
        throw new RuntimeException(e);
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL) Yaml(org.yaml.snakeyaml.Yaml)

Example 70 with Yaml

use of org.yaml.snakeyaml.Yaml in project wildfly-swarm by wildfly-swarm.

the class ConfigReader method readConfig.

public MetadataList readConfig(InputStream configStream) {
    Yaml yaml = new Yaml();
    MetadataList config = yaml.loadAs(configStream, MetadataList.class);
    log.info("Loaded config");
    return config;
}
Also used : Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

Yaml (org.yaml.snakeyaml.Yaml)276 Map (java.util.Map)104 HashMap (java.util.HashMap)85 IOException (java.io.IOException)58 FileInputStream (java.io.FileInputStream)49 InputStream (java.io.InputStream)49 File (java.io.File)43 DumperOptions (org.yaml.snakeyaml.DumperOptions)42 Constructor (org.yaml.snakeyaml.constructor.Constructor)30 Test (org.junit.Test)26 ArrayList (java.util.ArrayList)25 FileNotFoundException (java.io.FileNotFoundException)22 SafeConstructor (org.yaml.snakeyaml.constructor.SafeConstructor)22 List (java.util.List)21 Writer (java.io.Writer)18 Path (java.nio.file.Path)17 LinkedHashMap (java.util.LinkedHashMap)17 Reader (java.io.Reader)16 Properties (java.util.Properties)14 InputStreamReader (java.io.InputStreamReader)13