Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
topframe committed Jun 6, 2021
1 parent 2c63342 commit 2f804a2
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 49 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/com/aspectran/core/util/TextStyler.java
Expand Up @@ -56,7 +56,7 @@ public static String stripAponStyle(String text) {
char c = text.charAt(end);
if (start == 0 && c == AponFormat.TEXT_LINE_START) {
if (line > 0) {
sb.append(AponFormat.NEW_LINE);
sb.append(AponFormat.SYSTEM_NEW_LINE);
}
start = end + 1;
line++;
Expand Down
Expand Up @@ -45,7 +45,9 @@ public class AponFormat {

public static final char NEW_LINE_CHAR = '\n';

public static final String NEW_LINE = System.lineSeparator();
public static final String NEW_LINE = "\n";

public static final String SYSTEM_NEW_LINE = System.lineSeparator();

protected static final String DEFAULT_INDENT_STRING = " ";

Expand Down
Expand Up @@ -391,7 +391,7 @@ private String readText() throws IOException {
if (sb == null) {
sb = new StringBuilder();
} else {
sb.append(NEW_LINE);
sb.append(SYSTEM_NEW_LINE);
}
str = line.substring(line.indexOf(TEXT_LINE_START) + 1);
if (str.length() > 0) {
Expand Down
Expand Up @@ -405,7 +405,8 @@ private void writeString(String value) throws IOException {
if (value.indexOf(DOUBLE_QUOTE_CHAR) >= 0 ||
value.indexOf(SINGLE_QUOTE_CHAR) >= 0 ||
value.startsWith(SPACE) ||
value.endsWith(SPACE)) {
value.endsWith(SPACE) ||
value.contains(AponFormat.NEW_LINE)) {
writer.write(DOUBLE_QUOTE_CHAR);
writer.write(escape(value));
writer.write(DOUBLE_QUOTE_CHAR);
Expand Down Expand Up @@ -491,7 +492,7 @@ private void endText() throws IOException {
}

private void newLine() throws IOException {
writer.write(NEW_LINE);
writer.write(SYSTEM_NEW_LINE);
}

private void indent() throws IOException {
Expand Down
24 changes: 12 additions & 12 deletions core/src/main/java/com/aspectran/core/util/apon/JsonToApon.java
Expand Up @@ -46,51 +46,51 @@ public static <T extends Parameters> T from(String json, T container) throws IOE
return from(new StringReader(json), container);
}

public static Parameters from(Reader in) throws IOException {
return from(in, new VariableParameters());
public static Parameters from(Reader reader) throws IOException {
return from(reader, new VariableParameters());
}

public static <T extends Parameters> T from(Reader in, Class<T> requiredType) throws IOException {
public static <T extends Parameters> T from(Reader reader, Class<T> requiredType) throws IOException {
T container = ClassUtils.createInstance(requiredType);
from(in, container);
from(reader, container);
return container;
}

public static <T extends Parameters> T from(Reader in, T container) throws IOException {
if (in == null) {
throw new IllegalArgumentException("in must not be null");
public static <T extends Parameters> T from(Reader reader, T container) throws IOException {
if (reader == null) {
throw new IllegalArgumentException("reader must not be null");
}
if (container == null) {
throw new IllegalArgumentException("container must not be null");
}

try {
JsonReader reader = new JsonReader(in);
JsonReader jsonReader = new JsonReader(reader);
String name = (container instanceof ArrayParameters ? ArrayParameters.NONAME : null);
convert(reader, container, name);
read(jsonReader, container, name);
} catch (Exception e) {
throw new IOException("Failed to convert JSON to APON", e);
}

return container;
}

private static void convert(JsonReader reader, Parameters container, String name) throws IOException {
private static void read(JsonReader reader, Parameters container, String name) throws IOException {
switch (reader.peek()) {
case BEGIN_OBJECT:
reader.beginObject();
if (name != null) {
container = container.newParameters(name);
}
while (reader.hasNext()) {
convert(reader, container, reader.nextName());
read(reader, container, reader.nextName());
}
reader.endObject();
return;
case BEGIN_ARRAY:
reader.beginArray();
while (reader.hasNext()) {
convert(reader, container, name);
read(reader, container, name);
}
reader.endArray();
return;
Expand Down
Expand Up @@ -202,7 +202,7 @@ public void arraylize() {
public void putValue(Object value) {
if (value != null) {
if (valueTypeFixed) {
value = fitValue(value);
value = determineValue(value);
} else {
determineValueType(value);
}
Expand Down Expand Up @@ -454,12 +454,12 @@ private void checkValueType(ValueType valueType) {

private void determineValueType(Object value) {
if (valueType == ValueType.STRING) {
if (value.toString().indexOf(AponFormat.NEW_LINE_CHAR) != -1) {
if (value.toString().contains(AponFormat.NEW_LINE)) {
valueType = ValueType.TEXT;
}
} else if (valueType == ValueType.VARIABLE) {
if (value instanceof CharSequence) {
if (value.toString().indexOf(AponFormat.NEW_LINE_CHAR) != -1) {
if (value.toString().contains(AponFormat.NEW_LINE)) {
valueType = ValueType.TEXT;
} else {
valueType = ValueType.STRING;
Expand All @@ -470,11 +470,9 @@ private void determineValueType(Object value) {
}
}

private Object fitValue(Object value) {
if (valueType == ValueType.STRING) {
if (!(value instanceof String)) {
return value.toString();
}
private Object determineValue(Object value) {
if (valueType == ValueType.STRING || valueType == ValueType.TEXT) {
return value.toString();
} else if (valueType == ValueType.BOOLEAN) {
if (!(value instanceof Boolean)) {
return Boolean.valueOf(value.toString());
Expand Down
Expand Up @@ -81,10 +81,10 @@ public static String stripValueTypeHint(String name) {
public static ValueType determineValueType(Object value) {
ValueType type;
if (value instanceof CharSequence) {
if (value.toString().indexOf(AponFormat.NEW_LINE_CHAR) == -1) {
type = ValueType.STRING;
} else {
if (value.toString().contains(AponFormat.NEW_LINE)) {
type = ValueType.TEXT;
} else {
type = ValueType.STRING;
}
} else if (value instanceof Character) {
type = ValueType.STRING;
Expand Down
Expand Up @@ -46,7 +46,7 @@ void testAssemble0() {

String apon = "action1: {\n result1: value1\n result2: value2\n}";

String s1 = apon.replace("\n", AponFormat.NEW_LINE);
String s1 = apon.replace("\n", AponFormat.SYSTEM_NEW_LINE);
String s2 = ps.toString().trim();

assertEquals(s1, s2);
Expand Down Expand Up @@ -77,7 +77,7 @@ void testAssemble1() {

String apon = "action0: value0\naction1: value3";

String s1 = apon.replace("\n", AponFormat.NEW_LINE);
String s1 = apon.replace("\n", AponFormat.SYSTEM_NEW_LINE);
String s2 = ps.toString().trim();

assertEquals(s1, s2);
Expand Down Expand Up @@ -109,7 +109,7 @@ void testAssemble2() {

String apon = "content1: {\n action0: value0\n" + " action1: {\n" + " result1: value1\n" + " result2: value2\n" + " }\n" + " action3: value3\n" + "}";

String s1 = apon.replace("\n", AponFormat.NEW_LINE);
String s1 = apon.replace("\n", AponFormat.SYSTEM_NEW_LINE);
String s2 = ps.toString().trim();

assertEquals(s1, s2);
Expand Down Expand Up @@ -160,7 +160,7 @@ void testAssemble3() {

String apon = "content1: {\n action0: value0\n" + " action1: {\n" + " result1: value1\n" + " result2: value2\n" + " }\n" + " action3: value3\n" + "}\n" + "content2: {\n" + " action0: value0\n" + " action1: {\n" + " result1: value1\n" + " result2: value2\n" + " }\n" + " action3: value3\n" + "}";

String s1 = apon.replace("\n", AponFormat.NEW_LINE);
String s1 = apon.replace("\n", AponFormat.SYSTEM_NEW_LINE);
String s2 = ps.toString().trim();

assertEquals(s1, s2);
Expand All @@ -185,7 +185,7 @@ void testAssemble4() {

String apon = "key1: value1\nkey2: value2\n" + "key3: value3";

String s1 = apon.replace("\n", AponFormat.NEW_LINE);
String s1 = apon.replace("\n", AponFormat.SYSTEM_NEW_LINE);
String s2 = ps.toString().trim();

assertEquals(s1, s2);
Expand Down Expand Up @@ -219,7 +219,7 @@ void testAssemble5() {

String apon = "content1: {\n key1: value1\n key2: value2\n key3: value3\n action1: {\n result2: value2\n }\n action2: value3\n}";

String s1 = apon.replace("\n", AponFormat.NEW_LINE);
String s1 = apon.replace("\n", AponFormat.SYSTEM_NEW_LINE);
String s2 = ps.toString().trim();

assertEquals(s1, s2);
Expand Down
Expand Up @@ -85,10 +85,10 @@ void testTemplateCall() {
assertEquals("TEMPLATE-3", result2);

String result4 = templateRenderer.render("aponStyle");
assertEquals("line-1\nline-2\nline-3".replace("\n", AponFormat.NEW_LINE), result4);
assertEquals("line-1\nline-2\nline-3".replace("\n", AponFormat.SYSTEM_NEW_LINE), result4);

String result5 = templateRenderer.render("compactStyle");
assertEquals("line-1\nline-2\nline-3".replace("\n", AponFormat.NEW_LINE), result5);
assertEquals("line-1\nline-2\nline-3".replace("\n", AponFormat.SYSTEM_NEW_LINE), result5);

String result6 = templateRenderer.render("compressedStyle");
assertEquals("line-1line-2line-3", result6);
Expand Down
Expand Up @@ -109,7 +109,7 @@ void escapedUnicodeTest() throws IOException {
@Test
void stringWithNewlinesWriteTest() throws IOException {
String input = "1\n2\n3";
input = input.replace("\n", AponFormat.NEW_LINE);
input = input.replace("\n", AponFormat.SYSTEM_NEW_LINE);

Parameters parameters = new VariableParameters();
parameters.putValue("textParam", input);
Expand Down
Expand Up @@ -45,7 +45,7 @@ void testConvertJsonToApon() throws IOException {

Parameters ps = JsonToApon.from(sb.toString(), new ArrayParameters());

String s1 = apon.replace("\n", AponFormat.NEW_LINE);
String s1 = apon.replace("\n", AponFormat.SYSTEM_NEW_LINE);
String s2 = ps.toString().trim();

assertEquals(s1, s2);
Expand All @@ -58,10 +58,50 @@ void testConvertJsonToApon2() throws IOException {

Parameters ps = JsonToApon.from(json);

String s1 = apon.replace("\n", AponFormat.NEW_LINE);
String s1 = apon.replace("\n", AponFormat.SYSTEM_NEW_LINE);
String s2 = ps.toString().trim();

assertEquals(s1, s2);
}

@Test
void testConvertJsonToApon3() throws IOException {
String json = "{\"message\": \"11\\n22\"}";

Parameters messagePayload = JsonToApon.from(json, MessagePayload.class);
String result1 = messagePayload.toString();

MessagePayload messagePayload2 = new MessagePayload();
AponReader reader = new AponReader(messagePayload.toString());
Parameters parameters = reader.read(messagePayload2);
String result2 = messagePayload.toString();

assertEquals(result1, result2);
}

public static class MessagePayload extends AbstractParameters {

private static final ParameterKey message;

private static final ParameterKey[] parameterKeys;

static {
message = new ParameterKey("message", ValueType.STRING);
parameterKeys = new ParameterKey[] { message };
}

public MessagePayload() {
super(parameterKeys);
}

public String getContent() {
return getString(message);
}

public void setContent(String content) {
putValue(MessagePayload.message, content);
}

}

}
Expand Up @@ -76,7 +76,7 @@ void test1() throws IOException {
" \"name\": \"Guest1\",\n" + " \"age\": 21,\n" + " \"approved\": true\n" + " },\n" +
" {\n" + " \"id\": \"guest-2\",\n" + " \"name\": \"Guest2\",\n" + " \"age\": 22,\n" +
" \"approved\": true\n" + " }\n" + " ],\n" + " \"emptyMap\": {\n" + " }\n" + "}"
.replace("\n", AponFormat.NEW_LINE);
.replace("\n", AponFormat.SYSTEM_NEW_LINE);

assertEquals(expected, result.trim());
}
Expand All @@ -103,7 +103,7 @@ void test2() throws IOException {

String expected = "{\n" + " \"item1\": 1,\n" + " \"item2\": 2,\n" + " \"item3\": {\n" +
" \"item11\": 11,\n" + " \"item22\": 22\n" + " }\n" + "}"
.replace("\n", AponFormat.NEW_LINE);
.replace("\n", AponFormat.SYSTEM_NEW_LINE);

assertEquals(expected, result.trim());
}
Expand All @@ -126,7 +126,7 @@ void test3() throws IOException {
// System.out.println(result);

String expected = "{\n" + " \"localDate\": \"2019-11-19\",\n" + " \"localDateTime\": \"2019-11-19\"\n" + "}"
.replace("\n", AponFormat.NEW_LINE);
.replace("\n", AponFormat.SYSTEM_NEW_LINE);

assertEquals(expected, result.trim());
}
Expand Down Expand Up @@ -157,7 +157,7 @@ void test4() throws IOException {

String expected = "{\n" + " \"key1\": \"value\",\n" + " \"key2\": \"1234\",\n" + " \"json\": {\n" +
" \"key1\": \"value\",\n" + " \"key2\": \"1234\"\n" + " },\n" + " \"array\": [1, 2, 3]\n" + "}"
.replace("\n", AponFormat.NEW_LINE);
.replace("\n", AponFormat.SYSTEM_NEW_LINE);

assertEquals(expected, writer.toString().trim());
}
Expand Down
Expand Up @@ -193,7 +193,7 @@ private CommandResult describeAspectRule(DaemonService service, String[] targets
AspectParameters aspectParameters = RulesToParameters.toAspectParameters(aspectRule);
if (count > 0) {
writer.write("----------------------------------------------------------------------------");
writer.write(AponFormat.NEW_LINE);
writer.write(AponFormat.SYSTEM_NEW_LINE);
}
AponWriter aponWriter = new AponWriter(writer).nullWritable(false);
aponWriter.write(aspectParameters);
Expand Down Expand Up @@ -318,7 +318,7 @@ private CommandResult describeTransletRule(DaemonService service, String[] targe
TransletParameters transletParameters = RulesToParameters.toTransletParameters(transletRule);
if (count > 0) {
writer.write("----------------------------------------------------------------------------");
writer.write(AponFormat.NEW_LINE);
writer.write(AponFormat.SYSTEM_NEW_LINE);
}
AponWriter aponWriter = new AponWriter(writer).nullWritable(false);
aponWriter.write(transletParameters);
Expand Down Expand Up @@ -376,7 +376,7 @@ private CommandResult describeScheduledJobRule(DaemonService service, String[] t
ScheduleParameters scheduleParameters = RulesToParameters.toScheduleParameters(scheduleRule);
if (count > 0) {
writer.write("----------------------------------------------------------------------------");
writer.write(AponFormat.NEW_LINE);
writer.write(AponFormat.SYSTEM_NEW_LINE);
}
AponWriter aponWriter = new AponWriter(writer).nullWritable(false);
aponWriter.write(scheduleParameters);
Expand Down Expand Up @@ -410,7 +410,7 @@ private CommandResult describeScheduledJobRule(DaemonService service, String[] t
ScheduleParameters scheduleParameters = RulesToParameters.toScheduleParameters(jobRule.getScheduleRule(), jobRule);
if (count > 0) {
writer.write("----------------------------------------------------------------------------");
writer.write(AponFormat.NEW_LINE);
writer.write(AponFormat.SYSTEM_NEW_LINE);
}
AponWriter aponWriter = new AponWriter(writer).nullWritable(false);
aponWriter.write(scheduleParameters);
Expand Down
Expand Up @@ -80,7 +80,7 @@ void testEmptyResult() throws IOException {
aponWriter.write(parameters);
aponWriter.close();

String s1 = ("translet: emptyResult\n" + "result: (\n" + " |\n" + ")").replace("\n", AponFormat.NEW_LINE);
String s1 = ("translet: emptyResult\n" + "result: (\n" + " |\n" + ")").replace("\n", AponFormat.SYSTEM_NEW_LINE);
String s2 = aponWriter.toString().trim();

assertEquals(s1, s2);
Expand All @@ -96,7 +96,7 @@ void testNullResult() throws IOException {
aponWriter.write(parameters);
aponWriter.close();

String s1 = "translet: nullResult".replace("\n", AponFormat.NEW_LINE);
String s1 = "translet: nullResult".replace("\n", AponFormat.SYSTEM_NEW_LINE);
String s2 = aponWriter.toString().trim();

assertEquals(s1, s2);
Expand Down

0 comments on commit 2f804a2

Please sign in to comment.