<commit_before>package com.intellij.openapi.roots.impl.storage;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PathMacroManager;
import com.intellij.openapi.components.StateStorage;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.project.impl.ProjectMacrosUtil;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.impl.ModuleRootManagerImpl;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileAdapter;
import com.intellij.openapi.vfs.VirtualFileEvent;
import com.intellij.openapi.vfs.tracker.VirtualFileTracker;
import com.intellij.util.io.fs.FileSystem;
import com.intellij.util.io.fs.IFile;
import com.intellij.util.messages.MessageBus;
import org.jdom.Element;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Vladislav.Kaznacheev
 * Date: Mar 9, 2007
 * Time: 1:42:06 PM
 */
public class ClasspathStorage implements StateStorage {

  @NonNls public static final String DEFAULT_STORAGE = "default";
  @NonNls public static final String SPECIAL_STORAGE = "special";

  public static final String DEFAULT_STORAGE_DESCR = ProjectBundle.message("project.roots.classpath.format.default.descr");

  @NonNls public static final String CLASSPATH_OPTION = "classpath";
  @NonNls public static final String CLASSPATH_DIR_OPTION = "classpath-dir";

  @NonNls private static final String COMPONENT_TAG = "component";
  private Object mySession;
  private ClasspathStorageProvider.ClasspathConverter myConverter;


  public ClasspathStorage(Module module) {
    myConverter = getProvider(getStorageType(module)).createConverter(module);
    final MessageBus messageBus = module.getMessageBus();
    final VirtualFileTracker virtualFileTracker = (VirtualFileTracker)module.getPicoContainer().getComponentInstanceOfType(VirtualFileTracker.class);
    if (virtualFileTracker != null && messageBus != null) {
      final ArrayList<VirtualFile> files = new ArrayList<VirtualFile>();
      myConverter.getFileSet().listFiles(files);
      for (VirtualFile file : files) {
        final Listener listener = messageBus.syncPublisher(StateStorage.STORAGE_TOPIC);
        virtualFileTracker.addTracker(VfsUtil.pathToUrl(file.getPath()), new VirtualFileAdapter() {
          public void contentsChanged(final VirtualFileEvent event) {
            listener.storageFileChanged(event, ClasspathStorage.this);
          }
        }, true, module);
      }
    }
  }

  private FileSet getFileSet() {
    return myConverter.getFileSet();
  }

  @Nullable
  public <T> T getState(final Object component, final String componentName, Class<T> stateClass, @Nullable T mergeInto)
    throws StateStorageException {
    assert component instanceof ModuleRootManager;
    assert componentName.equals("NewModuleRootManager");
    assert stateClass == ModuleRootManagerImpl.ModuleRootManagerState.class;

    try {
      final Module module = ((ModuleRootManagerImpl)component).getModule();
      final Element element = new Element(ClasspathStorage.COMPONENT_TAG);
      final ClasspathStorageProvider.Classpath classpath = myConverter.getClasspath(element);
      final Set<String> macros = classpath.getUsedMacros();
      final boolean macrosOk = ProjectMacrosUtil.checkMacros(module.getProject(), macros);
      PathMacroManager.getInstance(module).expandPaths(element);
      ModuleRootManagerImpl.ModuleRootManagerState moduleRootManagerState = new ModuleRootManagerImpl.ModuleRootManagerState();
      moduleRootManagerState.readExternal(element);
      if (!macrosOk) {
        throw new StateStorageException(ProjectBundle.message("project.load.undefined.path.variables.error"));
      }
      //noinspection unchecked
      return (T)moduleRootManagerState;
    }
    catch (InvalidDataException e) {
      throw new StateStorageException(e);
    }
    catch (IOException e) {
      throw new StateStorageException(e);
    }
  }

  public boolean hasState(final Object component, final String componentName, final Class<?> aClass)
    throws StateStorage.StateStorageException {
    return true;
  }

  public void setState(Object component, final String componentName, Object state) throws StateStorageException {
    assert component instanceof ModuleRootManager;
    assert componentName.equals("NewModuleRootManager");
    assert state.getClass() == ModuleRootManagerImpl.ModuleRootManagerState.class;

    try {
      Module module = ((ModuleRootManagerImpl)component).getModule();
      final Element element = new Element(ClasspathStorage.COMPONENT_TAG);
      ((ModuleRootManagerImpl.ModuleRootManagerState)state).writeExternal(element);
      PathMacroManager.getInstance(module).collapsePaths(element);
      myConverter.setClasspath(element);
    }
    catch (WriteExternalException e) {
      throw new StateStorageException(e);
    }
    catch (IOException e) {
      throw new StateStorageException(e);
    }
  }

  @NotNull
  public ExternalizationSession startExternalization() {
    final ExternalizationSession session = new ExternalizationSession() {
      public void setState(final Object component, final String componentName, final Object state, final Storage storageSpec)
        throws StateStorageException {
        assert mySession == this;
        ClasspathStorage.this.setState(component, componentName, state);
      }
    };

    mySession = session;
    return session;
  }

