protected Path convertToAbsolute(final Path path) {
    if (path != null) {
      final RelativePathService service = property().service(RelativePathService.class);

      if (service == null) {
        if (enclosed() && path.segmentCount() > 0 && path.segment(0).equals("..")) {
          return null;
        }

        Path absolute = null;

        for (Path root : getBasePaths()) {
          try {
            final File file = root.append(path).toFile().getCanonicalFile();
            absolute = new Path(file.getPath());

            if (file.exists()) {
              break;
            }
          } catch (IOException e) {
            // Intentionally ignoring to continue to the next root. If none of the roots
            // produce a viable absolute path, a null return from this method signifies
            // being unable to convert the relative path. That is sufficient.
          }
        }

        return absolute;
      } else {
        return service.convertToAbsolute(path);
      }
    }

    return null;
  }
    public FileSystemNode find(final Path path) {
      for (FileSystemNode root : this.roots) {
        final Path rootPath = new Path(root.getFile().getPath());

        if (rootPath.isPrefixOf(path)) {
          return root.find(path.makeRelativeTo(rootPath));
        }
      }

      return null;
    }
  public static Set<String> getPossibleProfileIds(
      NewLiferayPluginProjectOp op, boolean includeNewProfiles) {
    final String activeProfilesValue = op.getActiveProfilesValue().content();

    final Path currentLocation = op.getLocation().content();

    final File param = currentLocation != null ? currentLocation.toFile() : null;

    final List<String> systemProfileIds =
        op.getProjectProvider().content().getData("profileIds", String.class, param);

    final ElementList<NewLiferayProfile> newLiferayProfiles = op.getNewLiferayProfiles();

    final Set<String> possibleProfileIds = new HashSet<String>();

    if (!CoreUtil.isNullOrEmpty(activeProfilesValue)) {
      final String[] vals = activeProfilesValue.split(",");

      if (!CoreUtil.isNullOrEmpty(vals)) {
        for (String val : vals) {
          if (!possibleProfileIds.contains(val) && !val.contains(StringPool.SPACE)) {
            possibleProfileIds.add(val);
          }
        }
      }
    }

    if (!CoreUtil.isNullOrEmpty(systemProfileIds)) {
      for (Object systemProfileId : systemProfileIds) {
        if (systemProfileId != null) {
          final String val = systemProfileId.toString();

          if (!possibleProfileIds.contains(val) && !val.contains(StringPool.SPACE)) {
            possibleProfileIds.add(val);
          }
        }
      }
    }

    if (includeNewProfiles) {
      for (NewLiferayProfile newLiferayProfile : newLiferayProfiles) {
        final String newId = newLiferayProfile.getId().content();

        if ((!CoreUtil.isNullOrEmpty(newId))
            && (!possibleProfileIds.contains(newId))
            && (!newId.contains(StringPool.SPACE))) {
          possibleProfileIds.add(newId);
        }
      }
    }

    return possibleProfileIds;
  }
    public FileSystemNode find(final Path path) {
      final int pathSegmentCount = path.segmentCount();

      if (pathSegmentCount == 0) {
        return this;
      } else {
        final String firstSegment = path.segment(0);

        for (FileSystemNode child : getChildren()) {
          if (child.getFile().getName().equals(firstSegment)) {
            return child.find(path.removeFirstSegments(1));
          }
        }

        return null;
      }
    }
  protected Path convertToRelative(final Path path) {
    if (path != null) {
      final RelativePathService service = property().service(RelativePathService.class);

      if (service == null) {
        if (enclosed()) {
          for (Path root : getBasePaths()) {
            if (root.isPrefixOf(path)) {
              return path.makeRelativeTo(root);
            }
          }
        } else {
          final String pathDevice = path.getDevice();

          for (Path root : getBasePaths()) {
            if (MiscUtil.equal(pathDevice, root.getDevice())) {
              return path.makeRelativeTo(root);
            }
          }
        }
      } else {
        return service.convertToRelative(path);
      }
    }

    return null;
  }
  public static void updateLocation(final NewLiferayPluginProjectOp op, final Path baseLocation) {
    final String projectName = getProjectNameWithSuffix(op);

    if (baseLocation == null) {
      return;
    }

    final String lastSegment = baseLocation.lastSegment();

    if (baseLocation != null && baseLocation.segmentCount() > 0) {
      if (lastSegment.equals(projectName)) {
        return;
      }
    }

    final Path newLocation = baseLocation.append(projectName);

    op.setLocation(newLocation);
  }
  protected final Status validateExtensions(final Path path) {
    if (this.fileExtensionsService != null) {
      final String fileName = path.lastSegment();

      if (fileName != null) {
        final List<String> extensions = this.fileExtensionsService.extensions();
        final int count = (extensions == null ? 0 : extensions.size());

        if (count > 0) {
          final String trimmedFileName = fileName.trim();
          final int lastdot = trimmedFileName.lastIndexOf('.');
          final String extension;

          if (lastdot == -1) {
            extension = "";
          } else {
            extension = trimmedFileName.substring(lastdot + 1);
          }

          boolean match = false;

          if (extension != null && extension.length() != 0) {
            for (String ext : extensions) {
              if (extension.equalsIgnoreCase(ext)) {
                match = true;
                break;
              }
            }
          }

          if (!match) {
            final String message;

            if (count == 1) {
              message = invalidFileExtensionOne.format(trimmedFileName, extensions.get(0));
            } else if (count == 2) {
              message =
                  invalidFileExtensionTwo.format(
                      trimmedFileName, extensions.get(0), extensions.get(1));
            } else {
              final StringBuilder buf = new StringBuilder();

              for (String ext : extensions) {
                if (buf.length() != 0) {
                  buf.append(", ");
                }

                buf.append(ext);
              }

              message = invalidFileExtensionMultiple.format(trimmedFileName, buf.toString());
            }

            return Status.createErrorStatus(message);
          }
        }
      }
    }

    return Status.createOkStatus();
  }
  @Override
  protected String browse(final SapphireRenderingContext context) {
    final Property property = property();
    final List<Path> roots = getBasePaths();
    String selectedAbsolutePath = null;

    final List<String> extensions;

    if (this.fileExtensionService == null) {
      extensions = this.staticFileExtensionsList;
    } else {
      extensions = this.fileExtensionService.extensions();
    }

    if (enclosed()) {
      final List<IContainer> baseContainers = new ArrayList<IContainer>();

      for (Path path : roots) {
        final IContainer baseContainer = getWorkspaceContainer(path.toFile());

        if (baseContainer != null) {
          baseContainers.add(baseContainer);
        } else {
          break;
        }
      }

      final ITreeContentProvider contentProvider;
      final ILabelProvider labelProvider;
      final ViewerComparator viewerComparator;
      final Object input;

      if (roots.size() == baseContainers.size()) {
        // All paths are in the Eclipse Workspace. Use the available content and label
        // providers.

        contentProvider = new WorkspaceContentProvider(baseContainers);
        labelProvider = new WorkbenchLabelProvider();
        viewerComparator = new ResourceComparator();
        input = ResourcesPlugin.getWorkspace().getRoot();
      } else {
        // At least one of the roots is not in the Eclipse Workspace. Use custom file
        // system content and label providers.

        contentProvider = new FileSystemContentProvider(roots);
        labelProvider = new FileSystemLabelProvider(context);
        viewerComparator = new FileSystemNodeComparator();
        input = new Object();
      }

      final ElementTreeSelectionDialog dialog =
          new ElementTreeSelectionDialog(context.getShell(), labelProvider, contentProvider);

      dialog.setTitle(property.definition().getLabel(false, CapitalizationType.TITLE_STYLE, false));
      dialog.setMessage(
          createBrowseDialogMessage(
              property.definition().getLabel(true, CapitalizationType.NO_CAPS, false)));
      dialog.setAllowMultiple(false);
      dialog.setHelpAvailable(false);
      dialog.setInput(input);
      dialog.setComparator(viewerComparator);

      final Path currentPathAbsolute = convertToAbsolute((Path) ((Value<?>) property).content());

      if (currentPathAbsolute != null) {
        Object initialSelection = null;

        if (contentProvider instanceof WorkspaceContentProvider) {
          final URI uri = currentPathAbsolute.toFile().toURI();
          final IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();

          final IFile[] files = wsroot.findFilesForLocationURI(uri);

          if (files.length > 0) {
            final IFile file = files[0];

            if (file.exists()) {
              initialSelection = file;
            }
          }

          if (initialSelection == null) {
            final IContainer[] containers = wsroot.findContainersForLocationURI(uri);

            if (containers.length > 0) {
              final IContainer container = containers[0];

              if (container.exists()) {
                initialSelection = container;
              }
            }
          }
        } else {
          initialSelection =
              ((FileSystemContentProvider) contentProvider).find(currentPathAbsolute);
        }

        if (initialSelection != null) {
          dialog.setInitialSelection(initialSelection);
        }
      }

      if (this.type == FileSystemResourceType.FILE) {
        dialog.setValidator(new FileSelectionStatusValidator());
      } else if (this.type == FileSystemResourceType.FOLDER) {
        dialog.addFilter(new ContainersOnlyViewerFilter());
      }

      if (!extensions.isEmpty()) {
        dialog.addFilter(new ExtensionBasedViewerFilter(extensions));
      }

      if (dialog.open() == Window.OK) {
        final Object firstResult = dialog.getFirstResult();

        if (firstResult instanceof IResource) {
          selectedAbsolutePath = ((IResource) firstResult).getLocation().toString();
        } else {
          selectedAbsolutePath = ((FileSystemNode) firstResult).getFile().getPath();
        }
      }
    } else if (this.type == FileSystemResourceType.FOLDER) {
      final DirectoryDialog dialog = new DirectoryDialog(context.getShell());
      dialog.setText(
          property.definition().getLabel(true, CapitalizationType.FIRST_WORD_ONLY, false));
      dialog.setMessage(
          createBrowseDialogMessage(
              property.definition().getLabel(true, CapitalizationType.NO_CAPS, false)));

      final Value<?> value = (Value<?>) property;
      final Path path = (Path) value.content();

      if (path != null) {
        dialog.setFilterPath(path.toOSString());
      } else if (roots.size() > 0) {
        dialog.setFilterPath(roots.get(0).toOSString());
      }

      selectedAbsolutePath = dialog.open();
    } else {
      final FileDialog dialog = new FileDialog(context.getShell());
      dialog.setText(
          property.definition().getLabel(true, CapitalizationType.FIRST_WORD_ONLY, false));

      final Value<?> value = (Value<?>) property;
      final Path path = (Path) value.content();

      if (path != null && path.segmentCount() > 1) {
        dialog.setFilterPath(path.removeLastSegments(1).toOSString());
        dialog.setFileName(path.lastSegment());
      } else if (roots.size() > 0) {
        dialog.setFilterPath(roots.get(0).toOSString());
      }

      if (!extensions.isEmpty()) {
        final StringBuilder buf = new StringBuilder();

        for (String extension : extensions) {
          if (buf.length() > 0) {
            buf.append(';');
          }

          buf.append("*.");
          buf.append(extension);
        }

        dialog.setFilterExtensions(new String[] {buf.toString()});
      }

      selectedAbsolutePath = dialog.open();
    }

    if (selectedAbsolutePath != null) {
      final Path relativePath = convertToRelative(new Path(selectedAbsolutePath));

      if (relativePath != null) {
        String result = relativePath.toPortableString();

        if (this.includeLeadingSlash) {
          result = "/" + result;
        }

        return result;
      }
    }

    return null;
  }