@Nullable
 public static Configuration load(final InputStream is) throws IOException, JDOMException {
   try {
     final Document document = JDOMUtil.loadDocument(is);
     final ArrayList<Element> elements = new ArrayList<Element>();
     elements.add(document.getRootElement());
     elements.addAll(document.getRootElement().getChildren("component"));
     final Element element =
         ContainerUtil.find(
             elements,
             new Condition<Element>() {
               public boolean value(final Element element) {
                 return "component".equals(element.getName())
                     && COMPONENT_NAME.equals(element.getAttributeValue("name"));
               }
             });
     if (element != null) {
       final Configuration cfg = new Configuration();
       cfg.loadState(element, false);
       return cfg;
     }
     return null;
   } finally {
     is.close();
   }
 }
 public boolean replaceInjections(
     List<? extends BaseInjection> newInjections,
     List<? extends BaseInjection> originalInjections,
     boolean forceLevel) {
   if (!forceLevel && !originalInjections.isEmpty()) {
     if (myParentConfiguration.replaceInjections(
         Collections.<BaseInjection>emptyList(), originalInjections, forceLevel)) {
       myParentConfiguration.replaceInjections(
           newInjections, Collections.<BaseInjection>emptyList(), forceLevel);
       return true;
     }
   }
   return super.replaceInjections(newInjections, originalInjections, forceLevel);
 }
  private void mergeWithDefaultConfiguration() {
    final ArrayList<Configuration> cfgList = new ArrayList<Configuration>();
    for (LanguageInjectionSupport support : InjectorUtils.getActiveInjectionSupports()) {
      final String config = support.getDefaultConfigUrl();
      final URL url = config == null ? null : support.getClass().getResource(config);
      if (url != null) {
        try {
          cfgList.add(load(url.openStream()));
        } catch (Exception e) {
          LOG.warn(e);
        }
      }
    }
    final THashSet<Object> visited = new THashSet<Object>();
    for (IdeaPluginDescriptor pluginDescriptor : PluginManager.getPlugins()) {
      if (pluginDescriptor instanceof IdeaPluginDescriptorImpl
          && !((IdeaPluginDescriptorImpl) pluginDescriptor).isEnabled()) continue;
      final ClassLoader loader = pluginDescriptor.getPluginClassLoader();
      if (!visited.add(loader)) continue;
      if (loader instanceof PluginClassLoader && ((PluginClassLoader) loader).getUrls().isEmpty())
        continue;
      try {
        final Enumeration<URL> enumeration = loader.getResources("META-INF/languageInjections.xml");
        if (enumeration == null) continue;
        while (enumeration.hasMoreElements()) {
          URL url = enumeration.nextElement();
          if (!visited.add(url.getFile())) continue; // for DEBUG mode
          try {
            cfgList.add(load(url.openStream()));
          } catch (Exception e) {
            LOG.warn(e);
          }
        }
      } catch (Exception e) {
        LOG.warn(e);
      }
    }

    final ArrayList<BaseInjection> originalInjections = new ArrayList<BaseInjection>();
    final ArrayList<BaseInjection> newInjections = new ArrayList<BaseInjection>();
    myDefaultInjections = new ArrayList<BaseInjection>();
    for (String supportId : InjectorUtils.getActiveInjectionSupportIds()) {
      for (Configuration cfg : cfgList) {
        final List<BaseInjection> imported = cfg.getInjections(supportId);
        myDefaultInjections.addAll(imported);
        importInjections(getInjections(supportId), imported, originalInjections, newInjections);
      }
    }
    replaceInjections(newInjections, originalInjections);
  }
 public CfgInfo(Configuration cfg, final String title) {
   this.cfg = cfg;
   this.title = title;
   bundledInjections.addAll(cfg.getDefaultInjections());
   originalInjections =
       new ArrayList<BaseInjection>(
           ContainerUtil.concat(
               InjectorUtils.getActiveInjectionSupportIds(),
               new Function<String, Collection<? extends BaseInjection>>() {
                 public Collection<? extends BaseInjection> fun(final String s) {
                   List<BaseInjection> injections =
                       CfgInfo.this.cfg instanceof Configuration.Prj
                           ? ((Configuration.Prj) CfgInfo.this.cfg).getOwnInjections(s)
                           : CfgInfo.this.cfg.getInjections(s);
                   return ContainerUtil.findAll(
                       injections,
                       new Condition<BaseInjection>() {
                         public boolean value(final BaseInjection injection) {
                           String id = injection.getInjectedLanguageId();
                           return InjectedLanguage.findLanguageById(id) != null
                               || ReferenceInjector.findById(id) != null;
                         }
                       });
                 }
               }));
   sortInjections(originalInjections);
   reset();
 }
 public void apply() {
   final List<BaseInjection> injectionList = getInjectionList(injectionInfos);
   cfg.replaceInjections(injectionList, originalInjections, true);
   originalInjections.clear();
   originalInjections.addAll(injectionList);
   sortInjections(originalInjections);
   FileContentUtil.reparseOpenedFiles();
 }
 /**
  * Import from another configuration (e.g. imported file). Returns the number of imported items.
  *
  * @param cfg configuration to import from
  * @return added injections count
  */
 public int importFrom(final Configuration cfg) {
   final ArrayList<BaseInjection> originalInjections = new ArrayList<BaseInjection>();
   final ArrayList<BaseInjection> newInjections = new ArrayList<BaseInjection>();
   for (String supportId : InjectorUtils.getActiveInjectionSupportIds()) {
     importInjections(
         getInjections(supportId),
         cfg.getInjections(supportId),
         originalInjections,
         newInjections);
   }
   if (!newInjections.isEmpty()) configurationModified();
   replaceInjections(newInjections, originalInjections);
   return newInjections.size();
 }
  private static List<BaseInjection> loadDefaultInjections() {
    final ArrayList<Configuration> cfgList = new ArrayList<Configuration>();
    final THashSet<Object> visited = new THashSet<Object>();
    for (LanguageInjectionConfigBean configBean :
        Extensions.getExtensions(LanguageInjectionSupport.CONFIG_EP_NAME)) {
      PluginDescriptor descriptor = configBean.getPluginDescriptor();
      final ClassLoader loader = descriptor.getPluginClassLoader();
      try {
        final Enumeration<URL> enumeration = loader.getResources(configBean.getConfigUrl());
        if (enumeration == null || !enumeration.hasMoreElements()) {
          LOG.warn(descriptor.getPluginId() + ": " + configBean.getConfigUrl() + " was not found");
        } else {
          while (enumeration.hasMoreElements()) {
            URL url = enumeration.nextElement();
            if (!visited.add(url.getFile())) continue; // for DEBUG mode
            try {
              cfgList.add(load(url.openStream()));
            } catch (Exception e) {
              LOG.warn(e);
            }
          }
        }
      } catch (Exception e) {
        LOG.warn(e);
      }
    }

    final ArrayList<BaseInjection> defaultInjections = new ArrayList<BaseInjection>();
    for (String supportId : InjectorUtils.getActiveInjectionSupportIds()) {
      for (Configuration cfg : cfgList) {
        final List<BaseInjection> imported = cfg.getInjections(supportId);
        defaultInjections.addAll(imported);
      }
    }
    return defaultInjections;
  }
 @Override
 public void loadState(final Element element) {
   myAdvancedConfiguration.loadState(element);
   super.loadState(element);
 }
 @Override
 public long getModificationCount() {
   return super.getModificationCount() + myParentConfiguration.getModificationCount();
 }
 @NotNull
 @Override
 public List<BaseInjection> getInjections(final String injectorId) {
   return ContainerUtil.concat(
       myParentConfiguration.getInjections(injectorId), getOwnInjections(injectorId));
 }
 @Override
 public List<BaseInjection> getDefaultInjections() {
   return myParentConfiguration.getDefaultInjections();
 }
 @Override
 public AdvancedConfiguration getAdvancedConfiguration() {
   return myParentConfiguration.getAdvancedConfiguration();
 }
  private void doImportAction(final DataContext dataContext) {
    final FileChooserDescriptor descriptor =
        new FileChooserDescriptor(true, false, true, false, true, false) {
          @Override
          public boolean isFileVisible(VirtualFile file, boolean showHiddenFiles) {
            return super.isFileVisible(file, showHiddenFiles)
                && (file.isDirectory()
                    || "xml".equals(file.getExtension())
                    || file.getFileType() == FileTypes.ARCHIVE);
          }

          @Override
          public boolean isFileSelectable(VirtualFile file) {
            return file.getFileType() == StdFileTypes.XML;
          }
        };
    descriptor.setDescription(
        "Please select the configuration file (usually named IntelliLang.xml) to import.");
    descriptor.setTitle("Import Configuration");

    descriptor.putUserData(LangDataKeys.MODULE_CONTEXT, LangDataKeys.MODULE.getData(dataContext));

    final SplitterProportionsData splitterData = new SplitterProportionsDataImpl();
    splitterData.externalizeFromDimensionService(
        "IntelliLang.ImportSettingsKey.SplitterProportions");

    final VirtualFile file = FileChooser.chooseFile(descriptor, myProject, null);
    if (file == null) return;
    try {
      final Configuration cfg = Configuration.load(file.getInputStream());
      if (cfg == null) {
        Messages.showWarningDialog(
            myProject,
            "The selected file does not contain any importable configuration.",
            "Nothing to Import");
        return;
      }
      final CfgInfo info = getDefaultCfgInfo();
      final Map<String, Set<InjInfo>> currentMap =
          ContainerUtil.classify(
              info.injectionInfos.iterator(),
              new Convertor<InjInfo, String>() {
                public String convert(final InjInfo o) {
                  return o.injection.getSupportId();
                }
              });
      final List<BaseInjection> originalInjections = new ArrayList<BaseInjection>();
      final List<BaseInjection> newInjections = new ArrayList<BaseInjection>();
      //// remove duplicates
      // for (String supportId : InjectorUtils.getActiveInjectionSupportIds()) {
      //  final Set<BaseInjection> currentInjections = currentMap.get(supportId);
      //  if (currentInjections == null) continue;
      //  for (BaseInjection injection : currentInjections) {
      //    Configuration.importInjections(newInjections, Collections.singleton(injection),
      // originalInjections, newInjections);
      //  }
      // }
      // myInjections.clear();
      // myInjections.addAll(newInjections);

      for (String supportId : InjectorUtils.getActiveInjectionSupportIds()) {
        ArrayList<InjInfo> list =
            new ArrayList<InjInfo>(
                ObjectUtils.notNull(currentMap.get(supportId), Collections.<InjInfo>emptyList()));
        final List<BaseInjection> currentInjections = getInjectionList(list);
        final List<BaseInjection> importingInjections = cfg.getInjections(supportId);
        if (currentInjections == null) {
          newInjections.addAll(importingInjections);
        } else {
          Configuration.importInjections(
              currentInjections, importingInjections, originalInjections, newInjections);
        }
      }
      info.replace(originalInjections, newInjections);
      myInjectionsTable.getListTableModel().setItems(getInjInfoList(myInfos));
      final int n = newInjections.size();
      if (n > 1) {
        Messages.showInfoMessage(
            myProject, n + " entries have been successfully imported", "Import Successful");
      } else if (n == 1) {
        Messages.showInfoMessage(
            myProject, "One entry has been successfully imported", "Import Successful");
      } else {
        Messages.showInfoMessage(myProject, "No new entries have been imported", "Import");
      }
    } catch (Exception ex) {
      Configuration.LOG.error(ex);

      final String msg = ex.getLocalizedMessage();
      Messages.showErrorDialog(
          myProject, msg != null && msg.length() > 0 ? msg : ex.toString(), "Import Failed");
    }
  }