private void clean(IFileStore root, boolean deleteIfEmpty, IProgressMonitor monitor)
     throws CoreException {
   if (monitor.isCanceled()) throw new OperationCanceledException();
   if (isUMLFile(root) && MDDUtil.isGenerated(root.toURI())) {
     safeDelete(root);
     return;
   }
   IFileStore[] children = root.childStores(EFS.NONE, null);
   for (int i = 0; i < children.length; i++) clean(children[i], false, monitor);
   if (deleteIfEmpty && root.childStores(EFS.NONE, null).length == 0) root.delete(EFS.NONE, null);
 }
  @Override
  public Object[] getChildren(Object element) {
    try {
      if (element instanceof IWorkspaceRoot) {
        IWorkspaceRoot root = (IWorkspaceRoot) element;

        List<Object> children = new ArrayList<Object>();

        children.addAll(Arrays.asList(root.members()));
        children.add(SdkDirectoryNode.INSTANCE);

        return children.toArray();
      } else if (element instanceof IContainer) {
        IContainer container = (IContainer) element;
        return filteredMembers(container).toArray();
      } else if (element instanceof IFileStore) {
        IFileStore fileStore = (IFileStore) element;
        return fileStore.childStores(EFS.NONE, null);
      } else if (element instanceof SdkDirectoryNode) {
        return ((SdkDirectoryNode) element).getLibraries();
      } else if (element instanceof SdkLibraryNode) {
        return ((SdkLibraryNode) element).getFiles();
      }
    } catch (CoreException ce) {
      // fall through
    }

    return NO_CHILDREN;
  }
