Search in sources :

Example 16 with Yaml

use of org.yaml.snakeyaml.Yaml in project mybatis-generator-gui-extension by spawpaw.

the class SCVXGeneratorPlugin method contextGenerateAdditionalJavaFiles.

@Override
public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
    log.info(">>>> generating extra files...");
    // : 2018/3/22 将 introspectedTable 转化为对象
    Map<String, String> map = new HashMap<>();
    for (Object o : properties.keySet()) {
        map.put(o.toString(), properties.getProperty(o.toString()));
    }
    Table table = new Table(context, introspectedTable, map);
    // : 2018/3/22 初始化context
    VelocityContext templateContext = new VelocityContext();
    templateContext.put("table", table);
    templateContext.put("projectDir.ss", projectDir);
    // 输出测试数据
    String content = renderTemplateAsString("test.vm", templateContext);
    log.info("hierarchical table structure: {}", content);
    // : 2018/3/22 保存到指定目录
    List<TemplateConfig> configs = new Yaml().loadAs(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.yml"), ConfigWrapper.class).getTemplateConfig();
    for (TemplateConfig config : configs) {
        String template = config.getTemplate();
        String destDir = config.getDestDir().replaceAll("\\.", "/");
        String destPackage = config.getDestPackage();
        String destFileName = config.getDestFileName();
        String absPath = (projectDir == null || projectDir.isEmpty() ? "" : projectDir) + destDir + "/" + destPackage.replace(".", "/") + "/" + destFileName;
        absPath = absPath.replace("//", "/");
        absPath = absPath.replace("${entityName}", table.getEntityName());
        absPath = absPath.replace("${basePackage}", basePackage.replace(".", "/"));
        log.info("generate file `{}` from template `{}`", absPath, template);
        content = renderTemplateAsString(config.getTemplate(), templateContext);
        // 写入文件
        try {
            FileUtil.writeStringToFile(absPath, content);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        log.info("content: {}", content);
    }
    log.info("<<< generated extra files.");
    return null;
}
Also used : IntrospectedTable(org.mybatis.generator.api.IntrospectedTable) Table(com.spawpaw.mybatis.generator.gui.entity.Table) ConfigWrapper(com.spawpaw.mybatis.generator.gui.entity.ConfigWrapper) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) TemplateConfig(com.spawpaw.mybatis.generator.gui.entity.TemplateConfig) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml)

Example 17 with Yaml

use of org.yaml.snakeyaml.Yaml in project cas by apereo.

the class AddPropertiesToConfigurationCommand method writeYamlConfigurationPropertiesToFile.

private void writeYamlConfigurationPropertiesToFile(final File filePath, final Map<String, ConfigurationMetadataProperty> results, final Properties yamlProps) throws Exception {
    final DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.AUTO);
    options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
    options.setPrettyFlow(true);
    options.setAllowUnicode(true);
    final Yaml yaml = new Yaml(options);
    try (Writer writer = Files.newBufferedWriter(filePath.toPath(), StandardCharsets.UTF_8)) {
        putResultsIntoProperties(results, yamlProps);
        yaml.dump(yamlProps, writer);
    }
}
Also used : DumperOptions(org.yaml.snakeyaml.DumperOptions) Yaml(org.yaml.snakeyaml.Yaml) Writer(java.io.Writer)

Example 18 with Yaml

use of org.yaml.snakeyaml.Yaml in project grakn by graknlabs.

the class MigrationCLI method extractOptionsFromConfiguration.

