Skip to content

Commit

Permalink
Expand system if of XML file association with rootUri (see #142)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Sep 25, 2018
1 parent 41f17a8 commit 66ade4e
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.eclipse.lsp4xml.emmet;

import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4xml.emmet.participants.EmmetCompletionParticipant;
import org.eclipse.lsp4xml.services.extensions.ICompletionParticipant;
import org.eclipse.lsp4xml.services.extensions.IXMLExtension;
Expand All @@ -19,7 +20,7 @@ public void updateSettings(Object settings) {
}

@Override
public void start(XMLExtensionsRegistry registry) {
public void start(InitializeParams params, XMLExtensionsRegistry registry) {
registry.registerCompletionParticipant(completionParticipant);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.eclipse.lsp4xml.xsl;

import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4xml.services.extensions.IXMLExtension;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lsp4xml.uriresolver.URIResolverExtensionManager;
Expand All @@ -18,7 +19,7 @@ public void updateSettings(Object settings) {
}

@Override
public void start(XMLExtensionsRegistry registry) {
public void start(InitializeParams params, XMLExtensionsRegistry registry) {
URIResolverExtensionManager.getInstance().registerResolver(uiResolver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
LOGGER.info("Initializing LSP4XML server " + getVersion());
this.parentProcessId = params.getProcessId();

// Update XML language service extensions with InitializeParams
xmlLanguageService.initializeParams(params);

capabilityManager.setClientCapabilities(params.getCapabilities());
updateSettings(InitializationOptionsSettings.getSettings(params));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.eclipse.lsp4xml.contentmodel;

import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4xml.contentmodel.model.ContentModelManager;
import org.eclipse.lsp4xml.contentmodel.participants.ContentModelCodeActionParticipant;
import org.eclipse.lsp4xml.contentmodel.participants.ContentModelCompletionParticipant;
Expand Down Expand Up @@ -57,7 +58,8 @@ private void updateSettings(ContentModelSettings settings) {
}

@Override
public void start(XMLExtensionsRegistry registry) {
public void start(InitializeParams params, XMLExtensionsRegistry registry) {
ContentModelManager.getInstance().setRootURI(params.getRootUri());
registry.registerCompletionParticipant(completionParticipant);
registry.registerHoverParticipant(hoverParticipant);
registry.registerDiagnosticsParticipant(diagnosticsParticipant);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static ContentModelManager getInstance() {

private final XMLCatalogResolverExtension catalogResolverExtension;
private final XMLFileAssociationResolverExtension fileAssociationResolver;
private String rootUri;

public ContentModelManager() {
loader = new XSLoaderImpl();
Expand Down Expand Up @@ -127,22 +128,6 @@ private CMDocument findCMDocument(String uri, String publicId, String systemId)
return cmDocument;
}

/**
* Returns the cached key of the content model document.
*
* @param uri
* @param publicId
* @param systemId
* @return the cached key of the content model document
*/
private String getCacheKey(String uri, String publicId, String systemId) {
if (publicId == null && systemId == null) {
// case of XML file association, the key is the file URI
return uri;
}
return publicId + "#" + systemId;
}

/**
* Set up XML catalogs.
*
Expand Down Expand Up @@ -182,4 +167,8 @@ public void setFileAssociations(XMLFileAssociation[] fileAssociations) {
this.fileAssociationResolver.setFileAssociations(fileAssociations);
}

public void setRootURI(String rootUri) {
fileAssociationResolver.setRootUri(rootUri);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.net.URI;
import java.util.Map;

import org.apache.xerces.impl.XMLEntityManager;
import org.apache.xerces.util.URI.MalformedURIException;
import org.eclipse.lsp4xml.contentmodel.settings.XMLFileAssociation;
import org.eclipse.lsp4xml.uriresolver.IExternalSchemaLocationProvider;
import org.eclipse.lsp4xml.uriresolver.URIResolverExtension;
Expand All @@ -23,16 +25,20 @@
*/
public class XMLFileAssociationResolverExtension implements URIResolverExtension, IExternalSchemaLocationProvider {

private String rootUri;

private XMLFileAssociation[] fileAssociations;

public void setFileAssociations(XMLFileAssociation[] fileAssociations) {
this.fileAssociations = fileAssociations;
expandSystemId();
}

@Override
public String resolve(String baseLocation, String publicId, String systemId) {
if (systemId != null) {
// system id is defined in the XML root element (ex : syetemId=Types.xsd for <Types
// system id is defined in the XML root element (ex : syetemId=Types.xsd for
// <Types
// xsi:noNamespaceSchemaLocation="Types.xsd">
// ignore XML file association
return null;
Expand All @@ -59,4 +65,25 @@ public Map<String, String> getExternalSchemaLocation(URI fileURI) {
return null;
}

public void setRootUri(String rootUri) {
this.rootUri = rootUri;
}

private void expandSystemId() {
if (fileAssociations != null && rootUri != null) {
for (XMLFileAssociation fileAssociation : fileAssociations) {
// Expand original system id by using the root uri.
try {
String expandSystemId = XMLEntityManager.expandSystemId(fileAssociation.getSystemId(), rootUri,
false);
if (expandSystemId != null) {
fileAssociation.setSystemId(expandSystemId);
}
} catch (MalformedURIException e) {
// Do nothing
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package org.eclipse.lsp4xml.services.extensions;

import org.eclipse.lsp4j.InitializeParams;

/**
* XML extension.
*
Expand All @@ -20,11 +22,12 @@ public interface IXMLExtension {
* Start method to register participants like {@link ICompletionParticipant},
* {@link IHoverParticipant}, {@link IDiagnosticsParticipant} in the given
* registry.
* @param params
*
* @param registry
* @param settings
*/
void start(XMLExtensionsRegistry registry);
void start(InitializeParams params, XMLExtensionsRegistry registry);

/**
* Stop method to un-register participants like {@link ICompletionParticipant},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.lsp4j.InitializeParams;

/**
* XML extensions registry.
*
Expand All @@ -31,6 +33,8 @@ public class XMLExtensionsRegistry {
private final List<IDiagnosticsParticipant> diagnosticsParticipants;
private final List<ICodeActionParticipant> codeActionsParticipants;

private InitializeParams params;

private Object settings;

private boolean initialized;
Expand All @@ -43,6 +47,14 @@ public XMLExtensionsRegistry() {
codeActionsParticipants = new ArrayList<>();
}

public void initializeParams(InitializeParams params) {
if (initialized) {
extensions.stream().forEach(extension -> extension.start(params, this));
} else {
this.params = params;
}
}

public void updateSettings(Object settings) {
if (initialized) {
extensions.stream().forEach(extension -> extension.updateSettings(settings));
Expand Down Expand Up @@ -97,7 +109,7 @@ private synchronized void initialize() {
void registerExtension(IXMLExtension extension) {
try {
extensions.add(extension);
extension.start(this);
extension.start(params, this);
extension.updateSettings(settings);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error while initializing extension <" + extension.getClass().getName() + ">", e);
Expand Down

0 comments on commit 66ade4e

Please sign in to comment.