示例#3
0
  /**
   * @param sourceStore the file to be copied
   * @param sourceRoot the source root
   * @param destinationRoot the destination root
   * @param monitor the progress monitor
   * @return true if the file is successfully copied, false if the operation did not go through for
   *     any reason
   */
  protected boolean copyFile(
      IFileStore sourceStore,
      IFileStore sourceRoot,
      IFileStore destinationRoot,
      IProgressMonitor monitor) {
    if (sourceStore == null) {
      return false;
    }

    boolean success = true;
    IFileStore[] sourceStores = null, targetStores = null;
    try {
      if (sourceStore.equals(sourceRoot)) {
        // copying the whole source
        sourceStores = sourceRoot.childStores(EFS.NONE, monitor);
        targetStores = new IFileStore[sourceStores.length];
        for (int i = 0; i < targetStores.length; ++i) {
          targetStores[i] = destinationRoot.getChild(sourceStores[i].getName());
        }
      } else if (sourceRoot.isParentOf(sourceStore)) {
        // finds the relative path of the file to be copied and maps to
        // the destination target
        sourceStores = new IFileStore[1];
        sourceStores[0] = sourceStore;

        targetStores = new IFileStore[1];
        String sourceRootPath = sourceRoot.toString();
        String sourcePath = sourceStore.toString();
        int index = sourcePath.indexOf(sourceRootPath);
        if (index > -1) {
          String relativePath = sourcePath.substring(index + sourceRootPath.length());
          targetStores[0] = destinationRoot.getFileStore(new Path(relativePath));
          // makes sure the parent folder is created on the destination side
          IFileStore parent = getFolderStore(targetStores[0]);
          if (parent != targetStores[0]) {
            parent.mkdir(EFS.NONE, monitor);
          }
        }
      }
      if (sourceStores == null) {
        // the file to be copied is not a child of the source root;
        // cannot copy
        success = false;
        sourceStores = new IFileStore[0];
        targetStores = new IFileStore[0];
      }

      for (int i = 0; i < sourceStores.length; ++i) {
        success = copyFile(sourceStores[i], targetStores[i], monitor) && success;
      }
    } catch (CoreException e) {
      // TODO: report the error
      success = false;
    }
    return success;
  }
 @Override
 public Object[] getChildren(Object object) {
   try {
     if (object instanceof IFileStore) {
       IFileStore fileStore = (IFileStore) object;
       return fileStore.childStores(EFS.NONE, null);
     }
   } catch (CoreException exception) {
     // fall through
   }
   return NO_CHILDREN;
 }
 private void encodeChildren(IFileStore dir, URI location, JSONObject result, int depth)
     throws CoreException {
   if (depth <= 0) return;
   JSONArray children = new JSONArray();
   IFileStore[] childStores = dir.childStores(EFS.NONE, null);
   for (IFileStore childStore : childStores) {
     IFileInfo childInfo = childStore.fetchInfo();
     String name = childInfo.getName();
     if (childInfo.isDirectory()) name += "/"; // $NON-NLS-1$
     URI childLocation = URIUtil.append(location, name);
     JSONObject childResult = ServletFileStoreHandler.toJSON(childStore, childInfo, childLocation);
     if (childInfo.isDirectory())
       encodeChildren(childStore, childLocation, childResult, depth - 1);
     children.put(childResult);
   }
   try {
     result.put(ProtocolConstants.KEY_CHILDREN, children);
   } catch (JSONException e) {
     // cannot happen
     throw new RuntimeException(e);
   }
 }
 private int compileTree(
     IRepository newRepo,
     IFileStore store,
     IFileStore baseDestination,
     int mode,
     IProgressMonitor monitor,
     IReferenceTracker refTracker,
     IProblemTracker problems)
     throws CoreException {
   try {
     if (!store.fetchInfo().exists()) return 0;
     if (!store.fetchInfo().isDirectory()) {
       boolean compiled =
           compileUnit(
               newRepo,
               store,
               mode,
               refTracker,
               problems,
               new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN));
       return compiled ? 1 : 0;
     }
     IFileStore[] children = store.childStores(EFS.NONE, null);
     int compiledUnits = 0;
     for (int i = 0; i < children.length; i++)
       compiledUnits +=
           compileTree(
               newRepo,
               children[i],
               baseDestination,
               mode,
               new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN),
               refTracker,
               problems);
     return compiledUnits;
   } finally {
     monitor.done();
   }
 }
  /**
   * Scan traceFolder for files that match the patterns specified in the template file. When there
   * is a match, the trace package element is used to determine the trace name and trace type.
   *
   * @param traceGroup The parent trace group element
   * @param parentElement The immediate parent trace group or folder element
   * @param traceFolder The folder to scan
   * @param recursionLevel The recursion level (needed to find directory traces under the
   *     traceFolder
   * @param monitor The progress monitor
   * @throws CoreException Thrown by the file system implementation
   * @throws InterruptedException Thrown if operation was cancelled
   */
  private void generateElementsFromArchive(
      final RemoteImportTraceGroupElement traceGroup,
      final TracePackageElement parentElement,
      final IFileStore traceFolder,
      final int recursionLevel,
      IProgressMonitor monitor)
      throws CoreException, InterruptedException {

    int localRecursionLevel = recursionLevel + 1;
    IFileStore[] sources = traceFolder.childStores(EFS.NONE, monitor);

    for (int i = 0; i < sources.length; i++) {
      ModalContext.checkCanceled(monitor);
      SubMonitor subMonitor = SubMonitor.convert(monitor, sources.length);

      IFileStore fileStore = sources[i];
      IPath fullArchivePath = TmfTraceCoreUtils.newSafePath(fileStore.toURI().getPath());

      IFileInfo sourceInfo = fileStore.fetchInfo();
      if (!sourceInfo.isDirectory()) {

        String rootPathString = traceGroup.getRootImportPath();
        IPath rootPath = TmfTraceCoreUtils.newSafePath(rootPathString);
        IPath relativeTracePath = Path.EMPTY;
        if (rootPath.isPrefixOf(fullArchivePath)) {
          relativeTracePath = fullArchivePath.makeRelativeTo(rootPath);
        }
        Entry<Pattern, TracePackageTraceElement> matchingTemplateEntry =
            getMatchingTemplateElement(relativeTracePath);
        if (matchingTemplateEntry != null) {
          TracePackageTraceElement matchingTemplateElement = matchingTemplateEntry.getValue();
          String traceType = matchingTemplateElement.getTraceType();

          // If a single file is part of a directory trace, use the parent directory instead
          TracePackageElement parent = parentElement;
          if (matchesDirectoryTrace(relativeTracePath, matchingTemplateEntry)) {
            fullArchivePath = fullArchivePath.removeLastSegments(1);
            fDirectoryTraces.add(fullArchivePath);
            fileStore = fileStore.getParent();
            parent = parentElement.getParent();
            // Let the auto-detection choose the best trace type
            traceType = null;
          } else if ((localRecursionLevel > 1) && (!traceGroup.isRecursive())) {
            // Don't consider file traces on level 2 if it's not recursive
            continue;
          }
          String traceName = fullArchivePath.lastSegment();
          String fileName = fileStore.getName();
          // create new elements to decouple from input elements
          TracePackageTraceElement traceElement =
              new TracePackageTraceElement(parent, traceName, traceType);
          RemoteImportTraceFilesElement tracePackageFilesElement =
              new RemoteImportTraceFilesElement(traceElement, fileName, fileStore);
          tracePackageFilesElement.setVisible(false);
        }
      } else {
        if (traceGroup.isRecursive() || localRecursionLevel < 2) {
          RemoteImportFolderElement folder =
              new RemoteImportFolderElement(parentElement, fileStore.getName());
          generateElementsFromArchive(
              traceGroup, folder, fileStore, localRecursionLevel, subMonitor);
        }
      }
    }
  }
 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;
 }