forked from eclipse-lemminx/lemminx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XML entities declared in a DTD are marked undeclared after XML file
change Fixes redhat-developer/vscode-xml#234 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
24cd010
commit 0d9ebfa
Showing
5 changed files
with
233 additions
and
170 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
158 changes: 158 additions & 0 deletions
158
...va/org/eclipse/lemminx/extensions/contentmodel/participants/diagnostics/LSPSAXParser.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,158 @@ | ||
package org.eclipse.lemminx.extensions.contentmodel.participants.diagnostics; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.text.MessageFormat; | ||
|
||
import org.apache.xerces.impl.Constants; | ||
import org.apache.xerces.impl.XMLEntityManager; | ||
import org.apache.xerces.impl.dtd.DTDGrammar; | ||
import org.apache.xerces.impl.dtd.XMLDTDDescription; | ||
import org.apache.xerces.impl.dtd.XMLEntityDecl; | ||
import org.apache.xerces.impl.validation.ValidationManager; | ||
import org.apache.xerces.parsers.SAXParser; | ||
import org.apache.xerces.xni.Augmentations; | ||
import org.apache.xerces.xni.NamespaceContext; | ||
import org.apache.xerces.xni.XMLLocator; | ||
import org.apache.xerces.xni.XNIException; | ||
import org.apache.xerces.xni.grammars.XMLGrammarPool; | ||
import org.apache.xerces.xni.parser.XMLParserConfiguration; | ||
import org.eclipse.lemminx.commons.BadLocationException; | ||
import org.eclipse.lemminx.dom.DOMDocument; | ||
import org.eclipse.lemminx.dom.DOMDocumentType; | ||
import org.eclipse.lemminx.extensions.contentmodel.participants.DTDErrorCode; | ||
import org.eclipse.lsp4j.DiagnosticSeverity; | ||
import org.eclipse.lsp4j.Range; | ||
import org.xml.sax.SAXNotRecognizedException; | ||
import org.xml.sax.SAXNotSupportedException; | ||
|
||
public class LSPSAXParser extends SAXParser { | ||
|
||
private static final String DTD_NOT_FOUND = "Cannot find DTD ''{0}''.\nCreate the DTD file or configure an XML catalog for this DTD."; | ||
|
||
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; | ||
|
||
private final DOMDocument document; | ||
|
||
private final LSPErrorReporterForXML reporter; | ||
|
||
private final XMLGrammarPool grammarPool; | ||
|
||
public LSPSAXParser(DOMDocument document, LSPErrorReporterForXML reporter, XMLParserConfiguration config, | ||
XMLGrammarPool grammarPool) { | ||
super(config); | ||
this.document = document; | ||
this.reporter = reporter; | ||
this.grammarPool = grammarPool; | ||
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); | ||
super.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false); //$NON-NLS-1$ | ||
super.setFeature("http://xml.org/sax/features/namespace-prefixes", true); //$NON-NLS-1$ | ||
super.setFeature("http://xml.org/sax/features/namespaces", true); //$NON-NLS-1$ | ||
super.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true); | ||
} catch (SAXNotRecognizedException | SAXNotSupportedException e) { | ||
// Should never occurs. | ||
} | ||
} | ||
|
||
private XMLLocator locator; | ||
|
||
@Override | ||
public void startDocument(XMLLocator locator, String encoding, NamespaceContext namespaceContext, | ||
Augmentations augs) throws XNIException { | ||
this.locator = locator; | ||
super.startDocument(locator, encoding, namespaceContext, augs); | ||
} | ||
|
||
@Override | ||
public void doctypeDecl(String rootElement, String publicId, String systemId, Augmentations augs) | ||
throws XNIException { | ||
if (publicId != null || systemId != null) { | ||
String eid = null; | ||
try { | ||
eid = XMLEntityManager.expandSystemId(systemId, locator.getExpandedSystemId(), false); | ||
} catch (java.io.IOException e) { | ||
} | ||
if (!exists(eid)) { | ||
// Report the DTD invalid error | ||
try { | ||
DOMDocumentType docType = document.getDoctype(); | ||
Range range = new Range(document.positionAt(docType.getSystemIdNode().getStart()), | ||
document.positionAt(docType.getSystemIdNode().getEnd())); | ||
reporter.addDiagnostic(range, MessageFormat.format(DTD_NOT_FOUND, eid), DiagnosticSeverity.Error, | ||
DTDErrorCode.dtd_not_found.getCode()); | ||
} catch (BadLocationException e) { | ||
// Do nothing | ||
} | ||
|
||
ValidationManager fValidationManager = (ValidationManager) fConfiguration | ||
.getProperty(VALIDATION_MANAGER); | ||
if (fValidationManager != null) { | ||
fValidationManager.setCachedDTD(true); | ||
} | ||
} else { | ||
|
||
XMLEntityManager entityManager = (XMLEntityManager) fConfiguration.getProperty(ENTITY_MANAGER); | ||
XMLDTDDescription grammarDesc = new XMLDTDDescription(publicId, systemId, locator.getExpandedSystemId(), | ||
eid, rootElement); | ||
DTDGrammar grammar = (DTDGrammar) grammarPool.retrieveGrammar(grammarDesc); | ||
if (grammar != null) { | ||
fillEntities(grammar, entityManager); | ||
} | ||
} | ||
} | ||
super.doctypeDecl(rootElement, publicId, systemId, augs); | ||
} | ||
|
||
private static boolean exists(String expandedSystemId) { | ||
if (expandedSystemId == null || expandedSystemId.isEmpty()) { | ||
return true; | ||
} | ||
try { | ||
URL location = new URL(expandedSystemId); | ||
URLConnection connect = location.openConnection(); | ||
if (!(connect instanceof HttpURLConnection)) { | ||
InputStream stream = connect.getInputStream(); | ||
stream.close(); | ||
} | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Fill entities from the given DTD grammar to the given entity manager. | ||
* | ||
* @param grammar the DTD grammar | ||
* @param entityManager the entitymanager to update with entities of the DTD | ||
* grammar. | ||
*/ | ||
private static void fillEntities(DTDGrammar grammar, XMLEntityManager entityManager) { | ||
int index = 0; | ||
XMLEntityDecl entityDecl = new XMLEntityDecl() { | ||
|
||
@Override | ||
public void setValues(String name, String publicId, String systemId, String baseSystemId, String notation, | ||
String value, boolean isPE, boolean inExternal) { | ||
if (inExternal) { | ||
entityManager.addInternalEntity(name, value); | ||
} | ||
}; | ||
}; | ||
while (grammar.getEntityDecl(index, entityDecl)) { | ||
index++; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.