public void add(Diagnostic diagnostic) {
   if (diagnostic == null) return;
   List<Diagnostic> kids = diagnostic.getChildren();
   if (!kids.isEmpty()) {
     for (Diagnostic kid : kids) add(kid);
   } else {
     List<?> objects = diagnostic.getData();
     CSTNode cstNode = null;
     if (!objects.isEmpty()) {
       Object object = objects.get(0);
       if (object != null) {
         if (environment != null) cstNode = environment.getASTMapping(object);
         else if (object instanceof CSTNode) cstNode = (CSTNode) object;
       }
     }
     int startOffset = cstNode != null ? cstNode.getStartOffset() : 0;
     int endOffset = cstNode != null ? cstNode.getEndOffset() : 0;
     Severity problemSeverity = Severity.INFO;
     if (diagnostic.getSeverity() >= Diagnostic.ERROR) problemSeverity = Severity.ERROR;
     else if (diagnostic.getSeverity() >= Diagnostic.WARNING) problemSeverity = Severity.WARNING;
     String problemMessage = diagnostic.getMessage();
     String problemContext = diagnostic.getSource();
     handleProblem(problemSeverity, problemMessage, problemContext, startOffset, endOffset);
   }
 }
示例#2
0
 /**
  *
  * <!-- begin-user-doc -->
  * Prints diagnostics with indentation.
  * <!--
  * end-user-doc -->
  *
  * @param diagnostic the diagnostic to print.
  * @param indent the indentation for printing.
  * @generated
  */
 protected static void printDiagnostic(Diagnostic diagnostic, String indent) {
   System.out.print(indent);
   System.out.println(diagnostic.getMessage());
   for (Diagnostic child : diagnostic.getChildren()) {
     printDiagnostic(child, indent + "  ");
   }
 }
示例#3
0
 public String isValid(Object object) {
   Object value;
   try {
     value =
         eDataType
             .getEPackage()
             .getEFactoryInstance()
             .createFromString(eDataType, (String) object);
   } catch (Exception exception) {
     String message = exception.getClass().getName();
     int index = message.lastIndexOf('.');
     if (index >= 0) {
       message = message.substring(index + 1);
     }
     if (exception.getLocalizedMessage() != null) {
       message = message + ": " + exception.getLocalizedMessage();
     }
     return message;
   }
   Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eDataType, value);
   if (diagnostic.getSeverity() == Diagnostic.OK) {
     return null;
   } else {
     return (diagnostic.getChildren().get(0))
         .getMessage()
         .replaceAll("'", "''")
         .replaceAll("\\{", "'{'"); // }}
   }
 }
