/*
 * Copyright 2000-2007 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.idea.svn;

import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.ContentRevision;
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCClient;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

/**
 * @author yole
*/
class SvnContentRevision implements ContentRevision {
  private final SvnVcs myVcs;
  protected final FilePath myFile;
  private String myContent = null;
  private final SVNRevision myRevision;
  private boolean myUseBaseRevision;

  protected SvnContentRevision(SvnVcs vcs, @NotNull final FilePath file, final SVNRevision revision, final boolean useBaseRevision) {
    myVcs = vcs;
    myRevision = revision;
    myUseBaseRevision = useBaseRevision;
    myFile = file;
  }

  public static SvnContentRevision create(@NotNull SvnVcs vcs, @NotNull final FilePath file, final SVNRevision revision) {
    if (file.getFileType().isBinary()) {
      return new SvnBinaryContentRevision(vcs, file, revision, true);
    }
    return new SvnContentRevision(vcs, file, revision, true);
  }

  public static SvnContentRevision createRemote(@NotNull SvnVcs vcs, @NotNull final FilePath file, final SVNRevision revision) {
    if (file.getFileType().isBinary()) {
      return new SvnBinaryContentRevision(vcs, file, revision, false);
    }
    return new SvnContentRevision(vcs, file, revision, false);
  }

  @Nullable
  public String getContent() throws VcsException {
    if (myContent == null) {
      try {
        final byte[] content = getUpToDateBinaryContent();
        if (content != null) {
          myContent = new String(content, myFile.getCharset().name());
        }
      }
      catch(Exception ex) {
        throw new VcsException(ex);
      }
    }
    return myContent;
  }

  @Nullable
  protected byte[] getUpToDateBinaryContent() throws SVNException, IOException {
    File file = myFile.getIOFile();
    File lock = new File(file.getParentFile(), SvnUtil.PATH_TO_LOCK_FILE);
    if (lock.exists()) {
      return null;
    }
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    SVNWCClient wcClient = myVcs.createWCClient();
    wcClient.doGetFileContents(file, SVNRevision.UNDEFINED, myUseBaseRevision ? SVNRevision.BASE : myRevision, true, buffer);
    buffer.close();
    return buffer.toByteArray();
  }

  @NotNull
  public FilePath getFile() {
    return myFile;
  }

  @NotNull
  public VcsRevisionNumber getRevisionNumber() {
    return new SvnRevisionNumber(myRevision);
  }

  @NonNls
  public String toString() {
    return "SvnContentRevision:" + myFile.toString();
  }
}
@@ -1181,16 +1181,52 @@
 ception;
+%0Aimport java.lang.ref.SoftReference;
 %0A%0A/**%0A *
@@ -1371,22 +1371,37 @@
 private 
+SoftReference%3C
 String
+%3E
  myConte
@@ -1406,15 +1406,8 @@
 tent
- = null
 ;%0A  
@@ -2418,15 +2418,109 @@
     
-if (myC
+SoftReference%3CString%3E ref = myContent;%0A    String content = ref == null ? null : ref.get();%0A    if (c
 onte
@@ -2566,17 +2566,21 @@
  byte%5B%5D 
-c
+byteC
 ontent =
@@ -2616,25 +2616,29 @@
         if (
-c
+byteC
 ontent != nu
@@ -2653,19 +2653,17 @@
         
-myC
+c
 ontent =
@@ -2674,17 +2674,21 @@
  String(
-c
+byteC
 ontent, 
@@ -2716,16 +2716,74 @@
 ame());%0A
+          myContent = new SoftReference%3CString%3E(content);%0A
         
@@ -2881,19 +2881,17 @@
  return 
-myC
+c
 ontent;%0A

store SVN content on soft reference (IDEADEV-26561)