Browse Source

16.03.2018 -- Fixing

master
Niklas Hartmann 2 years ago
parent
commit
42efd01a7a
8 changed files with 133 additions and 133 deletions
  1. +1
    -2
      README.md
  2. +21
    -35
      config.conf
  3. +1
    -1
      simplecommentconfig.iml
  4. +66
    -62
      src/main/java/de/moddylp/simplecommentconfig/Config.java
  5. +2
    -14
      src/main/java/de/moddylp/simplecommentconfig/ConfigManager.java
  6. +25
    -10
      src/main/java/de/moddylp/simplecommentconfig/Serilizer.java
  7. +14
    -3
      src/test/java/MainTest.java
  8. +3
    -6
      src/test/resources/test.yml

+ 1
- 2
README.md View File

@@ -2,7 +2,7 @@

Facts:
- [x] YML - compatible
- [x] Supports List (Block Format)
- [x] Supports List (InLine Format)
- [x] Supports Boolean (Block Format)
- [x] Supports Double (Block Format)
- [x] Supports Integer (Block Format)
@@ -11,7 +11,6 @@ Facts:
- [x] **__Supports Comment in front of Keys and File Header__**
- [x] **Adding Comments in runtime**
- [x] Sections(dictionary) support
- [ ] Inline Datatypes
- [ ] List of Sections
- [ ] Supports Boolean in several forms
- [ ] Multiline Values Support


+ 21
- 35
config.conf View File

@@ -1,50 +1,49 @@
#________________________________________________________________#
# Header #
# #
# #
# Tolles Programm #
# #
# Header #
# AncientRegions test de.moddylp.simplecommentconfig.Config #
# Ich bin eine TestConfiguraton #
# AncientRegions test de.moddylp.simplecommentconfig.Config #
# Ich bin eine TestConfiguraton #
# Header2 #
# AncientRegions test de.moddylp.simplecommentconfig.Config #
# Ich bin eine TestConfiguraton #
#________________________________________________________________#

#KeyValueTest
tada: 'tadavalue'

#KeyValueTest22
tada22: 56.8

tada23: 23.0
tollermainkey: 4444.0

2bSection:
tada2: 'tadavalue2'
tada3: false
tada4: true
tada5: 'adavalue5'

#Lala Section
a2Section:

#LOL
tada6: 'Anything'

listtest: '{Test, Zwei}'
wert: null

testnested:
nocheine: 5623.0

testnestedsection:
läuft: 'hahahaha'
lalala: 56112.0
valuenested: 'keynested'
läuft: 'hahahaha'
läuft: 'hahahaha'
laeuft: 'hahahaha'

zweitesection:

#Ich bin ein toller Kommentar
keytoll: 56765.0

liste:
- 3456.0
- 'tatatata'
- false
zeweiteliste:
- 9999.0
- 'hjdjdhjf'
- 'jhjdhds'
- true
liste: {3456.0,'tatatata',false}
zeweiteliste: {9999.0,'hjdjdhjf','jhjdhds',true}
leereliste: []

newtest:
lolvalue: 'hahahaha'
@@ -53,17 +52,4 @@ newtest:
#ICH bin ein ganz langer kommentar und mache jetzt alles kaputt damm damm dammmmmmmm. Aber villeicht auch nicht
lustig: 55555.0

#Lala Section
a2Section:

#LOL
tada6: 'Anything'

listtest:
- 'Test'
- 'zwei'

2bSection:
tada2: 'tadavalue2'
tada3: false


+ 1
- 1
simplecommentconfig.iml View File

@@ -5,7 +5,7 @@
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>


+ 66
- 62
src/main/java/de/moddylp/simplecommentconfig/Config.java View File

