/platform/vcs-api/src/com/intellij/openapi/vcs/vfs/VcsVirtualFile.java

https://bitbucket.org/nbargnesi/idea · Java · 146 lines · 109 code · 19 blank · 18 comment · 12 complexity · 985b33452f92020edac1c10555481d96 MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.openapi.vcs.vfs;
  17. import com.intellij.openapi.application.ApplicationManager;
  18. import com.intellij.openapi.diagnostic.Logger;
  19. import com.intellij.openapi.progress.ProcessCanceledException;
  20. import com.intellij.openapi.ui.Messages;
  21. import com.intellij.openapi.vcs.VcsBundle;
  22. import com.intellij.openapi.vcs.VcsException;
  23. import com.intellij.openapi.vcs.history.ShortVcsRevisionNumber;
  24. import com.intellij.openapi.vcs.history.VcsFileRevision;
  25. import com.intellij.openapi.vcs.history.VcsRevisionNumber;
  26. import com.intellij.openapi.vfs.CharsetToolkit;
  27. import com.intellij.openapi.vfs.VirtualFileSystem;
  28. import com.intellij.util.ArrayUtil;
  29. import org.jetbrains.annotations.NotNull;
  30. import java.io.IOException;
  31. import java.nio.charset.Charset;
  32. /**
  33. * author: lesya
  34. */
  35. public class VcsVirtualFile extends AbstractVcsVirtualFile {
  36. private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.vcs.vfs.VcsVirtualFile");
  37. private byte[] myContent;
  38. private final VcsFileRevision myFileRevision;
  39. private boolean myContentLoadFailed = false;
  40. private Charset myCharset;
  41. public VcsVirtualFile(String path,
  42. VcsFileRevision revision, VirtualFileSystem fileSystem) {
  43. super(path, fileSystem);
  44. myFileRevision = revision;
  45. }
  46. public VcsVirtualFile(String path,
  47. byte[] content,
  48. String revision, VirtualFileSystem fileSystem) {
  49. this(path, null, fileSystem);
  50. myContent = content;
  51. setRevision(revision);
  52. }
  53. @NotNull
  54. public byte[] contentsToByteArray() throws IOException {
  55. if (myContentLoadFailed || myProcessingBeforeContentsChange) {
  56. return ArrayUtil.EMPTY_BYTE_ARRAY;
  57. }
  58. if (myContent == null) {
  59. loadContent();
  60. }
  61. return myContent;
  62. }
  63. private void loadContent() throws IOException {
  64. if (myContent != null) return;
  65. final VcsFileSystem vcsFileSystem = ((VcsFileSystem)getFileSystem());
  66. try {
  67. myFileRevision.loadContent();
  68. fireBeforeContentsChange();
  69. myModificationStamp++;
  70. final VcsRevisionNumber revisionNumber = myFileRevision.getRevisionNumber();
  71. if (revisionNumber instanceof ShortVcsRevisionNumber) {
  72. setRevision(((ShortVcsRevisionNumber) revisionNumber).toShortString());
  73. }
  74. else {
  75. setRevision(revisionNumber.asString());
  76. }
  77. myContent = myFileRevision.getContent();
  78. myCharset = new CharsetToolkit(myContent).guessEncoding(myContent.length);
  79. ApplicationManager.getApplication().runWriteAction(new Runnable() {
  80. public void run() {
  81. vcsFileSystem.fireContentsChanged(this, VcsVirtualFile.this, 0);
  82. }
  83. });
  84. }
  85. catch (VcsException e) {
  86. myContentLoadFailed = true;
  87. ApplicationManager.getApplication().runWriteAction(new Runnable() {
  88. public void run() {
  89. vcsFileSystem.fireBeforeFileDeletion(this, VcsVirtualFile.this);
  90. }
  91. });
  92. myContent = ArrayUtil.EMPTY_BYTE_ARRAY;
  93. setRevision("0");
  94. Messages.showMessageDialog(
  95. VcsBundle.message("message.text.could.not.load.virtual.file.content", getPresentableUrl(), e.getLocalizedMessage()),
  96. VcsBundle.message("message.title.could.not.load.content"),
  97. Messages.getInformationIcon());
  98. ApplicationManager.getApplication().runWriteAction(new Runnable() {
  99. public void run() {
  100. vcsFileSystem.fireFileDeleted(this, VcsVirtualFile.this, getName(), getParent());
  101. }
  102. });
  103. }
  104. catch (ProcessCanceledException ex) {
  105. myContent = null;
  106. }
  107. }
  108. @Override
  109. public Charset getCharset() {
  110. if (myCharset != null) return myCharset;
  111. return super.getCharset();
  112. }
  113. public boolean isDirectory() {
  114. return false;
  115. }
  116. public String getRevision() {
  117. if (myRevision == null) {
  118. try {
  119. loadContent();
  120. }
  121. catch (IOException e) {
  122. LOG.info(e);
  123. }
  124. }
  125. return myRevision;
  126. }
  127. }