Beispiel #1
0
  /**
   * "Разрешает" ссылку. <br>
   * Примеры: <br>
   *
   * <table>
   * <tr>
   * <td>current dir</td>
   * <td>this</td>
   * <td>link</td>
   * <td>result</td>
   * </tr>
   *
   * <tr>
   * <td>/opt/jdk/default</td>
   * <td>/opt/jdk/default</td>
   * <td>jdk1.8.0_05</td>
   * <td>/opt/jdk/jdk1.8.0_05</td>
   * </tr>
   *
   * <tr>
   * <td>/opt/jdk/default</td>
   * <td>/opt/jdk/default</td>
   * <td>./jdk1.8.0_05</td>
   * <td>/opt/jdk/jdk1.8.0_05</td>
   * </tr>
   *
   * <tr>
   * <td>/opt/jdk/default</td>
   * <td>/opt/jdk/default</td>
   * <td>../jdk1.8.0_05</td>
   * <td>/opt/jdk1.8.0_05</td>
   * </tr>
   *
   * <tr>
   * <td>/opt/jdk/default</td>
   * <td>/opt/jdk/default</td>
   * <td>../../jdk1.8.0_05</td>
   * <td>/jdk1.8.0_05</td>
   * </tr>
   *
   * <tr>
   * <td>/opt/jdk/default</td>
   * <td>/opt/jdk/default</td>
   * <td>../../../../jdk1.8.0_05</td>
   * <td>/jdk1.8.0_05</td>
   * </tr>
   *
   * </table>
   *
   * @param link куда указывает ссылка
   * @param currentDir текущий каталог
   * @return цель
   */
  public UnixPath resolveLink(UnixPath link, UnixPath currentDir) {
    if (currentDir == null) throw new IllegalArgumentException("currentDir==null");
    if (link == null) throw new IllegalArgumentException("path==null");
    if (link.isAbsolute()) return link;

    UnixPath p = !isAbsolute() ? getAbsolute(currentDir) : this;

    ArrayList<String> cmpts = new ArrayList<String>();
    cmpts.addAll(Arrays.asList(p.getComponents()));

    if (cmpts.size() > 0) cmpts.remove(cmpts.size() - 1);
    for (String cmpt : link.getComponents()) {
      if (cmpt.equals(current)) continue;
      if (cmpt.equals(empty)) continue;
      if (cmpt.equals(parent)) {
        if (cmpts.size() > 1) {
          cmpts.remove(cmpts.size() - 1);
          continue;
        } else {
          if (resolveLink_allowRetNull()) {
            return null;
          } else {
            continue;
          }
        }
      }
      cmpts.add(cmpt);
    }
    return new UnixPath(cmpts.toArray(new String[] {}));
  }
Beispiel #2
0
  /**
   * Возвращает абсолютный путь относительно текущего каталога
   *
   * @param currentDir текущий каталог
   * @return абсолютный путь
   */
  public UnixPath getAbsolute(UnixPath currentDir) {
    if (this.isAbsolute()) return this;
    if (currentDir == null) throw new IllegalArgumentException("currentDir==null");
    if (!currentDir.isAbsolute()) throw new IllegalArgumentException("!currentDir.isAbsolute()");
    //        ArrayList<String> cmpts = new ArrayList<String>();
    //        cmpts.addAll(Arrays.asList(currentDir.getComponents()));
    //        for( String cmpt : components ){
    //            if( cmpt.equals(empty) )continue;
    //            if( cmpt.equals(current) )continue;
    //            if( cmpt.equals(parent) ){
    //                if( cmpts.size()>1 ){
    //                    cmpts.remove(cmpts.size()-1);
    //                    continue;
    //                }else{
    //                    if( getAbsolute_allowRetNull() ){
    //                        return null;
    //                    }else{
    //                        continue;
    //                    }
    //                }
    //            }
    //            cmpts.add(cmpt);
    //        }
    //        return new UnixPath(cmpts.toArray(new String[]{}));

    ArrayList<String> cmpts = new ArrayList<String>();
    for (String c : currentDir.getComponents()) {
      cmpts.add(c);
    }
    for (String c : this.getComponents()) {
      cmpts.add(c);
    }
    return new UnixPath(cmpts.toArray(new String[] {}));
  }