Java URI to Local Name getLocalName(URI uri)

Here you can find the source of getLocalName(URI uri)

Description

get Local Name

License

Apache License

Declaration

public static String getLocalName(URI uri) 

Method Source Code


//package com.java2s;
/*/*from w  w w  . j a  v  a  2s  .c  om*/
 * Copyright (c) 2013. Knowledge Media Institute - The Open University
 *
 * 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.URI;

import java.net.URL;

public class Main {
    public static String getLocalName(URI uri) {
        if (uri == null)
            return null;

        return getLocalName(uri.toASCIIString());
    }

    public static String getLocalName(URL url) {
        if (url == null)
            return null;
        return getLocalName(url.toString());
    }

    public static String getLocalName(String uriString) {

        if (uriString == null || uriString.equals(""))
            return uriString;

        int len = uriString.length();
        if (uriString.endsWith("/")) {
            uriString = uriString.substring(0, len - 1);
        }
        int idx = uriString.indexOf('^');
        if (idx >= 0) {
            uriString = uriString.substring(0, idx);
        }
        int localNameIdx = getLocalNameIndex(uriString);
        if (localNameIdx == 0) {
            // It's an absolute name
            return uriString;
        }

        return uriString.substring(localNameIdx);
    }

    private static int getLocalNameIndex(String uri) {
        int separatorIdx = uri.indexOf('#');

        if (separatorIdx < 0) {
            separatorIdx = uri.lastIndexOf('/');
        }

        if (separatorIdx < 0) {
            separatorIdx = uri.lastIndexOf(':');
        }

        if (separatorIdx < 0) {
            throw new IllegalArgumentException("No separator character founds in URI: " + uri);
        }

        return separatorIdx + 1;
    }
}

Related

  1. getLocalDomainURI(String originalHost, String dirPath, String childUri)
  2. getLocalName(String uri)
  3. getLocalName(URI uri)
  4. getLocalPart(String uri)
  5. getName(URI uri)
  6. getName(URI uri)
  7. getName(URI uri)