From 52ec9be442477af82d83281184aa54e88669b261 Mon Sep 17 00:00:00 2001 From: Simon Brown Date: Tue, 20 Aug 2024 11:09:14 +0100 Subject: [PATCH] Adds a way to add constants to the parser programmatically. --- .../structurizr/dsl/StructurizrDslParser.java | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/structurizr-dsl/src/main/java/com/structurizr/dsl/StructurizrDslParser.java b/structurizr-dsl/src/main/java/com/structurizr/dsl/StructurizrDslParser.java index cffcebef..cca16737 100644 --- a/structurizr-dsl/src/main/java/com/structurizr/dsl/StructurizrDslParser.java +++ b/structurizr-dsl/src/main/java/com/structurizr/dsl/StructurizrDslParser.java @@ -889,11 +889,11 @@ void parse(List lines, File dslFile, boolean include) throws Structurizr } else if (CONST_TOKEN.equalsIgnoreCase(firstToken)) { NameValuePair nameValuePair = new NameValueParser().parseConstant(tokens); - - if (constantsAndVariables.containsKey(nameValuePair.getName())) { - throw new StructurizrDslParserException("A constant/variable \"" + nameValuePair.getName() + "\" already exists"); + try { + addConstant(nameValuePair); + } catch (IllegalArgumentException e) { + throw new StructurizrDslParserException(e.getMessage()); } - constantsAndVariables.put(nameValuePair.getName(), nameValuePair); } else if (VAR_TOKEN.equalsIgnoreCase(firstToken)) { NameValuePair nameValuePair = new NameValueParser().parseVariable(tokens); @@ -1062,8 +1062,39 @@ private void registerIdentifier(String identifier, Relationship relationship) { relationship.addProperty(STRUCTURIZR_DSL_IDENTIFIER_PROPERTY_NAME, identifiersRegister.findIdentifier(relationship)); } + /** + * Gets the named constant. + * + * @param name the name of the constant + * @return the value, or an empty string if the named constant doesn't exist + */ public String getConstant(String name) { - return constantsAndVariables.get(name).getValue(); + NameValuePair nameValuePair = constantsAndVariables.get(name); + if (nameValuePair != null) { + return nameValuePair.getValue(); + } else { + return ""; + } + } + + /** + * Adds a constant to the parser. + * @param name the name of the constant + * @param value the value of the constant + */ + public void addConstant(String name, String value) { + if (StringUtils.isNullOrEmpty(name)) { + throw new IllegalArgumentException("A constant name must be specified"); + } + + addConstant(new NameValuePair(name, value)); + } + + private void addConstant(NameValuePair nameValuePair) { + if (constantsAndVariables.containsKey(nameValuePair.getName())) { + throw new IllegalArgumentException("A constant/variable \"" + nameValuePair.getName() + "\" already exists"); + } + constantsAndVariables.put(nameValuePair.getName(), nameValuePair); } private boolean inContext(Class clazz) {