Register Virtual File System and proprietary virtual file impl

Answered

Hi 

I'm trying to write my own virtual file system to load java files that are base64 encoded (The specific project java files are encoded in base64 while the user might use projects that are not encoded in base64)

I tried two approaches, none of them is working good....I am able to load my own vfs . Appreciate any help for understanding if it is doable (including compiling the code) and what am I doing wrong?

 

First approach:

1. extend DeprecatedVirtualFileSystem  and virtualFile 

2. the virtual file override the contentToByteArray() 

3. in plugin.xml I added extension point 

How can  I register the specific files on that folder to be opened using my virtual file system and still be able to compile the code?

In both approaches I do see EncVirtualFileSystem in list of VFS manager and it seems that in second approach *.java files are loaded with java icon but where in code I can decode the file? and  how to make compiler handle those files?

snippet from the code:

public class EncVirtualFileSystem extends DeprecatedVirtualFileSystem implements LocalFileProvider  {

private final static String PROTOCOL = "enc";

@Nonnull
@Override
public String getProtocol() {
return PROTOCOL;
}
@Nullable
public VirtualFile findFileByIoFile(@NotNull File ioFile) {
return new EncVirtualFile(this, ioFile);
}
.....

public class EncVirtualFile extends VirtualFile  {
private final EncVirtualFileSystem myFileSystem;
private final File myIoFile;
private VirtualFile[] myChildren;
private final NotNullLazyValue<Boolean> myIsDirectory;

@Override
public byte @NotNull [] contentsToByteArray() throws IOException {
return java.util.Base64.getDecoder().decode(getInputStream().readAllBytes());
}

@Override
public @NotNull InputStream getInputStream() throws IOException {
return VfsUtilCore.inputStreamSkippingBOM(new BufferedInputStream(new FileInputStream(myIoFile)), this);
}
...............


from plugin.xml

<extensions defaultExtensionNs="com.intellij">
<virtualFileSystem key="enc" implementationClass="EncVirtualFileSystem" physical="true"/>

</extensions>

Second Approach that I tried 

I tried to implement EncFileType and FileTypeOvverrider but that didn't work as well

 

public class FileTypeOverrider implements com.intellij.openapi.fileTypes.impl.FileTypeOverrider {
@Override
public @Nullable FileType getOverriddenFileType(@NotNull VirtualFile file) {
if (file.getName().endsWith(".java"))
return EncFileType.getInstance();
else
return null;
}
}

public class EncFileType implements FileTypeIdentifiableByVirtualFile {

private static EncFileType instance = new EncFileType();

@Override
public @NonNls @NotNull String getName() {
return "enc";
}

@Override
public boolean isMyFileType(@NotNull VirtualFile file) {
return file.getName().endsWith(".java");
}
@Override
public @Nullable Icon getIcon() {
return AllIcons.FileTypes.JavaClass;
}

.....

plugin.xml
<extensions defaultExtensionNs="com.intellij">
<virtualFileSystem key="enc" implementationClass="EncVirtualFileSystem" physical="true"/>
<fileType name="enc" implementationClass="EncFileType" fieldName="instance" extensions="java"/>
<fileTypeOverrider implementation="FileTypeOverrider"/>
</extensions>

 

1 comment
Comment actions Permalink

Our previous recommendation still stands. Encryption of source files should be done on OS level and not on FS level interfering with everything the IDE uses to access such files.

0

Please sign in to leave a comment.