Code improvement for pending sonar issues
[cli.git] / validate / sample-yaml-generator / src / main / java / org / onap / cli / sample / yaml / SampleYamlGenerator.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.cli.sample.yaml;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.stream.Collectors;
25
26 public class SampleYamlGenerator {
27
28     static int nTab;
29     private SampleYamlGenerator() {
30         throw new IllegalStateException("Utility class");
31     }
32     public static void generateSampleYaml(String cmdName, List<String> input, String output, String version,
33             String targetPath, boolean debug, String name) throws IOException {
34
35         PrintWriter writer = new PrintWriter(targetPath, "UTF-8");
36         writeKeyValuePair(writer, "open_cli_sample_version", "1.0");
37         writeKeyValuePair(writer, "name", cmdName);
38         writeKeyValuePair(writer, "version", version);
39
40         if (name == null) {
41             name = "sample1";
42         }
43         writeKey(writer, "samples");
44         writeKey(writer, name);
45
46         writeKeyValuePair(writer, "name", cmdName);
47         writeKeyValuePair(writer, "input", input.stream().collect(Collectors.joining(" ")).trim());
48         writeKeyValuePair(writer, "moco", new File(targetPath).getName().replaceAll("-sample.yaml", "-moco.json"));
49         writeMultilineKeyValue(writer, "output", output.trim(), debug);
50
51         writeEndKey();
52         writeEndKey();
53
54         writer.flush();
55         writer.close();
56     }
57
58     private static void writeMultilineKeyValue(PrintWriter writer, String key, String value, boolean debug) { //NOSONAR
59         writer.write(printTabs() + key + ":");
60         if (value.isEmpty()) {
61             return;
62         }
63         writer.write(" |\n");
64         nTab++;
65         String[] lines = value.split("\n");
66         Arrays.stream(lines).forEach(line -> writer.write(printTabs() + line + "\n")); // NOSONAR
67     }
68
69     private static String printTabs() {
70         StringBuilder spaces = new StringBuilder();
71         for (int i=0; i < nTab; i++) {
72             spaces.append("  ");
73         }
74         return spaces.toString();
75     }
76
77     private static void writeKeyValuePair(PrintWriter writer, String key, String value) {
78         writer.write(printTabs() +key + ": " + value + "\n");
79     }
80
81     private static void writeKey(PrintWriter writer, String key) {
82         writer.write(printTabs() + key + ":\n");
83         nTab++;
84     }
85
86     private static void writeEndKey() {
87         nTab--;
88     }
89 }