<commit_before>package com.intellij.openapi.components.impl.stores;

import com.intellij.openapi.components.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.*;
import com.intellij.util.DOMUtil;
import com.intellij.util.ReflectionCache;
import com.intellij.util.ReflectionUtil;
import com.intellij.util.xmlb.SerializationFilter;
import com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters;
import com.intellij.util.xmlb.XmlSerializer;
import org.jdom.Element;
import org.jetbrains.annotations.Nullable;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@SuppressWarnings({"deprecation"})
abstract class ComponentStoreImpl implements IComponentStore {

  private static final Logger LOG = Logger.getInstance("#com.intellij.components.ComponentStoreImpl");
  private Map<String, Object> myComponents = new TreeMap<String, Object>();
  private SerializationFilter mySerializationFilter = new SkipDefaultValuesSerializationFilters();
  private List<SettingsSavingComponent> mySettingsSavingComponents = new ArrayList<SettingsSavingComponent>();

  protected StateStorage getStateStorage(final Storage storageSpec) {
    throw new UnsupportedOperationException("Method getStateStorage is not supported in " + getClass());
  }

  protected StateStorage getOldStorage(final Object component) {
    throw new UnsupportedOperationException("Method getOldStorage is not supported in " + getClass());
  }

  protected StateStorage getDefaultsStorage() {
    throw new UnsupportedOperationException("Method getDefaultsStorage is not supported in " + getClass());
  }

  public void initComponent(final Object component) {
    if (component instanceof JDOMExternalizable) {
      initJdomExternalizable((JDOMExternalizable)component);
    }
    else if (component instanceof PersistentStateComponent) {
      initPersistentComponent((PersistentStateComponent<Object>)component);
    }
    else if (component instanceof SettingsSavingComponent) {
      SettingsSavingComponent settingsSavingComponent = (SettingsSavingComponent)component;
      mySettingsSavingComponents.add(settingsSavingComponent);
    }
  }


  public final void save() throws IOException {
    try {
      try {
        beforeSave();
      }
      catch (SaveCancelledException e) {
        return;
      }

      try {
        try {
          doSave();
        }
        catch (SaveCancelledException e) {
          return;
        }
      }
      finally {
        afterSave();
      }
    }
    catch (StateStorage.StateStorageException e) {
      LOG.info(e);
      throw new IOException(e.getMessage());
    }
  }


  protected void beforeSave() throws StateStorage.StateStorageException, SaveCancelledException {
    ShutDownTracker.getInstance().registerStopperThread(Thread.currentThread());
  }

  protected void afterSave() {
    ShutDownTracker.getInstance().unregisterStopperThread(Thread.currentThread());
  }

  protected void doSave() throws IOException {
    commit();
  }


  public void commit() {
    for (SettingsSavingComponent settingsSavingComponent : mySettingsSavingComponents) {
      settingsSavingComponent.save();
    }

    for (String name : myComponents.keySet()) {
      Object component = myComponents.get(name);
      if (component instanceof JDOMExternalizable) {
        saveJdomExternalizable((JDOMExternalizable)component);
      }
      else if (component instanceof PersistentStateComponent) {
        savePersistentComponent((PersistentStateComponent<Object>)component);
      }
    }
  }

  private void savePersistentComponent(final PersistentStateComponent<Object> persistentStateComponent) {
    try {
      Storage storageSpec = getComponentStorage(persistentStateComponent);
      StateStorage stateStorage = getStateStorage(storageSpec);

      if (stateStorage == null) return;

      org.w3c.dom.Element elementState = XmlSerializer.serialize(persistentStateComponent.getState(), DOMUtil.createDocument(), mySerializationFilter);
      stateStorage.setState(persistentStateComponent, getComponentName(persistentStateComponent), elementState);
    }
    catch (ParserConfigurationException e) {
      LOG.error(e);
    }
    catch (StateStorage.StateStorageException e) {
      LOG.error(e);
    }
  }

