Skip to content

Commit

Permalink
XSD extension used to 1) resolve http://www.w3.org/2001/XMLSchema, 2)
Browse files Browse the repository at this point in the history
cache XML Schema if no cache is set (see #178)
  • Loading branch information
angelozerr committed Nov 2, 2018
1 parent 32ded40 commit 4a8b5bf
Show file tree
Hide file tree
Showing 6 changed files with 2,768 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) 2018 Angelo ZERR.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
*/
package org.eclipse.lsp4xml.extensions.xsd;

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;

/**
* XSD plugin.
*/
public class XSDPlugin implements IXMLExtension {

private XSDURIResolverExtension uiResolver;

@Override
public void updateSettings(Object settings) {

}

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

@Override
public void stop(XMLExtensionsRegistry registry) {
URIResolverExtensionManager.getInstance().unregisterResolver(uiResolver);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright (c) 2018 Angelo ZERR.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
*/
package org.eclipse.lsp4xml.extensions.xsd;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.xerces.xni.XMLResourceIdentifier;
import org.apache.xerces.xni.XNIException;
import org.apache.xerces.xni.parser.XMLInputSource;
import org.eclipse.lsp4xml.services.IXMLDocumentProvider;
import org.eclipse.lsp4xml.uriresolver.CacheResourcesManager;
import org.eclipse.lsp4xml.uriresolver.CacheResourcesManager.ResourceToDeploy;
import org.eclipse.lsp4xml.uriresolver.URIResolverExtension;

/**
* Resolve the XSD XML Schema and DTD dependencies.
*
*/
public class XSDURIResolverExtension implements URIResolverExtension {

/**
* The XMLSchema namespace URI (= http://www.w3.org/2001/XMLSchema)
*/
private static final String SCHEMA_FOR_SCHEMA_URI_2001 = "http://www.w3.org/2001/XMLSchema"; //$NON-NLS-1$

private static final List<ResourceToDeploy> SCHEMA_URI_2001_RESOURCES = Arrays.asList(
new ResourceToDeploy("http://www.w3.org/2001/XMLSchema.xsd", "schemas/xsd/XMLSchema.xsd"),
new ResourceToDeploy("http://www.w3.org/2001/XMLSchema.dtd", "schemas/xsd/XMLSchema.dtd"),
new ResourceToDeploy("http://www.w3.org/2001/datatypes.dtd", "schemas/xsd/datatypes.dtd"));

/**
* The Namespace namespace URI (= http://www.w3.org/XML/1998/namespace)
*/
private static final String SCHEMA_FOR_NAMESPACE_URI_1998 = "http://www.w3.org/XML/1998/namespace"; //$NON-NLS-1$

private static final ResourceToDeploy NAMESPACE_URI_1998_RESOURCE = new ResourceToDeploy(
"https://www.w3.org/2001/xml.xsd", "schemas/xsd/xml.xsd");

public XSDURIResolverExtension(IXMLDocumentProvider documentProvider) {

}

@Override
public String resolve(String baseLocation, String publicId, String systemId) {
if (SCHEMA_FOR_SCHEMA_URI_2001.equals(publicId)) {
try {
// Deploy XML Schema and DTDs on file systems.
for (ResourceToDeploy resource : SCHEMA_URI_2001_RESOURCES) {
CacheResourcesManager.getResourceCachePath(resource);
}
// Returns the patch of "http://www.w3.org/2001/XMLSchema.xsd" file system
return SCHEMA_URI_2001_RESOURCES.get(0).getDeployedPath().toFile().toURI().toString();
} catch (Exception e) {
// Do nothing?
}
} else if (SCHEMA_FOR_NAMESPACE_URI_1998.equals(publicId)) {
try {
return CacheResourcesManager.getResourceCachePath(NAMESPACE_URI_1998_RESOURCE).toFile().toURI()
.toString();
} catch (Exception e) {
// Do nothing?
}
}
return null;
}

@Override
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException {
String publicId = resourceIdentifier.getNamespace();
if (SCHEMA_FOR_SCHEMA_URI_2001.equals(publicId) || SCHEMA_FOR_NAMESPACE_URI_1998.equals(publicId)) {
String baseLocation = resourceIdentifier.getBaseSystemId();
String xslFilePath = resolve(baseLocation, publicId, null);
if (xslFilePath != null) {
return new XMLInputSource(publicId, xslFilePath, xslFilePath);
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.eclipse.lsp4xml.extensions.contentmodel.ContentModelPlugin
org.eclipse.lsp4xml.extensions.references.XMLReferencesPlugin
org.eclipse.lsp4xml.extensions.xsd.XSDPlugin
org.eclipse.lsp4xml.extensions.xsl.XSLPlugin
Loading

0 comments on commit 4a8b5bf

Please sign in to comment.