/**
   * @param ex
   * @return
   */
  private boolean isThisProjectError(BuildException ex) {
    // The cause must be a BuildException.
    BuildException e = (BuildException) ex.getCause();

    if (null == e) return true;

    Location baseScriptLocation = e.getLocation();
    String baseScriptPath = null;

    if (baseScriptLocation == null) {
      return true;
    }

    String baseScriptFileName = baseScriptLocation.getFileName();
    if (baseScriptFileName == null) {
      return true;
    }
    baseScriptPath =
        baseScriptFileName.substring(0, baseScriptFileName.lastIndexOf(File.separator));

    assert baseScriptPath != null;

    Throwable current = e;
    Throwable old = current;
    while (current != null) {
      old = current;
      current = current.getCause();
    }
    if (old instanceof BuildException) {
      Location extScriptLocation = ((BuildException) old).getLocation();

      if (extScriptLocation != null) {
        String extScriptFileName = extScriptLocation.getFileName();

        // if the path of the bottom most level build file in exception is different with the second
        // level, means failed on dependency
        if (extScriptFileName != null && !extScriptFileName.startsWith(baseScriptPath)) {
          log(
              "Task failed on its dependency : "
                  + extScriptLocation.getFileName()
                  + " Original script path "
                  + baseScriptPath,
              Project.MSG_ERR); // $NON-NLS-1$ //$NON-NLS-2$
          return false;
        }
      }
    }
    return true;
  }
  /**
   * Returns <code>file</code>'s path relative to <code>location</code>'s parent folder.
   *
   * <p>If <code>file</code> and <code>location</code> have a common prefix, this method will return
   * a path to <code>file</code> that is relative to <code>location</code> Examples:
   *
   * <p>If parent is the parent of location it will return "", so that
   */
  public static String getRelativePath(File file, Location location) {
    String filePath = file.getAbsolutePath();
    File locationParent = new File(location.getFileName()).getParentFile();
    if (locationParent == null) {
      return filePath;
    }

    String locationParentPath = locationParent.getAbsolutePath();
    if (filePath.startsWith(locationParentPath)) {
      if (filePath.length() == locationParentPath.length()) {
        return "";
      } else {
        return filePath.substring(getPathWithSeparator(locationParentPath).length());
      }
    }

    int commonPrefixLength = getCommonPathPrefix(filePath, locationParentPath).length();
    if (commonPrefixLength > 0) {
      String locationSubPath = locationParentPath.substring(commonPrefixLength);
      // + 1 for the one folder you have to back out of, since the last / is part of the common path
      int folders = countOccurrences(locationSubPath, File.separatorChar) + 1;
      String fileSubPath = filePath.substring(commonPrefixLength);
      if (fileSubPath.charAt(0) == File.separatorChar) {
        fileSubPath = fileSubPath.substring(1);
      }
      StringBuilder builder = new StringBuilder();
      for (int i = 0; i < folders; i++) {
        builder.append("..").append(File.separatorChar);
      }
      builder.append(fileSubPath);
      return builder.toString();
    }

    return filePath;
  }