Skip to content

Commit

Permalink
XML completion based on DTD with XML catalog doesn't work
Browse files Browse the repository at this point in the history
Fixes eclipse-lemminx#849

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Aug 19, 2020
1 parent c1b83f4 commit 40dcfb8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.xerces.xni.grammars.XMLGrammarPool;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelProvider.Identifier;
import org.eclipse.lemminx.extensions.contentmodel.participants.diagnostics.LSPXMLGrammarPool;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLFileAssociation;
import org.eclipse.lemminx.extensions.contentmodel.uriresolver.XMLCacheResolverExtension;
Expand Down Expand Up @@ -116,10 +117,12 @@ public Collection<CMDocument> findCMDocument(DOMDocument xmlDocument, String nam
// The content model provider can collect the system ids
// ex for <?xml-model , the model provider which takes care of xml-model returns
// the href of xml-model.
Collection<String> systemIds = modelProvider.getSystemIds(xmlDocument, namespaceURI);
for (String systemId : systemIds) {
Collection<Identifier> identifiers = modelProvider.getIdentifiers(xmlDocument, namespaceURI);
for (Identifier identifier : identifiers) {
String systemId = identifier.getSystemId();
String publicId = identifier.getPublicId() != null ? identifier.getPublicId() : namespaceURI;
// get the content model document from the current system id
CMDocument cmDocument = findCMDocument(xmlDocument.getDocumentURI(), namespaceURI, systemId,
CMDocument cmDocument = findCMDocument(xmlDocument.getDocumentURI(), publicId, systemId,
modelProvider);
if (cmDocument != null) {
documents.add(cmDocument);
Expand Down Expand Up @@ -151,9 +154,11 @@ public boolean dependsOnGrammar(DOMDocument document, String grammarURI) {
}
for (ContentModelProvider modelProvider : modelProviders) {
if (modelProvider.adaptFor(document, false)) {
Collection<String> systemIds = modelProvider.getSystemIds(document, document.getNamespaceURI());
for (String systemId : systemIds) {
String key = resolverManager.resolve(document.getDocumentURI(), null, systemId);
Collection<Identifier> identifiers = modelProvider.getIdentifiers(document, document.getNamespaceURI());
for (Identifier identifier : identifiers) {
String publicId = identifier.getPublicId();
String systemId = identifier.getSystemId();
String key = resolverManager.resolve(document.getDocumentURI(), publicId, systemId);
if (grammarURI.equals(key)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,34 @@
*/
public interface ContentModelProvider {

public class Identifier {

private final String publicId;

private final String systemId;

public Identifier(String publicId, String systemId) {
this.publicId = publicId;
this.systemId = systemId;
}

public String getPublicId() {
return publicId;
}

public String getSystemId() {
return systemId;
}

}

/**
* Returns the content model provider by using standard association
* (xsi:schemaLocation, xsi:noNamespaceSchemaLocation, doctype) an dnull
* otherwise.
*
* @param document
* @param internal
* @param internal
* @return the content model provider by using standard association
* (xsi:schemaLocation, xsi:noNamespaceSchemaLocation, doctype) an dnull
* otherwise.
Expand All @@ -37,7 +58,7 @@ public interface ContentModelProvider {

boolean adaptFor(String uri);

Collection<String> getSystemIds(DOMDocument xmlDocument, String namespaceURI);
Collection<Identifier> getIdentifiers(DOMDocument xmlDocument, String namespaceURI);

CMDocument createCMDocument(String key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ public boolean adaptFor(String uri) {
}

@Override
public Collection<String> getSystemIds(DOMDocument xmlDocument, String namespaceURI) {
public Collection<Identifier> getIdentifiers(DOMDocument xmlDocument, String namespaceURI) {
/*
* <!DOCTYPE catalog PUBLIC
* "-//OASIS/DTD Entity Resolution XML Catalog V1.0//EN"
* "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">
*/
DOMDocumentType documentType = xmlDocument.getDoctype();
return Collections.singleton(documentType.getSystemIdWithoutQuotes());
return Collections.singleton(
new Identifier(documentType.getPublicIdWithoutQuotes(), documentType.getSystemIdWithoutQuotes()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ public boolean adaptFor(String uri) {
}

@Override
public Collection<String> getSystemIds(DOMDocument xmlDocument, String namespaceURI) {
return xmlDocument.getXMLModels().stream().map(node -> node.getHref())
.filter(href -> !StringUtils.isEmpty(href)).collect(Collectors.toList());
public Collection<Identifier> getIdentifiers(DOMDocument xmlDocument, String namespaceURI) {
return xmlDocument.getXMLModels().stream() //
.map(node -> node.getHref()) //
.filter(href -> !StringUtils.isEmpty(href)) //
.map(href -> new Identifier(null, href)) //
.collect(Collectors.toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public boolean adaptFor(String uri) {
}

@Override
public Collection<String> getSystemIds(DOMDocument xmlDocument, String namespaceURI) {
Collection<String> systemIds = new ArrayList<>();
public Collection<Identifier> getIdentifiers(DOMDocument xmlDocument, String namespaceURI) {
Collection<Identifier> identifiers = new ArrayList<>();
SchemaLocation schemaLocation = xmlDocument.getSchemaLocation();
if (schemaLocation != null) {
String location = schemaLocation.getLocationHint(namespaceURI);
if (!StringUtils.isEmpty(location)) {
systemIds.add(location);
identifiers.add(new Identifier(null, location));
}
} else {
NoNamespaceSchemaLocation noNamespaceSchemaLocation = xmlDocument.getNoNamespaceSchemaLocation();
Expand All @@ -70,12 +70,12 @@ public Collection<String> getSystemIds(DOMDocument xmlDocument, String namespace
// xsi:noNamespaceSchemaLocation doesn't define namespaces
String location = noNamespaceSchemaLocation.getLocation();
if (!StringUtils.isEmpty(location)) {
systemIds.add(location);
identifiers.add(new Identifier(null, location));
}
}
}
}
return systemIds;
return identifiers;
}

@Override
Expand All @@ -94,7 +94,7 @@ public CMDocument createInternalCMDocument(DOMDocument xmlDocument) {
return null;
}

public XSLoaderImpl getLoader() {
public XSLoaderImpl getLoader() {
XSLoaderImpl loader = new XSLoaderImpl();
loader.setParameter("http://apache.org/xml/properties/internal/entity-resolver", resolverExtensionManager);
loader.setParameter(Constants.DOM_ERROR_HANDLER, new DOMErrorHandler() {
Expand Down

0 comments on commit 40dcfb8

Please sign in to comment.