Skip to content
This repository was archived by the owner on Apr 18, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,8 @@ elements in RDF lists.
\* Adapted from [EditorConfig](https://editorconfig.org/#file-format-details)

## Release Notes
* Unreleased
* Replace Platform-dependent line terminators in multiline strings with unix style newlines (`\n`, `\r`, and `\r\n` -> `\n`)
* 1.2.16:
* Bugfix: Empty RDF lists are formatted as empty set of parenthesis again
* 1.2.15:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class TurtleFormatter implements Function<Model, String>, BiConsumer<Mode
* need to escape single quotes.
* </p>
*/
private static final Pattern STRING_ESCAPE_SEQUENCES = Pattern.compile( "[\t\b\n\r\f\"\\\\]" );
private static final Pattern STRING_ESCAPE_SEQUENCES = Pattern.compile( "(\r\n)|[\t\b\n\r\f\"\\\\]" );

private final FormattingStyle style;

Expand Down Expand Up @@ -680,13 +680,15 @@ private String quoteAndEscape( final RDFNode node ) {
final String quote = switch ( style.quoteStyle ) {
case ALWAYS_SINGE_QUOTES -> "\"";
case ALWAYS_TRIPLE_QUOTES -> "\"\"\"";
case TRIPLE_QUOTES_FOR_MULTILINE -> value.contains( "\n" ) ? "\"\"\"" : "\"";
case TRIPLE_QUOTES_FOR_MULTILINE -> (value.contains( "\n" ) || value.contains("\r"))? "\"\"\"" : "\"";
};


final Map<String, String> characterReplacements = Map.of(
"\t", "\\\\t",
"\b", "\\\\b",
"\r", "\\\\r",
"\r", quote.equals( "\"" ) ? "\\\\r": "\n", // in multiline strings that were read with mac style endings, replace \r with \n
"\r\n", quote.equals( "\"" ) ? "\\\\r\\\\n": "\n", // in multiline strings that were read with windows style endings, replace \r\n with \n
"\f", "\\\\f",
"\n", quote.equals( "\"" ) ? "\\\\n" : "\n", // Don't escape line breaks in triple-quoted strings
"\"", quote.equals( "\"" ) ? "\\\\\"" : "\"", // Don't escape quotes in triple-quoted strings
Expand Down
92 changes: 92 additions & 0 deletions src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,98 @@ public void testEmptyList() {
assertThat(result.trim()).isEqualTo(expected);
}

@Test
public void testMultilineStrings(){
String content = """
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.com/ns#> .
:thing :value \"""
First Line
Second Line
Third Line
\""" .
""";
String expected = """
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.com/ns#> .

:thing :value \"""
First Line
Second Line
Third Line
\""" .""";
final FormattingStyle style = FormattingStyle.DEFAULT;
final TurtleFormatter formatter = new TurtleFormatter(style);
final String result = formatter.applyToContent(content);
assertThat(result.trim()).isEqualTo(expected);
}

@Test
public void testStringsWithUnixStyleNewlinesToMultilineStrings(){
String content = """
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.com/ns#> .
:thing :value "\\n First Line\\n Second Line\\n Third Line\\n" .
""";
String expected = """
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.com/ns#> .

:thing :value \"""
First Line
Second Line
Third Line
\""" .""";
final FormattingStyle style = FormattingStyle.DEFAULT;
final TurtleFormatter formatter = new TurtleFormatter(style);
final String result = formatter.applyToContent(content);
assertThat(result.trim()).isEqualTo(expected);
}

@Test
public void testStringsWithWindowsStyleNewlinesToMultilineStrings(){
String content = """
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.com/ns#> .
:thing :value "\\r\\n First Line\\r\\n Second Line\\r\\n Third Line\\r\\n" .
""";
String expected = """
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.com/ns#> .

:thing :value \"""
First Line
Second Line
Third Line
\""" .""";
final FormattingStyle style = FormattingStyle.DEFAULT;
final TurtleFormatter formatter = new TurtleFormatter(style);
final String result = formatter.applyToContent(content);
assertThat(result.trim()).isEqualTo(expected);
}

@Test
public void testStringsWithMacStyleNewlinesToMultilineStrings(){
String content = """
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.com/ns#> .
:thing :value "\\r First Line\\r Second Line\\r Third Line\\r" .
""";
String expected = """
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.com/ns#> .

:thing :value \"""
First Line
Second Line
Third Line
\""" .""";
final FormattingStyle style = FormattingStyle.DEFAULT;
final TurtleFormatter formatter = new TurtleFormatter(style);
final String result = formatter.applyToContent(content);
assertThat(result.trim()).isEqualTo(expected);
}


private Model modelFromString( final String content ) {
final Model model = ModelFactory.createDefaultModel();
Expand Down