-
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.
Retrieve the right XSL Schema from the xsl:stylesheet/@Version + deploy
xsd in the "user.home" (see #91)
- Loading branch information
1 parent
4ec4c11
commit c9ce5a5
Showing
8 changed files
with
276 additions
and
34 deletions.
There are no files selected for viewing
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
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
83 changes: 58 additions & 25 deletions
83
...pse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsl/XSLURIResolverExtension.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 |
---|---|---|
@@ -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"; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/IXMLDocumentProvider.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,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); | ||
} |
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
91 changes: 91 additions & 0 deletions
91
org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/FilesUtils.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,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() : ""; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.