  @NotNull
  public SaveSession startSave(final ExternalizationSession externalizationSession) {
    assert mySession == externalizationSession;

    final SaveSession session = new SaveSession() {
      public boolean needsSave() throws StateStorageException {
        assert mySession == this;
        return ClasspathStorage.this.needsSave();
      }

      public void save() throws StateStorageException {
        assert mySession == this;
        ClasspathStorage.this.save();
      }

      public Set<String> getUsedMacros() {
        assert mySession == this;
        return Collections.EMPTY_SET;
      }

      @Nullable
      public Set<String> analyzeExternalChanges(final Set<Pair<VirtualFile,StateStorage>> changedFiles) {
        return null;
      }

      public Collection<IFile> getStorageFilesToSave() throws StateStorageException {
        return needsSave() ? getAllStorageFiles() : Collections.<IFile>emptyList();
      }

      public List<IFile> getAllStorageFiles() {
        final List<IFile> list = new ArrayList<IFile>();
        final ArrayList<VirtualFile> virtualFiles = new ArrayList<VirtualFile>();
        getFileSet().listFiles(virtualFiles);

        for (VirtualFile virtualFile : virtualFiles) {
          final File ioFile = VfsUtil.virtualToIoFile(virtualFile);
          if (ioFile == null) continue;
          list.add(FileSystem.FILE_SYSTEM.createFile(ioFile.getAbsolutePath()));
        }

        return list;
      }
    };

    mySession = session;
    return session;
  }

  public void finishSave(final SaveSession saveSession) {
    assert mySession == saveSession;
    mySession = null;
  }

  public void reload(final Set<String> changedComponents) throws StateStorage.StateStorageException {
    throw new UnsupportedOperationException("Method reload not implemented in " + getClass());
  }

  public boolean needsSave() throws StateStorageException {
    return getFileSet().hasChanged();
  }

  public void save() throws StateStorageException {
    final Ref<IOException> ref = new Ref<IOException>();
    ApplicationManager.getApplication().runWriteAction(new Runnable() {
      public void run() {
        try {
          getFileSet().commit();
        }
        catch (IOException e) {
          ref.set(e);
        }
      }
    });

    if (!ref.isNull()) {
      throw new StateStorageException(ref.get());
    }
  }

