|
| 1 | +/* |
| 2 | + * JASKL - Just Another Simple Konfig Library |
| 3 | + * Copyright (C) 2023 LeStegii, Almighty-Satan |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 2.1 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this library; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + * USA |
| 19 | + */ |
| 20 | + |
| 21 | +package io.github.almightysatan.jaskl.hocon; |
| 22 | + |
| 23 | +import com.typesafe.config.*; |
| 24 | +import io.github.almightysatan.jaskl.impl.Util; |
| 25 | +import io.github.almightysatan.jaskl.impl.WritableConfigEntry; |
| 26 | +import org.jetbrains.annotations.NotNull; |
| 27 | +import org.jetbrains.annotations.Nullable; |
| 28 | + |
| 29 | +import java.io.File; |
| 30 | +import java.io.FileWriter; |
| 31 | +import java.io.IOException; |
| 32 | +import java.math.BigDecimal; |
| 33 | +import java.math.BigInteger; |
| 34 | + |
| 35 | +/** |
| 36 | + * A hocon config implementation. |
| 37 | + */ |
| 38 | +public class HoconLegacyConfig extends HoconConfig { |
| 39 | + |
| 40 | + protected HoconLegacyConfig(@NotNull File file, @Nullable String description) { |
| 41 | + super(file, description); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void write() throws IOException { |
| 46 | + Config config = super.config; |
| 47 | + if (config == null) |
| 48 | + throw new IllegalStateException(); |
| 49 | + Util.createFileAndPath(this.file); |
| 50 | + |
| 51 | + for (WritableConfigEntry<?> configEntry : this.getCastedValues()) { |
| 52 | + if (configEntry.isModified()) { |
| 53 | + Object entryValue = configEntry.getValueToWrite(Object::toString); |
| 54 | + if (entryValue instanceof BigInteger) |
| 55 | + entryValue = entryValue.toString(); |
| 56 | + if (entryValue instanceof BigDecimal) |
| 57 | + entryValue = entryValue.toString(); |
| 58 | + ConfigValue value = ConfigValueFactory.fromAnyRef(entryValue); |
| 59 | + config = config.withValue(configEntry.getPath(), value); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + this.writeIfNecessary(config, true); |
| 64 | + } |
| 65 | + |
| 66 | + protected void writeIfNecessary(@NotNull Config config, boolean setDescription) throws IOException { |
| 67 | + if (config != super.config) { |
| 68 | + ConfigObject root = config.root(); |
| 69 | + String output = root.render(RENDER_OPTIONS); |
| 70 | + try (FileWriter fileWriter = new FileWriter(this.file)) { |
| 71 | + fileWriter.write(output); |
| 72 | + } |
| 73 | + super.config = config; |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments