<commit_before>package com.intellij.openapi.module.impl;

import com.intellij.application.options.ExpandMacroToPathMap;
import com.intellij.application.options.PathMacroMap;
import com.intellij.application.options.PathMacros;
import com.intellij.application.options.ReplacePathToMacroMap;
import com.intellij.ide.plugins.PluginDescriptor;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.application.ex.DecodeDefaultsUtil;
import com.intellij.openapi.components.BaseComponent;
import com.intellij.openapi.components.impl.ComponentManagerImpl;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleComponent;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.module.ModuleTypeManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.impl.BaseFileConfigurable;
import com.intellij.openapi.project.impl.ProjectImpl;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.*;
import com.intellij.openapi.vfs.pointers.VirtualFilePointer;
import com.intellij.openapi.vfs.pointers.VirtualFilePointerManager;
import com.intellij.pom.PomModel;
import com.intellij.pom.PomModule;
import com.intellij.pom.core.impl.PomModuleImpl;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;

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

/**
 * @author max
 */
public class ModuleImpl extends BaseFileConfigurable implements Module {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.module.impl.ModuleImpl");

  private final Project myProject;
  private VirtualFilePointer myFilePointer;
  private ModuleType myModuleType = null;
  private boolean myIsDisposed = false;
  private MyVirtualFileListener myVirtualFileListener;
  private boolean isModuleAdded;
  private Map<String, String> myOptions = new HashMap<String, String>();

  private static final String MODULE_LAYER = "module-components";
  private PomModule myPomModule;
  private PomModel myPomModel;

  public ModuleImpl(String filePath, Project project, PomModel pomModel, PathMacros pathMacros) {
    super(false, pathMacros);
    myProject = project;
    myPomModel =  pomModel;
    Extensions.instantiateArea("IDEA_MODULE", this, project);
    init(filePath);
  }

