/*
 * Copyright 2000-2006 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.
 */

/*
 * Created by IntelliJ IDEA.
 * User: yole
 * Date: 28.11.2006
 * Time: 17:48:18
 */
package org.jetbrains.idea.svn.history;

import com.intellij.openapi.vcs.changes.ContentRevision;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.peer.PeerFactory;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.svn.SvnBundle;
import org.jetbrains.idea.svn.SvnRevisionNumber;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntryPath;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.io.SVNRepository;

import java.io.OutputStream;
import java.io.File;
import java.io.ByteArrayOutputStream;

public class SvnContentRevision implements ContentRevision {
  private SVNRepository myRepository;
  private SVNLogEntryPath myLogEntryPath;
  private long myRevision;
  private String myContent;

  public SvnContentRevision(final SVNRepository repository, final SVNLogEntryPath logEntryPath, final long revision) {
    myLogEntryPath = logEntryPath;
    myRepository = repository;
    myRevision = revision;
  }

  @Nullable
  public String getContent() throws VcsException {
    if (myContent == null) {
      final String path = myLogEntryPath.getPath();
      final OutputStream buffer = new ByteArrayOutputStream();
      ContentLoader loader = new ContentLoader(path, buffer, myRevision);
      if (ApplicationManager.getApplication().isDispatchThread()) {
        ProgressManager.getInstance()
          .runProcessWithProgressSynchronously(loader, SvnBundle.message("progress.title.loading.file.content"), false, null);
      }
      else {
        loader.run();
      }
      final SVNException exception = loader.getException();
      if (exception != null) {
        throw new VcsException(exception);
      }
      myContent = buffer.toString();
    }
    return myContent;
  }

  @NotNull
  public FilePath getFile() {
    return PeerFactory.getInstance().getVcsContextFactory().createFilePathOn(new File(myLogEntryPath.getPath()));
  }

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

  private class ContentLoader implements Runnable {
    private String myPath;
    private long myRevision;
    private OutputStream myDst;
    private SVNException myException;

    public ContentLoader(String path, OutputStream dst, long revision) {
      myPath = path;
      myDst = dst;
      myRevision = revision;
    }

    public SVNException getException() {
      return myException;
    }

    public void run() {
      ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator();
      if (progress != null) {
        progress.setText(SvnBundle.message("progress.text.loading.contents", myPath));
        progress.setText2(SvnBundle.message("progress.text2.revision.information", myRevision));
      }
      try {
        myRepository.getFile(myPath, myRevision, null, myDst);
      }
      catch (SVNException e) {
        myException = e;
      }
    }
  }
}
@@ -756,35 +756,148 @@
 api.
-vcs.changes.ContentRevision
+application.ApplicationManager;%0Aimport com.intellij.openapi.progress.ProgressIndicator;%0Aimport com.intellij.openapi.progress.ProgressManager
 ;%0Aim
@@ -978,24 +978,81 @@
 sException;%0A
+import com.intellij.openapi.vcs.changes.ContentRevision;%0A
 import com.i
@@ -1122,42 +1122,24 @@
 lij.
-o
 pe
-napi.progress.ProgressIndica
+er.PeerFac
 tor
+y
 ;%0Aim
@@ -1147,151 +1147,41 @@
 ort 
-com.intellij.openapi.progress.ProgressManager;%0Aimport com.intellij.openapi.application.ApplicationManager;%0Aimport com.intellij.peer.PeerFactory
+org.jetbrains.annotations.NotNull
 ;%0Aim
@@ -1225,50 +1225,8 @@
 le;%0A
-import org.jetbrains.annotations.NotNull;%0A
 impo
@@ -1406,54 +1406,8 @@
 th;%0A
-import org.tmatesoft.svn.core.wc.SVNRevision;%0A
 impo
@@ -1454,57 +1454,91 @@
 ry;%0A
-%0A
 import 
-java.io.OutputStream;%0Aimport java.io.File
+org.tmatesoft.svn.core.wc.SVNRevision;%0A%0Aimport java.io.ByteArrayOutputStream
 ;%0Aim
@@ -1546,33 +1546,24 @@
 ort java.io.
-ByteArray
 OutputStream
@@ -2877,17 +2877,16 @@
 thOn
-(new File
+NonLocal
 (myL
@@ -2906,17 +2906,23 @@
 etPath()
-)
+, false
 );%0A  %7D%0A%0A

fix subversion repository path normalization (IDEADEV-10458)