Java URI Normalize normalize(String uri, String resourceLocation, String rootLocation)

Here you can find the source of normalize(String uri, String resourceLocation, String rootLocation)

Description

a 'null' rootLocation argument will causes uri that begins with a '/' to be treated as a workspace relative resource (i.e.

License

Open Source License

Declaration

public static String normalize(String uri, String resourceLocation,
        String rootLocation) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w  w w.j ava 2s  .c o m*/
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 *     IBM Corporation - Initial API and implementation
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 * 20071205   211262 ericdp@ca.ibm.com - Eric Peters, CopyWSDLTreeCommand fails to copy ?wsdl
 *******************************************************************************/

import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

public class Main {
    protected static final String PLATFORM_RESOURCE_PROTOCOL = "platform:/resource/";
    protected static final String PROTOCOL_PATTERN = ":";

    public static String normalize(String uri) {
        if (uri != null) {
            String protocol = getProtocol(uri);
            String file = uri;

            if (protocol != null) {
                try {
                    // 
                    URL url = new URL(uri);
                    // we use a 'Path' on the 'file' part of the url in order to normalize the '.' and '..' segments
                    IPath path = new Path(url.getFile());
                    URL url2 = new URL(url.getProtocol(), url.getHost(),
                            url.getPort(), path.toString());
                    uri = url2.toString();
                } catch (Exception e) {
                }
            } else {
                IPath path = new Path(file);
                uri = path.toString();
            }
        }
        return uri;
    }

    /**
     * a 'null' rootLocation argument will causes uri that begins with a '/' to be treated as a workspace relative resource
     * (i.e. the string "platform:/resource" is prepended and the uri is resolved via the Platform object)
     */
    public static String normalize(String uri, String resourceLocation,
            String rootLocation) {
        String result = null;

        if (uri != null) {
            // is the uri a url
            if (hasProtocol(uri)) {
                if (isPlatformResourceProtocol(uri)) {
                    result = resolvePlatformUrl(uri);
                } else {
                    result = uri;
                }
            }

            // is uri absolute
            //
            if (result == null) {
                if (uri.indexOf(":") != -1 || uri.startsWith("/")
                        || uri.startsWith("\\")) {
                    result = uri;
                }
            }

            // if uri is relative to the resourceLocation
            //
            if (result == null && resourceLocation != null) {
                if (resourceLocation.endsWith("/")) {
                    result = resourceLocation + uri;
                } else {
                    result = resourceLocation + "/../" + uri;
                }
            }

            if (result == null) {
                result = uri;
            }

            result = normalize(result);
        }

        //System.out.println("normalize(" + uri + ", " + resourceLocation + ", " + rootLocation + ") = " + result);
        return result;
    }

    protected static String getProtocol(String uri) {
        String result = null;
        if (uri != null) {
            int index = uri.indexOf(PROTOCOL_PATTERN);
            if (index > 2) // assume protocol with be length 3 so that the'C' in 'C:/' is not interpreted as a protocol
            {
                result = uri
                        .substring(0, index + PROTOCOL_PATTERN.length());
            }
        }
        return result;
    }

    public static boolean hasProtocol(String uri) {
        boolean result = false;
        if (uri != null) {
            int index = uri.indexOf(PROTOCOL_PATTERN);
            if (index != -1 && index > 2) // assume protocol with be length 3 so that the'C' in 'C:/' is not interpreted as a protocol
            {
                result = true;
            }
        }
        return result;
    }

    public static boolean isPlatformResourceProtocol(String uri) {
        return uri != null && uri.startsWith(PLATFORM_RESOURCE_PROTOCOL);
    }

    protected static String resolvePlatformUrl(String urlspec) {
        String result = null;
        try {
            urlspec = urlspec.replace('\\', '/');
            URL url = new URL(urlspec);
            URL resolvedURL = FileLocator.resolve(url);
            result = resolvedURL.toString();
        } catch (Exception e) {
        }
        return result;
    }
}

Related

  1. normalize(final String uri)
  2. normalize(URI input)
  3. normalize(URI location)
  4. normalize(URI uri)
  5. normalizedSetCookiePath(final String path, final URI originUri)