public static Document parse(InputSource inputSource,
Map dtdMappings) {
SAXParserFactory factory = null;
String parserProp = System.getProperty("xwork.saxParserFactory");
if (parserProp != null) {
try {
Class clazz = ObjectFactory.getObjectFactory().getClassInstance(parserProp);
factory = (SAXParserFactory) clazz.newInstance();
}
catch (ClassNotFoundException e) {
LOG.error("Unable to load saxParserFactory set by system property 'xwork.saxParserFactory': " + parserProp, e);
}
catch (Exception e) {
LOG.error("Unable to load saxParserFactory set by system property 'xwork.saxParserFactory': " + parserProp, e);
}
}
if (factory == null) {
factory = SAXParserFactory.newInstance();
}
factory.setValidating((dtdMappings != null));
factory.setNamespaceAware(true);
SAXParser parser = null;
try {
parser = factory.newSAXParser();
} catch (Exception ex) {
throw new XWorkException("Unable to create SAX parser", ex);
}
DOMBuilder builder = new DOMBuilder();
// Enhance the sax stream with location information
ContentHandler locationHandler = new LocationAttributes.Pipe(builder);
try {
parser.parse(inputSource, new StartHandler(locationHandler, dtdMappings));
} catch (Exception ex) {
throw new XWorkException(ex);
}
return builder.getDocument();
}
Creates a W3C Document that remembers the location of each element in
the source file. The location of element nodes can then be retrieved
using the #getLocationObject(Element) method. |