From 613e46c605f713fcc4895f430c8808ad93b8c69d Mon Sep 17 00:00:00 2001 From: Simon Brown Date: Sun, 12 Nov 2023 10:52:05 +0000 Subject: [PATCH] Resolves #361. --- build.gradle | 2 +- docs/changelog.md | 5 +- .../structurizr/dsl/IdentifiersRegister.java | 46 +++++++++++++++---- .../com/structurizr/dsl/PluginDslContext.java | 6 ++- .../structurizr/dsl/StructurizrDslParser.java | 2 +- .../dsl/StructurizrDslPluginContext.java | 14 +++++- .../dsl/PluginDslContextTests.java | 2 +- 7 files changed, 61 insertions(+), 16 deletions(-) diff --git a/build.gradle b/build.gradle index 820c88b..9c42505 100644 --- a/build.gradle +++ b/build.gradle @@ -28,7 +28,7 @@ targetCompatibility = 17 description = 'Structurizr DSL' group = 'com.structurizr' -version = '1.33.1' +version = '1.34.0' test { useJUnitPlatform() diff --git a/docs/changelog.md b/docs/changelog.md index f6d08d5..80d0f6f 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -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 on Windows) +- Fixes https://github.com/structurizr/dsl/issues/364 (.DS_Store file causes exception during !include on Windows). +- Adds a `getDslParser()` method to the `StructurizrDslPluginContext` class (https://github.com/structurizr/dsl/issues/361). ## 1.33.0 (27th October 2023) diff --git a/src/main/java/com/structurizr/dsl/IdentifiersRegister.java b/src/main/java/com/structurizr/dsl/IdentifiersRegister.java index ecbef0c..1d55245 100644 --- a/src/main/java/com/structurizr/dsl/IdentifiersRegister.java +++ b/src/main/java/com/structurizr/dsl/IdentifiersRegister.java @@ -20,18 +20,28 @@ public class IdentifiersRegister { private IdentifierScope identifierScope = IdentifierScope.Flat; - private Map elementsByIdentifier = new HashMap<>(); + private final Map elementsByIdentifier = new HashMap<>(); - private Map relationshipsByIdentifier = new HashMap<>(); + private final Map 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; } @@ -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(); } @@ -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(); } @@ -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); @@ -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); diff --git a/src/main/java/com/structurizr/dsl/PluginDslContext.java b/src/main/java/com/structurizr/dsl/PluginDslContext.java index 888c912..a648c2e 100644 --- a/src/main/java/com/structurizr/dsl/PluginDslContext.java +++ b/src/main/java/com/structurizr/dsl/PluginDslContext.java @@ -8,11 +8,13 @@ class PluginDslContext extends DslContext { private final String fullyQualifiedClassName; private final File dslFile; + private final StructurizrDslParser dslParser; private final Map 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) { @@ -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(); diff --git a/src/main/java/com/structurizr/dsl/StructurizrDslParser.java b/src/main/java/com/structurizr/dsl/StructurizrDslParser.java index c51e792..ba4abc7 100644 --- a/src/main/java/com/structurizr/dsl/StructurizrDslParser.java +++ b/src/main/java/com/structurizr/dsl/StructurizrDslParser.java @@ -274,7 +274,7 @@ void parse(List 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(); diff --git a/src/main/java/com/structurizr/dsl/StructurizrDslPluginContext.java b/src/main/java/com/structurizr/dsl/StructurizrDslPluginContext.java index 69161d3..10f1049 100644 --- a/src/main/java/com/structurizr/dsl/StructurizrDslPluginContext.java +++ b/src/main/java/com/structurizr/dsl/StructurizrDslPluginContext.java @@ -11,6 +11,7 @@ */ public class StructurizrDslPluginContext { + private final StructurizrDslParser dslParser; private final File dslFile; private final Workspace workspace; private final Map parameters; @@ -18,16 +19,27 @@ public class StructurizrDslPluginContext { /** * 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 parameters) { + public StructurizrDslPluginContext(StructurizrDslParser dslParser, File dslFile, Workspace workspace, Map 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. * diff --git a/src/test/java/com/structurizr/dsl/PluginDslContextTests.java b/src/test/java/com/structurizr/dsl/PluginDslContextTests.java index 1f49965..90900c7 100644 --- a/src/test/java/com/structurizr/dsl/PluginDslContextTests.java +++ b/src/test/java/com/structurizr/dsl/PluginDslContextTests.java @@ -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) {