Skip to content

Commit

Permalink
How to set catalog prefer as public in vscode setting?
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Aug 17, 2020
1 parent f45459e commit 918d125
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 16 deletions.
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 @@ -94,4 +96,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,51 @@
/*******************************************************************************
* 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 boolean DEFAULT_PREFER_PUBLIC = true;

public static boolean DEFAULT_USE_LITERAL_SYSTEM_ID = true;

public static final XMLCatalogSettings DEFAULT_CATALOG = new XMLCatalogSettings();

private boolean preferPublic;

private boolean useLiteralSystemId;

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

public boolean isPreferPublic() {
return preferPublic;
}

public void setPreferPublic(boolean preferPublic) {
this.preferPublic = preferPublic;
}

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_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

0 comments on commit 918d125

Please sign in to comment.