/**
  * Compiles a single source code unit.
  *
  * @param newRepo
  * @param source
  * @param baseDestination
  * @param mode
  * @param deferred
  * @param problemTracker
  * @throws CoreException
  */
 private boolean compileUnit(
     IRepository newRepo,
     final IFileStore source,
     int mode,
     IReferenceTracker refTracker,
     final IProblemTracker problemTracker,
     IProgressMonitor monitor)
     throws CoreException {
   if (monitor.isCanceled()) throw new OperationCanceledException();
   if (!source.fetchInfo().exists()) return false;
   Assert.isLegal(!source.fetchInfo().isDirectory());
   ICompiler compiler =
       findCompiler(source, newRepo.getProperties().getProperty(IRepository.DEFAULT_LANGUAGE));
   if (compiler == null) return false;
   // TODO need to provide a way for receiving the encoding to be used
   // (IResource has that)
   monitor.beginTask("Compiling " + source.toURI().getPath(), 1);
   Reader contents = null;
   try {
     contents =
         new InputStreamReader(
             new BufferedInputStream(source.openInputStream(EFS.NONE, null), 8192));
     compiler.compile(
         contents,
         new CompilationContext(
             refTracker,
             new LocalProblemTracker(problemTracker, source),
             newRepo,
             (mode & DEBUG) != 0));
   } catch (AbortedScopeCompilationException e) {
     // continue with next source unit
   } finally {
     if (contents != null)
       try {
         contents.close();
       } catch (IOException e) {
         // we don't care about these
       }
     monitor.done();
   }
   // we compiled this unit
   return true;
 }
 public synchronized IProblem[] compile(
     IFileStore[] toCompile,
     IRepository repository,
     final LocationContext context,
     final int mode,
     IProgressMonitor monitor)
     throws CoreException {
   boolean created = false;
   if (monitor == null) monitor = new NullProgressMonitor();
   if (toCompile == null) toCompile = context.getSourcePaths();
   monitor.beginTask("Compiling source files", IProgressMonitor.UNKNOWN);
   // empty all output locations before we start
   clean(context, new SubProgressMonitor(monitor, toCompile.length));
   if (mode == CLEAN || toCompile.length == 0) {
     // nothing else to do
     return new IProblem[0];
   }
   if (repository == null) {
     repository =
         MDDCore.createRepository(MDDUtil.fromJavaToEMF(context.getDefaultOutputPath().toURI()));
     for (IFileStore relatedRootPath : context.getRelatedPaths())
       for (IFileStore relatedEntry : relatedRootPath.childStores(EFS.NONE, null))
         if (isUMLFile(relatedEntry))
           repository.loadPackage(MDDUtil.fromJavaToEMF(relatedEntry.toURI()));
     created = true;
   }
   final BasicProblemTracker problemTracker = new BasicProblemTracker();
   final ReferenceTracker refTracker = new ReferenceTracker();
   final IFileStore[] tmpToCompile = toCompile;
   try {
     final IRepository[] tmpRepo = {repository};
     final IProgressMonitor[] tmpMonitor = {monitor};
     final CoreException[] tmpException = {null};
     repository.runInRepository(
         new Runnable() {
           public void run() {
             try {
               for (int i = 0; i < tmpToCompile.length; i++) {
                 IFileStore baseOutputPath = context.getOutputPath(tmpToCompile[i]);
                 if (baseOutputPath == null) baseOutputPath = context.getDefaultOutputPath();
                 int unitsInTree =
                     compileTree(
                         tmpRepo[0],
                         tmpToCompile[i],
                         baseOutputPath,
                         mode,
                         new SubProgressMonitor(tmpMonitor[0], IProgressMonitor.UNKNOWN),
                         refTracker,
                         problemTracker);
                 if (unitsInTree == 0)
                   problemTracker.add(
                       new UnclassifiedProblem(IProblem.Severity.INFO, "Nothing to compile"));
               }
               refTracker.resolve(tmpRepo[0], problemTracker);
             } catch (CoreException e) {
               tmpException[0] = e;
             }
           }
         });
     if (tmpException[0] != null) throw tmpException[0];
     repository.save(monitor);
   } catch (OperationCanceledException e) {
     // ignore
   } catch (Exception e) {
     problemTracker.add(new InternalProblem(e));
     LogUtils.logError(MDDCore.PLUGIN_ID, "Unexpected exception while compiling", e);
   } finally {
     monitor.done();
     if (created) repository.dispose();
   }
   IProblem[] allProblems = problemTracker.getAllProblems();
   Arrays.sort(allProblems);
   return allProblems;
 }