|
| 1 | +/* |
| 2 | + * This file is part of git-commit-id-plugin-core by Konrad 'ktoso' Malawski <konrad.malawski@java.pl> |
| 3 | + * |
| 4 | + * git-commit-id-plugin-core is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * git-commit-id-plugin-core is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public License |
| 15 | + * along with git-commit-id-plugin-core. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package pl.project13.core.util; |
| 19 | + |
| 20 | +import nu.studer.java.util.OrderedProperties; |
| 21 | +import org.yaml.snakeyaml.DumperOptions; |
| 22 | +import org.yaml.snakeyaml.LoaderOptions; |
| 23 | +import org.yaml.snakeyaml.Yaml; |
| 24 | +import pl.project13.core.CannotReadFileException; |
| 25 | + |
| 26 | +import javax.annotation.Nonnull; |
| 27 | +import java.io.File; |
| 28 | +import java.io.FileInputStream; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.InputStreamReader; |
| 31 | +import java.io.OutputStream; |
| 32 | +import java.io.OutputStreamWriter; |
| 33 | +import java.io.Writer; |
| 34 | +import java.nio.charset.Charset; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.Properties; |
| 38 | + |
| 39 | +public class YmlManager { |
| 40 | + public static void dumpYml(OutputStream outputStream, OrderedProperties sortedLocalProperties, Charset sourceCharset) throws IOException { |
| 41 | + try (Writer outputWriter = new OutputStreamWriter(outputStream, sourceCharset)) { |
| 42 | + DumperOptions dumperOptions = new DumperOptions(); |
| 43 | + dumperOptions.setAllowUnicode(true); |
| 44 | + dumperOptions.setAllowReadOnlyProperties(true); |
| 45 | + dumperOptions.setPrettyFlow(true); |
| 46 | + // dumperOptions.setCanonical(true); |
| 47 | + dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.AUTO); |
| 48 | + |
| 49 | + Yaml yaml = new Yaml(dumperOptions); |
| 50 | + |
| 51 | + Map<String, Object> dataMap = new HashMap<>(); |
| 52 | + for (Map.Entry<String, String> e: sortedLocalProperties.entrySet()) { |
| 53 | + dataMap.put(e.getKey(), e.getValue()); |
| 54 | + } |
| 55 | + yaml.dump(dataMap, outputWriter); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static Properties readYmlProperties(@Nonnull File xmlFile, Charset sourceCharset) throws CannotReadFileException { |
| 60 | + Properties retVal = new Properties(); |
| 61 | + |
| 62 | + try (FileInputStream fis = new FileInputStream(xmlFile)) { |
| 63 | + try (InputStreamReader reader = new InputStreamReader(fis, sourceCharset)) { |
| 64 | + LoaderOptions loaderOptions = new LoaderOptions(); |
| 65 | + loaderOptions.setAllowDuplicateKeys(false); |
| 66 | + loaderOptions.setAllowRecursiveKeys(false); |
| 67 | + loaderOptions.setProcessComments(false); |
| 68 | + Yaml yaml = new Yaml(loaderOptions); |
| 69 | + Map<String, Object> data = yaml.load(reader); |
| 70 | + for (Map.Entry<String, Object> e: data.entrySet()) { |
| 71 | + retVal.put(e.getKey(), e.getValue()); |
| 72 | + } |
| 73 | + } |
| 74 | + } catch (IOException e) { |
| 75 | + throw new CannotReadFileException(e); |
| 76 | + } |
| 77 | + return retVal; |
| 78 | + } |
| 79 | +} |
0 commit comments