Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for deleting nodes with constants #1256

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 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,12 @@ 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 +164,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 +175,29 @@ 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