Esempio n. 1
0
 /**
  * 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();
 }
Esempio n. 2
0
  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);
  }
  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;
  }
 @NotNull
 @Override
 public List<BaseInjection> getInjections(final String injectorId) {
   return ContainerUtil.concat(
       myParentConfiguration.getInjections(injectorId), getOwnInjections(injectorId));
 }
  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");
    }
  }