Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set catalog prefer as public in vscode setting? #844

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -73,10 +75,10 @@ private void validateCatalogPaths(ContentModelSettings cmSettings) {
return; // happen when notification service is not available
}
String[] catalogs = cmSettings.getCatalogs();
Set<String> invalidCatalogs = Arrays.stream(catalogs).filter(c -> {
Set<String> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down Expand Up @@ -165,9 +166,9 @@ public void stop(XMLExtensionsRegistry registry) {
public ContentModelSettings getContentModelSettings() {
return cmSettings;
}

public ContentModelManager getContentModelManager() {
return contentModelManager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class ContentModelSettings {

private String[] catalogs = null;

private XMLCatalogSettings catalog;

private XMLFileAssociation[] fileAssociations;

private XMLValidationSettings validation;
Expand Down Expand Up @@ -54,6 +56,7 @@ public void setUseCache(Boolean useCache) {
*
* @param catalogs
*/
@Deprecated
public void setCatalogs(String[] catalogs) {
this.catalogs = catalogs;
}
Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it used to never return null, you should keep that

}

public void setFileAssociations(XMLFileAssociation[] fileAssociations) {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*******************************************************************************
* 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 String DEFAULT_PREFER_PUBLIC = "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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default catalog should be immutable


private String prefer;

private String[] files = null;

private boolean useLiteralSystemId;

public XMLCatalogSettings() {
setPrefer(DEFAULT_PREFER_PUBLIC);
setUseLiteralSystemId(DEFAULT_USE_LITERAL_SYSTEM_ID);
}

public String getPrefer() {
return prefer;
}

public void setPrefer(String prefer) {
this.prefer = prefer;
}

public boolean isPreferPublic() {
return DEFAULT_PREFER_PUBLIC.equals(getPrefer());
}

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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> 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
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand Down Expand Up @@ -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
Expand All @@ -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";
Expand All @@ -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.",
Expand All @@ -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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
Expand Down