private ITemplateDesc findCodeGenTemplate(
      final String templateId, final ICodeGeneratorDescriptor code_gen) throws CoreException {
    ITemplateDesc template = null;
    final ITemplateDesc[] temps =
        RedhawkCodegenActivator.getCodeGeneratorTemplatesRegistry()
            .findTemplatesByCodegen(code_gen.getId(), "resource");
    if (templateId == null) {
      for (final ITemplateDesc t : temps) {
        if (t.isSelectable() && !t.notDefaultableGenerator()) {
          template = t;
          break;
        }
      }
    } else {
      template =
          RedhawkCodegenActivator.getCodeGeneratorTemplatesRegistry().findTemplate(templateId);
    }

    if (template == null) {
      throw new CoreException(
          new Status(
              IStatus.ERROR,
              CodegeneratorApplication.PLUGIN_ID,
              "Could not appropriate code generator template",
              null));
    }
    return template;
  }
 private IPortTemplateDesc addConfig(final IConfigurationElement element) {
   if (element.getName().equals(CodeGeneratorPortTemplatesRegistry.ATTR_TEMPLATE)) {
     final PortTemplateDescriptor desc = new PortTemplateDescriptor(element);
     if (!this.templateMap.containsKey(desc.getId())) {
       this.templateMap.put(desc.getId(), desc);
       final String[] ifaces = desc.getInterfaces();
       for (final String i : ifaces) {
         if (i == null) {
           continue;
         }
         Set<String> ids = this.repToIdMap.get(i);
         if (ids == null) {
           ids = new HashSet<String>();
           this.repToIdMap.put(i, ids);
         }
         ids.add(desc.getId());
       }
       return desc;
     } else {
       RedhawkCodegenActivator.logError(
           "Duplicate Code Generator registered with an ID of: " + desc.getId(), null);
     }
   }
   return null;
 }
 @Override
 public IStatus validate(final Object value) {
   final String s = (String) value;
   final ICodeGeneratorsRegistry registry = RedhawkCodegenActivator.getCodeGeneratorsRegistry();
   final ICodeGeneratorDescriptor[] generators = registry.findCodegenByLanguage(CodegenUtil.JAVA);
   if (generators.length > 0) {
     final String version = generators[0].getLanguageVersion();
     return JavaConventions.validatePackageName(s, version, version);
   }
   return new Status(
       IStatus.WARNING, "gov.redhawk.codegen.validation", "Unable to validate package name");
 }
  private ICodeGeneratorDescriptor findCodeGen(final String lang, final String codegenId)
      throws CoreException {
    ICodeGeneratorDescriptor code_gen = null;
    if (codegenId == null) {
      // TODO the code generator registry should eventually be fixed to be case-insensitive
      String kludge_lang = lang;
      if (lang.equalsIgnoreCase("c++")) {
        kludge_lang = "C++";
      } else if (lang.equalsIgnoreCase("python")) {
        kludge_lang = "Python";
      } else if (lang.equalsIgnoreCase("java")) {
        kludge_lang = "Java";
      }
      final ICodeGeneratorDescriptor[] code_gens =
          RedhawkCodegenActivator.getCodeGeneratorsRegistry()
              .findCodegenByLanguage(kludge_lang, "resource");
      for (final ICodeGeneratorDescriptor cg : code_gens) {
        if (!cg.notDefaultableGenerator()) {
          code_gen = cg;
          break;
        }
      }
    } else {
      code_gen = RedhawkCodegenActivator.getCodeGeneratorsRegistry().findCodegen(codegenId);
    }

    if (code_gen == null) {
      throw new CoreException(
          new Status(
              IStatus.ERROR,
              CodegeneratorApplication.PLUGIN_ID,
              "Could not find appropriate code generator",
              null));
    }
    return code_gen;
  }
 private ITemplateDesc addConfig(final IConfigurationElement element) {
   if (element.getName().equals(CodeGeneratorTemplatesRegistry.ATTR_TEMPLATE)) {
     final TemplateDescriptor desc = new TemplateDescriptor(element);
     if (!this.templateMap.containsKey(desc.getId())) {
       this.templateMap.put(desc.getId(), desc);
       final String lang = desc.getCodegenId();
       Set<String> ids = this.codegenToIdMap.get(lang);
       if (ids == null) {
         ids = new HashSet<String>();
         this.codegenToIdMap.put(lang, ids);
       }
       ids.add(desc.getId());
       return desc;
     } else {
       RedhawkCodegenActivator.logError(
           "Duplicate Code Generator registered with an ID of: " + desc.getId(), null);
     }
   }
   return null;
 }
  @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
  }
  @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));
  }