use of org.jdom2.input.SAXBuilder in project pcgen by PCGen.
the class NameGenPanel method loadData.
private void loadData(File path) {
if (path.isDirectory()) {
File[] dataFiles = path.listFiles(new XMLFilter());
SAXBuilder builder = new SAXBuilder();
GeneratorDtdResolver resolver = new GeneratorDtdResolver(path);
builder.setEntityResolver(resolver);
for (File dataFile : dataFiles) {
try {
URL url = dataFile.toURI().toURL();
Document nameSet = builder.build(url);
DocType dt = nameSet.getDocType();
if (dt.getElementName().equals("GENERATOR")) {
loadFromDocument(nameSet);
}
} catch (Exception e) {
Logging.errorPrint(e.getMessage(), e);
JOptionPane.showMessageDialog(this, "XML Error with file " + dataFile.getName());
}
}
loadDropdowns();
} else {
JOptionPane.showMessageDialog(this, "No data files in directory " + path.getPath());
}
}
use of org.jdom2.input.SAXBuilder in project pcgen by PCGen.
the class RoomBoardFactory method load.
public static RoomBoard load(File dataDir) {
//Create a new list for the room and board
PairList<RBCost> inns = new PairList<>();
PairList<RBCost> foods = new PairList<>();
PairList<RBCost> animals = new PairList<>();
File path = new File(dataDir, DIR_RNBPRICE);
if (path.isDirectory()) {
File[] dataFiles = path.listFiles(new XMLFilter());
SAXBuilder builder = new SAXBuilder();
for (int i = 0; i < dataFiles.length; i++) {
try {
Document methodSet = builder.build(dataFiles[i]);
DocType dt = methodSet.getDocType();
if (//$NON-NLS-1$
dt.getElementName().equals("RNBPRICE")) {
//Do work here
loadRBData(methodSet, inns, foods, animals);
}
methodSet = null;
dt = null;
} catch (Exception e) {
Logging.errorPrintLocalised("XML Error with file {0}", dataFiles[i].getName());
Logging.errorPrint(e.getMessage(), e);
}
}
} else {
//$NON-NLS-1$
Logging.errorPrintLocalised("in_plugin_overland_noDatafile", path.getPath());
}
return new RoomBoardImplementation(inns, foods, animals);
}
use of org.jdom2.input.SAXBuilder in project JMRI by JMRI.
the class XmlFile method processOneInstruction.
Document processOneInstruction(ProcessingInstruction p, Document doc) throws org.jdom2.transform.XSLTransformException, org.jdom2.JDOMException, java.io.IOException {
log.trace("handling ", p);
// check target
String target = p.getTarget();
if (!target.equals("transform-xslt")) {
return doc;
}
String href = p.getPseudoAttributeValue("href");
// we expect this to start with http://jmri.org/ and refer to the JMRI file tree
if (!href.startsWith("http://jmri.org/")) {
return doc;
}
href = href.substring(16);
// if starts with 'xml/' we remove that; findFile will put it back
if (href.startsWith("xml/")) {
href = href.substring(4);
}
// read the XSLT transform into a Document to get XInclude done
SAXBuilder builder = getBuilder(Validate.None);
Document xdoc = builder.build(new BufferedInputStream(new FileInputStream(findFile(href))));
org.jdom2.transform.XSLTransformer transformer = new org.jdom2.transform.XSLTransformer(xdoc);
return transformer.transform(doc);
}
use of org.jdom2.input.SAXBuilder in project JMRI by JMRI.
the class XmlFile method getRoot.
/**
* Get the root element from an XML document in a stream.
*
* @param stream input containing the XML document
* @return the root element of the XML document
* @throws org.jdom2.JDOMException if the XML document is invalid
* @throws java.io.IOException if the input cannot be read
*/
protected Element getRoot(InputStream stream) throws JDOMException, IOException {
log.trace("getRoot from stream");
SAXBuilder builder = getBuilder(getValidate());
Document doc = builder.build(new BufferedInputStream(stream));
// handle any process instructions
doc = processInstructions(doc);
// find root
return doc.getRootElement();
}
use of org.jdom2.input.SAXBuilder in project JMRI by JMRI.
the class XmlFileValidateAction method processFile.
protected void processFile(File file) {
if (log.isDebugEnabled()) {
log.debug("located file " + file + " for XML processing");
}
// handle the file (later should be outside this thread?)
try {
xmlfile.setValidate(XmlFile.Validate.CheckDtdThenSchema);
readFile(file);
} catch (Exception ex) {
// because of XInclude, we're doing this
// again to validate the entire file
// without losing the error message
Document doc;
try {
InputStream stream = new BufferedInputStream(new FileInputStream(file));
SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", false);
builder.setEntityResolver(new jmri.util.JmriLocalEntityResolver());
builder.setFeature("http://apache.org/xml/features/xinclude", true);
builder.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
builder.setFeature("http://apache.org/xml/features/validation/schema", false);
builder.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);
builder.setFeature("http://xml.org/sax/features/namespaces", true);
doc = builder.build(new BufferedInputStream(stream));
} catch (JDOMException | IOException ex2) {
showFailResults(_who, "Err(1): " + ex2);
return;
}
XMLOutputter outputter = new XMLOutputter();
outputter.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.PRESERVE));
StringWriter out = new StringWriter();
try {
outputter.output(doc, out);
} catch (IOException ex2) {
showFailResults(_who, "Err(4): " + ex2);
return;
}
StringReader input = new StringReader(new String(out.getBuffer()));
SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true);
builder.setEntityResolver(new jmri.util.JmriLocalEntityResolver());
builder.setFeature("http://apache.org/xml/features/xinclude", true);
builder.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
builder.setFeature("http://apache.org/xml/features/validation/schema", true);
builder.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
builder.setFeature("http://xml.org/sax/features/namespaces", true);
try {
builder.build(input).getRootElement();
} catch (JDOMException | IOException ex2) {
showFailResults(_who, "Err(2): " + ex2);
return;
}
showFailResults(_who, "Err(3): " + ex);
return;
}
showOkResults(_who, "OK");
if (log.isDebugEnabled()) {
log.debug("parsing complete");
}
}
Aggregations