/*
 * Copyright 2000-2015 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 com.intellij.configurationStore;

import com.intellij.openapi.components.StateStorage;
import com.intellij.openapi.components.impl.stores.StateStorageBase;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.vfs.SafeWriteRequestor;
import com.intellij.util.xmlb.SkipDefaultsSerializationFilter;
import com.intellij.util.xmlb.XmlSerializer;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class SaveSessionBase implements StateStorage.SaveSession, StateStorage.ExternalizationSession, SafeWriteRequestor {
  private static final Logger LOG = Logger.getInstance(StateStorageBase.class);

  private SkipDefaultsSerializationFilter serializationFilter;

  @SuppressWarnings("deprecation")
  @Override
  public final void setState(@NotNull Object component, @NotNull String componentName, @NotNull Object state) {
    Element element;
    try {
      if (state instanceof Element) {
        element = (Element)state;
      }
      else if (state instanceof JDOMExternalizable) {
        element = new Element("temp_element");
        ((JDOMExternalizable)state).writeExternal(element);
      }
      else {
        if (serializationFilter == null) {
          serializationFilter = new SkipDefaultsSerializationFilter();
        }
        element = XmlSerializer.serializeIfNotDefault(state, serializationFilter);
      }
    }
    catch (WriteExternalException e) {
      LOG.debug(e);
      return;
    }
    catch (Throwable e) {
      LOG.error("Unable to serialize " + componentName + " state", e);
      return;
    }

    setSerializedState(component, componentName, element);
  }

  protected abstract void setSerializedState(@NotNull Object component, @NotNull String componentName, @Nullable Element element);
}

@@ -633,17 +633,16 @@
 ionStore
-;
 %0A%0Aimport
@@ -686,17 +686,16 @@
 eStorage
-;
 %0Aimport 
@@ -754,17 +754,16 @@
 rageBase
-;
 %0Aimport 
@@ -800,17 +800,16 @@
 c.Logger
-;
 %0Aimport 
@@ -852,17 +852,16 @@
 alizable
-;
 %0Aimport 
@@ -908,17 +908,16 @@
 xception
-;
 %0Aimport 
@@ -959,17 +959,16 @@
 equestor
-;
 %0Aimport 
@@ -1021,17 +1021,16 @@
 onFilter
-;
 %0Aimport 
@@ -1065,17 +1065,16 @@
 rializer
-;
 %0Aimport 
@@ -1093,103 +1093,10 @@
 ment
-;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.annotations.Nullable;%0A%0Apublic 
+%0A%0A
 abst
@@ -1126,18 +1126,9 @@
 ase 
-implements
+:
  Sta
@@ -1222,88 +1222,32 @@
 ate 
-static final Logger LOG = Logger.getInstance(StateStorageBase.class);%0A%0A  private
+var serializationFilter:
  Ski
@@ -1278,29 +1278,16 @@
 lter
- serializationFilter;
+? = null
 %0A%0A  
@@ -1325,134 +1325,87 @@
 )%0A  
-@O
+o
 verride
-%0A  public final void setState(@NotNull Object component, @NotNull String componentName, @NotNull Object
+ fun setState(component: Any, componentName: String,
  state
+: Any
 ) %7B%0A    
 Elem
@@ -1404,24 +1404,29 @@
     
-E
+val e
 lement
- e
+: E
 lement
-;
+?
 %0A   
@@ -1449,25 +1449,17 @@
 (state i
-nstanceof
+s
  Element
@@ -1484,23 +1484,13 @@
 t = 
-(Element)
 state
-;
 %0A   
@@ -1520,17 +1520,9 @@
 te i
-nstanceof
+s
  JDO
@@ -1557,20 +1557,16 @@
 lement =
- new
  Element
@@ -1581,17 +1581,16 @@
 lement%22)
-;
 %0A       
@@ -1594,35 +1594,13 @@
     
-((JDOMExternalizable)
 state
-)
 .wri
@@ -1618,17 +1618,16 @@
 element)
-;
 %0A      %7D
@@ -1718,12 +1718,8 @@
 er =
- new
  Ski
@@ -1748,17 +1748,16 @@
 Filter()
-;
 %0A       
@@ -1840,17 +1840,16 @@
 nFilter)
-;
 %0A      %7D
@@ -1866,16 +1866,19 @@
  catch (
+e: 
 WriteExt
@@ -1891,18 +1891,16 @@
 xception
- e
 ) %7B%0A    
@@ -1913,17 +1913,16 @@
 debug(e)
-;
 %0A      r
@@ -1922,25 +1922,24 @@
       return
-;
 %0A    %7D%0A    c
@@ -1944,16 +1944,19 @@
  catch (
+e: 
 Throwabl
@@ -1956,18 +1956,16 @@
 hrowable
- e
 ) %7B%0A    
@@ -2001,12 +2001,9 @@
 ize 
-%22 + 
+$
 comp
@@ -2015,12 +2015,8 @@
 Name
- + %22
  sta
@@ -2022,17 +2022,16 @@
 ate%22, e)
-;
 %0A      r
@@ -2035,17 +2035,16 @@
   return
-;
 %0A    %7D%0A%0A
@@ -2100,17 +2100,16 @@
 element)
-;
 %0A  %7D%0A%0A  
@@ -2131,12 +2131,11 @@
 act 
-void
+fun
  set
@@ -2154,50 +2154,23 @@
 ate(
-@NotNull Object component, @NotNull String
+component: Any,
  com
@@ -2183,36 +2183,35 @@
 Name
-, @Nullable E
+: String, e
 lement
- e
+: E
 lement
+?
 )
-;
 %0A%7D%0A

move SaveSessionBase to configuration-store module