Skip to content

Commit

Permalink
Handle macros w/ default values
Browse files Browse the repository at this point in the history
  • Loading branch information
kasemir committed Jul 20, 2023
1 parent 7134cb1 commit 824ee70
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
39 changes: 38 additions & 1 deletion src/main/java/dbwr/macros/MacroUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 Oak Ridge National Laboratory.
* Copyright (c) 2019-2023 Oak Ridge National Laboratory.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the LICENSE
* which accompanies this distribution
Expand All @@ -10,9 +10,12 @@
import static dbwr.WebDisplayRepresentation.logger;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.w3c.dom.Element;

Expand All @@ -22,8 +25,12 @@

import dbwr.parser.XMLUtil;

/* Macro support */
public class MacroUtil
{
// This is simplified from the macro support in the desktop version
// TODO Update to use desktop version of macro support?

/** @param xml XML that contains '<macros>'
* @return Name, value map of macros. May be empty.
*/
Expand Down Expand Up @@ -91,6 +98,10 @@ public static String toJSON(final Map<String, String> macros) throws Exception
return buf.toString();
}

// From Desktop 'Macros.MACRO_NAME_PATTERN'
private static String MACRO_NAME_PATTERN = "[A-Za-z][A-Za-z0-9_.\\-\\[\\]]*";
// Find NAME and DEFAULT in "$(NAME=DEFAULT)"
private static Pattern MACRO_WITH_DEFAULT = Pattern.compile("\\$\\((" + MACRO_NAME_PATTERN + ")=([^)]+)\\)");

/** @param macros Macros
* @param text Text that might contain macro references
Expand All @@ -109,6 +120,18 @@ public static String expand(final MacroProvider macros, final String text)
while (result.contains("${") && --recursions > 0)
for (final String name : macros.getMacroNames())
result = result.replace("${" + name + "}", macros.getMacroValue(name));

// Expand unresolved macros that have default values
Matcher mac_w_def = MACRO_WITH_DEFAULT.matcher(result);
while (mac_w_def.find())
{
final String expression = mac_w_def.group(0);
final String name = mac_w_def.group(1);
final String def_val = mac_w_def.group(2);
final String value = macros.getMacroValue(name);
result =result.replace(expression, value != null ? value : def_val);
mac_w_def = MACRO_WITH_DEFAULT.matcher(result);
}

return result;
}
Expand All @@ -121,4 +144,18 @@ public static void expand(MacroProvider parent, Map<String, String> macros)
{
macros.replaceAll((name, value) -> expand(parent, value));
}

// Demo
public static void main(String[] args)
{
final Map<String, String> values = new HashMap<>();
values.put("S", "System");
values.put("N", "2");
final MacroProvider macros = MacroProvider.forMap(values);

System.out.println(expand(macros, "$(S):Motor$(N)"));
System.out.println(expand(macros, "$(TAB=7)"));
System.out.println(expand(macros, "$(S):Motor$(N=99)"));
System.out.println(expand(macros, "$(S):Motor$(N=99) on tab $(TAB=7)"));
}
}
24 changes: 23 additions & 1 deletion src/main/java/dbwr/parser/XMLUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019-2020 Oak Ridge National Laboratory.
* Copyright (c) 2019-2023 Oak Ridge National Laboratory.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -258,6 +258,28 @@ public static Optional<Integer> getChildInteger(final Element parent, final Stri
}
}

/** Given a parent element, locate integer value of a child node.
* @param macros Macros
* @param parent Parent element
* @param name Name of child element
* @return Value of child element, or empty result
* @throws Exception on error parsing the number
*/
public static Optional<Integer> getChildInteger(final MacroProvider macros, final Element parent, final String name) throws Exception
{
final Element child = getChildElement(parent, name);
if (child == null)
return Optional.empty();
try
{
return Optional.of(Integer.valueOf(MacroUtil.expand(macros, getString(child))));
}
catch (final NumberFormatException ex)
{
throw new Exception("Expected integer for <" + name +">", ex);
}
}

/** Given a parent element, locate long value of a child node.
* @param parent Parent element
* @param name Name of child element
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dbwr/widgets/NavTabsWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public NavTabsWidget(final ParentWidget parent, final Element xml) throws Except
tab_width = XMLUtil.getChildInteger(xml, "tab_width").orElse(100);
tab_height = XMLUtil.getChildInteger(xml, "tab_height").orElse(30);
tab_spacing = XMLUtil.getChildInteger(xml, "tab_spacing").orElse(2);
active_tab = XMLUtil.getChildInteger(xml, "active_tab").orElse(0);
active_tab = XMLUtil.getChildInteger(parent, xml, "active_tab").orElse(0);
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ view.jsp?cache=false&amp;display=file:/Path/to/Display+Builder/01_main.bob
<hr>

<div id="versions">
2023-07-20 Handle macros with default value<br>
2023-05-11 Navigation Tabs: Use parent macros even if no instance macros<br>
2023-04-28 Action Button: replace vs. new tab, also using ctrl key<br>
2023-04-26 Navigation Tabs: Use both instance and parent macros<br>
Expand Down

0 comments on commit 824ee70

Please sign in to comment.