Splits the rdf-style uri into XML namespace uri and local name - Android XML

Android examples for XML:Namespace

Description

Splits the rdf-style uri into XML namespace uri and local name

Demo Code

/***************************************
 *            ViPER                    *
 *  The Video Processing               *
 *         Evaluation Resource         *
 *                                     *
 *  Distributed under the GPL license  *
 *        Terms available at gnu.org.  *
 *                                     *
 *  Copyright University of Maryland,  *
 *                      College Park.  *
 ***************************************/
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String uri = "java2s.com";
        System.out.println(java.util.Arrays.toString(splitIntoQName(uri)));
    }/*from  w ww.j  a  v  a  2s.c  om*/

    /**
     * Splits the rdf-style uri into namespace
     * uri and local name 
     * @param uri the uri to split
     * @return the split uri
     */
    public static String[] splitIntoQName(String uri) {
        String[] S = new String[] { "", "" };
        int i = getQNameSplitPoint(uri);
        if (i > 0) {
            S[0] = uri.substring(0, i);
        }
        i++;
        if (i < uri.length()) {
            S[1] = uri.substring(i);
        }
        return S;
    }

    /**
     * Given a uri that represents a qualified name, tries to 
     * determine the index of the split point between the namespace
     * and the local name. Returns the last index of #, /, or :,
     * checking for each. If the string contains no :, it doesn't count 
     * as a uri and nothing is returned.
     * @param uri the uri
     * @return the split point, or -1 if not found
     */
    public static int getQNameSplitPoint(String uri) {
        int col = uri.indexOf(":");
        if (col < 0) {
            return -1;
        } else {
            int hash = uri.indexOf("#");
            if (hash < 0) {
                int slash = Math.max(uri.lastIndexOf("/"),
                        uri.lastIndexOf("\\"));
                if (slash > 0) {
                    return slash;
                } else {
                    return col;
                }
            } else {
                return hash;
            }
        }
    }
}

Related Tutorials