Java URI Parse parseRepositoryItemUri(URI uri)

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

Description

Parses the components of the given URI and returns each component in a string array.

License

Apache License

Parameter

Parameter Description
uri the repository item URI to be parsed

Return

String[]

Declaration

public static String[] parseRepositoryItemUri(URI uri) 

Method Source Code

//package com.java2s;
/**//from  ww  w.  ja  v  a  2 s.c o m
 * Copyright (C) 2014 OpenTravel Alliance (info@opentravel.org)
 *
 * 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.io.UnsupportedEncodingException;
import java.net.URI;

import java.net.URLDecoder;

public class Main {
    /**
     * Parses the components of the given URI and returns each component in a string array. Optional
     * elements that are not provided are null in the array that is returned.
     * 
     * <p>
     * The array elements that are returned are as follows:
     * <ul>
     * <li>[0] - the repository ID (URI authority)</li>
     * <li>[1] - the repository item's assigned namespace (optional)</li>
     * <li>[2] - the repository item's filename</li>
     * <li>[3] - the repository item's version scheme (URI fragment; optional)</li>
     * </ul>
     * 
     * @param uri
     *            the repository item URI to be parsed
     * @return String[]
     */
    public static String[] parseRepositoryItemUri(URI uri) {
        if ((uri.getScheme() == null) || !uri.getScheme().equals("otm")) {
            throw new IllegalArgumentException(
                    "The URI provided is not valid for an OTM repository: " + uri.toString());
        }
        String[] uriParts = new String[4];
        String path = uri.getPath();
        int pathSeparatorIdx;

        uriParts[0] = uri.getAuthority();
        uriParts[3] = uri.getFragment();

        if (path.startsWith("/")) {
            path = path.substring(1);
        }

        if ((pathSeparatorIdx = path.lastIndexOf('/')) < 0) {
            uriParts[2] = path;

        } else {
            if (pathSeparatorIdx < (path.length() - 1)) {
                uriParts[2] = path.substring(pathSeparatorIdx + 1);
            }
            try {
                uriParts[1] = URLDecoder.decode(path.substring(0, pathSeparatorIdx), "UTF-8");

            } catch (UnsupportedEncodingException e) {
                // Should never happen - UTF-8 is supported on all known platforms
            }
        }
        return uriParts;
    }
}

Related

  1. parseQueryString(URI uri)
  2. parseQueryString(URI uri, M queryParams)
  3. parseRegionName(URI endpoint)
  4. parseRegionName(URI endpoint)
  5. parseRegionName(URI endpoint)
  6. parseServiceName(URI endpoint)
  7. parseServiceName(URI endpoint)
  8. parseURI(final String string)
  9. parseURI(final String value)