Package com.complexible.common.util

# Class PrefixMapping

java.lang.Object
com.complexible.common.util.PrefixMapping

public class PrefixMapping extends Object

A class to convert URI's to QNames.

Since:
  2.0
Version:
  2.0
Author:
  Evren Sirin, Michael Grove

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final PrefixMapping`
  `GLOBAL`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `PrefixMapping()`
  Create a new PrefixMapping adding defaults for the common namespaces, OWL, RDF, RDFS and XSD
  `PrefixMapping(boolean theAddDefaults)`
  Create a new PrefixMapping optionally adding defaults for the common (OWL, RDF, RDFS, XSD) namespaces

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `addMapping(String prefix, String uri)`
  Add a new prefix/namespace mapping.
  `void`
  `clear()`
  Clears all existing prefix/namespace mappings
  `String`
  `getNamespace(String prefix)`
  Return the namespace for the given prefix
  `String`
  `getPrefix(String uri)`
  Return the prefix for the namespace
  `Collection<String>`
  `getPrefixes()`
  Return the list of prefixes currently bound in this PrefixMapping
  `String`
  `qname(String uri)`
  Return the qname for the given URI.
  `String`
  `qname(String uri, boolean autoGenerate)`
  Return the URI as a qname.
  `boolean`
  `removeMapping(String prefix, String uri)`
  Remove a prefix/namespace mapping.
  `void`
  `setMapping(String prefix, String uri)`
  Set a prefix/namespace mapping.
  `String`
  `toString()`
  `String`
  `uri(String qname)`
  Return the full URI for the provided qname.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Field Details

  - ### GLOBAL

    public static final PrefixMapping GLOBAL

- ## Constructor Details

  - ### PrefixMapping

    public PrefixMapping()
    Create a new PrefixMapping adding defaults for the common namespaces, OWL, RDF, RDFS and XSD

  - ### PrefixMapping

    public PrefixMapping(boolean theAddDefaults)
    Create a new PrefixMapping optionally adding defaults for the common (OWL, RDF, RDFS, XSD) namespaces
    Parameters:
      `theAddDefaults` - whether or not to add the default namespaces

- ## Method Details

  - ### getPrefix

    public String getPrefix(String uri)
    Return the prefix for the namespace
    Parameters:
      `uri` - the namespace URI
    Returns:
      the prefix for the namespace or null if one does not exist

  - ### getNamespace

    public String getNamespace(String prefix)
    Return the namespace for the given prefix
    Parameters:
      `prefix` - the prefix
    Returns:
      the namespace for the prefix, or null if one does not exist

  - ### addMapping

    public boolean addMapping(String prefix, String uri)
    Add a new prefix/namespace mapping. This will not overrwrite existing mappings for the given prefix and namespace. If you wish to overwrite an existing mapping, use `setMapping(java.lang.String, java.lang.String)`
    Parameters:
      `prefix` - the prefix
      `uri` - the namespace URI for the prefix
    Returns:
      whether or not the mapping was added

  - ### setMapping

    public void setMapping(String prefix, String uri)
    Set a prefix/namespace mapping. This will override any existing mapping. To optionally add the mapping, use `addMapping(java.lang.String, java.lang.String)`
    Parameters:
      `prefix` - the prefix
      `uri` - the namespace URI for the prefix

  - ### removeMapping

    public boolean removeMapping(String prefix, String uri)
    Remove a prefix/namespace mapping. Only the exact mapping is removed.
    Parameters:
      `prefix` - the prefix
      `uri` - the uri
    Returns:
      true if removed, false otherwise

  - ### clear

    public void clear()
    Clears all existing prefix/namespace mappings

  - ### getPrefixes

    public Collection<String> getPrefixes()
    Return the list of prefixes currently bound in this PrefixMapping
    Returns:
      the list of currently used prefixes

  - ### qname

    public String qname(String uri)
    Return the qname for the given URI. This will use the existing prefix definition if possible, and if one does not exist a new prefix of the form ns(int) (ex. ns2) will be created, saved, and returned. A null prefix returns a null qname.
    Parameters:
      `uri` - the URI
    Returns:
      the URI as a qname

  - ### qname

    public String qname(String uri, boolean autoGenerate)
    Return the URI as a qname. A prefix for the URI will be auto generated if autoGenerate is set to true and a prefix does not already exist for the namespace of the URI. A null prefix returns a null qname.
    Parameters:
      `uri` - the URI to shorten
      `autoGenerate` - whether or not to auto-generate a qname prefix for the URI if one does not exist
    Returns:
      the qname of the URI, or the URI itself if a prefix mapping does not exist and autoGenerate is false.

  - ### uri

    public String uri(String qname)
    Return the full URI for the provided qname. If there is no prefix mapping for the provided qname, the qname is returned, otherwise the prefix is replaced with the namespace as indicated by `addMapping(java.lang.String, java.lang.String)` or if it was added via `qname(java.lang.String)`. A null qname returns a null URI.
    Parameters:
      `qname` - the qname to expand
    Returns:
      the expanded qname, or the qname if it cannot be expanded

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`