use of org.apache.xerces.xni.XMLLocator in project ofbiz-framework by apache.
the class UtilXml method readXmlDocument.
public static Document readXmlDocument(InputStream is, boolean validate, String docDescription, boolean withPosition) throws SAXException, ParserConfigurationException, java.io.IOException {
if (!withPosition) {
return readXmlDocument(is, validate, docDescription);
}
if (is == null) {
Debug.logWarning("[UtilXml.readXmlDocument] InputStream was null, doing nothing", module);
return null;
}
long startTime = System.currentTimeMillis();
Document document = null;
DOMParser parser = new DOMParser() {
private XMLLocator locator = null;
private void setLineColumn(Node node) {
if (locator == null) {
throw new java.lang.IllegalStateException("XMLLocator is null");
}
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
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;
}
use of org.apache.xerces.xni.XMLLocator in project webtools.sourceediting by eclipse.
the class XMLValidator method createXMLReader.
/**
* Create an XML Reader.
*
* @return The newly created XML reader or null if unsuccessful.
* @throws Exception
*/
protected XMLReader createXMLReader(final XMLValidationInfo valinfo, XMLEntityResolver entityResolver) throws Exception {
XMLReader reader = null;
// move to Xerces-2... add the contextClassLoader stuff
ClassLoader prevClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
MyStandardParserConfiguration configuration = new MyStandardParserConfiguration(valinfo);
reader = new org.apache.xerces.parsers.SAXParser(configuration) {
private XMLLocator locator = null;
/* (non-Javadoc)
* @see org.apache.xerces.parsers.AbstractSAXParser#startDocument(org.apache.xerces.xni.XMLLocator, java.lang.String, org.apache.xerces.xni.NamespaceContext, org.apache.xerces.xni.Augmentations)
*/
public void startDocument(org.apache.xerces.xni.XMLLocator theLocator, java.lang.String encoding, NamespaceContext nscontext, org.apache.xerces.xni.Augmentations augs) {
locator = theLocator;
valinfo.setXMLLocator(theLocator);
super.startDocument(theLocator, encoding, nscontext, augs);
}
/* (non-Javadoc)
* @see org.apache.xerces.parsers.AbstractSAXParser#startElement(org.apache.xerces.xni.QName, org.apache.xerces.xni.XMLAttributes, org.apache.xerces.xni.Augmentations)
*/
public void startElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException {
valinfo.getStartElementLocations().push(new LocationCoordinate(locator.getLineNumber(), locator.getColumnNumber()));
super.startElement(element, attributes, augs);
}
/* (non-Javadoc)
* @see org.apache.xerces.parsers.AbstractSAXParser#endElement(org.apache.xerces.xni.QName, org.apache.xerces.xni.Augmentations)
*/
public void endElement(QName element, Augmentations augs) throws XNIException {
super.endElement(element, augs);
valinfo.getStartElementLocations().pop();
}
};
// $NON-NLS-1$
reader.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);
// $NON-NLS-1$
reader.setFeature("http://xml.org/sax/features/namespace-prefixes", valinfo.isNamespaceEncountered());
// $NON-NLS-1$
reader.setFeature("http://xml.org/sax/features/namespaces", valinfo.isNamespaceEncountered());
reader.setContentHandler(new DefaultHandler() {
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
valinfo.getErrorCustomizationManager().startElement(uri, localName);
}
public void endElement(String uri, String localName, String qName) throws SAXException {
valinfo.getErrorCustomizationManager().endElement(uri, localName);
}
});
// MH make sure validation works even when a customer entityResolver is note set (i.e. via setURIResolver())
if (entityResolver != null) {
// $NON-NLS-1$
reader.setProperty("http://apache.org/xml/properties/internal/entity-resolver", entityResolver);
}
// $NON-NLS-1$
reader.setProperty("http://xml.org/sax/properties/declaration-handler", new MyDeclHandler());
} catch (Exception e) {
Logger.logException(e);
// e.printStackTrace();
} finally {
Thread.currentThread().setContextClassLoader(prevClassLoader);
}
return reader;
}
use of org.apache.xerces.xni.XMLLocator in project webtools.sourceediting by eclipse.
the class XMLValidator method addValidationMessage.
/**
* Add a validation message to the specified list.
*
* @param valinfo The validation info object to add the error to.
* @param exception The exception that contains the validation information.
*/
protected void addValidationMessage(XMLValidationInfo valinfo, IOException exception) {
String validationMessageStr = exception.getMessage();
Throwable cause = exception.getCause() != null ? exception.getCause() : exception;
while (validationMessageStr == null && cause != null) {
String localizedMessage = cause.getLocalizedMessage();
cause = cause.getCause();
if (cause == null && localizedMessage != null) {
validationMessageStr = localizedMessage;
}
}
if (validationMessageStr != null) {
if (cause instanceof FileNotFoundException) {
validationMessageStr = NLS.bind(XMLValidationMessages._UI_PROBLEMS_VALIDATING_FILE_NOT_FOUND, new Object[] { validationMessageStr });
} else if (cause instanceof UnknownHostException) {
validationMessageStr = NLS.bind(XMLValidationMessages._UI_PROBLEMS_VALIDATING_UNKNOWN_HOST, new Object[] { validationMessageStr });
} else if (cause instanceof ConnectException) {
validationMessageStr = XMLValidationMessages._UI_PROBLEMS_CONNECTION_REFUSED;
}
}
if (validationMessageStr != null) {
XMLLocator locator = valinfo.getXMLLocator();
valinfo.addWarning(validationMessageStr, locator != null ? locator.getLineNumber() : 1, locator != null ? locator.getColumnNumber() : 0, valinfo.getFileURI(), FILE_NOT_FOUND_KEY, null);
}
}
Aggregations