Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Adds a way to set the character encoding used by the DSL parser (see #…
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Sep 19, 2023
1 parent 04a9c7e commit fbb243a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- DSL identifiers (if present) will now be loaded when extending a JSON workspace (see https://github.com/structurizr/dsl/discussions/328).
- Adds a `context` variable to inline/external scripts (see https://github.com/structurizr/dsl/issues/332).
- Fixes https://github.com/structurizr/dsl/issues/324 (Groups with no curly braces breaks diagrams).
- Adds a way to set the character encoding used by the DSL parser (see https://github.com/structurizr/dsl/issues/338).

## 1.32.0 (28th July 2023)

Expand Down
17 changes: 16 additions & 1 deletion src/main/java/com/structurizr/dsl/StructurizrDslParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;
Expand All @@ -32,6 +33,7 @@ public final class StructurizrDslParser extends StructurizrDslTokens {

private static final String STRUCTURIZR_DSL_IDENTIFIER_PROPERTY_NAME = "structurizr.dsl.identifier";

private Charset characterEncoding = StandardCharsets.UTF_8;
private IdentifierScope identifierScope = IdentifierScope.Flat;
private Stack<DslContext> contextStack;
private Set<String> parsedTokens = new HashSet<>();
Expand All @@ -53,6 +55,19 @@ public StructurizrDslParser() {
constants = new HashMap<>();
}

/**
* Provides a way to change the character encoding used by the DSL parser.
*
* @param characterEncoding a Charset instance
*/
public void setCharacterEncoding(Charset characterEncoding) {
if (characterEncoding == null) {
throw new IllegalArgumentException("A character encoding must be specified");
}

this.characterEncoding = characterEncoding;
}

IdentifierScope getIdentifierScope() {
return identifierScope;
}
Expand Down Expand Up @@ -125,7 +140,7 @@ public void parse(File path) throws StructurizrDslParserException {
List<File> files = FileUtils.findFiles(path);
try {
for (File file : files) {
parse(Files.readAllLines(file.toPath(), StandardCharsets.UTF_8), file);
parse(Files.readAllLines(file.toPath(), characterEncoding), file);
}
} catch (IOException e) {
throw new StructurizrDslParserException(e.getMessage());
Expand Down
5 changes: 5 additions & 0 deletions src/test/dsl/iso-8859.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
workspace {
model {
softwareSystem "Namé"
}
}
9 changes: 9 additions & 0 deletions src/test/java/com/structurizr/dsl/DslTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.jupiter.params.provider.ValueSource;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Base64;
Expand Down Expand Up @@ -1029,4 +1030,12 @@ void test_GroupWithoutBrace() throws Exception {
}
}

@Test
void test_ISO8859Encoding() throws Exception {
StructurizrDslParser parser = new StructurizrDslParser();
parser.setCharacterEncoding(StandardCharsets.ISO_8859_1);
parser.parse(new File("src/test/dsl/iso-8859.dsl"));
assertNotNull(parser.getWorkspace().getModel().getSoftwareSystemWithName("Namé"));
}

}

0 comments on commit fbb243a

Please sign in to comment.