@Override
  public IStatus validate(final IValidationContext ctx) {
    final EObject target = ctx.getTarget();
    IStatus retVal = null;

    if (target instanceof ImplementationSettings) {
      final ImplementationSettings implSettings = (ImplementationSettings) target;

      if (implSettings.getGeneratorId().contains("java")) {
        final ICodeGeneratorsRegistry registry =
            RedhawkCodegenActivator.getCodeGeneratorsRegistry();
        ICodeGeneratorDescriptor generator = registry.findCodegen(implSettings.getGeneratorId());
        if (generator == null) {
          return ctx.createSuccessStatus();
        }
        final String version =
            registry.findCodegen(implSettings.getGeneratorId()).getLanguageVersion();
        IStatus status = null;

        for (final Property prop : implSettings.getProperties()) {
          if (JavaGeneratorProperties.PROP_PACKAGE.equals(prop.getId())) {
            status = JavaConventions.validatePackageName(prop.getValue(), version, version);
            break;
          }
        }

        if (status == null) {
          retVal =
              new EnhancedConstraintStatus(
                  (ConstraintStatus) ctx.createFailureStatus("Invalid Package"),
                  CodegenPackage.Literals.IMPLEMENTATION_SETTINGS__PROPERTIES);
        } else if (status.getSeverity() >= IStatus.ERROR) {
          retVal =
              new EnhancedConstraintStatus(
                  (ConstraintStatus) ctx.createFailureStatus(status.getMessage()),
                  CodegenPackage.Literals.IMPLEMENTATION_SETTINGS__PROPERTIES);
        }
      }
    }
    return (retVal == null) ? ctx.createSuccessStatus() : retVal; // SUPPRESS CHECKSTYLE AvoidInline
  }
  private void initializeSoftPkg(
      final String lang,
      final String projectName,
      final ICodeGeneratorDescriptor code_gen,
      final ITemplateDesc template,
      final SoftPkg spd,
      final Implementation impl,
      final ImplementationSettings settings) {
    spd.setId(DceUuidUtil.createDceUUID());
    spd.setName(projectName);

    final ProgrammingLanguage pl = SpdFactory.eINSTANCE.createProgrammingLanguage();
    final HumanLanguage hl = SpdFactory.eINSTANCE.createHumanLanguage();
    pl.setName(lang);
    impl.setProgrammingLanguage(pl);
    hl.setName(RedhawkCodegenActivator.ENGLISH);
    impl.setHumanLanguage(hl);
    if (code_gen.getCompiler() != null) {
      final Compiler c = SpdFactory.eINSTANCE.createCompiler();
      c.setName(code_gen.getCompiler());
      c.setVersion(code_gen.getCompilerVersion());
      impl.setCompiler(c);
    } else {
      impl.setCompiler(null);
    }
    if (code_gen.getRuntime() != null) {
      final mil.jpeojtrs.sca.spd.Runtime r = SpdFactory.eINSTANCE.createRuntime();
      r.setName(code_gen.getRuntime());
      r.setVersion(code_gen.getRuntimeVersion());
      impl.setRuntime(r);
    } else {
      impl.setRuntime(null);
    }

    settings.setGeneratorId(code_gen.getId());
    settings.setOutputDir(CodegenFileHelper.createDefaultOutputDir(spd, code_gen));
    settings.setTemplate(template.getId());
    impl.setId(settings.getOutputDir());
  }
  @SuppressWarnings("deprecation")
  private WaveDevSettings getWaveDevSettings(
      final ResourceSet set, final SoftPkg softPkg, final String codegenId, final String templateId)
      throws CoreException {
    WaveDevSettings retVal = null;
    // First, try to get the .wavedev from disk. This will throw an exception if it fails.
    try {
      retVal =
          CodegenUtil.getWaveDevSettings(
              set.getResource(CodegenUtil.getSettingsURI(softPkg), true));
    } catch (final Exception e) {
      System.out.println("Unable to find the settings file, inferring defaults");
    }

    // if we weren't able to find the wavedev, create it
    if (retVal == null) {
      retVal = CodegenFactory.eINSTANCE.createWaveDevSettings();
      // Recreate the basic settings for each implementation
      // This makes assumptions that the defaults are selected for everything
      for (final Implementation impl : softPkg.getImplementation()) {
        final ImplementationSettings settings =
            CodegenFactory.eINSTANCE.createImplementationSettings();
        final String lang = impl.getProgrammingLanguage().getName();

        // Find the code generator if specified, otherwise pick the first one returned by the
        // registry
        ICodeGeneratorDescriptor codeGenDesc = null;
        if (codegenId != null) {
          codeGenDesc = RedhawkCodegenActivator.getCodeGeneratorsRegistry().findCodegen(codegenId);
        } else {
          final ICodeGeneratorDescriptor[] codeGens =
              RedhawkCodegenActivator.getCodeGeneratorsRegistry().findCodegenByLanguage(lang);
          if (codeGens.length > 0) {
            codeGenDesc = codeGens[0];
          }
        }

        // Proceed if we found one
        if (codeGenDesc != null) {
          final IScaComponentCodegen generator = codeGenDesc.getGenerator();

          // Assume that there is <name>[/].+<other> format for the entrypoint
          // Pick out <name> for both the output dir and settings name
          final String lf = impl.getCode().getEntryPoint();
          final String name = lf.substring(0, lf.indexOf('/'));

          // Set the generator, settings name and output directory
          settings.setGeneratorId(generator.getClass().getCanonicalName());
          settings.setName(name);
          settings.setOutputDir(lf.substring(0, lf.lastIndexOf('/')));

          // Find the template if specified, otherwise pick the first selectable and defaultable one
          // returned by the registry
          ITemplateDesc templateDesc = null;
          if (templateId != null) {
            templateDesc =
                RedhawkCodegenActivator.getCodeGeneratorTemplatesRegistry()
                    .findTemplate(templateId);
          } else {
            final ITemplateDesc[] templates =
                RedhawkCodegenActivator.getCodeGeneratorTemplatesRegistry()
                    .findTemplatesByCodegen(settings.getGeneratorId());
            for (final ITemplateDesc itd : templates) {
              if (itd.isSelectable() && !itd.notDefaultableGenerator()) {
                templateDesc = itd;
                break;
              }
            }
          }

          // If we found the template, use it
          if (templateDesc != null) {
            // Set the properties to their default values
            for (final IPropertyDescriptor prop : templateDesc.getPropertyDescriptors()) {
              final Property p = CodegenFactory.eINSTANCE.createProperty();
              p.setId(prop.getKey());
              p.setValue(prop.getDefaultValue());
              settings.getProperties().add(p);
            }
            // Set the template
            settings.setTemplate(templateDesc.getId());
          } else {
            System.err.println("Unable to find a valid template! Desired: " + templateId);
          }
        } else {
          System.err.println("Unable to find a valid Code Generator! Desired: " + codegenId);
        }
        // Save the created settings
        retVal.getImplSettings().put(impl.getId(), settings);
      }
      // Create the URI to the .wavedev file
      final URI uri =
          URI.createPlatformResourceURI(
              softPkg.getName() + "/." + softPkg.getName() + ".wavedev", false);
      final Resource res = set.createResource(uri);

      // Add the WaveDevSettings to the resource and save to disk to persist the newly created
      // WaveDevSettings
      res.getContents().add(retVal);
      try {
        res.save(null);
      } catch (final IOException e) {

      }
    }
    return retVal;
  }
  // TODO - turn this into an OSGi command
  private void generate_code(
      final String project_path,
      final String lang,
      String codegenId,
      final String templateId,
      final String[] preserveFiles,
      final NullProgressMonitor progressMonitor)
      throws CoreException {
    final SubMonitor monitor = SubMonitor.convert(progressMonitor, 2);

    final ResourceSet set = ScaResourceFactoryUtil.createResourceSet();

    final IPath projectPath = new Path(project_path);
    final IProject project = openProject(projectPath);
    final SoftPkg softPkg = getSoftPkg(project);

    if (softPkg == null) {
      throw new IllegalStateException("Could not load spd.xml for project");
    }

    // Create or open the existing settings
    final WaveDevSettings waveDev = getWaveDevSettings(set, softPkg, codegenId, templateId);
    if (waveDev == null) {
      throw new IllegalStateException("Could not load wavedev settings for project");
    }

    final EMap<String, ImplementationSettings> implSet = waveDev.getImplSettings();

    // Try generate each implementation, or just the specified language
    for (final Implementation impl : softPkg.getImplementation()) {
      final String currLang = impl.getProgrammingLanguage().getName();
      if ((lang != null) && !lang.equals(currLang.toLowerCase())) {
        continue;
      }

      // Prepare for generation
      final ImplementationSettings settings = implSet.get(impl.getId());
      final ArrayList<FileToCRCMap> crcMap = new ArrayList<FileToCRCMap>();

      System.out.println("\n\nGenerating " + currLang + " code for " + softPkg.getName());

      // Validate the settings name
      final String implName = CodegenFileHelper.safeGetImplementationName(impl, settings);
      if (!implName.equals(CodegenUtil.getValidName(implName))) {
        System.err.println("Invalid characters in implementation name for " + implName);
        continue;
      } else if (settings.getGeneratorId() != null) {
        // Find the desired code generator
        codegenId = settings.getGeneratorId();
        final ICodeGeneratorDescriptor codeGenDesc =
            RedhawkCodegenActivator.getCodeGeneratorsRegistry().findCodegen(codegenId);
        if (codeGenDesc == null) {
          System.err.println(
              "The code generator(" + codegenId + ") for this implementation could not be found.");
          continue;
        }
        // Get the actual code generator
        final IScaComponentCodegen generator = codeGenDesc.getGenerator();
        // Get files to generate
        final Set<FileStatus> fileStatusSet = generator.getGeneratedFilesStatus(settings, softPkg);
        final Set<String> fileList = new HashSet<String>();
        for (FileStatus s : fileStatusSet) {
          fileList.add(s.getFilename());
        }
        // Remove files we don't want to delete
        if (preserveFiles.length != 0) {
          if ("*".equals(preserveFiles[0])) {
            fileList.clear();
          } else {
            for (final String f : preserveFiles) {
              if (fileList.contains(f)) {
                fileList.remove(f);
              }
            }
          }
        }
        // Generate the files
        final IStatus status =
            generator.generate(
                settings,
                impl,
                System.out,
                System.err,
                monitor.newChild(1),
                fileList.toArray(new String[0]),
                generator.shouldGenerate(),
                crcMap);
        // Save the workspace
        final WorkspaceModifyOperation operation =
            new WorkspaceModifyOperation() {

              @Override
              protected void execute(final IProgressMonitor monitor)
                  throws CoreException, InvocationTargetException, InterruptedException {
                final IStatus saveStatus = ResourcesPlugin.getWorkspace().save(true, monitor);
                // Check the save results, hopefully this worked
                if (!saveStatus.isOK()) {
                  System.err.println(
                      "Generated files, but there was a problem saving the workspace: "
                          + saveStatus.getMessage());
                }
              }
            };
        try {
          operation.run(monitor.newChild(1));
        } catch (final InvocationTargetException e) {
          throw new CoreException(
              new Status(
                  IStatus.ERROR, CodegeneratorApplication.PLUGIN_ID, "Error saving resources", e));
        } catch (final InterruptedException e) {
          throw new CoreException(
              new Status(
                  IStatus.ERROR, CodegeneratorApplication.PLUGIN_ID, "Error saving resources", e));
        }

        // Check the results
        if (!status.isOK()) {
          System.err.println(
              "\nErrors occurred generating " + currLang + " code: " + status.getMessage());
          continue;
        } else {
          System.out.println("\nDone generating " + currLang + " code!");
        }
      } else {
        System.err.println(
            "No generator specified for implementation: " + implName + ". No code generated.");
      }
    }

    project.build(IncrementalProjectBuilder.FULL_BUILD, monitor.newChild(1));
  }
  public String generate(Object argument) throws CoreException {
    final StringBuffer stringBuffer = new StringBuffer();

    final TemplateParameter param = (TemplateParameter) argument;
    final EMap<String, ImplementationSettings> settings =
        ((WaveDevSettings) param.getImplSettings().eContainer().eContainer()).getImplSettings();
    final String name = param.getSoftPkg().getName();
    final String version;
    if (param.getSoftPkg().getVersion() != null
        && param.getSoftPkg().getVersion().trim().length() != 0) {
      version = param.getSoftPkg().getVersion();
    } else {
      version = "1.0.0";
    }

    // Interfaces
    final Set<String> interfaceNamespaces = new HashSet<String>();
    final List<Interface> interfaces =
        param.getSoftPkg().getDescriptor().getComponent().getInterfaces().getInterface();
    final Pattern idlPattern = Pattern.compile("^IDL:(\\w+)/");
    for (Interface iface : interfaces) {
      final Matcher match = idlPattern.matcher(iface.getRepid());
      if (match.find()) {
        if (!"CF".equals(match.group(1))) {
          interfaceNamespaces.add(match.group(1));
        }
      }
    }

    // Language
    boolean hasCpp = false, hasJava = false, hasPython = false;
    for (final Implementation impl : param.getSoftPkg().getImplementation()) {
      final String language = impl.getProgrammingLanguage().getName();
      if ("C++".equals(language)) {
        hasCpp = true;
      } else if ("Java".equals(language)) {
        hasJava = true;
      } else if ("Python".equals(language)) {
        hasPython = true;
      }
    }

    // Strings that depend on project type
    final String sdrrootFolder;
    final String projectType;
    final SoftwareComponent scd = param.getSoftPkg().getDescriptor().getComponent();
    final ComponentType componentType = SoftwareComponent.Util.getWellKnownComponentType(scd);
    switch (componentType) {
      case RESOURCE:
        sdrrootFolder = "dom/components";
        projectType = "Component";
        break;
      case DEVICE:
        sdrrootFolder = "dev/devices";
        projectType = "Device";
        break;
      case SERVICE:
        sdrrootFolder = "dev/services";
        projectType = "Service";
        break;
      default:
        // For backwards compatibility, support non-spec strings the IDE used to generate
        try {
          if (scd != null && scd.getComponentType() != null) {
            final String compTypeStr = scd.getComponentType();
            if (compTypeStr.equals("executabledevice") || compTypeStr.equals("loadabledevice")) {
              sdrrootFolder = "dev/devices";
              projectType = "Device";
              break;
            }
          }
        } catch (NullPointerException e) {
          // PASS
        }
        throw new CoreException(
            new Status(
                IStatus.ERROR,
                Activator.PLUGIN_ID,
                "Template file does not support the specified project type"));
    }

    stringBuffer.append(TEXT_1);
    stringBuffer.append(name);
    stringBuffer.append(TEXT_2);
    stringBuffer.append(projectType);
    stringBuffer.append(TEXT_3);

    if (param.getSoftPkg().getTitle() != null
        && param.getSoftPkg().getTitle().trim().length() > 0) {

      stringBuffer.append(TEXT_4);
      stringBuffer.append(param.getSoftPkg().getTitle());
      stringBuffer.append(TEXT_5);
    }
    stringBuffer.append(TEXT_6);
    stringBuffer.append(version);
    stringBuffer.append(TEXT_7);
    stringBuffer.append(projectType);
    stringBuffer.append(TEXT_8);

    if (interfaceNamespaces.size() > 0) {
      boolean foundOther = false;
      // these interfaces are available through the redhawk install
      //  so no additional rpms are needed
      for (String ifaceNamespace : interfaceNamespaces) {
        if ((!ifaceNamespace.equals("CF"))
            && (!ifaceNamespace.equals("PortTypes"))
            && (!ifaceNamespace.equals("ExtendedEvent"))
            && (!ifaceNamespace.equals("ExtendedCF"))
            && (!ifaceNamespace.equals("StandardEvent"))
            && (!ifaceNamespace.equals("WKP"))) {
          foundOther = true;
          break;
        }
      }
      if (foundOther) {

        stringBuffer.append(TEXT_9);

        for (String ifaceNamespace : interfaceNamespaces) {
          if ((!ifaceNamespace.equals("CF"))
              && (!ifaceNamespace.equals("PortTypes"))
              && (!ifaceNamespace.equals("ExtendedEvent"))
              && (!ifaceNamespace.equals("ExtendedCF"))
              && (!ifaceNamespace.equals("StandardEvent"))
              && (!ifaceNamespace.equals("WKP"))) {

            stringBuffer.append(TEXT_10);
            stringBuffer.append(ifaceNamespace.toLowerCase());
            stringBuffer.append(TEXT_11);
          }
        }

        stringBuffer.append(TEXT_12);

        for (String ifaceNamespace : interfaceNamespaces) {
          if ((!ifaceNamespace.equals("CF"))
              && (!ifaceNamespace.equals("PortTypes"))
              && (!ifaceNamespace.equals("ExtendedEvent"))
              && (!ifaceNamespace.equals("ExtendedCF"))
              && (!ifaceNamespace.equals("StandardEvent"))
              && (!ifaceNamespace.equals("WKP"))) {

            stringBuffer.append(TEXT_13);
            stringBuffer.append(ifaceNamespace.toLowerCase());
            stringBuffer.append(TEXT_14);
          }
        }

        stringBuffer.append(TEXT_15);
      }
    }
    if (!hasCpp && (hasJava | hasPython)) {

      stringBuffer.append(TEXT_16);
    }
    if (hasCpp) {

      stringBuffer.append(TEXT_17);
    }
    if (hasJava) {

      stringBuffer.append(TEXT_18);
    }
    if (hasPython) {

      stringBuffer.append(TEXT_19);
    }

    stringBuffer.append(TEXT_20);

    if (param.getSoftPkg().getDescription() != null) {

      stringBuffer.append(TEXT_21);
      stringBuffer.append(param.getSoftPkg().getDescription());

    } else {

      stringBuffer.append(TEXT_22);
      stringBuffer.append(projectType);
      stringBuffer.append(TEXT_23);
    }

    stringBuffer.append(TEXT_24);

    for (final Implementation impl : param.getSoftPkg().getImplementation()) {
      final ImplementationSettings implSettings = settings.get(impl.getId());
      if (implSettings == null) {
        continue;
      }

      stringBuffer.append(TEXT_25);
      stringBuffer.append(impl.getId());
      stringBuffer.append(TEXT_26);
      stringBuffer.append(implSettings.getOutputDir());
      stringBuffer.append(TEXT_27);
      stringBuffer.append(sdrrootFolder);
      stringBuffer.append(TEXT_28);
      stringBuffer.append(name);
      stringBuffer.append(TEXT_29);
      stringBuffer.append(implSettings.getOutputDir());
      stringBuffer.append(TEXT_30);
    }

    stringBuffer.append(TEXT_31);

    for (final Implementation impl : param.getSoftPkg().getImplementation()) {
      final ImplementationSettings implSettings = settings.get(impl.getId());
      if (implSettings == null) {
        continue;
      }

      stringBuffer.append(TEXT_32);
      stringBuffer.append(impl.getId());
      stringBuffer.append(TEXT_33);
      stringBuffer.append(implSettings.getOutputDir());
      stringBuffer.append(TEXT_34);
      stringBuffer.append(sdrrootFolder);
      stringBuffer.append(TEXT_35);
      stringBuffer.append(name);
      stringBuffer.append(TEXT_36);
      stringBuffer.append(implSettings.getOutputDir());
      stringBuffer.append(TEXT_37);
    }

    stringBuffer.append(TEXT_38);
    stringBuffer.append(sdrrootFolder);
    stringBuffer.append(TEXT_39);
    stringBuffer.append(sdrrootFolder);
    stringBuffer.append(TEXT_40);
    stringBuffer.append(ModelUtil.getSpdFileName(param.getSoftPkg()));

    if (param.getSoftPkg().getPropertyFile() != null) {

      stringBuffer.append(TEXT_41);
      stringBuffer.append(sdrrootFolder);
      stringBuffer.append(TEXT_42);
      stringBuffer.append(ModelUtil.getPrfFileName(param.getSoftPkg().getPropertyFile()));
    }

    stringBuffer.append(TEXT_43);
    stringBuffer.append(sdrrootFolder);
    stringBuffer.append(TEXT_44);
    stringBuffer.append(ModelUtil.getScdFileName(param.getSoftPkg()));

    for (final Implementation impl : param.getSoftPkg().getImplementation()) {
      final ImplementationSettings implSettings = settings.get(impl.getId());
      if (implSettings == null) {
        continue;
      }

      stringBuffer.append(TEXT_45);
      stringBuffer.append(sdrrootFolder);
      stringBuffer.append(TEXT_46);
      stringBuffer.append(implSettings.getOutputDir());
    }

    return stringBuffer.toString();
  }