Skip to content

Commit

Permalink
Retrieve the right XSL Schema from the xsl:stylesheet/@Version + deploy
Browse files Browse the repository at this point in the history
xsd in the "user.home" (see #91)
  • Loading branch information
angelozerr committed Oct 9, 2018
1 parent 4ec4c11 commit c9ce5a5
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.lsp4xml.commons.TextDocument;
import org.eclipse.lsp4xml.dom.XMLDocument;
import org.eclipse.lsp4xml.logs.LogHelper;
import org.eclipse.lsp4xml.services.IXMLDocumentProvider;
import org.eclipse.lsp4xml.services.XMLLanguageService;
import org.eclipse.lsp4xml.services.extensions.CompletionSettings;
import org.eclipse.lsp4xml.settings.InitializationOptionsSettings;
Expand All @@ -48,7 +49,8 @@
* XML language server.
*
*/
public class XMLLanguageServer implements LanguageServer, ProcessLanguageServer, XMLCustomService {
public class XMLLanguageServer
implements LanguageServer, ProcessLanguageServer, XMLCustomService, IXMLDocumentProvider {

private static final Logger LOGGER = Logger.getLogger(XMLLanguageServer.class.getName());

Expand All @@ -62,6 +64,7 @@ public class XMLLanguageServer implements LanguageServer, ProcessLanguageServer,

public XMLLanguageServer() {
xmlLanguageService = new XMLLanguageService();
xmlLanguageService.setDocumentProvider(this);
xmlTextDocumentService = new XMLTextDocumentService(this);
xmlWorkspaceService = new XMLWorkspaceService(this);
delayer = Executors.newScheduledThreadPool(1);
Expand Down Expand Up @@ -127,7 +130,7 @@ public void updateSettings(Object initializationOptionsSettings) {
if (newCompletions != null) {
xmlTextDocumentService.updateCompletionSettings(newCompletions);
}

// Experimental capabilities
XMLExperimentalCapabilities experimental = clientSettings.getExperimental();
if (experimental != null) {
Expand Down Expand Up @@ -200,4 +203,11 @@ public CompletableFuture<String> closeTag(TextDocumentPositionParams params) {
return getXMLLanguageService().doAutoClose(xmlDocument, params.getPosition());
});
}

@Override
public XMLDocument getDocument(String uri) {
TextDocument document = xmlTextDocumentService.getDocument(uri);
return document != null ? xmlTextDocumentService.getXMLDocument(document) : null;
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
/**
* 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.xsl;

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;

/**
* XSL plugin.
*
* @author azerr
*
*/
public class XSLPlugin implements IXMLExtension {

private final XSLURIResolverExtension uiResolver;

public XSLPlugin() {
uiResolver = new XSLURIResolverExtension();
}
private XSLURIResolverExtension uiResolver;

@Override
public void updateSettings(Object settings) {
Expand All @@ -20,6 +32,7 @@ public void updateSettings(Object settings) {

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,93 @@
/**
* 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.xsl;

import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.eclipse.lsp4xml.dom.Element;
import org.eclipse.lsp4xml.dom.XMLDocument;
import org.eclipse.lsp4xml.services.IXMLDocumentProvider;
import org.eclipse.lsp4xml.uriresolver.URIResolverExtension;
import org.eclipse.lsp4xml.utils.FilesUtils;

/**
* Resolve the XSL XML Schema to use according the xsl:stylesheet/@version
*
*/
public class XSLURIResolverExtension implements URIResolverExtension {

/**
* The XSL namespace URI (= http://www.w3.org/1999/XSL/Transform)
*/
private static final String XSL_NAMESPACE_URI = "http://www.w3.org/1999/XSL/Transform"; //$NON-NLS-1$

private final IXMLDocumentProvider documentProvider;

public XSLURIResolverExtension(IXMLDocumentProvider documentProvider) {
this.documentProvider = documentProvider;
}

@Override
public String resolve(String baseLocation, String publicId, String systemId) {
if (!XSL_NAMESPACE_URI.equals(publicId)) {
return null;
} else {

}
else {

String version = getVersion(baseLocation);
if (version == null) {
return null;
}
// TODO: extract version from XML Document
String version = "1.0";
String schemaFileName = "xslt-" + version + ".xsd";
String schemaPath = "/schemas/xslt/" + schemaFileName;

String schemaFileName = "xslt-" + version + ".xsd";
String schemaPath = "schemas/xslt/" + schemaFileName;
try {
Path baseDir = Paths.get("/lsp4xml");
Files.createDirectories(baseDir);
Path outFile = baseDir.resolve(schemaFileName);
Path outFile = FilesUtils.getDeployedPath(Paths.get(schemaPath));
if (!outFile.toFile().exists()) {
InputStream in = XSLURIResolverExtension.class.getResourceAsStream(schemaPath);
String xml = convertStreamToString(in);
saveToFile(xml, outFile);
try (InputStream in = XSLURIResolverExtension.class.getResourceAsStream("/" + schemaPath)) {
FilesUtils.saveToFile(in, outFile);
}
}
return outFile.toFile().toURI().toString();
} catch (Exception e) {
e.printStackTrace();
// Do nothing?
}
return null;
}

private static void saveToFile(String xml, Path outFile) throws IOException {
try (Writer writer = Files.newBufferedWriter(outFile, StandardCharsets.UTF_8)) {
writer.write(xml);
/**
* Returns the version coming from xsl:stylesheet/@version of the XML document
* retrieved by the given uri
*
* @param uri
* @return the version coming from xsl:stylesheet/@version of the XML document
* retrieved by the given uri
*/
private String getVersion(String uri) {
if (documentProvider == null) {
return null;
}
}

static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
XMLDocument document = documentProvider.getDocument(uri);
if (document != null) {
Element element = document.getDocumentElement();
if (element != null) {
String version = element.getAttributeValue("version");
if (version != null) {
return version;
}
}
}
return "1.0";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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.services;

import org.eclipse.lsp4xml.dom.XMLDocument;

/**
* {@link XMLDocument} provider.
*
* @author Angelo ZERR
*
*/
public interface IXMLDocumentProvider {

/**
* Returns the {@link XMLDocument} instance from the given <code>uri</code> and
* null otherwise.
*
* @param uri the document URI.
* @return the {@link XMLDocument} instance from the given <code>uri</code> and
* null otherwise.
*/
XMLDocument getDocument(String uri);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.logging.Logger;

import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4xml.services.IXMLDocumentProvider;

/**
* XML extensions registry.
Expand All @@ -33,6 +34,8 @@ public class XMLExtensionsRegistry {
private final List<IDiagnosticsParticipant> diagnosticsParticipants;
private final List<ICodeActionParticipant> codeActionsParticipants;

private IXMLDocumentProvider documentProvider;

private InitializeParams params;

private Object settings;
Expand Down Expand Up @@ -152,4 +155,22 @@ public void registerCodeActionParticipant(ICodeActionParticipant codeActionsPart
public void unregisterCodeActionParticipant(ICodeActionParticipant codeActionsParticipant) {
codeActionsParticipants.add(codeActionsParticipant);
}
}

/**
* Returns the XML Document provider and null otherwise.
*
* @return the XML Document provider and null otherwise.
*/
public IXMLDocumentProvider getDocumentProvider() {
return documentProvider;
}

/**
* Set the XML Document provider
*
* @param documentProvider XML Document provider
*/
public void setDocumentProvider(IXMLDocumentProvider documentProvider) {
this.documentProvider = documentProvider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* 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.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

/**
* Files utilities.
*
*/
public class FilesUtils {

private static final Path DEPLOYED_BASE_PATH = getDeployedBasePath();

private static Path getDeployedBasePath() {
String dir = System.getProperty("user.home");
if (dir == null) {
dir = System.getProperty("user.dir");
}
if (dir == null) {
dir = "";
}
return Paths.get(dir, ".lsp4xml");
}

/**
* Returns the deployed path from the given <code>path</code>.
*
* @param path the path
* @return the deployed path from the given <code>path</code>.
* @throws IOException
*/
public static Path getDeployedPath(Path path) throws IOException {
Path deployedPath = DEPLOYED_BASE_PATH.resolve(path);
if (Files.exists(deployedPath)) {
return deployedPath;
}
if (!Files.exists(deployedPath.getParent())) {
Files.createDirectories(deployedPath.getParent());
}
return deployedPath;
}

/**
* Save the given input stream <code>in</code> in the give out file
* <code>outFile</code>
*
* @param in the input stream
* @param outFile the output file
* @throws IOException
*/
public static void saveToFile(InputStream in, Path outFile) throws IOException {
saveToFile(toString(in), outFile);
}

/**
* Save the given String <code>content</code> in the give out file
* <code>outFile</code>
*
* @param content the string content
* @param outFile the output file
* @throws IOException
*/
public static void saveToFile(String content, Path outFile) throws IOException {
try (Writer writer = Files.newBufferedWriter(outFile, StandardCharsets.UTF_8)) {
writer.write(content);
}
}

static String toString(InputStream is) {
try (Scanner s = new Scanner(is)) {
s.useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public static void testCompletionFor(XMLLanguageService xmlLanguageService, Stri
TextDocument document = new TextDocument(value, fileURI != null ? fileURI : "test://test/test.html");
Position position = document.positionAt(offset);
XMLDocument htmlDoc = XMLParser.getInstance().parse(document);

xmlLanguageService.setDocumentProvider((uri) -> htmlDoc);

// Configure XML catalog for XML schema
if (catalogPath != null) {
ContentModelSettings settings = new ContentModelSettings();
Expand Down
Loading

0 comments on commit c9ce5a5

Please sign in to comment.