我合法地被困在这里。我目前有这种方法:
public void set(Class<? extends ParentXMLModule> parent, Class<? extends XMLModule> moduleClass, String moduleValue, ContinuingMap<String, String> attributeMapping) {
System.out.println("SET par " + parent + " AND mod " + moduleClass);
try {
final Map<String, String> rawAttributeMapping = attributeMapping.getRaw();
final XMLModule module = XMLModule.of(moduleClass);
System.out.println("NO PRE MOD " + module.getTag() + "?? " + (document.getElementsByTagName(module.getTag()).getLength() == 0));
ParentXMLModule parentModule = XMLConstants.MAIN_BODY_MODULE;
if (parent != null) parentModule = (ParentXMLModule) XMLModule.of(parent);
Validate.isTrue(parentModule.getChildXMLModules() == null || parentModule.getChildXMLModules().contains(module.getClass()), "Parent module must accept child.");
Element moduleElement = (Element) expressions.getExpression(module.getTag()).evaluate(document, XPathConstants.NODE);
if (moduleElement != null) {
moduleElement.setTextContent(moduleValue);
} else {
moduleElement = document.createElement(module.getTag());
moduleElement.setTextContent(moduleValue);
final XPathExpression expression = expressions.getExpression("(" + parentModule.getTag() + ")[1]");
Node parentNode = (Node) expression.evaluate(document, XPathConstants.NODE);
if (parentNode == null) {
System.out.println((parentNode == null) + "VAL");
set(null, parentModule.getClass());
System.out.println("FROM " + parentModule.getTag() + ": " + document.getElementsByTagName(parentModule.getTag()).getLength());
parentNode = (Node) expression.evaluate(document, XPathConstants.NODE);
}
System.out.println((parentNode == null) + "VAL2");
parentNode.appendChild(moduleElement);
}
if (module.getXMLAttributes() != null) {
final Element finalModuleElement = moduleElement;
module.getXMLAttributes().forEach(xmlAttribute -> {
final String attributeName = xmlAttribute.getName();
String value = rawAttributeMapping.containsKey(attributeName) ? rawAttributeMapping.get(attributeName) : xmlAttribute.getDefaultValue();
if ((xmlAttribute.getValidValues() == null || !xmlAttribute.getValidValues().contains(value)) && xmlAttribute.isRequired())
value = xmlAttribute.getDefaultValue();
finalModuleElement.setAttribute(attributeName, value);
});
}
System.out.println("POST MOD " + module.getTag() + "?? " + (document.getElementsByTagName(module.getTag()).getLength() > 0));
System.out.println("--------------------------- for " + module.getTag());
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
但这篇文章主要关注这些内容
final XPathExpression expression = expressions.getExpression("(" + parentModule.getTag() + ")[1]");
Node parentNode = (Node) expression.evaluate(document, XPathConstants.NODE);
if (parentNode == null) {
System.out.println((parentNode == null) + "VAL");
set(null, parentModule.getClass());
System.out.println("FROM " + parentModule.getTag() + ": " + document.getElementsByTagName(parentModule.getTag()).getLength());
parentNode = (Node) expression.evaluate(document, XPathConstants.NODE);
}
System.out.println((parentNode == null) + "VAL2");
parentNode.appendChild(moduleElement);
我得到了
的调试输出handler.set(ContributorsParentModule.class, ContributorModule.class, "A contributor.", ContinuingMap.from("contribution", "Helping.").add("uuid", "a-u-u-i-d"));
(handler
是其中包含set方法的类)
SET par class com.github.wesjd.overcastmappacker.xml.module.impl.general.main.parents.ContibutorsParentModule AND mod class com.github.wesjd.overcastmappacker.xml.module.impl.general.main.ContributorModule
NO PRE MOD contributor?? true
trueVAL
SET par null AND mod class com.github.wesjd.overcastmappacker.xml.module.impl.general.main.parents.ContributorsParentModule
NO PRE MOD contributors?? true
falseVAL2
POST MOD contributors?? true
--------------------------- for contributors
FROM contributors: 1
trueVAL2
让我打破这一点,让你更好地理解。
SET par class com.github.wesjd.overcastmappacker.xml.module.impl.general.main.parents.ContibutorsParentModule AND mod class com.github.wesjd.overcastmappacker.xml.module.impl.general.main.ContributorModule
NO PRE MOD contributor?? true
trueVAL
停止并启动set方法以创建父模块。这是父模块的调试创建
SET par null AND mod class com.github.wesjd.overcastmappacker.xml.module.impl.general.main.parents.ContributorsParentModule
NO PRE MOD contributors?? true
falseVAL2
POST MOD contributors?? true
--------------------------- for contributors
这是设置父模块后继续的代码
FROM contributors: 1
trueVAL2
然后我得到
的NPEException in thread "main" java.lang.NullPointerException
at com.github.wesjd.overcastmappacker.xml.DocumentHandler.set(DocumentHandler.java:119)
at com.github.wesjd.overcastmappacker.OvercastMapPacker.main(OvercastMapPacker.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
这表示parentNode
为空。
现在,这对你有意义吗?在父模块创建之后,通过父名称获取所有标记大于0,但是从expressions.getExpression("(" + parentModule.getTag() + ")[1]")
评估我的XPathExpression将返回null,表示文档中没有父模块。
我是否已经失去了理智,或者我只是想成为一个使用API的菜鸟?