Skip to content

Commit

Permalink
#204 NewLine is now Platform-agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Mar 22, 2020
1 parent 95a4860 commit fca6643
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final class AllYamlLines implements YamlLines {
public String toString() {
final StringBuilder builder = new StringBuilder();
for (final YamlLine line : this.lines) {
builder.append(line.toString()).append("\n");
builder.append(line.toString()).append(System.lineSeparator());
}
return builder.toString();
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/amihaiemil/eoyaml/ReadFoldedBlockScalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ final class ReadFoldedBlockScalar extends ComparableScalar {
@Override
public String indent(final int indentation) {
StringBuilder builder = new StringBuilder();
final String newLine = System.lineSeparator();
for(final YamlLine line: this.lines) {
if(line.trimmed().length() == 0 || line.indentation() > 0) {
if(this.doNotEndWithNewLine(builder)) {
builder.append('\n');
builder.append(newLine);
}
int spaces = line.indentation();
if(spaces > 0) {
Expand All @@ -74,7 +75,7 @@ public String indent(final int indentation) {
}
}
builder.append(line.trimmed());
builder.append('\n');
builder.append(newLine);
} else {
if(this.doNotEndWithNewLine(builder)) {
builder.append(' ');
Expand All @@ -96,25 +97,26 @@ public String indent(final int indentation) {
*/
private boolean doNotEndWithNewLine(final StringBuilder builder) {
return builder.length() > 0
&& builder.charAt(builder.length()-1) != '\n';
&& !builder.toString().endsWith(System.lineSeparator());
}
/**
* Value of this scalar.
* @return String
*/
public String value() {
StringBuilder builder = new StringBuilder();
final String newLine = System.lineSeparator();
for(final YamlLine line: this.lines) {
if(line.trimmed().length() == 0 || line.indentation() > 0) {
if(this.doNotEndWithNewLine(builder)) {
builder.append('\n');
builder.append(newLine);
}
int indentation = line.indentation();
for(int i = 0; i < indentation; i++) {
builder.append(' ');
}
builder.append(line.trimmed());
builder.append('\n');
builder.append(newLine);
} else {
if(this.doNotEndWithNewLine(builder)) {
builder.append(' ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public String indent(final int indentation) {
spaces--;
}
printed.append(line.trimmed());
printed.append('\n');
printed.append(System.lineSeparator());
}
printed.delete(printed.length()-1, printed.length());
return printed.toString();
Expand All @@ -81,7 +81,7 @@ public String value() {
StringBuilder builder = new StringBuilder();
for(final YamlLine line: this.lines) {
builder.append(line.trimmed());
builder.append('\n');
builder.append(System.lineSeparator());
}
builder.delete(builder.length()-1, builder.length());
return builder.toString();
Expand Down
39 changes: 20 additions & 19 deletions src/main/java/com/amihaiemil/eoyaml/YamlMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public interface YamlMapping extends YamlNode {
* @return The found YamlNode or null if nothing is found.
*/
YamlNode value(final YamlNode key);

/**
* Return the keys' set of this mapping.<br><br>
* <b>Pay attention: </b> the keys are ordered.
Expand All @@ -120,6 +120,7 @@ default String indent(final int indentation) {
"Indentation level has to be >=0"
);
}
final String newLine = System.lineSeparator();
final StringBuilder print = new StringBuilder();
int spaces = indentation;
final StringBuilder indent = new StringBuilder();
Expand All @@ -133,27 +134,27 @@ default String indent(final int indentation) {
if(key instanceof Scalar) {
print.append(key.toString()).append(": ");
if (value instanceof Scalar) {
print.append(value.toString()).append("\n");
print.append(value.toString()).append(newLine);
} else {
print
.append("\n")
.append(newLine)
.append(value.indent(indentation + 2))
.append("\n");
.append(newLine);
}
} else {
print
.append("?\n")
.append(key.indent(indentation + 2)).append("\n")
.append("?").append(newLine)
.append(key.indent(indentation + 2)).append(newLine)
.append(indent).append(":");
if(value instanceof Scalar) {
print
.append(" ").append(value);
} else {
print
.append("\n")
.append(newLine)
.append(value.indent(indentation + 2));
}
print.append("\n");
print.append(newLine);
}
}
String printed = print.toString();
Expand All @@ -162,7 +163,7 @@ default String indent(final int indentation) {
}
return printed;
}

/**
* Convenience method to directly read an integer value
* from this map. It is equivalent to:
Expand All @@ -179,7 +180,7 @@ default String indent(final int indentation) {
default int integer(final String key) {
return this.integer(new BuiltPlainScalar(key));
}

/**
* Convenience method to directly read an integer value
* from this map. It is equivalent to:
Expand All @@ -200,7 +201,7 @@ default int integer(final YamlNode key) {
}
return -1;
}

/**
* Convenience method to directly read a float value
* from this map. It is equivalent to:
Expand All @@ -217,7 +218,7 @@ default int integer(final YamlNode key) {
default float floatNumber(final String key) {
return this.floatNumber(new BuiltPlainScalar(key));
}

/**
* Convenience method to directly read a float value
* from this map. It is equivalent to:
Expand All @@ -238,7 +239,7 @@ default float floatNumber(final YamlNode key) {
}
return -1;
}

/**
* Convenience method to directly read a double value
* from this map. It is equivalent to:
Expand All @@ -255,7 +256,7 @@ default float floatNumber(final YamlNode key) {
default double doubleNumber(final String key) {
return this.doubleNumber(new BuiltPlainScalar(key));
}

/**
* Convenience method to directly read a double value
* from this map. It is equivalent to:
Expand All @@ -276,7 +277,7 @@ default double doubleNumber(final YamlNode key) {
}
return -1.0;
}

/**
* Convenience method to directly read a long value
* from this map. It is equivalent to:
Expand All @@ -293,7 +294,7 @@ default double doubleNumber(final YamlNode key) {
default long longNumber(final String key) {
return this.longNumber(new BuiltPlainScalar(key));
}

/**
* Convenience method to directly read a long value
* from this map. It is equivalent to:
Expand Down Expand Up @@ -330,7 +331,7 @@ default long longNumber(final YamlNode key) {
default LocalDate date(final String key) {
return this.date(new BuiltPlainScalar(key));
}

/**
* Convenience method to directly read a LocalDate value
* from this map. It is equivalent to:
Expand All @@ -350,7 +351,7 @@ default LocalDate date(final YamlNode key) {
}
return null;
}

/**
* Convenience method to directly read a LocalDateTime value
* from this map. It is equivalent to:
Expand All @@ -366,7 +367,7 @@ default LocalDate date(final YamlNode key) {
default LocalDateTime dateTime(final String key) {
return this.dateTime(new BuiltPlainScalar(key));
}

/**
* Convenience method to directly read a LocalDateTime value
* from this map. It is equivalent to:
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/com/amihaiemil/eoyaml/YamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public interface YamlSequence extends YamlNode, Iterable<YamlNode> {
* @return Iterator of YamlNode.
*/
Iterator<YamlNode> iterator();

/**
* Indent this YamlSequence. This is a default method since indentation
* logic should be identical for any kind of YamlSequence, regardless of
Expand All @@ -91,6 +91,7 @@ default String indent(final int indentation) {
"Indentation level has to be >=0"
);
}
final String newLine = System.lineSeparator();
final StringBuilder print = new StringBuilder();
int spaces = indentation;
final StringBuilder indent = new StringBuilder();
Expand All @@ -102,10 +103,12 @@ default String indent(final int indentation) {
print.append(indent)
.append("- ");
if (node instanceof Scalar) {
print.append(node.toString()).append("\n");
print.append(node.toString()).append(newLine);
} else {
print.append("\n").append(node.indent(indentation + 2))
.append("\n");
print
.append(newLine)
.append(node.indent(indentation + 2))
.append(newLine);
}
}
String printed = print.toString();
Expand All @@ -114,7 +117,7 @@ default String indent(final int indentation) {
}
return printed;
}

/**
* Convenience method to directly read an integer value
* from this sequence. It is equivalent to:
Expand All @@ -134,7 +137,7 @@ default int integer(final int index) {
}
return -1;
}

/**
* Convenience method to directly read a float value
* from this sequence. It is equivalent to:
Expand All @@ -154,7 +157,7 @@ default float floatNumber(final int index) {
}
return -1;
}

/**
* Convenience method to directly read a double value
* from this sequence. It is equivalent to:
Expand All @@ -174,7 +177,7 @@ default double doubleNumber(final int index) {
}
return -1.0;
}

/**
* Convenience method to directly read a long value
* from this sequence. It is equivalent to:
Expand Down Expand Up @@ -213,7 +216,7 @@ default LocalDate date(final int index) {
}
return null;
}

/**
* Convenience method to directly read a LocalDateTime value
* from this sequence. It is equivalent to:
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/com/amihaiemil/eoyaml/YamlStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ public interface YamlStream extends YamlNode, Stream<YamlNode> {
/**
* Indent this YamlStream. It will take all elements and separate
* them with "---". It also starts with "---".
* If the YamlStream is empty, it will just print:
* If the YamlStream is empty, it will just print:
* <pre>
* ---
* ...
* </pre>
*
*
* Here is an example of printed YamlStream:
* <pre>
* ---
* architect: amihaiemil
* developers:
* developers:
* - amihaiemil
* - salikjan
* ---
* architect: yegor256
* developers:
* developers:
* - paolo
* - mario
* </pre>
Expand All @@ -72,6 +72,7 @@ default String indent(final int indentation) {
);
}
final StringBuilder print = new StringBuilder();
final String newLine = System.lineSeparator();
int spaces = indentation;
final StringBuilder indent = new StringBuilder();
while (spaces > 0) {
Expand All @@ -82,15 +83,17 @@ default String indent(final int indentation) {
String printed;
if(values.size() == 0) {
print
.append(indent).append("---\n")
.append(indent).append("---")
.append(newLine)
.append(indent).append("...");
printed = print.toString();
} else {
for (final YamlNode node : values) {
print.append(indent)
.append("---\n");
.append("---")
.append(newLine);
print.append(node.indent(indentation + 2));
print.append("\n");
print.append(newLine);
}
printed = print.toString();
if(printed.length() > 0) {
Expand All @@ -99,5 +102,5 @@ default String indent(final int indentation) {
}
return printed;
}

}

0 comments on commit fca6643

Please sign in to comment.