if (domBuilderFactory == null) {
domBuilderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
}
DocumentBuilder parser = domBuilderFactory.newDocumentBuilder();
Document doc;
if (obj instanceof String) {
try {
// first try to interpret string as URL
new URL(obj.toString());
doc = parser.parse(obj.toString());
} catch (MalformedURLException nourl) {
// if not a URL, maybe it is the XML itself
doc = parser.parse(new InputSource(new StringReader(obj.toString())));
}
} else if (obj instanceof InputStream) {
doc = parser.parse(new InputSource((InputStream) obj));
} else if (obj instanceof Reader) {
doc = parser.parse(new InputSource((Reader) obj));
} else {
throw new RuntimeException("Unrecognized argument to parseXml: " + obj);
}
doc.normalize();
return doc;
}