org.nbnResolving.common
Class URNUtils

java.lang.Object
  extended by org.nbnResolving.common.URNUtils

public class URNUtils
extends Object

Class to generate URNs and/or URN-Checksums See the URN documentation and www.persistent-identifier.de Some URL normalizing utility functions are also implemented.

Since:
20060626
Version:
20130524
Author:
Kadir Karaca Koçer, German National Library
See Also:
URI, {@link "http://tools.ietf.org/html/rfc2141"} {@link "http://tools.ietf.org/html/rfc3188"} {@link "http://tools.ietf.org/html/rfc3986"}

Constructor Summary
URNUtils()
           
 
Method Summary
static AbstractMap<String,String> addMaps(AbstractMap<String,String> map1, AbstractMap<String,String> map2)
          Adds two given maps and returns a map that contains key|value pairs of both.
static String addParamsToUrl(String urlStr, AbstractMap<String,String> params)
          Adds given new parameters to existing URL.
static String addQueryToUrl(String urlStr, String aq)
          Adds given new query to existing URL.
static String buildUrl(String protocol, String userInfo, String host, int port, String path, AbstractMap<String,String> params, boolean encodeParams, String anchor, boolean checkJavaURI)
          Builds an URI from its given components.
static String correctUrlString(String urlStr, boolean correctEncoding, boolean checkJavaURI)
          Tries to return a RFC compatible, normalized URL as string from a given string.
static String correctUtf8error(String strToCorrect)
          Corrects wrong converted or encoded strings.
static int getChecksum(String tmpurn)
          Calculates the checksum of the given string.
static String getDateURN(String ns)
          Returns a valid URN for the given Namespace beginning with actual date.
static String getNormalisedPath(String path)
          Normalises given URL Path.
static TreeMap<String,String> getParameterMap(String query, String regex)
          Parses a URL query and returns key, value pairs as a TreeMap.
static TreeMap<String,String> getParameterMap(URL url)
          Parses an URL query and returns key|value pairs as a TreeMap.
static String getRandomURN(String ns)
          Returns a random generated, valid URN for the given Namespace.
static String getURN(String tmpurn)
          Returns a valid URN with checksum for the given identifier without checksum.
static String getURNforId(String ns, String id)
          Returns a valid URN for the given Namespace and another id.
static String mapToEncodedString(AbstractMap<String,String> map, String delimiter)
          Iterates all elements of a map and returns key=value pairs separated with given delimiter as URLEncoded string.
static String mapToString(AbstractMap<String,String> map, String delimiter)
          Iterates all elements of a map and returns key=value pairs separated with given delimiter as string.
static String normalize(String str, boolean lowerCase)
          Normalizes the "Percent Encoding" of an URI
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

URNUtils

public URNUtils()
Method Detail

getChecksum

public static int getChecksum(String tmpurn)
Calculates the checksum of the given string. See homepage for the algorithm.

Parameters:
tmpurn - URN without checksum
Returns:
Checksum of URN

getURN

public static String getURN(String tmpurn)
Returns a valid URN with checksum for the given identifier without checksum.

Parameters:
tmpurn - URN without checksum
Returns:
URN with checksum added

getURNforId

public static String getURNforId(String ns,
                                 String id)
Returns a valid URN for the given Namespace and another id.

Parameters:
ns - Namespace
id - Another unique number
Returns:
URN with checksum added

getRandomURN

public static String getRandomURN(String ns)
Returns a random generated, valid URN for the given Namespace.

Parameters:
ns - Namespace for this URN.
Returns:
URN with checksum added

getDateURN

public static String getDateURN(String ns)
Returns a valid URN for the given Namespace beginning with actual date.

Parameters:
ns - Namespace for this URN.
Returns:
URN with checksum added

mapToString

public static String mapToString(AbstractMap<String,String> map,
                                 String delimiter)
Iterates all elements of a map and returns key=value pairs separated with given delimiter as string. To get this string URL-Encoded please use mapToEncodedString() instead.

Parameters:
map - A map containing Key, Value pairs
delimiter - String between Key=Value pairs. (normally "&" or ";" for URLs).
Returns:
String representation of those Key, Value pairs. Empty string if map is null or empty.

mapToEncodedString

public static String mapToEncodedString(AbstractMap<String,String> map,
                                        String delimiter)
                                 throws UnsupportedEncodingException
Iterates all elements of a map and returns key=value pairs separated with given delimiter as URLEncoded string.

Parameters:
map - A map containing Key, Value pairs
delimiter - String between Key=Value pairs. "&" or ";" for URIs.
Returns:
String representation of those Key, Value pairs. Empty string if map is null or empty.
Throws:
UnsupportedEncodingException
See Also:
URLEncoder

getParameterMap

public static TreeMap<String,String> getParameterMap(String query,
                                                     String regex)
                                              throws UnsupportedEncodingException
Parses a URL query and returns key, value pairs as a TreeMap.

