Java Stack Usage removeDotSegments(String relativePath)

Here you can find the source of removeDotSegments(String relativePath)

Description

remove Dot Segments

License

Open Source License

Declaration

public static String removeDotSegments(String relativePath) 

Method Source Code

//package com.java2s;
/**/*from   w  ww . j a v  a 2  s. c  o  m*/
 * aletheia
 * A browser like application to send raw http requests. It is designed for 
 * debugging and finding security issues in web applications. For the current 
 * version and more informations visit <http://code.google.com/p/aletheia>
 * 
 * Copyright (c) 2010-2015 Christoph Kappestein <k42b3.x@gmail.com>
 * 
 * This file is part of Aletheia. Aletheia is free software: you can 
 * redistribute it and/or modify it under the terms of the GNU 
 * General Public License as published by the Free Software Foundation, 
 * either version 3 of the License, or at any later version.
 * 
 * Aletheia is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Aletheia. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Stack;

public class Main {
    public static String removeDotSegments(String relativePath) {
        // remove query or fragment part if any
        int pos = relativePath.indexOf('?');

        if (pos != -1) {
            relativePath = relativePath.substring(0, pos);
        }

        pos = relativePath.indexOf('#');

        if (pos != -1) {
            relativePath = relativePath.substring(0, pos);
        }

        // if the path contains no slash we have nothing to resolve
        if (relativePath.indexOf('/') == -1) {
            return relativePath;
        }

        String[] parts = relativePath.split("/");
        Stack<String> path = new Stack<String>();
        String part;

        for (int i = 0; i < parts.length; i++) {
            part = parts[i].trim();

            if (part.isEmpty() || part.equals(".")) {
            } else if (part.equals("..")) {
                path.pop();
            } else {
                path.add(part);
            }
        }

        // build absolute url
        String absoluteUrl = "";

        if (path.size() > 0) {
            for (int i = 0; i < path.size(); i++) {
                if (i > 0 && path.get(i).indexOf('.') != -1 && path.get(i - 1).equals(path.get(i))) {
                    // if the element before has the same name and it contains
                    // an dot we have probably an file name
                    continue;
                }

                absoluteUrl += "/" + path.get(i);
            }
        } else {
            absoluteUrl = "/";
        }

        // add last slash
        if (relativePath.endsWith("/") && !absoluteUrl.endsWith("/")) {
            absoluteUrl = absoluteUrl + "/";
        }

        return absoluteUrl;
    }
}

Related

  1. getRelativePath(Stack pathStack)
  2. getSyllables(String pinyin)
  3. isDoubleQuote(Stack bufStack)
  4. normalizeAbsolutePath(String curDir)
  5. removeBackets(String cont)
  6. removeParenthesis(String text)
  7. resolveOneLineExpression(String line, String space, List target)
  8. resolveSeparateIfSentence(List lines, int cursor, List target, String space)
  9. split(final String str)