optimize line breaks and value parsing

This commit is contained in:
marhali 2022-07-01 12:33:21 +02:00
parent 02d7d7d051
commit 045bb6ac94
3 changed files with 125 additions and 3 deletions

View File

@ -52,9 +52,8 @@ public class PropertiesParserStrategy extends ParserStrategy {
PropertiesMapper.write(file.getLocale(), output, targetData, converter);
try(StringWriter writer = new StringWriter()) {
output.store(writer, null);
// Current implementation only works with \n line separators (IntelliJ Document Formatting)
return writer.toString().replaceAll("\r\n", "\n");
output.store(writer);
return writer.toString();
}
}
}

View File

@ -1,5 +1,12 @@
package de.marhali.easyi18n.io.parser.properties;
import de.marhali.easyi18n.util.IntelliJBufferedWriter;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.*;
/**
@ -38,6 +45,95 @@ public class SortableProperties extends Properties {
return this.properties.put(key, value);
}
@Override
@Deprecated
public void store(OutputStream out, @Nullable String comments) throws IOException {
throw new UnsupportedOperationException("Not implemented");
}
@Override
@Deprecated
public void store(Writer writer, String comments) throws IOException {
throw new UnsupportedOperationException("Not implemented");
}
public void store(Writer writer) throws IOException {
IntelliJBufferedWriter bw = new IntelliJBufferedWriter(writer);
boolean escUnicode = false;
synchronized (this) {
for (Map.Entry<Object, Object> e : entrySet()) {
String key = String.valueOf(e.getKey());
String val = String.valueOf(e.getValue());
key = saveConvert(key, true, escUnicode);
/* No need to escape embedded and trailing spaces for value, hence
* pass false to flag.
*/
val = saveConvert(val, false, escUnicode);
bw.write(key + "=" + val);
bw.newLine();
}
}
bw.flush();
}
/*
* Converts unicodes to encoded &#92;uxxxx and escapes
* special characters with a preceding slash
*/
private String saveConvert(String theString,
boolean escapeSpace,
boolean escapeUnicode) {
int len = theString.length();
int bufLen = len * 2;
if (bufLen < 0) {
bufLen = Integer.MAX_VALUE;
}
StringBuilder outBuffer = new StringBuilder(bufLen);
for(int x=0; x<len; x++) {
char aChar = theString.charAt(x);
// Handle common case first, selecting largest block that
// avoids the specials below
if ((aChar > 61) && (aChar < 127)) {
if (aChar == '\\') {
outBuffer.append('\\'); outBuffer.append('\\');
continue;
}
outBuffer.append(aChar);
continue;
}
switch(aChar) {
case ' ':
if (x == 0 || escapeSpace)
outBuffer.append('\\');
outBuffer.append(' ');
break;
case '\t':outBuffer.append('\\'); outBuffer.append('t');
break;
case '\n':outBuffer.append('\\'); outBuffer.append('n');
break;
case '\r':outBuffer.append('\\'); outBuffer.append('r');
break;
case '\f':outBuffer.append('\\'); outBuffer.append('f');
break;
case '=': // Fall through
case ':': // Fall through
case '#': // Fall through
case '!':
outBuffer.append('\\'); outBuffer.append(aChar);
break;
default:
if (((aChar < 0x0020) || (aChar > 0x007e)) & escapeUnicode ) {
outBuffer.append("\\u");
outBuffer.append(Integer.toHexString(aChar));
} else {
outBuffer.append(aChar);
}
}
}
return outBuffer.toString();
}
@Override
public synchronized String toString() {
return this.properties.toString();

View File

@ -0,0 +1,27 @@
package de.marhali.easyi18n.util;
import org.jetbrains.annotations.NotNull;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;
/**
* IntelliJ aware BufferedWriter implementation.
* (Document PSI uses \n as line separator)
* @author marhali
*/
public class IntelliJBufferedWriter extends BufferedWriter {
public IntelliJBufferedWriter(@NotNull Writer out) {
super(out);
}
public IntelliJBufferedWriter(@NotNull Writer out, int sz) {
super(out, sz);
}
@Override
public void newLine() throws IOException {
write("\n");
}
}