  public static ClasspathStorageProvider getProvider(final String type) {
    for (ClasspathStorageProvider provider : getProviders()) {
      if (type.equals(provider.getID())) {
        return provider;
      }<commit_msg>removed throws<commit_after>package com.intellij.openapi.roots.impl.storage;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PathMacroManager;
import com.intellij.openapi.components.StateStorage;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.project.impl.ProjectMacrosUtil;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.impl.ModuleRootManagerImpl;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileAdapter;
import com.intellij.openapi.vfs.VirtualFileEvent;
import com.intellij.openapi.vfs.tracker.VirtualFileTracker;
import com.intellij.util.io.fs.FileSystem;
import com.intellij.util.io.fs.IFile;
import com.intellij.util.messages.MessageBus;
import org.jdom.Element;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Vladislav.Kaznacheev
 * Date: Mar 9, 2007
 * Time: 1:42:06 PM
 */
public class ClasspathStorage implements StateStorage {

  @NonNls public static final String DEFAULT_STORAGE = "default";
  @NonNls public static final String SPECIAL_STORAGE = "special";

  public static final String DEFAULT_STORAGE_DESCR = ProjectBundle.message("project.roots.classpath.format.default.descr");

  @NonNls public static final String CLASSPATH_OPTION = "classpath";
  @NonNls public static final String CLASSPATH_DIR_OPTION = "classpath-dir";

  @NonNls private static final String COMPONENT_TAG = "component";
  private Object mySession;
  private ClasspathStorageProvider.ClasspathConverter myConverter;


  public ClasspathStorage(Module module) {
    myConverter = getProvider(getStorageType(module)).createConverter(module);
    final MessageBus messageBus = module.getMessageBus();
    final VirtualFileTracker virtualFileTracker = (VirtualFileTracker)module.getPicoContainer().getComponentInstanceOfType(VirtualFileTracker.class);
    if (virtualFileTracker != null && messageBus != null) {
      final ArrayList<VirtualFile> files = new ArrayList<VirtualFile>();
      myConverter.getFileSet().listFiles(files);
      for (VirtualFile file : files) {
        final Listener listener = messageBus.syncPublisher(StateStorage.STORAGE_TOPIC);
        virtualFileTracker.addTracker(VfsUtil.pathToUrl(file.getPath()), new VirtualFileAdapter() {
          public void contentsChanged(final VirtualFileEvent event) {
            listener.storageFileChanged(event, ClasspathStorage.this);
          }
        }, true, module);
      }
    }
  }

  private FileSet getFileSet() {
    return myConverter.getFileSet();
  }

  @Nullable
  public <T> T getState(final Object component, final String componentName, Class<T> stateClass, @Nullable T mergeInto)
    throws StateStorageException {
    assert component instanceof ModuleRootManager;
    assert componentName.equals("NewModuleRootManager");
    assert stateClass == ModuleRootManagerImpl.ModuleRootManagerState.class;

    try {
      final Module module = ((ModuleRootManagerImpl)component).getModule();
      final Element element = new Element(ClasspathStorage.COMPONENT_TAG);
      final ClasspathStorageProvider.Classpath classpath = myConverter.getClasspath(element);
      final Set<String> macros = classpath.getUsedMacros();
      final boolean macrosOk = ProjectMacrosUtil.checkMacros(module.getProject(), macros);
      PathMacroManager.getInstance(module).expandPaths(element);
      ModuleRootManagerImpl.ModuleRootManagerState moduleRootManagerState = new ModuleRootManagerImpl.ModuleRootManagerState();
      moduleRootManagerState.readExternal(element);
      if (!macrosOk) {
        throw new StateStorageException(ProjectBundle.message("project.load.undefined.path.variables.error"));
      }
      //noinspection unchecked
      return (T)moduleRootManagerState;
    }
    catch (InvalidDataException e) {
      throw new StateStorageException(e);
    }
    catch (IOException e) {
      throw new StateStorageException(e);
    }
  }

  public boolean hasState(final Object component, final String componentName, final Class<?> aClass)
    throws StateStorage.StateStorageException {
    return true;
  }

  public void setState(Object component, final String componentName, Object state) throws StateStorageException {
    assert component instanceof ModuleRootManager;
    assert componentName.equals("NewModuleRootManager");
    assert state.getClass() == ModuleRootManagerImpl.ModuleRootManagerState.class;

    try {
      Module module = ((ModuleRootManagerImpl)component).getModule();
      final Element element = new Element(ClasspathStorage.COMPONENT_TAG);
      ((ModuleRootManagerImpl.ModuleRootManagerState)state).writeExternal(element);
      PathMacroManager.getInstance(module).collapsePaths(element);
      myConverter.setClasspath(element);
    }
    catch (WriteExternalException e) {
      throw new StateStorageException(e);
    }
    catch (IOException e) {
      throw new StateStorageException(e);
    }
  }

  @NotNull
  public ExternalizationSession startExternalization() {
    final ExternalizationSession session = new ExternalizationSession() {
      public void setState(final Object component, final String componentName, final Object state, final Storage storageSpec)
        throws StateStorageException {
        assert mySession == this;
        ClasspathStorage.this.setState(component, componentName, state);
      }
    };

    mySession = session;
    return session;
  }

  @NotNull
  public SaveSession startSave(final ExternalizationSession externalizationSession) {
    assert mySession == externalizationSession;

    final SaveSession session = new SaveSession() {
      public boolean needsSave() throws StateStorageException {
        assert mySession == this;
        return ClasspathStorage.this.needsSave();
      }

      public void save() throws StateStorageException {
        assert mySession == this;
        ClasspathStorage.this.save();
      }

      public Set<String> getUsedMacros() {
        assert mySession == this;
        return Collections.EMPTY_SET;
      }

      @Nullable
      public Set<String> analyzeExternalChanges(final Set<Pair<VirtualFile,StateStorage>> changedFiles) {
        return null;
      }

      public Collection<IFile> getStorageFilesToSave() throws StateStorageException {
        return needsSave() ? getAllStorageFiles() : Collections.<IFile>emptyList();
      }

      public List<IFile> getAllStorageFiles() {
        final List<IFile> list = new ArrayList<IFile>();
        final ArrayList<VirtualFile> virtualFiles = new ArrayList<VirtualFile>();
        getFileSet().listFiles(virtualFiles);

        for (VirtualFile virtualFile : virtualFiles) {
          final File ioFile = VfsUtil.virtualToIoFile(virtualFile);
          if (ioFile == null) continue;
          list.add(FileSystem.FILE_SYSTEM.createFile(ioFile.getAbsolutePath()));
        }

        return list;
      }
    };

    mySession = session;
    return session;
  }

  public void finishSave(final SaveSession saveSession) {
    assert mySession == saveSession;
    mySession = null;
  }

  public void reload(final Set<String> changedComponents) throws StateStorage.StateStorageException {
  }

  public boolean needsSave() throws StateStorageException {
    return getFileSet().hasChanged();
  }

  public void save() throws StateStorageException {
    final Ref<IOException> ref = new Ref<IOException>();
    ApplicationManager.getApplication().runWriteAction(new Runnable() {
      public void run() {
        try {
          getFileSet().commit();
        }
        catch (IOException e) {
          ref.set(e);
        }
      }
    });

    if (!ref.isNull()) {
      throw new StateStorageException(ref.get());
    }
  }

  public static ClasspathStorageProvider getProvider(final String type) {
    for (ClasspathStorageProvider provider : getProviders()) {
      if (type.equals(provider.getID())) {
        return provider;
      }
{'fraction_non_alphanumeric': 0.09182290490173933, 'fraction_numerical': 0.00112943302462164, 'mean_word_length': 4.150959860383944, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4741158', 'n_tokens_mistral': 4850, 'n_tokens_neox': 4746, 'n_words': 1031}