Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read the cached XSD, DTD grammar file with lazy mode #687

Merged
merged 1 commit into from
May 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.lemminx.extensions.contentmodel.uriresolver;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

Expand All @@ -32,6 +33,30 @@
*/
public class XMLCacheResolverExtension implements URIResolverExtension {

private static class XMLFileInputSource extends XMLInputSource {

private final Path file;

public XMLFileInputSource(XMLResourceIdentifier resourceIdentifier, Path file) {
super(resourceIdentifier);
this.file = file;
}

@Override
public InputStream getByteStream() {
// Load the file input stream only if it is used
InputStream input = super.getByteStream();
if (input == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if path exists and is a file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is done before (file is null when it doesn't exists), in otherwise don't need to do that here.

try {
super.setByteStream(Files.newInputStream(file));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return super.getByteStream();
}
}

private final CacheResourcesManager cacheResourcesManager;

public XMLCacheResolverExtension() {
Expand All @@ -53,9 +78,7 @@ public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) th
Path file = getCachedResource(url);
if (file != null) {
// The resource was downloaded locally, use it.
XMLInputSource source = new XMLInputSource(resourceIdentifier);
source.setByteStream(Files.newInputStream(file));
return source;
return new XMLFileInputSource(resourceIdentifier, file);
}
return null;
}
Expand Down