private static List<String[]> extractOptionsFromConfiguration(String path, String[] args) {
    // check file exists
    File configuration = new File(path);
    if (!configuration.exists()) {
        throw new IllegalArgumentException("Could not find configuration file " + path);
    }
    try (InputStreamReader reader = new InputStreamReader(new FileInputStream(configuration), Charset.defaultCharset())) {
        List<Map<String, String>> config = (List<Map<String, String>>) new Yaml().load(reader);
        List<String[]> options = new ArrayList<>();
        for (Map<String, String> c : config) {
            List<String> parameters = new ArrayList<>(Arrays.asList(args));
            c.entrySet().stream().flatMap(m -> Stream.of("-" + m.getKey(), m.getValue())).forEach(parameters::add);
            options.add(parameters.toArray(new String[parameters.size()]));
        }
        return options;
    } catch (IOException e) {
        throw new RuntimeException("Could not parse configuration file.");
    }
}
Also used : RULE(ai.grakn.util.Schema.MetaSchema.RULE) Arrays(java.util.Arrays) ATTRIBUTE(ai.grakn.util.Schema.MetaSchema.ATTRIBUTE) Graql.count(ai.grakn.graql.Graql.count) RELATIONSHIP(ai.grakn.util.Schema.MetaSchema.RELATIONSHIP) HelpFormatter(org.apache.commons.cli.HelpFormatter) Function(java.util.function.Function) Yaml(org.yaml.snakeyaml.Yaml) ArrayList(java.util.ArrayList) ENTITY(ai.grakn.util.Schema.MetaSchema.ENTITY) Client(ai.grakn.client.Client) Charset(java.nio.charset.Charset) Files(com.google.common.io.Files) GraknTx(ai.grakn.GraknTx) CommonUtil(ai.grakn.util.CommonUtil) Map(java.util.Map) OutputStreamWriter(java.io.OutputStreamWriter) Grakn(ai.grakn.Grakn) GraknTxType(ai.grakn.GraknTxType) QueryBuilder(ai.grakn.graql.QueryBuilder) PrintWriter(java.io.PrintWriter) Graql.var(ai.grakn.graql.Graql.var) BufferedWriter(java.io.BufferedWriter) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Graql.label(ai.grakn.graql.Graql.label) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) Collections(java.util.Collections) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Yaml(org.yaml.snakeyaml.Yaml) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) Map(java.util.Map)

Example 19 with Yaml

use of org.yaml.snakeyaml.Yaml in project cxf by apache.

the class AbstractSwagger2ServiceDescriptionTest method testApiListingIsProperlyReturnedYAML.

@Test
@Ignore
public void testApiListingIsProperlyReturnedYAML() throws Exception {
    final WebClient client = createWebClient("/swagger.yaml");
    try {
        final Response r = client.get();
        assertEquals(Status.OK.getStatusCode(), r.getStatus());
        // REVISIT find a better way of reliably comparing two yaml instances.
        // I noticed that yaml.load instantiates a Map and
        // for an integer valued key, an Integer or a String is arbitrarily instantiated,
        // which leads to the assertion error. So, we serilialize the yamls and compare the re-serialized texts.
        Yaml yaml = new Yaml();
        assertEquals(yaml.load(getExpectedValue(getExpectedFileYaml(), getPort())).toString(), yaml.load(IOUtils.readStringFromStream((InputStream) r.getEntity())).toString());
    } finally {
        client.close();
    }
}
Also used : Response(javax.ws.rs.core.Response) InputStream(java.io.InputStream) WebClient(org.apache.cxf.jaxrs.client.WebClient) Yaml(org.yaml.snakeyaml.Yaml) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 20 with Yaml

use of org.yaml.snakeyaml.Yaml in project java by kubernetes-client.

the class KubeConfig method loadKubeConfig.

/**
 * Load a Kubernetes config from a Reader
 */
public static KubeConfig loadKubeConfig(Reader input) {
    Yaml yaml = new Yaml(new SafeConstructor());
    Object config = yaml.load(input);
    Map<String, Object> configMap = (Map<String, Object>) config;
    String currentContext = (String) configMap.get("current-context");
    ArrayList<Object> contexts = (ArrayList<Object>) configMap.get("contexts");
    ArrayList<Object> clusters = (ArrayList<Object>) configMap.get("clusters");
    ArrayList<Object> users = (ArrayList<Object>) configMap.get("users");
    KubeConfig kubeConfig = new KubeConfig(contexts, clusters, users);
    kubeConfig.setContext(currentContext);
    return kubeConfig;
}
Also used : ArrayList(java.util.ArrayList) SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor) HashMap(java.util.HashMap) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

Yaml (org.yaml.snakeyaml.Yaml)253 Map (java.util.Map)92 HashMap (java.util.HashMap)73 IOException (java.io.IOException)55 FileInputStream (java.io.FileInputStream)47 InputStream (java.io.InputStream)47 File (java.io.File)43 DumperOptions (org.yaml.snakeyaml.DumperOptions)39 Constructor (org.yaml.snakeyaml.constructor.Constructor)27 ArrayList (java.util.ArrayList)25 FileNotFoundException (java.io.FileNotFoundException)21 List (java.util.List)21 Test (org.junit.Test)21 LinkedHashMap (java.util.LinkedHashMap)17 Writer (java.io.Writer)13 FileWriter (java.io.FileWriter)12 InputStreamReader (java.io.InputStreamReader)12 Path (java.nio.file.Path)12 FileReader (java.io.FileReader)10 Reader (java.io.Reader)9