Java URI to Local Name getLocalName(URI uri)

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

Description

Return the local name of a URI.

License

Open Source License

Parameter

Parameter Description
uri a parameter

Declaration

public static String getLocalName(URI uri) 

Method Source Code

//package com.java2s;
//License from project: GNU General Public License 

import java.net.URI;

public class Main {
    /**//w w w  . j av  a  2 s  .c  o m
     * Return the local name of a URI. This function is not equiavlent to
     * URI.getFragment() because it tries to handle handle slashy URI's 
     * such as the ones found in Dublin Core. It is equiavalent to
     * getLocalName(uri.toString()).
     * 
     * @param uri
     * @return
     */
    public static String getLocalName(URI uri) {
        return getLocalName(uri.toString());
    }

    /**
     * Return the local name of a URI string. This naive implementation splits
     * the URI from the position of a '#' character or the last occurunce of
     * '/' character. If neither of these characters are found, the parameter
     * itself is returned. 
     * 
     * @param uri
     * @return
     */
    public static String getLocalName(String uri) {
        int index = splitPos(uri);

        if (index == -1)
            return uri;

        return uri.substring(index + 1);
    }

    private static int splitPos(String uri) {
        int pos = uri.indexOf("#");

        if (pos == -1)
            pos = uri.lastIndexOf("/");

        return pos;
    }
}

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)