示例#4
0
  public MavenMetadata(URI uri) throws CoreException {
    Resource resource = getResourceSet().getResource(uri, true);
    EList<EObject> content = resource.getContents();
    if (content.size() != 1)
      throw ExceptionUtils.fromMessage(
          "ECore Resource did not contain one resource. It had %d",
          Integer.valueOf(content.size()));

    documentRoot = (DocumentRoot) content.get(0);
    Diagnostic diag = Diagnostician.INSTANCE.validate(documentRoot);
    if (diag.getSeverity() == Diagnostic.ERROR) {
      for (Diagnostic childDiag : diag.getChildren()) LogUtils.error(childDiag.getMessage());
      throw ExceptionUtils.fromMessage(
          "Maven Metadata model validation failed: %s", diag.getMessage());
    }
  }
  protected Diagnostic findProblem(Diagnostic diagnostic, EObject target) {
    if (ExpressionsValidator.DIAGNOSTIC_SOURCE.equals(diagnostic.getSource())
        && (diagnostic.getSeverity() != Diagnostic.OK)
        && diagnostic.getData().contains(target)) {
      return diagnostic;
    }

    for (Diagnostic child : diagnostic.getChildren()) {
      Diagnostic result = findProblem(child, target);

      if (result != null) {
        return result;
      }
    }

    return null;
  }
 public void merge(Diagnostic diagnostic) {
   if (diagnostic.getChildren().isEmpty()) add(diagnostic);
   else addAll(diagnostic);
 }
 public void addAll(Diagnostic diagnostic) {
   for (Diagnostic child : diagnostic.getChildren()) add(child);
 }
  @Override
  public void launch(
      ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor)
      throws CoreException {
    try {
      this.launch = launch;
      cleanup();

      final MitraLaunchConfigurationAdapter configAdapter =
          new MitraLaunchConfigurationAdapter(configuration);

      String consoleName = "Mitra Interpreter [" + configAdapter.getName() + "] " + mode;
      console = findConsole(consoleName);
      console.activate();
      initConsoleStream();

      clearConsole();

      final StandaloneExecutor executor =
          MitraStandaloneLaunchFactory.createExecutor(configAdapter);
      executor.getProjectContext().setMessageAcceptor(new ConsoleMessageAcceptor(console));

      console.addPatternMatchListener(new MitraConsoleTracker(executor.getProjectContext()));

      executor.getProjectContext().out = new PrintStream(createMessageStream(SWT.COLOR_BLACK));

      PrintStream err = new PrintStream(createMessageStream(SWT.COLOR_RED));
      executor.getProjectContext().err = err;

      executor.getProjectContext().log = new PrintStream(createMessageStream(SWT.COLOR_BLUE));

      try {
        executor.prepareInterpreter();
      } catch (Exception ex) {
        throw new CoreException(
            new Status(
                IStatus.ERROR,
                MitraDebugPlugin.PLUGIN_ID,
                "Error preparing Mitra interpreter: " + ex.getMessage(),
                ex));
      }

      executor
          .getDebugable()
          .addInterpreterListener(
              new IDebugableListener() {

                @Override
                public void terminated() {

                  out("Mitra interpreter terminated.");
                  try {
                    for (String fileName : configAdapter.getOutputModelFilenames().values()) {
                      URI uri = URI.createFileURI(fileName);
                      refreshPath(uri);
                    }
                    for (String fileName : configAdapter.getInputModelFilenames().values()) {
                      URI uri = URI.createFileURI(fileName);
                      refreshPath(uri);
                    }
                    if (configAdapter.isSaveTraces()) {
                      URI uri = URI.createFileURI(configAdapter.getTraceModelFilename());
                      refreshPath(uri);
                    }
                  } catch (CoreException ex) {
                    out("Error refreshing project path: " + ex);
                  }
                }

                @Override
                public void processDebugableEvent(DebugEventType eventType) {
                  // not interested in
                }

                @Override
                public void suspended() {
                  // not interested in
                }
              });

      ResourceSet rs = executor.getProjectContext().getResourceSet();
      int errorCount = 0;
      int warningCount = 0;
      int moduleCount = 0;
      // iterate over copy, as new resources may be
      // added while resolving proxies
      List<Resource> resources = new ArrayList<Resource>(rs.getResources());
      for (Resource resource : resources) {
        errorCount += resource.getErrors().size();
        warningCount += resource.getWarnings().size();

        if (resource.getContents().size() > 0) {
          for (EObject model : resource.getContents()) {

            if (model instanceof Module) {
              moduleCount++;
            }
            Diagnostic diagnostic = Diagnostician.INSTANCE.validate(model);
            for (Diagnostic child : diagnostic.getChildren()) {
              switch (child.getSeverity()) {
                case Diagnostic.ERROR:
                  if (!(model instanceof Module)) {
                    err.println(model.eResource().getURI() + ": " + child.getMessage());
                  }
                  errorCount++;
                  break;
                case Diagnostic.WARNING:
                  warningCount++;
              }
            }
          }
        }
      }

      MessageFormat form = new MessageFormat("Loaded {0}{1}, {2}.");
      double[] limits = {0, 1, 2};
      String[] moduleparts = {"no module", "1 module", "{0,number} modules"};
      ChoiceFormat moduleform = new ChoiceFormat(limits, moduleparts);
      form.setFormatByArgumentIndex(0, moduleform);
      String[] errorparts = {" successfully: 0 errors", ": 1 error", ": {1,number} errors"};
      ChoiceFormat errorform = new ChoiceFormat(limits, errorparts);
      form.setFormatByArgumentIndex(1, errorform);
      String[] warningparts = {"0 warnings", "1 warning", "{2,number} warnings"};
      ChoiceFormat warningform = new ChoiceFormat(limits, warningparts);
      form.setFormatByArgumentIndex(2, warningform);

      String msg = form.format(new Object[] {moduleCount, errorCount, warningCount});
      if (errorCount > 0) {
        msg += " Cannot start Mitra intpreter.";
      }
      out(msg);
      if (errorCount > 0 || moduleCount == 0) {
        throw new CoreException(new Status(IStatus.ERROR, MitraDebugPlugin.PLUGIN_ID, msg));
      }

      IDebuggable debugable = executor.getDebugable(); // debugable
      // supports
      // listeners!

      if (debugable == null) {
        throw new CoreException(
            new Status(
                IStatus.ERROR, MitraDebugPlugin.PLUGIN_ID, "No debuggable interpreter prepared."));
      }

      RuleDeclaration decl = debugable.getCalledRule();

      if (decl == null) {
        throw new CoreException(
            new Status(IStatus.ERROR, MitraDebugPlugin.PLUGIN_ID, "No rule selected."));
      }
      IDebugTarget debugTarget =
          new MitraDebugTarget(
              launch, new MitraDebugProxy(debugable), decl.eResource().getURI().toFileString());

      Display display = Display.getCurrent();
      if (display == null) { // start in normal thread
        if (log.isLoggable(Level.INFO)) {
          log.info("Start mitra interpreter via new thread");
        }
        out("Start Mitra interpreter on new thread");

        Thread interpreterThread = new Thread(debugable);
        if (mode.equals(ILaunchManager.DEBUG_MODE)) {
          debugable.enableDebug(true);
          interpreterThread.setName("MitraInterpreter");
          interpreterThread.start();
          launch.addDebugTarget(debugTarget);
        } else {
          interpreterThread.start();
        }
      } else { // start via display
        if (log.isLoggable(Level.INFO)) {
          log.info("Start mitra interpreter via display.asyncExec");
        }
        out("Start Mitra interpreter on display thread");

        if (mode.equals(ILaunchManager.DEBUG_MODE)) {
          debugable.enableDebug(true);
          display.asyncExec(debugable);
          // interpreterThread.setName("MitraInterpreter");
          // interpreterThread.start();
          launch.addDebugTarget(debugTarget);
        } else {
          display.asyncExec(debugable);
        }
      }
      out("Interpreter running...");
    } finally {
      console = null;
    }
  }