Java URI to Local Name getNameSpace(URI uri)

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

Description

get Name Space

License

Apache License

Declaration

public static URI getNameSpace(URI uri) throws URISyntaxException 

Method Source Code


//package com.java2s;
/*/*from ww w.ja  v  a 2s  . c o  m*/
 * 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.URISyntaxException;

public class Main {
    public static URI getNameSpace(URI uri) throws URISyntaxException {
        return getNameSpace(uri.toASCIIString());
    }

    public static URI getNameSpace(String uriString) throws URISyntaxException {
        int localNameIdx = getLocalNameIndex(uriString);
        return new URI(uriString.substring(0, localNameIdx - 1));
    }

    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. getName(URI uri)
  2. getName(URI uri)
  3. getName(URI uri)
  4. getName(URI uri)
  5. getNameSpace(URI uri)