Java URI Create convertToURI(String uriString, Map aliasMap)

Here you can find the source of convertToURI(String uriString, Map aliasMap)

Description

Replace an alias in a URI, if one is recognized.

License

Apache License

Parameter

Parameter Description
uriString A string with the initial uri to check for aliases.
aliasMap The map of known aliases to the associated URIs

Return

A new URI with the alias replaced, or the original if no alias is found.

Declaration

public static URI convertToURI(String uriString, Map<String, URI> aliasMap) 

Method Source Code


//package com.java2s;
/*//from w  ww .  ja va  2 s  .co m
 * Copyright 2008 Fedora Commons, Inc.
 *
 * 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;
import java.util.Map;

public class Main {
    /**
     * Replace an alias in a URI, if one is recognized.
     * @param uriString A string with the initial uri to check for aliases.
     * @param aliasMap The map of known aliases to the associated URIs
     * @return A new URI with the alias replaced, or the original if no alias is found.
     */
    public static URI convertToURI(String uriString, Map<String, URI> aliasMap) {
        try {
            URI uri = new URI(uriString);
            if (uri.isOpaque()) {
                // Attempt qname-to-URI substitution for aliased namespace prefixes
                URI mapping = aliasMap.get(uri.getScheme());
                if (mapping != null) {
                    uri = new URI(mapping.toString() + uri.getSchemeSpecificPart()
                            + (uri.getFragment() != null ? "#" + uri.getFragment() : ""));
                }
            }
            return uri;
        } catch (URISyntaxException e) {
            throw new RuntimeException("Bad URI syntax in resource", e);
        }
    }
}

Related

  1. addParams(URI uri, Map> params, Set overridenParams)
  2. addQueryParametersToUri(URI uri, Map parameters)
  3. addQueryParamsToUri(String uri, Map queryParams)
  4. addQueryString(final URI uri, final String queryString)
  5. convertToURI(String uriOrPath)
  6. convertToURI(String value)
  7. create(final String uri)
  8. createAbsoluteURI(URI base, String uri)
  9. createConnection(final String uri, final String authValue)