-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate DTD file with a dedicated DTD validator (see #223)
- Loading branch information
1 parent
b3b16e8
commit 41efab4
Showing
5 changed files
with
130 additions
and
10 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
41 changes: 41 additions & 0 deletions
41
...c/main/java/org/eclipse/lsp4xml/extensions/dtd/diagnostics/DTDDiagnosticsParticipant.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) 2018 Angelo ZERR | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Angelo Zerr <[email protected]> - initial API and implementation | ||
*/ | ||
package org.eclipse.lsp4xml.extensions.dtd.diagnostics; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.xerces.xni.parser.XMLEntityResolver; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.eclipse.lsp4j.jsonrpc.CancelChecker; | ||
import org.eclipse.lsp4xml.dom.XMLDocument; | ||
import org.eclipse.lsp4xml.services.extensions.diagnostics.IDiagnosticsParticipant; | ||
import org.eclipse.lsp4xml.utils.DOMUtils; | ||
|
||
/** | ||
* Validate XSD file with Xerces. | ||
* | ||
*/ | ||
public class DTDDiagnosticsParticipant implements IDiagnosticsParticipant { | ||
|
||
@Override | ||
public void doDiagnostics(XMLDocument xmlDocument, List<Diagnostic> diagnostics, CancelChecker monitor) { | ||
if (!DOMUtils.isDTD(xmlDocument)) { | ||
// Don't use the DTD validator, if it's a DTD | ||
return; | ||
} | ||
// Get entity resolver (XML catalog resolver, XML schema from the file | ||
// associations settings., ...) | ||
XMLEntityResolver entityResolver = xmlDocument.getResolverExtensionManager(); | ||
// Process validation | ||
DTDValidator.doDiagnostics(xmlDocument, entityResolver, diagnostics, monitor); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...se.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/diagnostics/DTDValidator.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,52 @@ | ||
/** | ||
* Copyright (c) 2018 Angelo ZERR. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Angelo Zerr <[email protected]> - initial API and implementation | ||
*/ | ||
package org.eclipse.lsp4xml.extensions.dtd.diagnostics; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
import org.apache.xerces.impl.dtd.XMLDTDLoader; | ||
import org.apache.xerces.xni.parser.XMLEntityResolver; | ||
import org.apache.xerces.xni.parser.XMLInputSource; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.eclipse.lsp4j.jsonrpc.CancelChecker; | ||
import org.eclipse.lsp4xml.dom.XMLDocument; | ||
import org.eclipse.lsp4xml.extensions.contentmodel.participants.diagnostics.LSPErrorReporterForXML; | ||
|
||
/** | ||
* DTD validator | ||
* | ||
*/ | ||
public class DTDValidator { | ||
|
||
public static void doDiagnostics(XMLDocument document, XMLEntityResolver entityResolver, | ||
List<Diagnostic> diagnostics, CancelChecker monitor) { | ||
try { | ||
XMLDTDLoader loader = new XMLDTDLoader(); | ||
loader.setProperty("http://apache.org/xml/properties/internal/error-reporter", | ||
new LSPErrorReporterForXML(document, diagnostics)); | ||
|
||
if (entityResolver != null) { | ||
loader.setEntityResolver(entityResolver); | ||
} | ||
|
||
String content = document.getText(); | ||
String uri = document.getDocumentURI(); | ||
InputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)); | ||
XMLInputSource source = new XMLInputSource(null, uri, uri, inputStream, null); | ||
loader.loadGrammar(source); | ||
} catch (Exception e) { | ||
|
||
} | ||
} | ||
} |
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