@@ -10,13 +10,13 @@ import java.util.concurrent.atomic.AtomicReference;
* Copyright 2017
*/
public class Config {
private final String MAINSECTIONID = "<<main>>";
private final String MAINSECTIONID = "<<!!!!>>";
private final String NEWLN = "\n";
private LinkedHashMap<String, String> comments = new LinkedHashMap<String, String>();
private LinkedHashMap<String, LinkedHashMap<String, Object>> sections = new LinkedHashMap<>();
private ArrayList<String> header = new ArrayList<>();
private File configfile;
private AtomicReference<String> currentsection = new AtomicReference<>("<<main>>");
private AtomicReference<String> currentsection = new AtomicReference<>("<<!!!!>>");

public Config(File configfile) {
this.configfile = configfile;
@@ -51,18 +51,17 @@ public class Config {
for (int i = 0; i < lines.size(); i++) {
String current = lines.get(i);
if (i == 0 && current.startsWith("#")) {
int checkline = 0;
ArrayList<String> headerlist = new ArrayList<>();
while (lines.get(checkline) != null && lines.get(checkline).startsWith("#") && !lines.get(checkline).trim().equals("")) {
headerlist.add(lines.get(checkline));
checkline++;
}
do {
headerlist.add(lines.get(i));
i++;
} while (lines.get(i) != null && lines.get(i).startsWith("#"));
setHeader(headerlist.toArray(new String[]{}));
i = i + checkline;
}
if (!processSection(current, currentsection, lines, i)) {
processComment(current, lines, i);
processKeyValue(current, currentsection, lines, i);
} else {
if (!processSection(current, currentsection, lines, i)) {
processComment(current, lines, i);
processKeyValue(current, currentsection, lines, i);
}
}
}
reader.close();
@@ -101,17 +100,17 @@ public class Config {
public String returnConfigString() {
StringBuilder builder = new StringBuilder();
builder.append(printHeader());
builder.append("\n");
builder.append("\n\n");
if (sections.get(MAINSECTIONID) != null) {
for (String key : sections.get(MAINSECTIONID).keySet()) {
if (getCommentbyKey(key) != null) {
ConfigUtils.appendNewLine(builder);
builder.append(getCommentbyKey(key));
newlines(1, builder);
builder.append(key).append(": ").append(Serilizer.convertObjecttoString(sections.get(MAINSECTIONID).get(key), " ")).append(NEWLN);
builder.append(key).append(": ").append(Serilizer.convertObjecttoString(sections.get(MAINSECTIONID).get(key))).append(NEWLN);
newlines(1, builder);
} else {
builder.append(key).append(": ").append(Serilizer.convertObjecttoString(sections.get(MAINSECTIONID).get(key), " ")).append(NEWLN);
builder.append(key).append(": ").append(Serilizer.convertObjecttoString(sections.get(MAINSECTIONID).get(key))).append(NEWLN);
}
}
}
@@ -147,12 +146,12 @@ public class Config {
newlines(1, builder);
builder.append(indent).append(" ")
.append(key).append(": ")
.append(Serilizer.convertObjecttoString(sections.get(realsection).get(key), " "));
.append(Serilizer.convertObjecttoString(sections.get(realsection).get(key)));
newlines(2, builder);
} else {
builder.append(indent).append(" ")
.append(key).append(": ")
.append(Serilizer.convertObjecttoString(sections.get(realsection).get(key), " "));
.append(Serilizer.convertObjecttoString(sections.get(realsection).get(key)));
newlines(1, builder);
}
}
@@ -193,7 +192,7 @@ public class Config {
}

private void processComment(String current, ArrayList<String> lines, int linenumber) {
if (current.contains("#") && lines.get(linenumber + 1) != null && !lines.get(linenumber + 1).contains("#")) {
if (current.contains("#") && !current.contains("___") && lines.get(linenumber + 1) != null && !lines.get(linenumber + 1).contains("#")) {
String nextline = lines.get(linenumber + 1);
comments.put(getKeyofLine(nextline), current.trim());
}
@@ -203,15 +202,6 @@ public class Config {
if (current.startsWith("#")) {
return false;
}
if (!currentsection.get().equals(MAINSECTIONID)) {
if (lines.size() > linenumber + 1 && lines.get(linenumber + 1).length() > 0 && !lines.get(linenumber + 1).startsWith(" ")) {
createnewSection(MAINSECTIONID);
return true;
} else if (lines.size() > linenumber + 2 && lines.get(linenumber + 2).length() > 0 && !lines.get(linenumber + 2).startsWith(" ")) {
createnewSection(MAINSECTIONID);
return true;
}
}
if (current.contains(":")) {
String value = current.substring(current.indexOf(":") + 1, current.length()).trim();
if (value.length() == 0 && lines.size() > linenumber + 1 && !lines.get(linenumber + 1).contains("-")) {
@@ -230,59 +220,64 @@ public class Config {
return current.contains(":") && !current.contains("#") && !current.contains("-");
}

private void processkey(String section, String value, String key) {
if (value.length() > 0 && sections.get(section) != null) {
Object valueobject = Serilizer.getObjectfromLine(value);
sections.get(currentsection.get()).put(key, valueobject);
}
}

private void processKeyValue(String current, AtomicReference<String> currentsection, ArrayList<String> lines, int linenumber) {
String key = getKeyofLine(current);
if (key != null) {
if (checkNormalValue(current)) {
String value = current.substring(current.indexOf(":") + 1, current.length()).trim();
if (value.length() > 0 && sections.get(currentsection.get()) != null) {
Object valueobject = Serilizer.getObjectfromLine(value);
sections.get(currentsection.get()).put(key, valueobject);
}
if (value.length() == 0 && lines.size() > linenumber + 1 && lines.get(linenumber + 1).contains("-")) {
ArrayList<Object> valuelist = new ArrayList<>();
int currentlineint = linenumber + 1;
while (lines.size() > currentlineint && lines.get(currentlineint).contains("-")) {
valuelist.add(Serilizer.getObjectfromLine(lines.get(currentlineint).replaceAll("-", "").trim()));
currentlineint++;
}
sections.get(currentsection.get()).put(key, valuelist);
if (!current.startsWith(" ")) {
processkey(currentsection.get(), value, key);
} else {
processkey(MAINSECTIONID, value, key);
}

}
}
}

private String printHeader() {
int longestline = 0;
for (String headerline : header) {
if (headerline.length() > longestline) {
longestline = headerline.length();
}
}
longestline = longestline + 6;
StringBuilder sb = new StringBuilder();
int headerline = 0;
if (longestline % 2 > 0) {
headerline = longestline;
} else {
headerline = longestline + 1;
}
sb.append("#").append(ConfigUtils.createLine(headerline)).append("#").append("\n");
for (String line : header) {
int linelenght = line.length();
String left = ConfigUtils.createFiller((longestline - linelenght) / 2);
String right = "";
if (linelenght % 2 > 0) {
right = ConfigUtils.createFiller(((longestline - linelenght) / 2) - 1);
if (header.size() > 0) {
int longestline = 0;
for (String headerline : header) {
if (headerline.length() > longestline) {
longestline = headerline.length();
}
}
longestline = longestline + 6;
int headerline = 0;
if (longestline % 2 > 0) {
headerline = longestline;
} else {
right = ConfigUtils.createFiller(((longestline - linelenght) / 2));
headerline = longestline + 1;
}
sb.append("#").append(ConfigUtils.createLine(headerline)).append("#").append("\n");
for (String line : header) {
int linelenght = line.length();
String left = ConfigUtils.createFiller((longestline - linelenght) / 2);
String right = "";
if (linelenght % 2 > 0) {
right = ConfigUtils.createFiller(((longestline - linelenght) / 2) - 1);
} else {
right = ConfigUtils.createFiller(((longestline - linelenght) / 2));
}
sb.append("#").append(left).append(line).append(right).append("#").append("\n");
}
sb.append("#").append(left).append(line).append(right).append("#").append("\n");
sb.append("#").append(ConfigUtils.createLine(headerline)).append("#");
}
sb.append("#").append(ConfigUtils.createLine(headerline)).append("#");
return sb.toString();
}

public String getAbsolutePath() {
return configfile.getAbsolutePath();
}

private String getCommentbyKey(String key) {
if (comments.containsKey(key)) {
@@ -357,6 +352,9 @@ public class Config {
orginalpath.append(options[0]);
optionpath = optionpath.replace(options[0] + ".", "");
}
if (orginalpath.length() == 0) {
orginalpath.append(MAINSECTIONID);
}
if (sections.get(orginalpath.toString()) != null) {
return sections.get(orginalpath.toString()).get(key);
}
@@ -386,6 +384,9 @@ public class Config {
createSection(orginalpath.toString());
optionpath = optionpath.replace(options[0] + ".", "");
}
if (orginalpath.length() == 0) {
orginalpath.append(MAINSECTIONID);
}
if (sections.get(orginalpath.toString()) != null) {
sections.get(orginalpath.toString()).put(key, value);
}
@@ -407,6 +408,9 @@ public class Config {
if (section.equals(path) || section.equals(key)) {
found = true;
}
if (sections.get(section).containsKey(path) || sections.get(section).containsKey(key)) {
found = true;
}
}
return found;
}


+ 2
- 14
src/main/java/de/moddylp/simplecommentconfig/ConfigManager.java View File

@@ -22,12 +22,8 @@ public class ConfigManager {
try {
File configfile = new File(path);
if (!configfile.exists()) {
if (!configfile.getParentFile().mkdirs()) {
System.err.println("Folder exists? or no Access");
}
if (!configfile.createNewFile()) {
System.err.println("File exists? or no Access");
}
configfile.getParentFile().mkdirs();
configfile.createNewFile();
}
Config config = new Config(configfile);
configs.put(path, config);
@@ -55,12 +51,4 @@ public class ConfigManager {
ex.printStackTrace();
}
}
public void saveToFile(String path) {
if (configs.containsKey(path)) {
configs.get(path).saveToFile();
} else {
getConfig(path);
saveToFile(path);
}
}
}

+ 25
- 10
src/main/java/de/moddylp/simplecommentconfig/Serilizer.java View File

@@ -16,12 +16,22 @@ public class Serilizer {
return Double.parseDouble(value);
} else if (isBoolean(value)) {
return Boolean.parseBoolean(value);
} else if (value.contains("[]") || value.contains("{}")) {
return new ArrayList<>();
} else if (value.contains("{") && value.contains("}")) {
String list = value.replaceAll("\\{", "").replaceAll("}", "").trim();
String[] values = list.split(",");
ArrayList<Object> objects = new ArrayList<>();
for (String val: values) {
objects.add(getObjectfromLine(val));
}
return objects;
} else {
System.err.println("Serializer Error: "+value);
return null;
}
}
public static String convertObjecttoString(Object value, String indent) {
public static String convertObjecttoString(Object value) {
if (value instanceof String) {
return "\'"+value.toString()+"\'";
} else if (value instanceof Double) {
@@ -31,18 +41,23 @@ public class Serilizer {
} else if (value instanceof Boolean) {
return Boolean.toString((Boolean)value);
} else if (value instanceof ArrayList) {
return convertObjecttoString(((ArrayList) value).toArray(), indent);
return convertObjecttoString(((ArrayList) value).toArray());
} else if (value instanceof Object[]) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\n");
List<Object> objects = Arrays.asList((Object[]) value);
for (int i = 0; i < objects.size(); i++) {
if (objects.size() == i+1) {
stringBuilder.append(indent).append(" - ").append(convertObjecttoString(objects.get(i), ""));
} else {
stringBuilder.append(indent).append(" - ").append(convertObjecttoString(objects.get(i), "")).append("\n");
}
if (((Object[])value).length > 0) {
List<Object> objects = Arrays.asList((Object[]) value);
stringBuilder.append("{");
for (int i = 0; i < objects.size(); i++) {
if (objects.size() == i + 1) {
stringBuilder.append(convertObjecttoString(objects.get(i)));
} else {
stringBuilder.append(convertObjecttoString(objects.get(i))).append(",");
}

}
stringBuilder.append("}");
} else {
stringBuilder.append("[]");
}
return stringBuilder.toString();
}


+ 14
- 3
src/test/java/MainTest.java View File

@@ -7,6 +7,7 @@ import org.junit.runners.MethodSorters;
import java.util.ArrayList;
import java.util.Arrays;

import static junit.framework.Assert.fail;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNotSame;
import static junit.framework.TestCase.assertSame;
@@ -27,28 +28,38 @@ public class MainTest {
@Test
public void aaa_parseRessoureConfig() {
manager = new ConfigManager();
//manager.copyRessource("C:\\Users\\N.Hartmann\\workspace\\ymlconfig\\config.conf", Config.class, "test.yml");
manager.copyRessource("./config.conf", this.getClass(), "test.yml");
config = manager.getConfig("./config.conf");
System.out.println(config.getAbsolutePath());
assertNotNull( manager);
assertNotNull( config);
config.addHeader(new String[] {"AncientRegions test de.moddylp.simplecommentconfig.Config", "Ich bin eine TestConfiguraton"});
config.set("testnested.testnestedsection.läuft", "hahahaha");
config.set("testnested.testnestedsection.laeuft", "hahahaha");
config.set("newtest.lolvalue", "hahahaha");
config.set("newtest.test", 56);
config.set("testnested.zweitesection.keytoll", 56765, "Ich bin ein toller Kommentar");
config.set("testnested.zweitesection.liste", new Object[]{3456, "tatatata", false});
config.set("testnested.zweitesection.zeweiteliste", new ArrayList<>(Arrays.asList(9999, "hjdjdhjf", "jhjdhds", true)));
config.set("testnested.zweitesection.leereliste", new ArrayList<>());
config.saveToFile();
assertNotNull(config.get("testnested.testnestedsection.läuft"));
assertNotNull(config.get("testnested.testnestedsection.laeuft"));
assertNotNull(config.get("newtest.lolvalue"));
assertNotNull(config.get("newtest.test"));
assertNotNull(config.get("testnested.zweitesection.keytoll"));
assertNotNull(config.get("testnested.zweitesection.liste"));
assertNotNull(config.get("testnested.zweitesection.zeweiteliste"));
assertNotNull(config.get("tollermainkey", 4444));
assertNotNull(config.get("newtest.lustig", 55555,
"ICH bin ein ganz langer kommentar und mache jetzt alles kaputt damm damm dammmmmmmm. Aber villeicht auch nicht"));
assertNotNull(config.get("a2Section.tada6", 55555,
"LOL"));
try {
if (((ArrayList) config.get("testnested.zweitesection.leereliste")).size() != 0) {
fail();
}
} catch (Exception ex) {
fail();
}
config.saveToFile();
config.reload();
assertSame(true, config.containsKey("newtest.test"));


src/main/resources/test.yml → src/test/resources/test.yml View File

@@ -3,9 +3,7 @@
#
#Tolles Programm
#
#Header

#KeyValueTest
#Header2
tada: 'tadavalue'

#KeyValueTest22
@@ -23,9 +21,8 @@ tada23: 23
#Lala Section
a2Section:
tada6: 'Anything'
listtest:
- 'Test'
- 'zwei'
listtest: {'Test', 'Zwei'}
wert: lolo

testnested:
nocheine: 5623

Loading…
Cancel
Save