// return quickly if the XML has already been set
if (xml == null || xml.equals(this.xml)) {
return;
}
this.xml = xml;
// use JDOM to parse the XML
Document xmlDoc = null;
try {
// attempt to validate the XML
SAXBuilder saxBuilder = new SAXBuilder(true);
xmlDoc = saxBuilder.build(new StringReader(this.xml), uri);
this.statusLabel.setText("XML is well formed and valid");
} catch (Exception ignored) {
// the data is not valid, but we should parse it again
// to see if it is well formed
}
if (xmlDoc == null) {
try {
// don't validate
SAXBuilder saxBuilder = new SAXBuilder(false);
xmlDoc = saxBuilder.build(new StringReader(this.xml));
this.statusLabel.setText("XML is well formed, but not valid");
} catch (Exception ex) {
this.statusLabel.setText("Data is not well formed XML");
// show the stack trace in the text area
StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
this.xmlArea.setText(sw.toString());
}
}
// if the document was parsed, show it
if (xmlDoc != null) {
try {
// pretty-print the XML by indenting two spaces
XMLOutputter xmlOut = new XMLOutputter(" ", true);
// xmlOut.setNewlines(false); // JTextArea doesn't like newlines
StringWriter sw = new StringWriter();
xmlOut.output(xmlDoc, sw);
this.xmlArea.setText(sw.toString());
} catch (Exception ex) {
this.statusLabel.setText("Data could not be displayed.");
// show the stack trace in the text area
StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
this.xmlArea.setText(sw.toString());
}
}