Parameters:
query - (URI) Query to parse
regex - Regular expression to use for splitting.
Returns:
Key, Value pairs as Map
Throws:
UnsupportedEncodingException

getParameterMap

public static TreeMap<String,String> getParameterMap(URL url)
                                              throws UnsupportedEncodingException
Parses an URL query and returns key|value pairs as a TreeMap.

Parameters:
url - URL to parse
Returns:
Key, Value pairs as Map
Throws:
UnsupportedEncodingException

addMaps

public static AbstractMap<String,String> addMaps(AbstractMap<String,String> map1,
                                                 AbstractMap<String,String> map2)
                                          throws UnsupportedEncodingException
Adds two given maps and returns a map that contains key|value pairs of both. Please keep in mind:
  • all the key | value pairs are normalized with the help of java.net.URLEncoder routines
  • if the second map contains a key that is already in the first map this will be ignored the resulting map will have one key|value pair and the value will be the value of the first map.
  • if both maps contain key|value pairs, the resulting map is a sorted one.

    Parameters:
    map1 - First map.
    map2 - Second map.
    Returns:
    A Java map containing all elements of two given maps.
    Throws:
    UnsupportedEncodingException
    See Also:
    TreeMap, URLDecoder, URLEncoder

  • getNormalisedPath

    public static String getNormalisedPath(String path)
                                    throws UnsupportedEncodingException
    Normalises given URL Path.

    Parameters:
    path - URL Path to parse.
    Returns:
    Path as normalised and encoded string.
    Throws:
    UnsupportedEncodingException
    See Also:
    {@link "http://www.w3.org/TR/html401"}

    correctUtf8error

    public static String correctUtf8error(String strToCorrect)
                                   throws UnsupportedEncodingException
    Corrects wrong converted or encoded strings. This version only works for false encoded UTF-8 strings to correct Latin1 strings

    Parameters:
    strToCorrect - String to check.
    Returns:
    Corrected String.
    Throws:
    UnsupportedEncodingException

    correctUrlString

    public static String correctUrlString(String urlStr,
                                          boolean correctEncoding,
                                          boolean checkJavaURI)
    Tries to return a RFC compatible, normalized URL as string from a given string.

    Parameters:
    urlStr - URL to correct as string.
    correctEncoding - Try to correct common wrong Latin1 - UTF-8 conversions.
    checkJavaURI - Check if Java can parse this string as Java URL and URI without exception
    Returns:
    Corrected URL as string. "http://www.w3.org/TR/html401"

    addQueryToUrl

    public static String addQueryToUrl(String urlStr,
                                       String aq)
                                throws MalformedURLException,
                                       UnsupportedEncodingException,
                                       URISyntaxException
    Adds given new query to existing URL.

    Parameters:
    urlStr - URL to correct as string.
    aq - Additional query: query to add to given URL. "http://www.w3.org/TR/html401"
    Returns:
    Corrected URL as string.
    Throws:
    MalformedURLException
    UnsupportedEncodingException
    URISyntaxException

    addParamsToUrl

    public static String addParamsToUrl(String urlStr,
                                        AbstractMap<String,String> params)
                                 throws UnsupportedEncodingException,
                                        MalformedURLException,
                                        URISyntaxException
    Adds given new parameters to existing URL.

    Parameters:
    urlStr - URL as java string.
    params - New parameters to add.
    Returns:
    URL as string containing the given key|value pairs in map as query parameters.
    Throws:
    UnsupportedEncodingException
    MalformedURLException
    URISyntaxException

    buildUrl

    public static String buildUrl(String protocol,
                                  String userInfo,
                                  String host,
                                  int port,
                                  String path,
                                  AbstractMap<String,String> params,
                                  boolean encodeParams,
                                  String anchor,
                                  boolean checkJavaURI)
                           throws UnsupportedEncodingException,
                                  URISyntaxException,
                                  MalformedURLException
    Builds an URI from its given components.

    Parameters:
    protocol - Protocol part of this URI
    userInfo - User info part of this URI
    host - Host part of this URI
    port - Port part of this URI
    path - Path part of this URI
    params - Parameters part of this URI
    encodeParams - TRUE if the parameters must be encoded
    anchor - Anchor part of this URI
    checkJavaURI - TRUE if extra validation wanted
    Returns:
    A valid URL (correct encoded) as string.
    Throws:
    UnsupportedEncodingException
    URISyntaxException
    MalformedURLException

    normalize

    public static String normalize(String str,
                                   boolean lowerCase)
                            throws UnsupportedEncodingException
    Normalizes the "Percent Encoding" of an URI

    Parameters:
    str - The URI to normalize as Java String
    lowerCase - TRUE if the string should be converted to all lower case.
    Returns:
    URL as normalized string.
    Throws:
    UnsupportedEncodingException


    Copyright © 2013 Deutsche Nationalbibliothek / German National Library. All Rights Reserved.