private List<ModelExtensionDefinitionHeader> getHeaderList(
     List<ModelExtensionDefinition> medList) {
   CoreArgCheck.isNotNull(medList, "medList is null"); // $NON-NLS-1$
   List<ModelExtensionDefinitionHeader> headerList = new ArrayList(medList.size());
   for (ModelExtensionDefinition med : medList) {
     headerList.add(med.getHeader());
   }
   return headerList;
 }
  /**
   * If a MED with the same NS Prefix is already registered, it will be removed and replaced with
   * the supplied MED
   *
   * @param medFile the file containing the med definition
   * @throws Exception throws exception if the add operation failed
   */
  private static void updateExtensionInRegistry(IFile medFile) throws Exception {
    ModelExtensionRegistry registry = ExtensionPlugin.getInstance().getRegistry();

    // If MED with this prefix is registered, remove it first
    ModelExtensionDefinition med =
        RegistryDeploymentValidator.parseMed(medFile.getContents(), false);
    if (registry.isNamespacePrefixRegistered(med.getNamespacePrefix())) {
      registry.removeDefinition(med.getNamespacePrefix());
    }

    // Add the supplied MED
    addExtensionToRegistry(medFile);
  }
 /**
  * Get the list of registered ModelExtensionDefinitions that can be added to this ModelResource.
  * If the model already contains a MED it will not be included.
  *
  * @param modelResource the ModelResource
  * @param currentModelHeaders the Med Headers already applied to the model resource
  * @return the list of valid ModelExtensionDefinitions for the supplied ModelResource
  */
 private List<ModelExtensionDefinition> getAddableMEDs(
     ModelResource modelResource, List<ModelExtensionDefinitionHeader> currentModelHeaders) {
   List<ModelExtensionDefinition> resultList = new ArrayList<ModelExtensionDefinition>();
   IFile file = (IFile) modelResource.getResource();
   Collection<ModelExtensionDefinition> allDefinitions = this.registry.getAllDefinitions();
   for (ModelExtensionDefinition med : allDefinitions) {
     ModelExtensionAssistant assistant = med.getModelExtensionAssistant();
     if (assistant.supportsMedOperation(ExtensionConstants.MedOperations.ADD_MED_TO_MODEL, file)
         && !headerListContains(currentModelHeaders, med.getNamespacePrefix(), med.getVersion())) {
       resultList.add(med);
     }
   }
   return resultList;
 }
  public void addModelExtensionDefinition(ModelExtensionDefinition med) {
    CoreArgCheck.isNotNull(med, "med is null"); // $NON-NLS-1$

    // if the current MED header list does not contain a match already, then add this med
    ModelExtensionDefinitionHeader medHeader = med.getHeader();
    if (!this.currentMedHeaderList.contains(medHeader)) {
      this.currentMedHeaderList.add(medHeader);

      // Include in the 'add' list - If the original Med list does not contain this
      if (!this.originalMedHeaderList.contains(medHeader)) {
        this.medsToAddList.add(med);
      }
    }

    // If the added meds prefix was on the remove list, take it off
    String nsPrefix = med.getNamespacePrefix();
    this.namespacesToRemoveList.remove(nsPrefix);
  }
  /**
   * {@inheritDoc}
   *
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString() {
    StringBuilder text = new StringBuilder();
    text.append("\nMED Headers Edit Manager: \n"); // $NON-NLS-1$
    text.append("Original Headers: \n"); // $NON-NLS-1$
    for (ModelExtensionDefinitionHeader header : originalMedHeaderList) {
      text.append(
          "  NSPrefix: "
              + header.getNamespacePrefix()
              + ", Version: "
              + header.getVersion()
              + '\n'); //$NON-NLS-1$ //$NON-NLS-2$
    }
    text.append("Current Headers: \n"); // $NON-NLS-1$
    for (ModelExtensionDefinitionHeader header : currentMedHeaderList) {
      text.append(
          "  NSPrefix: "
              + header.getNamespacePrefix()
              + ", Version: "
              + header.getVersion()
              + '\n'); //$NON-NLS-1$ //$NON-NLS-2$
    }
    text.append("MEDs to ADD: \n"); // $NON-NLS-1$
    for (ModelExtensionDefinition med : medsToAddList) {
      text.append(
          "  NSPrefix: "
              + med.getNamespacePrefix()
              + ", Version: "
              + med.getVersion()
              + '\n'); //$NON-NLS-1$ //$NON-NLS-2$
    }
    text.append("NsPrefixes to REMOVE: \n"); // $NON-NLS-1$
    for (String nsPrefix : namespacesToRemoveList) {
      text.append("  NSPrefix: " + nsPrefix + '\n'); // $NON-NLS-1$
    }
    text.append("NsPrefixes to UPDATE: \n"); // $NON-NLS-1$
    for (String nsPrefix : this.namespacesToUpdateList) {
      text.append("  NSPrefix: " + nsPrefix + '\n'); // $NON-NLS-1$
    }

    return text.toString();
  }
    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.jface.viewers.ColumnLabelProvider#getText(java.lang.Object)
     */
    @Override
    public String getText(Object element) {
      assert element instanceof ModelExtensionDefinition;
      ModelExtensionDefinition med = (ModelExtensionDefinition) element;

      if (this.columnIndex == ColumnIndexes.NAMESPACE_PREFIX) {
        return med.getNamespacePrefix();
      }

      if (this.columnIndex == ColumnIndexes.VERSION) {
        return Integer.toString(med.getVersion());
      }

      if (this.columnIndex == ColumnIndexes.DESCRIPTION) {
        return med.getDescription();
      }

      // shouldn't happen
      assert false : "Unknown column index of " + this.columnIndex; // $NON-NLS-1$
      return null;
    }
  public void updateModelExtensionDefinition(ModelExtensionDefinition med) {
    CoreArgCheck.isNotNull(med, "med is null"); // $NON-NLS-1$

    // if the current MED header list does not contain a match already, then add this med
    ModelExtensionDefinitionHeader updateMedHeader = med.getHeader();

    // replace current header with this new one
    for (ModelExtensionDefinitionHeader header : this.currentMedHeaderList) {
      if (header.getNamespacePrefix().equals(updateMedHeader.getNamespacePrefix())) {
        this.currentMedHeaderList.remove(header);
        this.currentMedHeaderList.add(updateMedHeader);
        break;
      }
    }

    this.namespacesToUpdateList.add(updateMedHeader.getNamespacePrefix());
  }
  public static void deploy(final IFile medToDeploy) {
    CoreArgCheck.isNotNull(medToDeploy, "medToDeploy is null"); // $NON-NLS-1$

    // Check whether there is currently an open editor for the selected Med
    IEditorPart editor = getOpenEditor(medToDeploy);

    // If editor is open and dirty, ask user whether to save
    if ((editor != null) && editor.isDirty()) {
      if (!MessageDialog.openQuestion(
          getShell(),
          Messages.updateMedInRegistryMedDirtyTitle,
          Messages.updateMedInRegistryMedDirtyMsg)) {
        return;
      }

      editor.doSave(new NullProgressMonitor());
    }

    // If the file has any error markers, user is informed to fix them first
    if (RegistryDeploymentValidator.checkProblemMarkers(medToDeploy)) {
      return;
    }

    ModelExtensionRegistry registry = ExtensionPlugin.getInstance().getRegistry();
    InputStream fileContents = null;

    try {
      fileContents = medToDeploy.getContents();
    } catch (CoreException e) {
      UTIL.log(
          IStatus.ERROR, e, NLS.bind(Messages.medFileGetContentsErrorMsg, medToDeploy.getName()));
    }

    if (fileContents != null) {
      // Parse file contents to get the MED. Show info dialog if parse errors.
      ModelExtensionDefinition med = null;

      try {
        ModelExtensionDefinitionParser parser =
            new ModelExtensionDefinitionParser(ExtensionPlugin.getInstance().getMedSchema());
        med =
            parser.parse(
                fileContents,
                ExtensionPlugin.getInstance().createDefaultModelObjectExtensionAssistant());

        if (!parser.getErrors().isEmpty()) {
          MessageDialog.openError(
              getShell(),
              Messages.registerMedActionFailedTitle,
              NLS.bind(Messages.medFileParseErrorMsg, medToDeploy.getName()));
          return;
        }
      } catch (Exception e) {
        UTIL.log(e);
        MessageDialog.openError(
            getShell(), Messages.registerMedActionFailedTitle, Messages.registerMedActionFailedMsg);
        return;
      }

      // Continue checks on parsable MED
      if (med != null) {
        ModelExtensionDefinition medNSPrefixMatch =
            RegistryDeploymentValidator.getRegisteredMedWithNSPrefix(
                registry, med.getNamespacePrefix());
        ModelExtensionDefinition medNSUriMatch =
            RegistryDeploymentValidator.getRegisteredMedWithNSUri(registry, med.getNamespaceUri());

        boolean nsPrefixConflict = false;
        boolean nsUriConflict = false;
        boolean nsPrefixAndUriConflictSameMed = false;
        boolean nsPrefixConflictMedBuiltIn = false;
        boolean nsUriConflictMedBuiltIn = false;

        if (medNSPrefixMatch != null) {
          nsPrefixConflict = true;
          nsPrefixConflictMedBuiltIn = medNSPrefixMatch.isBuiltIn();
        }
        if (medNSUriMatch != null) {
          nsUriConflict = true;
          nsUriConflictMedBuiltIn = medNSUriMatch.isBuiltIn();
        }
        if (nsPrefixConflict && nsUriConflict && medNSPrefixMatch.equals(medNSUriMatch))
          nsPrefixAndUriConflictSameMed = true;

        // No conflicts - add it to the registry
        if (!nsPrefixConflict && !nsUriConflict) {
          // Add the selected Med
          BusyIndicator.showWhile(
              Display.getDefault(),
              new Runnable() {
                @Override
                public void run() {
                  internalRun(medToDeploy, false);
                }
              });
          // If the NS Prefix conflicts with a Built-in, prompt user to fix
        } else if (nsPrefixConflictMedBuiltIn) {
          RegistryDeploymentValidator.showMedNSPrefixConflictsWBuiltInDialog();
          // If the NS URI conflicts with a Built-in, prompt user to fix
        } else if (nsUriConflictMedBuiltIn) {
          RegistryDeploymentValidator.showMedNSUriConflictsWBuiltInDialog();
          // If there is (1) just a NS Prefix Conflict or (2) NS Prefix AND URI, but they are same
          // MED, prompt user
          // whether to update
        } else if (nsPrefixConflict
            && (!nsUriConflict || (nsUriConflict && nsPrefixAndUriConflictSameMed))) {
          // Do not re-deploy the same MED
          if (med.equals(medNSPrefixMatch)) {
            RegistryDeploymentValidator.showMedNSPrefixAlreadyRegisteredDialog();
          } else {
            boolean doUpdate =
                RegistryDeploymentValidator.showMedNSPrefixAlreadyRegisteredDoUpdateDialog();
            if (doUpdate) {
              // Add the selected Med
              BusyIndicator.showWhile(
                  Display.getDefault(),
                  new Runnable() {
                    @Override
                    public void run() {
                      internalRun(medToDeploy, true);
                    }
                  });
            }
          }
          // If there is a NS URI Conflict, prompt user to fix it
        } else if (nsUriConflict) {
          RegistryDeploymentValidator.showMedNSUriAlreadyRegisteredDialog();
        }
      }
    }
  }