Skip to content

Commit

Permalink
Download external resources preference
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr authored and fbricon committed Jan 28, 2022
1 parent 1159498 commit c6c90b8
Show file tree
Hide file tree
Showing 30 changed files with 808 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.lemminx.extensions.contentmodel.participants.DocumentTelemetryParticipant;
import org.eclipse.lemminx.extensions.contentmodel.participants.diagnostics.ContentModelDiagnosticsParticipant;
import org.eclipse.lemminx.extensions.contentmodel.settings.ContentModelSettings;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLDownloadExternalResourcesSettings;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings;
import org.eclipse.lemminx.services.IXMLDocumentProvider;
import org.eclipse.lemminx.services.IXMLValidationService;
Expand Down Expand Up @@ -131,33 +132,32 @@ private void updateSettings(ContentModelSettings settings, ISaveContext context)
boolean catalogPathsChanged = contentModelManager.setCatalogs(settings.getCatalogs());
if (catalogPathsChanged) {
// Validate all opened XML files
context.collectDocumentToValidate(d -> {
DOMDocument xml = context.getDocument(d.getDocumentURI());
if (xml == null) {
return false;
}
xml.resetGrammar();
return true;
});
validateAllOpenedDocument(context);
}
}
if (settings.getFileAssociations() != null) {
// Update XML file associations
boolean fileAssociationsChanged = contentModelManager.setFileAssociations(settings.getFileAssociations());
if (fileAssociationsChanged) {
// Validate all opened XML files
context.collectDocumentToValidate(d -> {
DOMDocument xml = context.getDocument(d.getDocumentURI());
xml.resetGrammar();
return true;
});
validateAllOpenedDocument(context);
}
}
// Update use cache, only if it is set in the settings.
Boolean useCache = settings.isUseCache();
if (useCache != null) {
contentModelManager.setUseCache(useCache);
}
// Download external resources
XMLDownloadExternalResourcesSettings downloadExternalResources = settings.getDownloadExternalResources();
boolean downloadExternalResourcesEnabled = downloadExternalResources == null
|| downloadExternalResources.isEnabled();
if (contentModelManager.isDownloadExternalResources() != downloadExternalResourcesEnabled) {
contentModelManager.setDownloadExternalResources(downloadExternalResourcesEnabled);
// Validate all opened XML files
validateAllOpenedDocument(context);
}

// Update symbols
boolean showReferencedGrammars = settings.isShowReferencedGrammars();
symbolsProviderParticipant.setEnabled(showReferencedGrammars);
Expand All @@ -169,6 +169,18 @@ private void updateSettings(ContentModelSettings settings, ISaveContext context)
}
}

private void validateAllOpenedDocument(ISaveContext context) {
// Validate all opened XML files
context.collectDocumentToValidate(d -> {
DOMDocument xml = context.getDocument(d.getDocumentURI());
if (xml == null) {
return false;
}
xml.resetGrammar();
return true;
});
}

