Hi,
Previously I had issue with comments, but it was fixed and I was able to load from txt with the same content as on Wikipedia.
Could you please check?
Thanks
package com.codegym.task.task20.task2003;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
Introducing properties
*/
public class Solution {
public static Map<String, String> properties = new HashMap<>();
public void fillInPropertiesMap() throws Exception {
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
String filename = consoleReader.readLine();
consoleReader.close();
FileInputStream fileInputStream = new FileInputStream(filename);
load(fileInputStream);
}
public void save(OutputStream outputStream) throws Exception {
for (Map.Entry<String, String> pair : properties.entrySet()) {
Property property = new Property(pair.getKey(), pair.getValue(), (pair.getValue().trim().startsWith("\\u")) ? ":" : "=" );
outputStream.write(property.toString().getBytes());
outputStream.write("\n".charAt(0));
}
}
public void load(InputStream inputStream) throws Exception {
properties.clear();
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
String allPropertiesSerialized = new String(buffer);
String[] allProperties = allPropertiesSerialized.split("\n");
List<String> allPropertiesNormalized = new ArrayList<>();
int i = 0;
boolean mustBeConcat = false;
while (i<allProperties.length) {
if (!mustBeConcat & (allProperties[i].startsWith("#") || allProperties[i].startsWith("!"))) {
i++;
continue;
}
if (mustBeConcat && allProperties[i].endsWith("\\") && !allProperties[i].endsWith("\\\\")) {
allPropertiesNormalized.set(allPropertiesNormalized.size() - 1, allPropertiesNormalized.get(allPropertiesNormalized.size() - 1) + System.lineSeparator() + allProperties[i]);
mustBeConcat = true;
} else if (mustBeConcat & !(allProperties[i].endsWith("\\") && !allProperties[i].endsWith("\\\\"))) {
allPropertiesNormalized.set(allPropertiesNormalized.size() - 1, allPropertiesNormalized.get(allPropertiesNormalized.size() - 1) + System.lineSeparator() + allProperties[i]);
mustBeConcat = false;
} else if (!mustBeConcat && allProperties[i].endsWith("\\") && !allProperties[i].endsWith("\\\\") ) {
allPropertiesNormalized.add(allProperties[i]);
mustBeConcat = true;
} else {
allPropertiesNormalized.add(allProperties[i]);
mustBeConcat = false;
}
i++;
}
String patternString = "[^\\\\](=|:)";
Pattern pattern = Pattern.compile(patternString);
for (String s : allPropertiesNormalized) {
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
String key = s.substring(0, matcher.start()+1).trim();
String value = s.substring(matcher.start()+2).trim();
properties.put(key, value);
break;
}
}
}
public static void main(String[] args) throws Exception {
properties.put("website", "https://en.wikipedia.org/");
properties.put("language", "English");
properties.put("message", "Welcome to \\\n" +
" Wikipedia!");
properties.put("key", "valueOverOneLine\\\\");
properties.put("key\\:with\\=colonAndEqualsSign", "This is the value for the key \"key:with=colonAndEqualsSign\"");
properties.put("tab", "\\u0009");
properties.put("path", "c:\\\\wiki\\\\templates");
File yourFile = File.createTempFile("your_file_name", null);
OutputStream outputStream = new FileOutputStream(yourFile);
InputStream inputStream = new FileInputStream(yourFile);
Map<String, String> copy = new HashMap<>(properties);
Solution solution = new Solution();
solution.save(outputStream);
Solution otherSolution = new Solution();
solution.load(inputStream);
for (Map.Entry<String, String> pair : properties.entrySet()) {
//System.out.println(pair.getKey() + " EQUALITY " + pair.getValue());
if (!copy.containsKey(pair.getKey())) {
System.out.println("Not found key: " + pair.getKey());
} else {
if (!pair.getValue().equals(copy.get(pair.getKey()))) {
System.out.println("Value not equal: <" + pair.getValue() + "<, <" + copy.get(pair.getKey()) + ">");
}
}
}
}
}