package org.jetbrains.jps.model.serialization;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jetbrains.jps.model.JpsElement;

import java.io.File;
import java.io.IOException;

/**
 * @author nik
 */
public abstract class JpsLoaderBase {
  private static final Logger LOG = Logger.getInstance("#org.jetbrains.jps.model.serialization.JpsLoaderBase");
  private final JpsMacroExpander myMacroExpander;

  protected JpsLoaderBase(JpsMacroExpander macroExpander) {
    myMacroExpander = macroExpander;
  }

  protected Element loadRootElement(final File file) {
    return loadRootElement(file, myMacroExpander);
  }

  protected <E extends JpsElement> void loadComponents(File dir,
                                                       final String defaultFileName,
                                                       JpsElementExtensionSerializerBase<E> serializer,
                                                       final E element) {
    String fileName = serializer.getConfigFileName();
    File configFile = new File(dir, fileName != null ? fileName : defaultFileName);
    if (configFile.exists()) {
      Element componentTag = JDomSerializationUtil.findComponent(loadRootElement(configFile), serializer.getComponentName());
      if (componentTag != null) {
        serializer.loadExtension(element, componentTag);
      }
      else {
        serializer.loadExtensionWithDefaultSettings(element);
      }
    }
    else {
      LOG.debug("Cannot load component " + serializer.getComponentName() + ": " + configFile + " doesn't exist");
    }
  }

  protected static Element loadRootElement(final File file, final JpsMacroExpander macroExpander) {
    try {
      final Element element = JDOMUtil.loadDocument(file).getRootElement();
      macroExpander.substitute(element, SystemInfo.isFileSystemCaseSensitive);
      return element;
    }
    catch (JDOMException e) {
      throw new RuntimeException("Cannot parse xml file " + file.getAbsolutePath() + ": " + e.getMessage(), e);
    }
    catch (IOException e) {
      throw new RuntimeException("Cannot read file " + file.getAbsolutePath() + ": " + e.getMessage(), e);
    }
  }

  protected static boolean isXmlFile(File file) {
    return file.isFile() && FileUtil.getExtension(file.getName()).equalsIgnoreCase("xml");
  }
}

@@ -1271,24 +1271,50 @@
 tFileName);%0A
+    Element componentTag;%0A
     if (conf
@@ -1334,32 +1334,24 @@
 ()) %7B%0A      
-Element 
 componentTag
@@ -1448,24 +1448,73 @@
 tName());%0A  
+  %7D%0A    else %7B%0A      componentTag = null;%0A    %7D%0A%0A
     if (comp
@@ -1529,26 +1529,24 @@
  != null) %7B%0A
-  
       serial
@@ -1588,18 +1588,16 @@
 ntTag);%0A
-  
     %7D%0A  
@@ -1598,27 +1598,23 @@
   %7D%0A    
-  
 else %7B%0A
-  
       se
@@ -1669,147 +1669,8 @@
 t);%0A
-      %7D%0A    %7D%0A    else %7B%0A      LOG.debug(%22Cannot load component %22 + serializer.getComponentName() + %22: %22 + configFile + %22 doesn't exist%22);%0A
     

load default settings for extension if corresponding config file doesn't exist