Added for preprocessing
This commit is contained in:
parent
918cc1ddf3
commit
682bfccbed
3 changed files with 232 additions and 94 deletions
|
@ -1,9 +1,12 @@
|
|||
package cr0s.warpdrive.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
|
@ -77,7 +80,140 @@ public class XmlPreprocessor {
|
|||
|
||||
}
|
||||
|
||||
public static void doLogicPreprocessing(Element root) {
|
||||
public static void doLogicPreprocessing(Node root) throws InvalidXmlException {
|
||||
|
||||
|
||||
NodeList children = root.getChildNodes();
|
||||
for (int i = 0; i < children.getLength(); i++)
|
||||
doLogicPreprocessing(children.item(i));
|
||||
|
||||
if (root.getNodeType() == Node.ELEMENT_NODE && ((Element) root).getTagName().equalsIgnoreCase("for")) {
|
||||
|
||||
Element forTag = (Element) root;
|
||||
|
||||
String varName = forTag.getAttribute("variable");
|
||||
if(varName.isEmpty())
|
||||
throw new InvalidXmlException("A for tag must include a variable attribute!");
|
||||
|
||||
//In supersedes from
|
||||
if (forTag.hasAttribute("in")) {
|
||||
String inOptions = forTag.getAttribute("in");
|
||||
|
||||
for(String input : inOptions.split(",")) {
|
||||
|
||||
NodeList allChildren = root.getChildNodes();
|
||||
for(int chI = 0; chI < allChildren.getLength(); chI ++) {
|
||||
|
||||
Node copy = getCopyVarReplace(allChildren.item(chI), varName, input);
|
||||
root.getParentNode().appendChild(copy);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
String fromStr = forTag.getAttribute("from");
|
||||
String toStr = forTag.getAttribute("to");
|
||||
|
||||
if (toStr.isEmpty() || fromStr.isEmpty())
|
||||
throw new InvalidXmlException("If a for doesnt have an in attr, it must have a from and to!");
|
||||
|
||||
int from, to;
|
||||
try {
|
||||
from = Integer.parseInt(fromStr);
|
||||
to = Integer.parseInt(toStr);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidXmlException(e);
|
||||
}
|
||||
|
||||
for (; from <= to; from++) {
|
||||
|
||||
NodeList allChildren = root.getChildNodes();
|
||||
for (int chI = 0; chI < allChildren.getLength(); chI++) {
|
||||
|
||||
Node copy = getCopyVarReplace(allChildren.item(chI), varName, "" + from);
|
||||
root.getParentNode().appendChild(copy);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Remove the old node
|
||||
root.getParentNode().removeChild(root);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static Node getCopyVarReplace(Node toCopy, String varName, String value) {
|
||||
|
||||
Node copy = toCopy.cloneNode(true);
|
||||
replaceVar(copy, varName, value);
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
private static void replaceVar(Node root, String varName, String value) {
|
||||
|
||||
ArrayList<String> toRemove = new ArrayList<String>();
|
||||
ArrayList<Attr> toAdd = new ArrayList<Attr>();
|
||||
|
||||
if (root.getNodeType() == Node.ELEMENT_NODE) {
|
||||
|
||||
//First replace attributes
|
||||
NamedNodeMap attrs = root.getAttributes();
|
||||
for (int i = 0; i < attrs.getLength(); i++) {
|
||||
|
||||
Attr attr = (Attr) attrs.item(i);
|
||||
String name = attr.getName();
|
||||
String newName = name.replace("%" + varName + "%", value);
|
||||
|
||||
if (name.equals(newName)) {
|
||||
|
||||
//Easy, just adjust value
|
||||
attr.setValue(attr.getValue().replace("%" + varName + "%", value));
|
||||
|
||||
} else {
|
||||
|
||||
//The name changed
|
||||
toRemove.add(name);
|
||||
|
||||
Attr newAttr = attr.getOwnerDocument().createAttribute(newName);
|
||||
newAttr.setValue(attr.getValue().replace("%" + varName + "%", value));
|
||||
toAdd.add(newAttr);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Now do the adds and removals
|
||||
for (String attr : toRemove)
|
||||
attrs.removeNamedItem(attr);
|
||||
|
||||
for (Attr attr : toAdd)
|
||||
attrs.setNamedItem(attr);
|
||||
}
|
||||
|
||||
//Now that Attributes are done, go through all of the children
|
||||
|
||||
NodeList children = root.getChildNodes();
|
||||
for (int i = 0; i < children.getLength(); i++) {
|
||||
Node child = children.item(i);
|
||||
|
||||
switch (child.getNodeType()) {
|
||||
case Node.ELEMENT_NODE://Recurse on the element
|
||||
replaceVar(child, varName, value);
|
||||
break;
|
||||
case Node.TEXT_NODE:
|
||||
child.setTextContent(child.getTextContent().replace("%" + varName + "%", value));
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -79,6 +79,7 @@ public class FillerManager {
|
|||
|
||||
// Remove elements based on mod reqs sanitation
|
||||
XmlPreprocessor.doModReqSanitation(base);
|
||||
XmlPreprocessor.doLogicPreprocessing(base);
|
||||
|
||||
// Initially add FillerSets
|
||||
NodeList nodesFillerSet = base.getElementsByTagName("FillerSet");
|
||||
|
|
|
@ -71,6 +71,7 @@ public class StructureManager {
|
|||
}
|
||||
|
||||
XmlPreprocessor.doModReqSanitation(base);
|
||||
XmlPreprocessor.doLogicPreprocessing(base);
|
||||
|
||||
NodeList structures = base.getElementsByTagName("structure");
|
||||
for (int i = 0; i < structures.getLength(); i++) {
|
||||
|
|
Loading…
Reference in a new issue