Java URL Normalize normalizeUrl(URL url)

Here you can find the source of normalizeUrl(URL url)

Description

Normalizes the given URL path to remove relative folder path references (e.g.

License

Apache License

Parameter

Parameter Description
url the URL to normalize

Return

URL

Declaration

public static final URL normalizeUrl(URL url) 

Method Source Code

//package com.java2s;
/**//from   ww  w. j  a va  2  s  . c  o  m
 * Copyright (C) 2014 OpenTravel Alliance (info@opentravel.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern pathPartPattern = Pattern.compile("(/[^/]*)/");

    /**
     * Normalizes the given URL path to remove relative folder path references (e.g. '/.' and
     * '/..').
     * 
     * @param url
     *            the URL to normalize
     * @return URL
     */
    public static final URL normalizeUrl(URL url) {
        String urlPath = url.getPath();
        URL result = url;

        if ((urlPath.indexOf("/./") >= 0) || (urlPath.indexOf("/../") >= 0)) {
            try {
                String sourceUrl = url.toExternalForm();
                StringBuilder targetUrl = new StringBuilder(sourceUrl.substring(0, sourceUrl.lastIndexOf(urlPath)));
                List<String> pathList = new ArrayList<String>();

                // Use the stack to resolve relative path references
                boolean pathStarted = false;
                Matcher m;

                while ((m = pathPartPattern.matcher(urlPath)).find()) {
                    String pathPart = m.group(1);

                    if (pathPart.equals("/") && !pathStarted) {
                        pathList.add(pathPart);

                    } else if (pathPart.equals("/.") || pathPart.equals("/")) {
                        // no action - discard
                        pathStarted = true;

                    } else if (pathPart.equals("/..")) {
                        if (!pathList.isEmpty()) {
                            pathList.remove(pathList.size() - 1);
                        }
                        pathStarted = true;

                    } else {
                        pathList.add(pathPart);
                        pathStarted = true;
                    }
                    urlPath = urlPath.substring(m.end(1));
                }
                if (urlPath.length() > 0) {
                    pathList.add(urlPath);
                }

                // Append the remaining path parts to the target URL
                for (String pathPart : pathList) {
                    targetUrl.append(pathPart);
                }
                if (url.getQuery() != null) {
                    targetUrl.append(url.getQuery());
                }
                result = new URL(targetUrl.toString());

            } catch (MalformedURLException e) {
                // ignore and return original URL
            }
        }
        return result;
    }
}

Related

  1. normalizeUrl(String url)
  2. normalizeUrl(String url)
  3. normalizeUrl(String url)
  4. normalizeURL(URL codebase)
  5. normalizeUrl(URL url)
  6. normalizeURL(URL url)
  7. normalizeUrl(URL url)
  8. normalizeUrl(URL url)
  9. normalizeURL(URL url)