From 8a9b8887de350d372dcfd52cb73abdf58025047d Mon Sep 17 00:00:00 2001 From: azerr Date: Thu, 13 Aug 2020 17:25:14 +0200 Subject: [PATCH] How to set catalog prefer as public in vscode setting? See https://github.com/redhat-developer/vscode-xml/issues/295 Signed-off-by: azerr --- .../extensions/catalog/XMLCatalogPlugin.java | 10 +-- .../contentmodel/ContentModelPlugin.java | 7 +- .../model/ContentModelManager.java | 4 +- .../settings/ContentModelSettings.java | 18 ++++- .../settings/XMLCatalogSettings.java | 71 +++++++++++++++++++ .../XMLCatalogResolverExtension.java | 21 ++++-- .../XMLSchemaPublishDiagnosticsTest.java | 14 ++-- 7 files changed, 124 insertions(+), 21 deletions(-) create mode 100644 org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/settings/XMLCatalogSettings.java diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/catalog/XMLCatalogPlugin.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/catalog/XMLCatalogPlugin.java index 6455cf0b30..fa06fc0e7b 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/catalog/XMLCatalogPlugin.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/catalog/XMLCatalogPlugin.java @@ -14,6 +14,7 @@ import java.nio.file.Files; import java.util.Arrays; +import java.util.Collections; import java.util.Set; import java.util.stream.Collectors; @@ -45,7 +46,8 @@ public XMLCatalogPlugin() { @Override public void doSave(ISaveContext context) { Object initializationOptionsSettings = context.getSettings(); - ContentModelSettings cmSettings = ContentModelSettings.getContentModelXMLSettings(initializationOptionsSettings); + ContentModelSettings cmSettings = ContentModelSettings + .getContentModelXMLSettings(initializationOptionsSettings); if (cmSettings == null) { return; } @@ -73,10 +75,10 @@ private void validateCatalogPaths(ContentModelSettings cmSettings) { return; // happen when notification service is not available } String[] catalogs = cmSettings.getCatalogs(); - Set invalidCatalogs = Arrays.stream(catalogs).filter(c -> { + Set invalidCatalogs = catalogs != null ? Arrays.stream(catalogs).filter(c -> { return Files.notExists(FilesUtils.getPath(c)); - }).collect(Collectors.toSet()); - + }).collect(Collectors.toSet()) : Collections.emptySet(); + if (invalidCatalogs.size() > 0) { this.pathWarner.onInvalidFilePath(invalidCatalogs, PathFeature.CATALOGS); } else { diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/ContentModelPlugin.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/ContentModelPlugin.java index 991da4459a..2075a34603 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/ContentModelPlugin.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/ContentModelPlugin.java @@ -104,7 +104,8 @@ private void updateSettings(ISaveContext saveContext) { private void updateSettings(ContentModelSettings settings, ISaveContext context) { if (settings.getCatalogs() != null) { // Update XML catalog settings - boolean catalogPathsChanged = contentModelManager.setCatalogs(settings.getCatalogs()); + boolean catalogPathsChanged = contentModelManager.setCatalogs(settings.getCatalogs(), + settings.getCatalog().isPreferPublic(), settings.getCatalog().isUseLiteralSystemId()); if (catalogPathsChanged) { // Validate all opened XML files context.collectDocumentToValidate(d -> { @@ -165,9 +166,9 @@ public void stop(XMLExtensionsRegistry registry) { public ContentModelSettings getContentModelSettings() { return cmSettings; } - + public ContentModelManager getContentModelManager() { return contentModelManager; } - + } diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/model/ContentModelManager.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/model/ContentModelManager.java index f56e63a5d2..2c3974e15f 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/model/ContentModelManager.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/model/ContentModelManager.java @@ -263,8 +263,8 @@ public ContentModelProvider getModelProviderByURI(String uri) { * @param catalogs list of XML catalog files. * @return true if catalogs changed and false otherwise */ - public boolean setCatalogs(String[] catalogs) { - return catalogResolverExtension.setCatalogs(catalogs); + public boolean setCatalogs(String[] catalogs, boolean preferPublic, boolean useLiteralSystemId) { + return catalogResolverExtension.setCatalogs(catalogs, preferPublic, useLiteralSystemId); } /** diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/settings/ContentModelSettings.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/settings/ContentModelSettings.java index 655d6dad59..b31360ed63 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/settings/ContentModelSettings.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/settings/ContentModelSettings.java @@ -24,6 +24,8 @@ public class ContentModelSettings { private String[] catalogs = null; + private XMLCatalogSettings catalog; + private XMLFileAssociation[] fileAssociations; private XMLValidationSettings validation; @@ -54,6 +56,7 @@ public void setUseCache(Boolean useCache) { * * @param catalogs */ + @Deprecated public void setCatalogs(String[] catalogs) { this.catalogs = catalogs; } @@ -64,7 +67,13 @@ public void setCatalogs(String[] catalogs) { * @return the list of the XML catalogs file path. */ public String[] getCatalogs() { - return catalogs == null ? new String[0] : catalogs; + XMLCatalogSettings catalog = getCatalog(); + if (catalog != null) { + if (catalog.getFiles() != null) { + return catalog.getFiles(); + } + } + return catalogs; } public void setFileAssociations(XMLFileAssociation[] fileAssociations) { @@ -94,4 +103,11 @@ public XMLValidationSettings getValidation() { return validation; } + public XMLCatalogSettings getCatalog() { + return catalog != null ? catalog : XMLCatalogSettings.DEFAULT_CATALOG; + } + + public void setCatalog(XMLCatalogSettings catalog) { + this.catalog = catalog; + } } diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/settings/XMLCatalogSettings.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/settings/XMLCatalogSettings.java new file mode 100644 index 0000000000..1ee4afb613 --- /dev/null +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/settings/XMLCatalogSettings.java @@ -0,0 +1,71 @@ +/******************************************************************************* +* Copyright (c) 2020 Red Hat Inc. and others. +* All rights reserved. This program and the accompanying materials +* 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.settings; + +/** + * XML catalog settings. + * + */ +public class XMLCatalogSettings { + + public static Prefer DEFAULT_PREFER_PUBLIC = Prefer.PUBLIC; + + public static boolean DEFAULT_USE_PREFER_PUBLIC = true; + + public static boolean DEFAULT_USE_LITERAL_SYSTEM_ID = true; + + public static final XMLCatalogSettings DEFAULT_CATALOG = new XMLCatalogSettings(); + + public enum Prefer { + PUBLIC, SYSTEM; + } + + private Prefer prefer; + + private String[] files = null; + + private boolean useLiteralSystemId; + + public XMLCatalogSettings() { + setPrefer(DEFAULT_PREFER_PUBLIC); + setUseLiteralSystemId(DEFAULT_USE_LITERAL_SYSTEM_ID); + } + + public Prefer getPrefer() { + return prefer; + } + + public void setPrefer(Prefer prefer) { + this.prefer = prefer; + } + + public boolean isPreferPublic() { + return getPrefer() == Prefer.PUBLIC; + } + + public String[] getFiles() { + return files; + } + + public void setFiles(String[] files) { + this.files = files; + } + + public boolean isUseLiteralSystemId() { + return useLiteralSystemId; + } + + public void setUseLiteralSystemId(boolean useLiteralSystemId) { + this.useLiteralSystemId = useLiteralSystemId; + } + +} diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/uriresolver/XMLCatalogResolverExtension.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/uriresolver/XMLCatalogResolverExtension.java index 4af98af69a..b2ff081e67 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/uriresolver/XMLCatalogResolverExtension.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/uriresolver/XMLCatalogResolverExtension.java @@ -25,6 +25,7 @@ import org.apache.xerces.xni.XMLResourceIdentifier; import org.apache.xerces.xni.XNIException; import org.apache.xerces.xni.parser.XMLInputSource; +import org.eclipse.lemminx.extensions.contentmodel.settings.XMLCatalogSettings; import org.eclipse.lemminx.uriresolver.URIResolverExtension; import org.eclipse.lemminx.utils.FilesUtils; @@ -95,17 +96,23 @@ public void setRootUri(String rootUri) { /** * Initialize catalogs path. * - * @param catalogs the catalog path array. + * @param catalogs the catalog path array. + * @param preferPublic + * @param useLiteralSystemId * @return true if catalogs changed and false otherwise */ - public boolean setCatalogs(String[] catalogs) { + public boolean setCatalogs(String[] catalogs, boolean preferPublic, boolean useLiteralSystemId) { String[] oldCatalogs = catalogResolver != null ? catalogResolver.getCatalogList() : null; + boolean oldPreferPublic = catalogResolver != null ? catalogResolver.getPreferPublic() + : XMLCatalogSettings.DEFAULT_USE_PREFER_PUBLIC; + boolean oldUseLiteralSystemId = catalogResolver != null ? catalogResolver.getUseLiteralSystemId() + : XMLCatalogSettings.DEFAULT_USE_LITERAL_SYSTEM_ID; if (catalogs != null) { List xmlCatalogFiles = new ArrayList<>(); for (String catalogPath : catalogs) { // resolve catalog file path with root uri String fullPath = expandSystemId(catalogPath); - + if (Files.exists(FilesUtils.getPath(fullPath))) { xmlCatalogFiles.add(fullPath); LOGGER.info("Adding XML catalog '" + catalogPath + "' with expand system id '" + fullPath @@ -117,6 +124,8 @@ public boolean setCatalogs(String[] catalogs) { } if (xmlCatalogFiles.size() > 0) { XMLCatalogResolver catalogResolver = new XMLCatalogResolver(xmlCatalogFiles.toArray(new String[0])); + catalogResolver.setPreferPublic(preferPublic); + catalogResolver.setUseLiteralSystemId(useLiteralSystemId); setCatalogResolver(catalogResolver); } else { setCatalogResolver(null); @@ -125,7 +134,8 @@ public boolean setCatalogs(String[] catalogs) { setCatalogResolver(null); } String[] newCatalogs = catalogResolver != null ? catalogResolver.getCatalogList() : null; - return !Objects.equals(oldCatalogs, newCatalogs); + return !Objects.equals(oldCatalogs, newCatalogs) || catalogResolver.getPreferPublic() != oldPreferPublic + || catalogResolver.getUseLiteralSystemId() != oldUseLiteralSystemId; } private String expandSystemId(String path) { @@ -145,7 +155,8 @@ private void setCatalogResolver(XMLCatalogResolver catalogResolver) { */ public void refreshCatalogs() { if (catalogResolver != null) { - setCatalogs(catalogResolver.getCatalogList()); + setCatalogs(catalogResolver.getCatalogList(), catalogResolver.getPreferPublic(), + catalogResolver.getUseLiteralSystemId()); } } } diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSchemaPublishDiagnosticsTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSchemaPublishDiagnosticsTest.java index d304c1b5e7..35a803f378 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSchemaPublishDiagnosticsTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSchemaPublishDiagnosticsTest.java @@ -113,7 +113,7 @@ public void schemaWithUrlInvalidPathWithNamespace() throws Exception { ""; XMLAssert.testPublishDiagnosticsFor(xml, fileURI, configuration, pd(fileURI, // new Diagnostic(r(2, 20, 2, 40), - "SchemaLocation: schemaLocation value = 'http://invoice.xsd' must have even number of URI's.", + "SchemaLocation: schemaLocation value = 'http://invoice.xsd' must have even number of URI's.", DiagnosticSeverity.Warning, "xml", "SchemaLocation"), // new Diagnostic(r(1, 1, 1, 8), "cvc-elt.1.a: Cannot find the declaration of element 'invoice'.", DiagnosticSeverity.Error, "xml", "cvc-elt.1.a"))); @@ -145,8 +145,10 @@ public void schemaWithUrlWithCache() throws Exception { pd(fileURI, new Diagnostic(r(1, 1, 1, 8), "The resource 'http://invoice.xsd' is downloading.", DiagnosticSeverity.Information, "XML")), - pd(fileURI, new Diagnostic(r(1, 1, 1, 8), "Error while downloading 'http://invoice.xsd' to "+expectedLocation+".", - DiagnosticSeverity.Error, "XML"))); + pd(fileURI, + new Diagnostic(r(1, 1, 1, 8), + "Error while downloading 'http://invoice.xsd' to " + expectedLocation + ".", + DiagnosticSeverity.Error, "XML"))); } @Test @@ -163,7 +165,7 @@ public void schemaWithUrlWithCacheAndWithCatalog() throws Exception { contentModelManager.setUseCache(true); // use catalog which defines bind src/test/xsd/invoice.xsd with // http://invoice.xsd namespace - contentModelManager.setCatalogs(new String[] { "src/test/resources/catalogs/catalog.xml" }); + contentModelManager.setCatalogs(new String[] { "src/test/resources/catalogs/catalog.xml" }, true, true); }; String fileURI = "test.xml"; @@ -184,7 +186,7 @@ public void schemaWithUrlWithCacheAndWithCatalog() throws Exception { XMLAssert.testPublishDiagnosticsFor(xml, fileURI, configuration, pd(fileURI, // new Diagnostic(r(3, 8, 3, 26), - "Content of type 'date' is expected.\n\nThe following content is not a valid type:\n '2017-11-30_INVALID'\n\nCode:", + "Content of type 'date' is expected.\n\nThe following content is not a valid type:\n '2017-11-30_INVALID'\n\nCode:", DiagnosticSeverity.Error, "xml", XMLSchemaErrorCode.cvc_datatype_valid_1_2_1.getCode()), // new Diagnostic(r(3, 8, 3, 26), "cvc-type.3.1.3: The value '2017-11-30_INVALID' of element 'date' is not valid.", @@ -208,7 +210,7 @@ public void schemaWithUrlWithoutCacheAndWithCatalog() throws Exception { contentModelManager.setUseCache(false); // use catalog which defines bind src/test/xsd/invoice.xsd with // http://invoice.xsd namespace - contentModelManager.setCatalogs(new String[] { "src/test/resources/catalogs/catalog.xml" }); + contentModelManager.setCatalogs(new String[] { "src/test/resources/catalogs/catalog.xml" }, true, true); }; String fileURI = "test.xml"; String xml = "\r\n" + //