Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
794 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...in/java/org/eclipse/lemminx/extensions/relaxng/grammar/rng/RNGDiagnosticsParticipant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.extensions.relaxng.grammar.rng; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.xerces.xni.parser.XMLEntityResolver; | ||
import org.eclipse.lemminx.dom.DOMDocument; | ||
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelManager; | ||
import org.eclipse.lemminx.extensions.contentmodel.participants.diagnostics.XMLValidator; | ||
import org.eclipse.lemminx.extensions.contentmodel.settings.SchemaEnabled; | ||
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLSchemaSettings; | ||
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings; | ||
import org.eclipse.lemminx.extensions.relaxng.RelaxNGPlugin; | ||
import org.eclipse.lemminx.extensions.xerces.LSPXMLEntityResolver; | ||
import org.eclipse.lemminx.services.extensions.diagnostics.DiagnosticsResult; | ||
import org.eclipse.lemminx.services.extensions.diagnostics.IDiagnosticsParticipant; | ||
import org.eclipse.lemminx.utils.DOMUtils; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.eclipse.lsp4j.jsonrpc.CancelChecker; | ||
|
||
/** | ||
* Validate RNG file with Jing. | ||
* | ||
*/ | ||
public class RNGDiagnosticsParticipant implements IDiagnosticsParticipant { | ||
|
||
private final RelaxNGPlugin rngPlugin; | ||
|
||
public RNGDiagnosticsParticipant(RelaxNGPlugin rngPlugin) { | ||
this.rngPlugin = rngPlugin; | ||
} | ||
|
||
@Override | ||
public void doDiagnostics(DOMDocument xmlDocument, List<Diagnostic> diagnostics, | ||
XMLValidationSettings validationSettings, CancelChecker monitor) { | ||
if (!DOMUtils.isRelaxNGXMLSyntax(xmlDocument)) { | ||
// Don't use the RNG validator, if the XML document is not a RNG Schema. | ||
return; | ||
} | ||
|
||
// Get entity resolver (XML catalog resolver, XML schema from the file | ||
// associations settings., ...) | ||
XMLEntityResolver entityResolver = xmlDocument.getResolverExtensionManager(); | ||
LSPXMLEntityResolver entityResolverWrapper = new LSPXMLEntityResolver(entityResolver, | ||
(DiagnosticsResult) diagnostics); | ||
ContentModelManager contentModelManager = rngPlugin.getContentModelManager(); | ||
if (!isSchemaEnabled(validationSettings)) { | ||
// Validate only XML syntax for RNG | ||
// Process validation | ||
XMLValidator.doDiagnostics(xmlDocument, entityResolverWrapper, diagnostics, validationSettings, | ||
contentModelManager, monitor); | ||
return; | ||
} | ||
|
||
// Process RNG validation | ||
RNGValidator.doDiagnostics(xmlDocument, entityResolverWrapper, diagnostics, validationSettings, | ||
rngPlugin.getContentModelManager(), monitor); | ||
} | ||
|
||
private static boolean isSchemaEnabled(XMLValidationSettings validationSettings) { | ||
if (validationSettings == null) { | ||
return true; | ||
} | ||
XMLSchemaSettings schemaSettings = validationSettings.getSchema(); | ||
if (schemaSettings == null) { | ||
return true; | ||
} | ||
return !SchemaEnabled.never.equals(schemaSettings.getEnabled()); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
.../main/java/org/eclipse/lemminx/extensions/relaxng/grammar/rng/RNGParserConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.extensions.relaxng.grammar.rng; | ||
|
||
import org.apache.xerces.parsers.XIncludeAwareParserConfiguration; | ||
import org.eclipse.lemminx.extensions.contentmodel.participants.diagnostics.LSPErrorReporterForXML; | ||
|
||
/** | ||
* RNG parser configuration used to set the LSP error reporter. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class RNGParserConfiguration extends XIncludeAwareParserConfiguration { | ||
|
||
|
||
public RNGParserConfiguration (LSPErrorReporterForXML reporter) { | ||
fErrorReporter = reporter; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...emminx/src/main/java/org/eclipse/lemminx/extensions/relaxng/grammar/rng/RNGSAXParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.extensions.relaxng.grammar.rng; | ||
|
||
import org.apache.xerces.impl.Constants; | ||
import org.apache.xerces.parsers.SAXParser; | ||
import org.apache.xerces.xni.parser.XMLParserConfiguration; | ||
import org.eclipse.lemminx.extensions.contentmodel.participants.diagnostics.LSPErrorReporterForXML; | ||
import org.xml.sax.SAXNotRecognizedException; | ||
import org.xml.sax.SAXNotSupportedException; | ||
|
||
/** | ||
* Extends Xerces SAX Parser to use the LSP error reporter. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class RNGSAXParser extends SAXParser { | ||
|
||
protected static final String VALIDATION_MANAGER = Constants.XERCES_PROPERTY_PREFIX | ||
+ Constants.VALIDATION_MANAGER_PROPERTY; | ||
|
||
protected static final String ENTITY_MANAGER = Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; | ||
|
||
public RNGSAXParser(LSPErrorReporterForXML reporter, XMLParserConfiguration config) { | ||
super(config); | ||
init(reporter); | ||
} | ||
|
||
private void init(LSPErrorReporterForXML reporter) { | ||
try { | ||
// Add LSP error reporter to fill LSP diagnostics from Xerces errors | ||
super.setProperty("http://apache.org/xml/properties/internal/error-reporter", reporter); | ||
} catch (SAXNotRecognizedException | SAXNotSupportedException e) { | ||
// Should never occur. | ||
} | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
...emminx/src/main/java/org/eclipse/lemminx/extensions/relaxng/grammar/rng/RNGValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.extensions.relaxng.grammar.rng; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CancellationException; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import org.apache.xerces.xni.parser.XMLEntityResolver; | ||
import org.apache.xerces.xni.parser.XMLParseException; | ||
import org.eclipse.lemminx.dom.DOMDocument; | ||
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelManager; | ||
import org.eclipse.lemminx.extensions.contentmodel.participants.diagnostics.LSPErrorReporterForXML; | ||
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings; | ||
import org.eclipse.lemminx.extensions.relaxng.jing.SchemaProvider; | ||
import org.eclipse.lemminx.extensions.xerces.ReferencedGrammarDiagnosticsInfo; | ||
import org.eclipse.lemminx.utils.DOMUtils; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.eclipse.lsp4j.jsonrpc.CancelChecker; | ||
import org.xml.sax.InputSource; | ||
|
||
import com.thaiopensource.relaxng.pattern.MySchemaPatternBuilder; | ||
import com.thaiopensource.validate.IncorrectSchemaException; | ||
import com.thaiopensource.xml.sax.XMLReaderCreator; | ||
|
||
/** | ||
* RNG validator utilities class. | ||
* | ||
*/ | ||
public class RNGValidator { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(RNGValidator.class.getName()); | ||
|
||
public static void doDiagnostics(DOMDocument document, XMLEntityResolver entityResolver, | ||
List<Diagnostic> diagnostics, XMLValidationSettings validationSettings, | ||
ContentModelManager contentModelManager, CancelChecker monitor) { | ||
|
||
Map<String, ReferencedGrammarDiagnosticsInfo> referencedGrammarDiagnosticsInfoCache = new HashMap<>(); | ||
// When referenced grammar (XSD, DTD) have an error (ex : syntax error), the | ||
// error must be reported. | ||
// We create a reporter for grammar since Xerces reporter stores the XMLLocator | ||
// for XML and Grammar. | ||
LSPErrorReporterForXML reporterForRNG = new LSPErrorReporterForXML(document, diagnostics, contentModelManager, | ||
validationSettings != null ? validationSettings.isRelatedInformation() : false, | ||
referencedGrammarDiagnosticsInfoCache); | ||
try { | ||
InputSource input = DOMUtils.createInputSource(document); | ||
XMLReaderCreator creator = new RNGXMLReaderCreator(reporterForRNG); | ||
SchemaProvider.loadSchema(input, entityResolver, reporterForRNG, | ||
new MySchemaPatternBuilder(), creator); | ||
} catch (IOException | CancellationException | XMLParseException | IncorrectSchemaException exception ) { | ||
// ignore error | ||
} catch (Exception e) { | ||
LOGGER.log(Level.SEVERE, "Unexpected RNG Validator error", e); | ||
} finally { | ||
reporterForRNG.endReport(); | ||
} | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...src/main/java/org/eclipse/lemminx/extensions/relaxng/grammar/rng/RNGXMLReaderCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.extensions.relaxng.grammar.rng; | ||
|
||
import org.eclipse.lemminx.extensions.contentmodel.participants.diagnostics.LSPErrorReporterForXML; | ||
import org.xml.sax.SAXException; | ||
import org.xml.sax.XMLReader; | ||
|
||
import com.thaiopensource.xml.sax.XMLReaderCreator; | ||
|
||
/** | ||
* {@link XMLReaderCreator} to create the RNG parser. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class RNGXMLReaderCreator implements XMLReaderCreator { | ||
|
||
private final LSPErrorReporterForXML reporter; | ||
|
||
public RNGXMLReaderCreator(LSPErrorReporterForXML reporter) { | ||
this.reporter = reporter; | ||
} | ||
|
||
public XMLReader createXMLReader() throws SAXException { | ||
RNGSAXParser parser = new RNGSAXParser(reporter, new RNGParserConfiguration(reporter)); | ||
parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); //$NON-NLS-1$ | ||
parser.setFeature("http://xml.org/sax/features/namespaces", true); //$NON-NLS-1$ | ||
parser.setFeature("http://xml.org/sax/features/validation", false); | ||
return parser; | ||
} | ||
} |
Oops, something went wrong.