long startTime = System.currentTimeMillis();
Document document = null;
DOMParser parser = new DOMParser() {
private XMLLocator locator;
private void setLineColumn(Node node) {
if (node.getUserData("startLine") != null) {
return;
}
node.setUserData("systemId",locator.getLiteralSystemId(), null);
node.setUserData("startLine",locator.getLineNumber(), null);
node.setUserData("startColumn",locator.getColumnNumber(), null);
}
private void setLineColumn() {
try {
Node node = (Node) getProperty("http://apache.org/xml/properties/dom/current-element-node");
if (node != null) {
setLineColumn(node);
}
} catch (SAXException ex) {
Debug.logWarning(ex, module);
}
}
private void setLastChildLineColumn() {
try {
Node node = (Node) getProperty("http://apache.org/xml/properties/dom/current-element-node");
if (node != null) {
setLineColumn(node.getLastChild());
}
} catch (SAXException ex) {
Debug.logWarning(ex, module);
}
}
@Override
public void startGeneralEntity(String name, XMLResourceIdentifier identifier, String encoding, Augmentations augs) throws XNIException {
super.startGeneralEntity(name, identifier, encoding, augs);
setLineColumn();
}
@Override
public void comment(XMLString text, Augmentations augs) throws XNIException {
super.comment(text, augs);
setLastChildLineColumn();
}
@Override
public void processingInstruction(String target, XMLString data, Augmentations augs) throws XNIException {
super.processingInstruction(target, data, augs);
setLastChildLineColumn();
}
@Override
public void startDocument(XMLLocator locator, String encoding, NamespaceContext namespaceContext, Augmentations augs) throws XNIException {
super.startDocument(locator, encoding, namespaceContext, augs);
this.locator = locator;
setLineColumn();
}
@Override
public void doctypeDecl(String rootElement, String publicId, String systemId, Augmentations augs) throws XNIException {
super.doctypeDecl(rootElement, publicId, systemId, augs);
}
@Override
public void startElement(QName elementQName, XMLAttributes attrList, Augmentations augs) throws XNIException {
super.startElement(elementQName, attrList, augs);
setLineColumn();
}
@Override
public void characters(XMLString text, Augmentations augs) throws XNIException {
super.characters(text, augs);
setLastChildLineColumn();
}
@Override
public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException {
super.ignorableWhitespace(text, augs);
setLastChildLineColumn();
}
};
parser.setFeature("http://xml.org/sax/features/namespaces", true);
parser.setFeature("http://xml.org/sax/features/validation", validate);
parser.setFeature("http://apache.org/xml/features/validation/schema", validate);
parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
// with a SchemaUrl, a URL object
//factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
//factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", SchemaUrl);
if (validate) {
LocalResolver lr = new LocalResolver(new DefaultHandler());
ErrorHandler eh = new LocalErrorHandler(docDescription, lr);
parser.setEntityResolver(lr);
parser.setErrorHandler(eh);
}
InputSource inputSource = new InputSource(is);
inputSource.setSystemId(docDescription);
parser.parse(inputSource);
document = parser.getDocument();
double totalSeconds = (System.currentTimeMillis() - startTime)/1000.0;
if (Debug.verboseOn()) Debug.logVerbose("XML Read " + totalSeconds + "s: " + docDescription, module);
return document;
}