48 lines
2.0 KiB
Java
48 lines
2.0 KiB
Java
|
package com.github.dtschust.zork.parser;
|
||
|
|
||
|
import com.github.dtschust.zork.parser.dom.DOMElement;
|
||
|
import com.github.dtschust.zork.types.ZorkContainer;
|
||
|
import com.github.dtschust.zork.types.ZorkCreature;
|
||
|
import com.github.dtschust.zork.types.ZorkItem;
|
||
|
import com.github.dtschust.zork.types.ZorkRoom;
|
||
|
import org.w3c.dom.Element;
|
||
|
|
||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||
|
import java.io.File;
|
||
|
import java.nio.channels.NonReadableChannelException;
|
||
|
|
||
|
public class ZorkXMLParser extends ZorkParser {
|
||
|
public ZorkXMLParser(final PropertyParseStrategy<ZorkCreature> creatureStrategy,
|
||
|
final PropertyParseStrategy<ZorkContainer> containerStrategy,
|
||
|
final PropertyParseStrategy<ZorkItem> itemStrategy,
|
||
|
final PropertyParseStrategy<ZorkRoom> roomStrategy) {
|
||
|
super(creatureStrategy, containerStrategy, itemStrategy, roomStrategy);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected Property getRootProperty(String filename) {
|
||
|
File file = new File(filename);
|
||
|
if (!file.canRead()) {
|
||
|
System.out.println("Error opening file. Exiting...");
|
||
|
throw new NonReadableChannelException();
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
// Open the xml file
|
||
|
final DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
|
||
|
// Limit XML features to mitigate vulnerabilities
|
||
|
builder.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||
|
builder.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
||
|
builder.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
||
|
|
||
|
final Element rootElement = builder.newDocumentBuilder().parse(file).getDocumentElement();
|
||
|
return DOMElement.of(rootElement);
|
||
|
} catch (final Exception e) {
|
||
|
System.out.println("Invalid XML file, exiting");
|
||
|
e.printStackTrace();
|
||
|
System.exit(-1);
|
||
|
return null; // never reached
|
||
|
}
|
||
|
}
|
||
|
}
|