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

Commit

Permalink
Resolves #361.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Nov 12, 2023
1 parent a971416 commit 613e46c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ targetCompatibility = 17

description = 'Structurizr DSL'
group = 'com.structurizr'
version = '1.33.1'
version = '1.34.0'

test {
useJUnitPlatform()
Expand Down
5 changes: 3 additions & 2 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog

## 1.33.1 (unreleased)
## 1.34.0 (unreleased)

- Fixes https://github.com/structurizr/dsl/issues/364 (.DS_Store file causes exception during !include <directory> on Windows)
- Fixes https://github.com/structurizr/dsl/issues/364 (.DS_Store file causes exception during !include <directory> on Windows).
- Adds a `getDslParser()` method to the `StructurizrDslPluginContext` class (https://github.com/structurizr/dsl/issues/361).

## 1.33.0 (27th October 2023)

Expand Down
46 changes: 38 additions & 8 deletions src/main/java/com/structurizr/dsl/IdentifiersRegister.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,28 @@ public class IdentifiersRegister {

private IdentifierScope identifierScope = IdentifierScope.Flat;

private Map<String, Element> elementsByIdentifier = new HashMap<>();
private final Map<String, Element> elementsByIdentifier = new HashMap<>();

private Map<String, Relationship> relationshipsByIdentifier = new HashMap<>();
private final Map<String, Relationship> relationshipsByIdentifier = new HashMap<>();

IdentifiersRegister() {
}

IdentifierScope getIdentifierScope() {
/**
* Gets the identifier scope in use (i.e. Flat or Hierarchical ... applies to elements only).
*
* @return an IdentifierScope enum
*/
public IdentifierScope getIdentifierScope() {
return identifierScope;
}

void setIdentifierScope(IdentifierScope identifierScope) {
/**
* Sets the identifier scope (i.e. Flat or Hierarchical ... applies to elements only).
*
* @param identifierScope an IdentifierScope enum
*/
public void setIdentifierScope(IdentifierScope identifierScope) {
this.identifierScope = identifierScope;
}

Expand Down Expand Up @@ -64,7 +74,17 @@ public Element getElement(String identifier) {
return elementsByIdentifier.get(identifier);
}

void register(String identifier, Element element) {
/**
* Registers an element with the given identifier.
*
* @param identifier an identifier
* @param element an Element instance
*/
public void register(String identifier, Element element) {
if (element == null) {
throw new IllegalArgumentException("An element must be specified");
}

if (StringUtils.isNullOrEmpty(identifier)) {
identifier = UUID.randomUUID().toString();
}
Expand Down Expand Up @@ -109,7 +129,17 @@ public Relationship getRelationship(String identifier) {
return relationshipsByIdentifier.get(identifier);
}

void register(String identifier, Relationship relationship) {
/**
* Registers a relationship with the given identifier.
*
* @param identifier an identifier
* @param relationship a Relationship instance
*/
public void register(String identifier, Relationship relationship) {
if (relationship == null) {
throw new IllegalArgumentException("A relationship must be specified");
}

if (StringUtils.isNullOrEmpty(identifier)) {
identifier = UUID.randomUUID().toString();
}
Expand Down Expand Up @@ -146,7 +176,7 @@ private String calculateHierarchicalIdentifier(String identifier, Element elemen
* @return a String identifier (could be null if no identifier was explicitly specified)
*/
public String findIdentifier(Element element) {
if (elementsByIdentifier.values().contains(element)) {
if (elementsByIdentifier.containsValue(element)) {
for (String identifier : elementsByIdentifier.keySet()) {
Element e = elementsByIdentifier.get(identifier);

Expand All @@ -166,7 +196,7 @@ public String findIdentifier(Element element) {
* @return a String identifier (could be null if no identifier was explicitly specified, or for implied relationships)
*/
public String findIdentifier(Relationship relationship) {
if (relationshipsByIdentifier.values().contains(relationship)) {
if (relationshipsByIdentifier.containsValue(relationship)) {
for (String identifier : relationshipsByIdentifier.keySet()) {
Relationship r = relationshipsByIdentifier.get(identifier);

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/structurizr/dsl/PluginDslContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ class PluginDslContext extends DslContext {

private final String fullyQualifiedClassName;
private final File dslFile;
private final StructurizrDslParser dslParser;
private final Map<String,String> parameters = new HashMap<>();

PluginDslContext(String fullyQualifiedClassName, File dslFile) {
PluginDslContext(String fullyQualifiedClassName, File dslFile, StructurizrDslParser dslParser) {
this.fullyQualifiedClassName = fullyQualifiedClassName;
this.dslFile = dslFile;
this.dslParser = dslParser;
}

void addParameter(String name, String value) {
Expand All @@ -24,7 +26,7 @@ void end() {
try {
Class pluginClass = loadClass(fullyQualifiedClassName, dslFile);
StructurizrDslPlugin plugin = (StructurizrDslPlugin)pluginClass.getDeclaredConstructor().newInstance();
StructurizrDslPluginContext pluginContext = new StructurizrDslPluginContext(dslFile, getWorkspace(), parameters);
StructurizrDslPluginContext pluginContext = new StructurizrDslPluginContext(dslParser, dslFile, getWorkspace(), parameters);
plugin.run(pluginContext);
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ void parse(List<String> lines, File dslFile) throws StructurizrDslParserExceptio
} else if (PLUGIN_TOKEN.equalsIgnoreCase(firstToken)) {
if (!restricted) {
String fullyQualifiedClassName = new PluginParser().parse(getContext(), tokens.withoutContextStartToken());
startContext(new PluginDslContext(fullyQualifiedClassName, dslFile));
startContext(new PluginDslContext(fullyQualifiedClassName, dslFile, this));
if (!shouldStartContext(tokens)) {
// run the plugin immediately, without looking for parameters
endContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,35 @@
*/
public class StructurizrDslPluginContext {

private final StructurizrDslParser dslParser;
private final File dslFile;
private final Workspace workspace;
private final Map<String,String> parameters;

/**
* Creates a new instance.
*
* @param dslParser a reference to the DSL parser that loaded the plugin
* @param dslFile a reference to the DSL file that loaded the plugin
* @param workspace the workspace
* @param parameters a map of name/value pairs representing parameters
*/
public StructurizrDslPluginContext(File dslFile, Workspace workspace, Map<String,String> parameters) {
public StructurizrDslPluginContext(StructurizrDslParser dslParser, File dslFile, Workspace workspace, Map<String,String> parameters) {
this.dslParser = dslParser;
this.dslFile = dslFile;
this.workspace = workspace;
this.parameters = parameters;
}

/**
* Gets a reference to the DSL parser that initiated this plugin context.
*
* @return a StructurizrDslParser instance
*/
public StructurizrDslParser getDslParser() {
return dslParser;
}

/**
* Gets a reference to the DSL file that initiated this plugin context.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PluginDslContextTests extends AbstractTests {
@Test
void test_end_ThrowsAnException_WhenThePluginClassDoesNotExist() {
try {
PluginDslContext context = new PluginDslContext("com.structurizr.TestPlugin", new File("src/test/dsl"));
PluginDslContext context = new PluginDslContext("com.structurizr.TestPlugin", new File("src/test/dsl"), null);
context.end();
fail();
} catch (Exception e) {
Expand Down

0 comments on commit 613e46c

Please sign in to comment.