  private void saveJdomExternalizable(final JDOMExternalizable component) {
    final String componentName = ((BaseComponent)component).getComponentName();
    StateStorage stateStorage = getOldStorage(component);

    try {
      final Element element = new Element("temp_element");
      component.writeExternal(element);
      final org.w3c.dom.Element domElement = JDOMUtil.convertToDOM(element);
      stateStorage.setState(component, componentName, domElement);
    }
    catch (WriteExternalException ex) {<commit_msg>faster externalization for JDOMExternalizable components<commit_after>package com.intellij.openapi.components.impl.stores;

import com.intellij.openapi.components.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.*;
import com.intellij.util.DOMUtil;
import com.intellij.util.ReflectionCache;
import com.intellij.util.ReflectionUtil;
import com.intellij.util.xmlb.SerializationFilter;
import com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters;
import com.intellij.util.xmlb.XmlSerializer;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.DOMOutputter;
import org.jetbrains.annotations.Nullable;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@SuppressWarnings({"deprecation"})
abstract class ComponentStoreImpl implements IComponentStore {

  private static final Logger LOG = Logger.getInstance("#com.intellij.components.ComponentStoreImpl");
  private Map<String, Object> myComponents = new TreeMap<String, Object>();
  private SerializationFilter mySerializationFilter = new SkipDefaultValuesSerializationFilters();
  private List<SettingsSavingComponent> mySettingsSavingComponents = new ArrayList<SettingsSavingComponent>();

  protected StateStorage getStateStorage(final Storage storageSpec) {
    throw new UnsupportedOperationException("Method getStateStorage is not supported in " + getClass());
  }

  protected StateStorage getOldStorage(final Object component) {
    throw new UnsupportedOperationException("Method getOldStorage is not supported in " + getClass());
  }

  protected StateStorage getDefaultsStorage() {
    throw new UnsupportedOperationException("Method getDefaultsStorage is not supported in " + getClass());
  }

  public void initComponent(final Object component) {
    if (component instanceof JDOMExternalizable) {
      initJdomExternalizable((JDOMExternalizable)component);
    }
    else if (component instanceof PersistentStateComponent) {
      initPersistentComponent((PersistentStateComponent<Object>)component);
    }
    else if (component instanceof SettingsSavingComponent) {
      SettingsSavingComponent settingsSavingComponent = (SettingsSavingComponent)component;
      mySettingsSavingComponents.add(settingsSavingComponent);
    }
  }


  public final void save() throws IOException {
    try {
      try {
        beforeSave();
      }
      catch (SaveCancelledException e) {
        return;
      }

      try {
        try {
          doSave();
        }
        catch (SaveCancelledException e) {
          return;
        }
      }
      finally {
        afterSave();
      }
    }
    catch (StateStorage.StateStorageException e) {
      LOG.info(e);
      throw new IOException(e.getMessage());
    }
  }


  protected void beforeSave() throws StateStorage.StateStorageException, SaveCancelledException {
    ShutDownTracker.getInstance().registerStopperThread(Thread.currentThread());
  }

  protected void afterSave() {
    ShutDownTracker.getInstance().unregisterStopperThread(Thread.currentThread());
  }

  protected void doSave() throws IOException {
    commit();
  }


  public void commit() {
    for (SettingsSavingComponent settingsSavingComponent : mySettingsSavingComponents) {
      settingsSavingComponent.save();
    }

    for (String name : myComponents.keySet()) {
      Object component = myComponents.get(name);
      if (component instanceof JDOMExternalizable) {
        saveJdomExternalizable((JDOMExternalizable)component);
      }
      else if (component instanceof PersistentStateComponent) {
        savePersistentComponent((PersistentStateComponent<Object>)component);
      }
    }
  }

  private void savePersistentComponent(final PersistentStateComponent<Object> persistentStateComponent) {
    try {
      Storage storageSpec = getComponentStorage(persistentStateComponent);
      StateStorage stateStorage = getStateStorage(storageSpec);

      if (stateStorage == null) return;

      org.w3c.dom.Element elementState = XmlSerializer.serialize(persistentStateComponent.getState(), DOMUtil.createDocument(), mySerializationFilter);
      stateStorage.setState(persistentStateComponent, getComponentName(persistentStateComponent), elementState);
    }
    catch (ParserConfigurationException e) {
      LOG.error(e);
    }
    catch (StateStorage.StateStorageException e) {
      LOG.error(e);
    }
  }

  private void saveJdomExternalizable(final JDOMExternalizable component) {
    final String componentName = ((BaseComponent)component).getComponentName();
    StateStorage stateStorage = getOldStorage(component);

    try {
      final Element element = new Element("temp_element");
      component.writeExternal(element);
      final org.w3c.dom.Element domElement;
      try {
        final Document d = new Document();
        d.addContent(element);
        domElement = new DOMOutputter().output(d).getDocumentElement();
      }
      catch (JDOMException e1) {
        throw new RuntimeException(e1);
      }
      stateStorage.setState(component, componentName, domElement);
    }
    catch (WriteExternalException ex) {
{'fraction_non_alphanumeric': 0.08803126526624329, 'fraction_numerical': 0.0005862237420615535, 'mean_word_length': 4.309128630705394, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '5109219', 'n_tokens_mistral': 2658, 'n_tokens_neox': 2630, 'n_words': 555}