public VirtualFile copy(final Object requestor, @NotNull final VirtualFile newParent, @NotNull final String copyName) throws IOException { if (getFileSystem() != newParent.getFileSystem()) { throw new IOException(VfsBundle.message("file.copy.error", newParent.getPresentableUrl())); } if (!newParent.isDirectory()) { throw new IOException(VfsBundle.message("file.copy.target.must.be.directory")); } return EncodingRegistry.doActionAndRestoreEncoding(this, new ThrowableComputable<VirtualFile, IOException>() { @Override public VirtualFile compute() throws IOException { return getFileSystem().copyFile(requestor, VirtualFile.this, newParent, copyName); } }); }
@Override public VirtualFile copy(final Object requestor, @NotNull final VirtualFile newParent, @NotNull final String copyName) throws IOException { if (getFileSystem() != newParent.getFileSystem()) { throw new IOException(VfsBundle.message("file.copy.error", newParent.getPresentableUrl())); } if (!newParent.isDirectory()) { throw new IOException(VfsBundle.message("file.copy.target.must.be.directory")); } return EncodingRegistry.doActionAndRestoreEncoding(this, new ThrowableComputable<VirtualFile, IOException>() { @Override public VirtualFile compute() throws IOException { return ourPersistence.copyFile(requestor, VirtualFileSystemEntry.this, newParent, copyName); } }); }
public static void setApplication(@NotNull Application instance, @NotNull Getter<FileTypeRegistry> fileTypeRegistryGetter, @NotNull Getter<EncodingRegistry> encodingRegistryGetter, @NotNull Disposable parent) { final Application old = ourApplication; final Getter<FileTypeRegistry> oldFileTypeRegistry = FileTypeRegistry.ourInstanceGetter; final Getter<EncodingRegistry> oldEncodingRegistry = EncodingRegistry.ourInstanceGetter; Disposer.register(parent, new Disposable() { @Override public void dispose() { if (old != null) { // to prevent NPEs in threads still running setApplication(old); //noinspection AssignmentToStaticFieldFromInstanceMethod FileTypeRegistry.ourInstanceGetter = oldFileTypeRegistry; //noinspection AssignmentToStaticFieldFromInstanceMethod EncodingRegistry.ourInstanceGetter = oldEncodingRegistry; } } }); setApplication(instance); FileTypeRegistry.ourInstanceGetter = fileTypeRegistryGetter; EncodingRegistry.ourInstanceGetter = encodingRegistryGetter; }
public void initApplication() { //if (ApplicationManager.getApplication() instanceof MockApplicationEx) return; final MockApplicationEx instance = new MockApplicationEx(getTestRootDisposable()); ApplicationManager.setApplication(instance, new Getter<FileTypeRegistry>() { @Override public FileTypeRegistry get() { return FileTypeManager.getInstance(); } }, new Getter<EncodingRegistry>() { @Override public EncodingRegistry get() { return EncodingManager.getInstance(); } }, getTestRootDisposable()); getApplication().registerService(EncodingManager.class, EncodingManagerImpl.class); }
@Nullable public String getContent() throws VcsException { if (myContents == null) { BufferExposingByteArrayOutputStream bos = new BufferExposingByteArrayOutputStream(2048); try { myRepository.getFile(myPath, -1, null, bos); myRepository.closeSession(); } catch (SVNException e) { throw new VcsException(e); } final byte[] bytes = bos.toByteArray(); final Charset charset = myFilePath.getCharset(); myContents = charset == null ? CharsetToolkit.bytesToString(bytes, EncodingRegistry.getInstance().getDefaultCharset()) : CharsetToolkit.bytesToString(bytes, charset); } return myContents; }
public VirtualFile copy(final Object requestor, @Nonnull final VirtualFile newParent, @Nonnull final String copyName) throws IOException { if (getFileSystem() != newParent.getFileSystem()) { throw new IOException(VfsBundle.message("file.copy.error", newParent.getPresentableUrl())); } if (!newParent.isDirectory()) { throw new IOException(VfsBundle.message("file.copy.target.must.be.directory")); } return EncodingRegistry.doActionAndRestoreEncoding(this, new ThrowableComputable<VirtualFile, IOException>() { @Override public VirtualFile compute() throws IOException { return getFileSystem().copyFile(requestor, VirtualFile.this, newParent, copyName); } }); }
@Override public void move(final Object requestor, @NotNull final VirtualFile newParent) throws IOException { if (!exists()) { throw new IOException("File to move does not exist: " + getPath()); } if (!newParent.exists()) { throw new IOException("Destination folder does not exist: " + newParent.getPath()); } if (!newParent.isDirectory()) { throw new IOException("Destination is not a folder: " + newParent.getPath()); } final VirtualFile child = newParent.findChild(getName()); if (child != null) { throw new IOException("Destination already exists: " + newParent.getPath() + "/" + getName()); } EncodingRegistry.doActionAndRestoreEncoding(this, new ThrowableComputable<VirtualFile, IOException>() { @Override public VirtualFile compute() throws IOException { getFileSystem().moveFile(requestor, NewVirtualFile.this, newParent); return NewVirtualFile.this; } }); }
public String stringFromBytes(@NotNull byte[] bytes, @NotNull String path) { try { VirtualFile file = findVirtualFile(path); if (file == null) { return CharsetToolkit.bytesToString(bytes, EncodingRegistry.getInstance().getDefaultCharset()); } return new String(bytes, file.getCharset().name()); } catch (UnsupportedEncodingException e1) { return new String(bytes); } }
/** * Moves this file to another directory. This method should be only called within write-action. * See {@link Application#runWriteAction}. * * @param requestor any object to control who called this method. Note that * it is considered to be an external change if <code>requestor</code> is <code>null</code>. * See {@link VirtualFileEvent#getRequestor} * @param newParent the directory to move this file to * @throws IOException if file failed to be moved */ public void move(final Object requestor, @NotNull final VirtualFile newParent) throws IOException { if (getFileSystem() != newParent.getFileSystem()) { throw new IOException(VfsBundle.message("file.move.error", newParent.getPresentableUrl())); } EncodingRegistry.doActionAndRestoreEncoding(this, new ThrowableComputable<VirtualFile, IOException>() { @Override public VirtualFile compute() throws IOException { getFileSystem().moveFile(requestor, VirtualFile.this, newParent); return VirtualFile.this; } }); }
/** * @return Retrieve the charset file has been loaded with (if loaded) and would be saved with (if would). */ @NotNull public Charset getCharset() { Charset charset = getStoredCharset(); if (charset == null) { charset = EncodingRegistry.getInstance().getDefaultCharset(); setCharset(charset); } return charset; }
@Override public void move(final Object requestor, @NotNull final VirtualFile newParent) throws IOException { if (getFileSystem() != newParent.getFileSystem()) { throw new IOException(VfsBundle.message("file.move.error", newParent.getPresentableUrl())); } EncodingRegistry.doActionAndRestoreEncoding(this, new ThrowableComputable<VirtualFile, IOException>() { @Override public VirtualFile compute() throws IOException { ourPersistence.moveFile(requestor, VirtualFileSystemEntry.this, newParent); return VirtualFileSystemEntry.this; } }); }
@NotNull public static Charset detectCharset(@NotNull VirtualFile virtualFile, @NotNull byte[] content, @NotNull FileType fileType) { Charset charset = null; Trinity<Charset,CharsetToolkit.GuessedEncoding, byte[]> guessed = guessFromContent(virtualFile, content, content.length); if (guessed != null && guessed.first != null) { charset = guessed.first; } else { String charsetName = fileType.getCharset(virtualFile, content); if (charsetName == null) { Charset specifiedExplicitly = EncodingRegistry.getInstance().getEncoding(virtualFile, true); if (specifiedExplicitly != null) { charset = specifiedExplicitly; } } else { charset = CharsetToolkit.forName(charsetName); } } if (charset == null) { charset = EncodingRegistry.getInstance().getDefaultCharset(); } if (fileType.getName().equals("Properties") && EncodingRegistry.getInstance().isNative2Ascii(virtualFile)) { charset = Native2AsciiCharset.wrap(charset); } virtualFile.setCharset(charset); return charset; }
private static String bytesToString(FilePath path, @NotNull byte[] bytes) { Charset charset = null; if (path.getVirtualFile() != null) { charset = path.getVirtualFile().getCharset(); } if (charset != null) { int bomLength = CharsetToolkit.getBOMLength(bytes, charset); final CharBuffer charBuffer = charset.decode(ByteBuffer.wrap(bytes, bomLength, bytes.length - bomLength)); return charBuffer.toString(); } return CharsetToolkit.bytesToString(bytes, EncodingRegistry.getInstance().getDefaultCharset()); }
@Override public String getCharset(@NotNull VirtualFile file, @NotNull final byte[] content) { Charset charset = EncodingRegistry.getInstance().getDefaultCharsetForPropertiesFiles(file); if (charset == null) { charset = CharsetToolkit.getDefaultSystemCharset(); } return charset.name(); }
@Nullable @Override public String getCharset(@NotNull VirtualFile virtualFile, @NotNull byte[] bytes) { Charset charset = EncodingRegistry.getInstance().getDefaultCharsetForPropertiesFiles(virtualFile); if (charset == null) { charset = CharsetToolkit.getDefaultSystemCharset(); } return charset.name(); }
@SuppressWarnings({"EmptyCatchBlock"}) @Nullable public Document getDocument() { if (myDocument == null) { if (isBinary()) return null; String text = null; try { if (myCharset == null) { text = CharsetToolkit.bytesToString(myBytes, EncodingRegistry.getInstance().getDefaultCharset()); } else { text = CharsetToolkit.bytesToString(myBytes, myCharset); } } catch (IllegalCharsetNameException e) { } // Still NULL? only if not supported or an exception was thrown. // Decode a string using the truly default encoding. if (text == null) text = new String(myBytes); text = LineTokenizer.correctLineSeparators(text); myDocument = EditorFactory.getInstance().createDocument(text); myDocument.setReadOnly(true); } return myDocument; }
/** * Moves this file to another directory. This method should be only called within write-action. * See {@link com.intellij.openapi.application.Application#runWriteAction}. * * @param requestor any object to control who called this method. Note that * it is considered to be an external change if <code>requestor</code> is <code>null</code>. * See {@link VirtualFileEvent#getRequestor} * @param newParent the directory to move this file to * @throws IOException if file failed to be moved */ public void move(final Object requestor, @NotNull final VirtualFile newParent) throws IOException { if (getFileSystem() != newParent.getFileSystem()) { throw new IOException(VfsBundle.message("file.move.error", newParent.getPresentableUrl())); } EncodingRegistry.doActionAndRestoreEncoding(this, new ThrowableComputable<VirtualFile, IOException>() { @Override public VirtualFile compute() throws IOException { getFileSystem().moveFile(requestor, VirtualFile.this, newParent); return VirtualFile.this; } }); }
/** * @return Retrieve the charset file has been loaded with (if loaded) and would be saved with (if would). */ public Charset getCharset() { Charset charset = getStoredCharset(); if (charset == null) { charset = EncodingRegistry.getInstance().getDefaultCharset(); setCharset(charset); } return charset; }
private static Charset detectCharset(@NotNull VirtualFile virtualFile, @NotNull byte[] content) { Charset charset = null; Trinity<Charset,CharsetToolkit.GuessedEncoding, byte[]> guessed = guessFromContent(virtualFile, content, content.length); if (guessed != null && guessed.first != null) { charset = guessed.first; } else { FileType fileType = virtualFile.getFileType(); String charsetName = fileType.getCharset(virtualFile, content); if (charsetName == null) { Charset specifiedExplicitly = EncodingRegistry.getInstance().getEncoding(virtualFile, true); if (specifiedExplicitly != null) { charset = specifiedExplicitly; } } else { charset = CharsetToolkit.forName(charsetName); } } charset = charset == null ? EncodingRegistry.getInstance().getDefaultCharset() : charset; if (EncodingRegistry.getInstance().isNative2Ascii(virtualFile)) { charset = Native2AsciiCharset.wrap(charset); } virtualFile.setCharset(charset); return charset; }
@Nullable("null means no luck, otherwise it's tuple(guessed encoding, hint about content if was unable to guess, BOM)") public static Trinity<Charset, CharsetToolkit.GuessedEncoding, byte[]> guessFromContent(@NotNull VirtualFile virtualFile, @NotNull byte[] content, int length) { EncodingRegistry settings = EncodingRegistry.getInstance(); boolean shouldGuess = settings != null && settings.isUseUTFGuessing(virtualFile); CharsetToolkit toolkit = shouldGuess ? new CharsetToolkit(content, EncodingRegistry.getInstance().getDefaultCharset()) : null; String detectedFromBytes = null; try { if (shouldGuess) { toolkit.setEnforce8Bit(true); Charset charset = toolkit.guessFromBOM(); if (charset != null) { detectedFromBytes = AUTO_DETECTED_FROM_BOM; byte[] bom = CharsetToolkit.getMandatoryBom(charset); if (bom == null) bom = CharsetToolkit.UTF8_BOM; return Trinity.create(charset, null, bom); } CharsetToolkit.GuessedEncoding guessed = toolkit.guessFromContent(length); if (guessed == CharsetToolkit.GuessedEncoding.VALID_UTF8) { detectedFromBytes = "auto-detected from bytes"; return Trinity.create(CharsetToolkit.UTF8_CHARSET, guessed, null); //UTF detected, ignore all directives } return Trinity.create(null, guessed,null); } return null; } finally { setCharsetWasDetectedFromBytes(virtualFile, detectedFromBytes); } }
private void loadFrom(@NotNull VirtualFile file) { try { final String text = CharsetToolkit.bytesToString(file.contentsToByteArray(), EncodingRegistry.getInstance().getDefaultCharset()); fillDocument(text); myChanged = false; } catch (IOException e) { Messages.showErrorDialog(e.getMessage(), "Cannot load file"); } }
@Nullable public String getContent() throws VcsException { if (myContent == null) { byte[] content = loadContent(); if (content != null) { final Charset charset = myLocalFile.getCharset(); myContent = charset == null ? CharsetToolkit.bytesToString(content, EncodingRegistry.getInstance().getDefaultCharset()) : CharsetToolkit.bytesToString(content, charset); } } return myContent; }
@Override public void move(final Object requestor, @Nonnull final VirtualFile newParent) throws IOException { if (!exists()) { throw new IOException("File to move does not exist: " + getPath()); } if (!newParent.exists()) { throw new IOException("Destination folder does not exist: " + newParent.getPath()); } if (!newParent.isDirectory()) { throw new IOException("Destination is not a folder: " + newParent.getPath()); } final VirtualFile child = newParent.findChild(getName()); if (child != null) { throw new IOException("Destination already exists: " + newParent.getPath() + "/" + getName()); } EncodingRegistry.doActionAndRestoreEncoding(this, new ThrowableComputable<VirtualFile, IOException>() { @Override public VirtualFile compute() throws IOException { getFileSystem().moveFile(requestor, NewVirtualFile.this, newParent); return NewVirtualFile.this; } }); }
public String stringFromBytes(@Nonnull byte[] bytes, @Nonnull String path) { try { VirtualFile file = findVirtualFile(path); if (file == null) { return CharsetToolkit.bytesToString(bytes, EncodingRegistry.getInstance().getDefaultCharset()); } return new String(bytes, file.getCharset().name()); } catch (UnsupportedEncodingException e1) { return new String(bytes); } }
/** * Moves this file to another directory. This method should be only called within write-action. * See {@link Application#runWriteAction}. * * @param requestor any object to control who called this method. Note that * it is considered to be an external change if <code>requestor</code> is <code>null</code>. * See {@link VirtualFileEvent#getRequestor} * @param newParent the directory to move this file to * @throws IOException if file failed to be moved */ public void move(final Object requestor, @Nonnull final VirtualFile newParent) throws IOException { if (getFileSystem() != newParent.getFileSystem()) { throw new IOException(VfsBundle.message("file.move.error", newParent.getPresentableUrl())); } EncodingRegistry.doActionAndRestoreEncoding(this, new ThrowableComputable<VirtualFile, IOException>() { @Override public VirtualFile compute() throws IOException { getFileSystem().moveFile(requestor, VirtualFile.this, newParent); return VirtualFile.this; } }); }
/** * @return Retrieve the charset file has been loaded with (if loaded) and would be saved with (if would). */ @Nonnull public Charset getCharset() { Charset charset = getStoredCharset(); if (charset == null) { charset = EncodingRegistry.getInstance().getDefaultCharset(); setCharset(charset); } return charset; }
@Override public VirtualFile copy(final Object requestor, @Nonnull final VirtualFile newParent, @Nonnull final String copyName) throws IOException { if (getFileSystem() != newParent.getFileSystem()) { throw new IOException(VfsBundle.message("file.copy.error", newParent.getPresentableUrl())); } if (!newParent.isDirectory()) { throw new IOException(VfsBundle.message("file.copy.target.must.be.directory")); } return EncodingRegistry.doActionAndRestoreEncoding(this, () -> ourPersistence.copyFile(requestor, this, newParent, copyName)); }
@Override public void move(final Object requestor, @Nonnull final VirtualFile newParent) throws IOException { if (getFileSystem() != newParent.getFileSystem()) { throw new IOException(VfsBundle.message("file.move.error", newParent.getPresentableUrl())); } EncodingRegistry.doActionAndRestoreEncoding(this, () -> { ourPersistence.moveFile(requestor, this, newParent); return this; }); }
@Nonnull private static Charset detectCharset(@Nonnull VirtualFile virtualFile, @Nonnull byte[] content, @Nonnull FileType fileType) { Charset charset = null; String charsetName = fileType.getCharset(virtualFile, content); if (charsetName != null) { charset = CharsetToolkit.forName(charsetName); } else { Trinity<Charset,CharsetToolkit.GuessedEncoding, byte[]> guessed = guessFromContent(virtualFile, content, content.length); Charset hardCodedCharset = guessed == null ? null : guessed.first; if (hardCodedCharset == null) { Charset specifiedExplicitly = EncodingRegistry.getInstance().getEncoding(virtualFile, true); if (specifiedExplicitly != null) { charset = specifiedExplicitly; } } else { charset = hardCodedCharset; } } if (charset == null) { charset = EncodingRegistry.getInstance().getDefaultCharset(); } virtualFile.setCharset(charset); return charset; }
private static String bytesToString(FilePath path, @Nonnull byte[] bytes) { Charset charset = null; if (path.getVirtualFile() != null) { charset = path.getVirtualFile().getCharset(); } if (charset != null) { int bomLength = CharsetToolkit.getBOMLength(bytes, charset); final CharBuffer charBuffer = charset.decode(ByteBuffer.wrap(bytes, bomLength, bytes.length - bomLength)); return charBuffer.toString(); } return CharsetToolkit.bytesToString(bytes, EncodingRegistry.getInstance().getDefaultCharset()); }
private void loadFrom(@Nonnull VirtualFile file) { try { final String text = CharsetToolkit.bytesToString(file.contentsToByteArray(), EncodingRegistry.getInstance().getDefaultCharset()); fillDocument(text); myChanged = false; } catch (IOException e) { Messages.showErrorDialog(e.getMessage(), "Cannot load file"); } }
/** * @param elementToCopy PsiFile or PsiDirectory * @param newName can be not null only if elements.length == 1 * @param choice a horrible way to pass/keep user preference * @return first copied PsiFile (recursively); null if no PsiFiles copied */ @Nullable public static PsiFile copyToDirectory(@NotNull PsiFileSystemItem elementToCopy, @Nullable String newName, @NotNull PsiDirectory targetDirectory, @Nullable int[] choice) throws IncorrectOperationException, IOException { if (elementToCopy instanceof PsiFile) { PsiFile file = (PsiFile)elementToCopy; String name = newName == null ? file.getName() : newName; if (checkFileExist(targetDirectory, choice, file, name, "Copy")) return null; return targetDirectory.copyFileFrom(name, file); } else if (elementToCopy instanceof PsiDirectory) { PsiDirectory directory = (PsiDirectory)elementToCopy; if (directory.equals(targetDirectory)) { return null; } if (newName == null) newName = directory.getName(); final PsiDirectory existing = targetDirectory.findSubdirectory(newName); final PsiDirectory subdirectory = existing == null ? targetDirectory.createSubdirectory(newName) : existing; EncodingRegistry.doActionAndRestoreEncoding(directory.getVirtualFile(), new ThrowableComputable<VirtualFile, IOException>() { @Override public VirtualFile compute() { return subdirectory.getVirtualFile(); } }); PsiFile firstFile = null; PsiElement[] children = directory.getChildren(); for (PsiElement child : children) { PsiFileSystemItem item = (PsiFileSystemItem)child; PsiFile f = copyToDirectory(item, item.getName(), subdirectory, choice); if (firstFile == null) { firstFile = f; } } return firstFile; } else { throw new IllegalArgumentException("unexpected elementToCopy: " + elementToCopy); } }
@Nullable public static Element read(byte[] bytes, @Nullable ErrorHandler handler) { return doRead(CharsetToolkit.bytesToString(bytes, EncodingRegistry.getInstance().getDefaultCharset()), handler); }
public CoreApplicationEnvironment(Disposable parentDisposable) { myParentDisposable = parentDisposable; Extensions.cleanRootArea(myParentDisposable); myFileTypeRegistry = new CoreFileTypeRegistry(); myEncodingRegistry = new CoreEncodingRegistry(); myApplication = createApplication(myParentDisposable); ApplicationManager.setApplication(myApplication, new StaticGetter<FileTypeRegistry>(myFileTypeRegistry), new StaticGetter<EncodingRegistry>(myEncodingRegistry), myParentDisposable); myLocalFileSystem = createLocalFileSystem(); myJarFileSystem = createJarFileSystem(); Extensions.registerAreaClass(ExtensionAreas.IDEA_PROJECT, null); final MutablePicoContainer appContainer = myApplication.getPicoContainer(); registerComponentInstance(appContainer, FileDocumentManager.class, new MockFileDocumentManagerImpl(new Function<CharSequence, Document>() { @Override public Document fun(CharSequence charSequence) { return new DocumentImpl(charSequence); } }, null)); VirtualFileSystem[] fs = {myLocalFileSystem, myJarFileSystem}; VirtualFileManagerImpl virtualFileManager = new VirtualFileManagerImpl(fs, new MessageBusImpl(myApplication, null)); registerComponentInstance(appContainer, VirtualFileManager.class, virtualFileManager); registerApplicationService(VirtualFilePointerManager.class, createVirtualFilePointerManager()); myApplication.registerService(DefaultASTFactory.class, new CoreASTFactory()); myApplication.registerService(PsiBuilderFactory.class, new PsiBuilderFactoryImpl()); myApplication.registerService(ReferenceProvidersRegistry.class, new MockReferenceProvidersRegistry()); myApplication.registerService(StubTreeLoader.class, new CoreStubTreeLoader()); myApplication.registerService(PsiReferenceService.class, new PsiReferenceServiceImpl()); myApplication.registerService(MetaDataRegistrar.class, new MetaRegistry()); registerApplicationExtensionPoint(ContentBasedFileSubstitutor.EP_NAME, ContentBasedFileSubstitutor.class); registerExtensionPoint(Extensions.getRootArea(), BinaryFileStubBuilders.EP_NAME, FileTypeExtensionPoint.class); registerExtensionPoint(Extensions.getRootArea(), FileContextProvider.EP_NAME, FileContextProvider.class); registerApplicationExtensionPoint(MetaDataContributor.EP_NAME, MetaDataContributor.class); ProgressIndicatorProvider.ourInstance = createProgressIndicatorProvider(); myApplication.registerService(JobLauncher.class, createJobLauncher()); }