Skip to content

Commit

Permalink
Add support for deleting nodes with constants
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 committed Oct 13, 2023
1 parent efb4df6 commit 54d9ac8
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions core/src/main/java/tc/oc/pgm/map/MapFilePreprocessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jdom2.JDOMException;
import org.jdom2.Text;
import org.jdom2.input.SAXBuilder;
import org.jetbrains.annotations.Nullable;
import tc.oc.pgm.api.map.MapSource;
import tc.oc.pgm.api.map.exception.MapMissingException;
import tc.oc.pgm.api.map.includes.MapInclude;
Expand Down Expand Up @@ -92,7 +93,11 @@ public Document getDocument()

for (Element constant :
XMLUtils.flattenElements(document.getRootElement(), "constants", "constant", 0)) {
constants.put(XMLUtils.parseRequiredId(constant), constant.getText());
boolean isDelete = XMLUtils.parseBoolean(constant.getAttribute("delete"), false);
String text = constant.getText();
if ((text == null || text.isEmpty()) != isDelete)
throw new InvalidXMLException("Delete attribute cannot be combined with having an inner text", constant);
constants.put(XMLUtils.parseRequiredId(constant), isDelete ? null : constant.getText());
}

// If no constants are set, assume we can skip the step
Expand Down Expand Up @@ -158,7 +163,9 @@ private List<Content> processConditional(Element el, boolean shouldContain)

private void postprocessChildren(Element parent) throws InvalidXMLException {
for (Attribute attribute : parent.getAttributes()) {
attribute.setValue(postprocessString(parent, attribute.getValue()));
String result = postprocessString(parent, attribute.getValue());
if (result == null) parent.removeAttribute(attribute);
else attribute.setValue(result);
}

for (int i = 0; i < parent.getContentSize(); i++) {
Expand All @@ -167,22 +174,28 @@ private void postprocessChildren(Element parent) throws InvalidXMLException {
postprocessChildren((Element) content);
} else if (content instanceof Text) {
Text text = (Text) content;
text.setText(postprocessString(parent, text.getText()));

String result = postprocessString(parent, text.getText());
if (result == null) parent.removeContent(text);
else text.setText(result);
}
}
}

private String postprocessString(Element el, String text) throws InvalidXMLException {
private @Nullable String postprocessString(Element el, String text) throws InvalidXMLException {
Matcher matcher = CONSTANT_PATTERN.matcher(text);

StringBuffer result = new StringBuffer();
while (matcher.find()) {
String constant = matcher.group(1);
String replacement = constants.get(matcher.group(1));
if (replacement == null)
throw new InvalidXMLException(
"No constant '" + constant + "' is used but has not been defined", el);

String replacement = constants.get(constant);
if (replacement == null) {
if (!constants.containsKey(constant)) {
throw new InvalidXMLException("Constant '" + constant + "' is used but has not been defined", el);
} else {
return null;
}
}
matcher.appendReplacement(result, replacement);
}
matcher.appendTail(result);
Expand Down

0 comments on commit 54d9ac8

Please sign in to comment.