@Override
public void start(InitializeParams params, XMLExtensionsRegistry registry) {
URIResolverExtensionManager resolverManager = registry.getComponent(URIResolverExtensionManager.class);
Expand All @@ -188,7 +200,8 @@ public void start(InitializeParams params, XMLExtensionsRegistry registry) {
registry.registerSymbolsProviderParticipant(symbolsProviderParticipant);
codeLensParticipant = new ContentModelCodeLensParticipant(contentModelManager);
registry.registerCodeLensParticipant(codeLensParticipant);
documentTelemetryParticipant = new DocumentTelemetryParticipant(registry.getTelemetryManager(), contentModelManager);
documentTelemetryParticipant = new DocumentTelemetryParticipant(registry.getTelemetryManager(),
contentModelManager);
registry.registerDocumentLifecycleParticipant(documentTelemetryParticipant);

// Register custom commands to re-validate XML files
Expand All @@ -204,8 +217,7 @@ public void start(InitializeParams params, XMLExtensionsRegistry registry) {
new AssociateGrammarCommand(documentProvider));
commandService.registerCommand(CheckBoundGrammarCommand.COMMAND_ID,
new CheckBoundGrammarCommand(documentProvider));
commandService.registerCommand(CheckFilePatternCommand.COMMAND_ID,
new CheckFilePatternCommand());
commandService.registerCommand(CheckFilePatternCommand.COMMAND_ID, new CheckFilePatternCommand());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class ContentModelManager {
private final XMLCacheResolverExtension cacheResolverExtension;
private final XMLCatalogResolverExtension catalogResolverExtension;
private final XMLFileAssociationResolverExtension fileAssociationResolver;
private final XMLGrammarPool grammarPool;
private final LSPXMLGrammarPool grammarPool;

public ContentModelManager(URIResolverExtensionManager resolverManager) {
this.resolverManager = resolverManager;
Expand Down Expand Up @@ -404,6 +404,14 @@ public void setUseCache(boolean useCache) {
grammarPool.clear();
}
}

public boolean isDownloadExternalResources() {
return cacheResolverExtension.isDownloadExternalResources();
}

public void setDownloadExternalResources(boolean downloadExternalResources) {
cacheResolverExtension.setDownloadExternalResources(downloadExternalResources);
}

/**
* Remove the referenced grammar from the given document and clear the Xerces
Expand Down Expand Up @@ -470,7 +478,7 @@ public void unregisterModelProvider(ContentModelProvider modelProvider) {
modelProviders.remove(modelProvider);
}

public XMLGrammarPool getGrammarPool() {
public LSPXMLGrammarPool getGrammarPool() {
return cacheResolverExtension.isUseCache() ? grammarPool : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.xerces.xni.XMLLocator;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMDocumentType;
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.dom.DOMRange;
import org.eclipse.lemminx.extensions.contentmodel.participants.codeactions.ElementDeclUnterminatedCodeAction;
Expand Down Expand Up @@ -217,6 +218,12 @@ public static Range toLSPRange(XMLLocator location, DTDErrorCode code, Object[]
if (locationRange != null) {
return XMLPositionUtility.createRange(locationRange);
}
try {
DOMDocumentType docType = document.getDoctype();
return new Range(document.positionAt(docType.getSystemIdNode().getStart()),
document.positionAt(docType.getSystemIdNode().getEnd()));
} catch (BadLocationException e) {
}
return null;
}
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) 2022 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* 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.contentmodel.participants;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.lemminx.services.extensions.diagnostics.IXMLErrorCode;

/**
* External resource error code when referenced XSD, DTD (with DOCTYPE,
* xsi:schemaLocation) is downloaded.
*
*/
public enum ExternalResourceErrorCode implements IXMLErrorCode {

DownloadResourceDisabled, //
DownloadingResource, //
ResourceNotInDeployedPath, //
DownloadProblem;

private final String code;

private ExternalResourceErrorCode() {
this(null);
}

private ExternalResourceErrorCode(String code) {
this.code = code;
}

@Override
public String getCode() {
if (code == null) {
return name();
}
return code;
}

private final static Map<String, ExternalResourceErrorCode> codes;

static {
codes = new HashMap<>();
for (ExternalResourceErrorCode errorCode : values()) {
codes.put(errorCode.getCode(), errorCode);
}
}

public static ExternalResourceErrorCode get(String name) {
return codes.get(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.extensions.contentmodel.ContentModelPlugin;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings;
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;
Expand Down Expand Up @@ -46,9 +48,13 @@ public void doDiagnostics(DOMDocument xmlDocument, List<Diagnostic> diagnostics,
// Get entity resolver (XML catalog resolver, XML schema from the file
// associations settings., ...)
XMLEntityResolver entityResolver = xmlDocument.getResolverExtensionManager();
LSPXMLEntityResolver entityResolverWrapper = new LSPXMLEntityResolver(entityResolver,
(DiagnosticsResult) diagnostics);

// Process validation
XMLValidator.doDiagnostics(xmlDocument, entityResolver, diagnostics, validationSettings,
XMLValidator.doDiagnostics(xmlDocument, entityResolverWrapper, diagnostics, validationSettings,
contentModelPlugin.getContentModelManager(), monitor);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,4 @@ private Set<ReferencedGrammarInfo> getReferencedGrammars() {
}
return referencedGrammars = contentModelManager.getReferencedGrammarInfos(super.getDOMDocument());
}

}
Loading

0 comments on commit c6c90b8

Please sign in to comment.