  private void init(String filePath) {
    getPicoContainer().registerComponentInstance(Module.class, this);
    myPomModule = new PomModuleImpl(myPomModel, this);

    LOG.assertTrue(filePath != null);
    String path = filePath.replace(File.separatorChar, '/');
    String url = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, path);
    myFilePointer = VirtualFilePointerManager.getInstance().create(url, null);
    myVirtualFileListener = new MyVirtualFileListener();
    VirtualFileManager.getInstance().addVirtualFileListener(myVirtualFileListener);
  }

  protected void initJdomExternalizable(Class componentClass, BaseComponent component) {
    if (((ProjectImpl)myProject).isOptimiseTestLoadSpeed()) return; //test load speed optimization
    doInitJdomExternalizable(componentClass, component);
  }

  public void loadModuleComponents() {
    loadComponentsConfiguration(MODULE_LAYER);

    if (PluginManager.shouldLoadPlugins()) {
      final PluginDescriptor[] plugins = PluginManager.getPlugins();
      for (int i = 0; i < plugins.length; i++) {
        PluginDescriptor plugin = plugins[i];
        if (!PluginManager.shouldLoadPlugin(plugin)) continue;

        final Element moduleComponents = plugin.getModuleComponents();
        if (moduleComponents != null) {
          loadComponentsConfiguration(moduleComponents, plugin);
        }
      }
    }
  }

  protected boolean isComponentSuitable(Map<String,String> options) {
    if (!super.isComponentSuitable(options)) return false;
    if (options == null) return true;

    Set<String> optionNames = options.keySet();
    for (Iterator<String> iterator = optionNames.iterator(); iterator.hasNext();) {
      String optionName = iterator.next();
      if (Comparing.equal("workspace", optionName)) continue;
      if (!parseOptionValue(options.get(optionName)).contains(getOptionValue(optionName))) return false;
    }

    return true;
  }

  private List<String> parseOptionValue(String optionValue) {
    if (optionValue == null) return new ArrayList<String>(0);
    return Arrays.asList(optionValue.split(";"));
  }

  protected Element getDefaults(BaseComponent component) throws IOException, JDOMException, InvalidDataException {
    //[mike] speed optimization. If you need default initialization in tests
    //use field initializers instead.
    if (((ProjectImpl)myProject).isOptimiseTestLoadSpeed()) return null;

    InputStream stream = DecodeDefaultsUtil.getDefaultsInputStream(component);
    if (stream != null) {
      Document document = JDOMUtil.loadDocument(stream);
      stream.close();

      if (document == null) {
        throw new InvalidDataException();
      }
      Element root = document.getRootElement();
      if (root == null || !"component".equals(root.getName())) {
        throw new InvalidDataException();
      }

      //TODO:
      //JDOMUtil.replaceText(root, getExpandMacroReplacements(), SystemInfo.isFileSystemCaseSensitive);

      return root;
    }
    return null;
  }

  protected ComponentManagerImpl getParentComponentManager() {
    return (ComponentManagerImpl)myProject;
  }

  public VirtualFile getModuleFile() {
    return myFilePointer.getFile();
  }

  void rename(String newName) {
    final VirtualFile file = myFilePointer.getFile();
    try {
      if (file != null) {
        file.rename(ModuleUtil.MODULE_RENAMING_REQUESTOR, newName + ".iml");
        return;
      }
    }
    catch (IOException e) {
    }
    // [dsl] we get here if either old file didn't exist or renaming failed
    final File oldFile = new File(getModuleFilePath());
    final File parentFile = oldFile.getParentFile();
    final File newFile = new File(parentFile, newName + ".iml");
    final String newUrl = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, newFile.getPath().replace(File.separatorChar, '/'));
    myFilePointer = VirtualFilePointerManager.getInstance().create(newUrl, null);
  }

  public String getModuleFilePath() {
    String url = myFilePointer.getUrl();
    String protocol = VirtualFileManager.extractProtocol(url);
    LOG.assertTrue(LocalFileSystem.PROTOCOL.equals(protocol));
    String path = VirtualFileManager.extractPath(url);
    return path.replace('/', File.separatorChar);
  }

  public void dispose() {
    isModuleAdded = false;
    disposeComponents();
    myIsDisposed = true;
    VirtualFileManager.getInstance().removeVirtualFileListener(myVirtualFileListener);
    Extensions.disposeArea(this);
  }

  public VirtualFile[] getConfigurationFiles() {
    final VirtualFile moduleFile = getModuleFile();
    return moduleFile == null? VirtualFile.EMPTY_ARRAY : new VirtualFile[]{moduleFile};
  }

  public String[] getConfigurationFilePaths() {
    return new String[]{getModuleFilePath()};
  }

  protected String getRootNodeName() {
    return "module";
  }

  protected VirtualFile getComponentConfigurationFile(Class componentInterface) {
    return getModuleFile();
  }

  public ReplacePathToMacroMap getMacroReplacements() {
    ReplacePathToMacroMap result = new ReplacePathToMacroMap();
    getModuleHomeReplacements(result, false);
    result.putAll(super.getMacroReplacements());
    getModuleHomeReplacements(result, mySavePathsRelative);<commit_msg>Added cleanup.<commit_after>package com.intellij.openapi.module.impl;

import com.intellij.application.options.ExpandMacroToPathMap;
import com.intellij.application.options.PathMacroMap;
import com.intellij.application.options.PathMacros;
import com.intellij.application.options.ReplacePathToMacroMap;
import com.intellij.ide.plugins.PluginDescriptor;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.application.ex.DecodeDefaultsUtil;
import com.intellij.openapi.components.BaseComponent;
import com.intellij.openapi.components.impl.ComponentManagerImpl;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleComponent;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.module.ModuleTypeManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.impl.BaseFileConfigurable;
import com.intellij.openapi.project.impl.ProjectImpl;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.*;
import com.intellij.openapi.vfs.pointers.VirtualFilePointer;
import com.intellij.openapi.vfs.pointers.VirtualFilePointerManager;
import com.intellij.pom.PomModel;
import com.intellij.pom.PomModule;
import com.intellij.pom.core.impl.PomModuleImpl;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;

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

/**
 * @author max
 */
public class ModuleImpl extends BaseFileConfigurable implements Module {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.module.impl.ModuleImpl");

  private final Project myProject;
  private VirtualFilePointer myFilePointer;
  private ModuleType myModuleType = null;
  private boolean myIsDisposed = false;
  private MyVirtualFileListener myVirtualFileListener;
  private boolean isModuleAdded;
  private Map<String, String> myOptions = new HashMap<String, String>();

  private static final String MODULE_LAYER = "module-components";
  private PomModule myPomModule;
  private PomModel myPomModel;

  public ModuleImpl(String filePath, Project project, PomModel pomModel, PathMacros pathMacros) {
    super(false, pathMacros);
    myProject = project;
    myPomModel =  pomModel;
    Extensions.instantiateArea("IDEA_MODULE", this, project);
    init(filePath);
  }

  private void init(String filePath) {
    getPicoContainer().registerComponentInstance(Module.class, this);
    myPomModule = new PomModuleImpl(myPomModel, this);

    LOG.assertTrue(filePath != null);
    String path = filePath.replace(File.separatorChar, '/');
    String url = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, path);
    myFilePointer = VirtualFilePointerManager.getInstance().create(url, null);
    myVirtualFileListener = new MyVirtualFileListener();
    VirtualFileManager.getInstance().addVirtualFileListener(myVirtualFileListener);
  }

  protected void initJdomExternalizable(Class componentClass, BaseComponent component) {
    if (((ProjectImpl)myProject).isOptimiseTestLoadSpeed()) return; //test load speed optimization
    doInitJdomExternalizable(componentClass, component);
  }

  public void loadModuleComponents() {
    loadComponentsConfiguration(MODULE_LAYER);

    if (PluginManager.shouldLoadPlugins()) {
      final PluginDescriptor[] plugins = PluginManager.getPlugins();
      for (int i = 0; i < plugins.length; i++) {
        PluginDescriptor plugin = plugins[i];
        if (!PluginManager.shouldLoadPlugin(plugin)) continue;

        final Element moduleComponents = plugin.getModuleComponents();
        if (moduleComponents != null) {
          loadComponentsConfiguration(moduleComponents, plugin);
        }
      }
    }
  }

  protected boolean isComponentSuitable(Map<String,String> options) {
    if (!super.isComponentSuitable(options)) return false;
    if (options == null) return true;

    Set<String> optionNames = options.keySet();
    for (Iterator<String> iterator = optionNames.iterator(); iterator.hasNext();) {
      String optionName = iterator.next();
      if (Comparing.equal("workspace", optionName)) continue;
      if (!parseOptionValue(options.get(optionName)).contains(getOptionValue(optionName))) return false;
    }

    return true;
  }

  private List<String> parseOptionValue(String optionValue) {
    if (optionValue == null) return new ArrayList<String>(0);
    return Arrays.asList(optionValue.split(";"));
  }

  protected Element getDefaults(BaseComponent component) throws IOException, JDOMException, InvalidDataException {
    //[mike] speed optimization. If you need default initialization in tests
    //use field initializers instead.
    if (((ProjectImpl)myProject).isOptimiseTestLoadSpeed()) return null;

    InputStream stream = DecodeDefaultsUtil.getDefaultsInputStream(component);
    if (stream != null) {
      Document document = JDOMUtil.loadDocument(stream);
      stream.close();

      if (document == null) {
        throw new InvalidDataException();
      }
      Element root = document.getRootElement();
      if (root == null || !"component".equals(root.getName())) {
        throw new InvalidDataException();
      }

      //TODO:
      //JDOMUtil.replaceText(root, getExpandMacroReplacements(), SystemInfo.isFileSystemCaseSensitive);

      return root;
    }
    return null;
  }

  protected ComponentManagerImpl getParentComponentManager() {
    return (ComponentManagerImpl)myProject;
  }

  public VirtualFile getModuleFile() {
    return myFilePointer.getFile();
  }

  void rename(String newName) {
    final VirtualFile file = myFilePointer.getFile();
    try {
      if (file != null) {
        file.rename(ModuleUtil.MODULE_RENAMING_REQUESTOR, newName + ".iml");
        return;
      }
    }
    catch (IOException e) {
    }
    // [dsl] we get here if either old file didn't exist or renaming failed
    final File oldFile = new File(getModuleFilePath());
    final File parentFile = oldFile.getParentFile();
    final File newFile = new File(parentFile, newName + ".iml");
    final String newUrl = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, newFile.getPath().replace(File.separatorChar, '/'));
    myFilePointer = VirtualFilePointerManager.getInstance().create(newUrl, null);
  }

  public String getModuleFilePath() {
    String url = myFilePointer.getUrl();
    String protocol = VirtualFileManager.extractProtocol(url);
    LOG.assertTrue(LocalFileSystem.PROTOCOL.equals(protocol));
    String path = VirtualFileManager.extractPath(url);
    return path.replace('/', File.separatorChar);
  }

  public void dispose() {
    isModuleAdded = false;
    disposeComponents();
    Extensions.disposeArea(this);
    myIsDisposed = true;
    VirtualFileManager.getInstance().removeVirtualFileListener(myVirtualFileListener);
    Extensions.disposeArea(this);
  }

  public VirtualFile[] getConfigurationFiles() {
    final VirtualFile moduleFile = getModuleFile();
    return moduleFile == null? VirtualFile.EMPTY_ARRAY : new VirtualFile[]{moduleFile};
  }

  public String[] getConfigurationFilePaths() {
    return new String[]{getModuleFilePath()};
  }

  protected String getRootNodeName() {
    return "module";
  }

  protected VirtualFile getComponentConfigurationFile(Class componentInterface) {
    return getModuleFile();
  }

  public ReplacePathToMacroMap getMacroReplacements() {
    ReplacePathToMacroMap result = new ReplacePathToMacroMap();
    getModuleHomeReplacements(result, false);
    result.putAll(super.getMacroReplacements());
    getModuleHomeReplacements(result, mySavePathsRelative);
{'fraction_non_alphanumeric': 0.10256898192197907, 'fraction_numerical': 0.0002537266095781795, 'mean_word_length': 4.739352020385875, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, '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': '3398259', 'n_tokens_mistral': 4497, 'n_tokens_neox': 4405, 'n_words': 921}