-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XSD extension used to 1) resolve http://www.w3.org/2001/XMLSchema, 2)
cache XML Schema if no cache is set (see #178)
- Loading branch information
1 parent
32ded40
commit 4a8b5bf
Showing
6 changed files
with
2,768 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsd/XSDPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...pse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsd/XSDURIResolverExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...rc/main/resources/META-INF/services/org.eclipse.lsp4xml.services.extensions.IXMLExtension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.