Skip to content

Commit

Permalink
Support RelaxNG validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Aug 17, 2020
1 parent 7416ae2 commit f45459e
Show file tree
Hide file tree
Showing 9 changed files with 655 additions and 15 deletions.
6 changes: 6 additions & 0 deletions org.eclipse.lemminx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@
<version>9.4.17.v20190418</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.thaiopensource</groupId>
<artifactId>jing</artifactId>
<version>20091111</version>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.eclipse.lemminx.extensions.contentmodel.participants.diagnostics;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

public class MultipleContentHandler implements ContentHandler {

private final List<ContentHandler> handlers;

public MultipleContentHandler() {
this.handlers = new ArrayList<>();
}

@Override
public void setDocumentLocator(Locator locator) {
for (ContentHandler contentHandler : handlers) {
contentHandler.setDocumentLocator(locator);
}
}

@Override
public void startDocument() throws SAXException {
for (ContentHandler contentHandler : handlers) {
contentHandler.startDocument();
}
}

@Override
public void endDocument() throws SAXException {
for (ContentHandler contentHandler : handlers) {
contentHandler.endDocument();
}
}

@Override
public void startPrefixMapping(String prefix, String uri) throws SAXException {
for (ContentHandler contentHandler : handlers) {
contentHandler.startPrefixMapping(prefix, uri);
}
}

@Override
public void endPrefixMapping(String prefix) throws SAXException {
for (ContentHandler contentHandler : handlers) {
contentHandler.endPrefixMapping(prefix);
}
}

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
for (ContentHandler contentHandler : handlers) {
contentHandler.startElement(uri, localName, qName, atts);
}
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
for (ContentHandler contentHandler : handlers) {
contentHandler.endElement(uri, localName, qName);
}
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
for (ContentHandler contentHandler : handlers) {
contentHandler.characters(ch, start, length);
}
}

@Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
for (ContentHandler contentHandler : handlers) {
contentHandler.ignorableWhitespace(ch, start, length);
}
}

@Override
public void processingInstruction(String target, String data) throws SAXException {
for (ContentHandler contentHandler : handlers) {
contentHandler.processingInstruction(target, data);
}
}

@Override
public void skippedEntity(String name) throws SAXException {
for (ContentHandler contentHandler : handlers) {
contentHandler.skippedEntity(name);
}
}

public void addContentHandler(ContentHandler contentHandler) {
handlers.add(contentHandler);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.xerces.util.MessageFormatter;
import org.apache.xerces.xni.XMLLocator;
import org.apache.xerces.xni.XNIException;
import org.apache.xerces.xni.parser.XMLErrorHandler;
import org.apache.xerces.xni.parser.XMLParseException;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.commons.TextDocument;
Expand Down Expand Up @@ -60,27 +61,37 @@ public String reportError(XMLLocator location, String domain, String key, Object
Exception exception) throws XNIException {
// format message
MessageFormatter messageFormatter = getMessageFormatter(domain);
String message;
String message = null;
if (messageFormatter != null) {
message = messageFormatter.formatMessage(fLocale, key, arguments);
} else {
StringBuilder str = new StringBuilder();
str.append(domain);
str.append('#');
str.append(key);
int argCount = arguments != null ? arguments.length : 0;
if (argCount > 0) {
str.append('?');
for (int i = 0; i < argCount; i++) {
str.append(arguments[i]);
if (i < argCount - 1) {
str.append('&');
if (exception != null) {
message = exception.getMessage();
}
if (message == null) {
StringBuilder str = new StringBuilder();
str.append(domain);
str.append('#');
str.append(key);
int argCount = arguments != null ? arguments.length : 0;
if (argCount > 0) {
str.append('?');
for (int i = 0; i < argCount; i++) {
str.append(arguments[i]);
if (i < argCount - 1) {
str.append('&');
}
}
}
message = str.toString();
}
message = str.toString();
}

return reportError(location, key, arguments, severity, exception, message);
}

private String reportError(XMLLocator location, String key, Object[] arguments, short severity, Exception exception,
String message) {
Range adjustedRange = internalToLSPRange(location, key, arguments, xmlDocument);

if (adjustedRange == null) {
Expand Down Expand Up @@ -162,7 +173,6 @@ private Range internalToLSPRange(XMLLocator location, String key, Object[] argum
}

protected abstract Range toLSPRange(XMLLocator location, String key, Object[] arguments, DOMDocument document);


/**
* Returns the LSP position from the SAX location.
Expand All @@ -182,4 +192,28 @@ private static Position toLSPPosition(int offset, XMLLocator location, TextDocum
return location != null ? new Position(location.getLineNumber() - 1, location.getColumnNumber() - 1) : null;
}
}

@Override
public ErrorHandler getSAXErrorHandler() {
if (fErrorHandler == null) {
fErrorHandler = new XMLErrorHandler() {

@Override
public void warning(String domain, String key, XMLParseException exception) throws XNIException {
reportError(domain, key, null, SEVERITY_WARNING, exception);
}

@Override
public void fatalError(String domain, String key, XMLParseException exception) throws XNIException {
reportError(domain, key, null, SEVERITY_FATAL_ERROR, exception);
}

@Override
public void error(String domain, String key, XMLParseException exception) throws XNIException {
reportError(domain, key, null, SEVERITY_ERROR, exception);
}
};
}
return super.getSAXErrorHandler();
}
}
Loading

0 comments on commit f45459e

Please sign in to comment.