Class PrefixMapping


  • public class PrefixMapping
    extends java.lang.Object

    A class to convert URI's to QNames.

    Since:
    2.0
    Version:
    2.0
    Author:
    Evren Sirin, Michael Grove
    • 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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean addMapping​(java.lang.String prefix, java.lang.String uri)
      Add a new prefix/namespace mapping.
      void clear()
      Clears all existing prefix/namespace mappings
      java.lang.String getNamespace​(java.lang.String prefix)
      Return the namespace for the given prefix
      java.lang.String getPrefix​(java.lang.String uri)
      Return the prefix for the namespace
      java.util.Collection<java.lang.String> getPrefixes()
      Return the list of prefixes currently bound in this PrefixMapping
      java.lang.String qname​(java.lang.String uri)
      Return the qname for the given URI.
      java.lang.String qname​(java.lang.String uri, boolean autoGenerate)
      Return the URI as a qname.
      boolean removeMapping​(java.lang.String prefix, java.lang.String uri)
      Remove a prefix/namespace mapping.
      void setMapping​(java.lang.String prefix, java.lang.String uri)
      Set a prefix/namespace mapping.
      java.lang.String toString()
      java.lang.String uri​(java.lang.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
    • Constructor Detail

      • 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 Detail

      • getPrefix

        public java.lang.String getPrefix​(java.lang.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 java.lang.String getNamespace​(java.lang.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​(java.lang.String prefix,
                                  java.lang.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​(java.lang.String prefix,
                               java.lang.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​(java.lang.String prefix,
                                     java.lang.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 java.util.Collection<java.lang.String> getPrefixes()
        Return the list of prefixes currently bound in this PrefixMapping
        Returns:
        the list of currently used prefixes
      • qname

        public java.lang.String qname​(java.lang.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 java.lang.String qname​(java.lang.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 java.lang.String uri​(java.lang.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 java.lang.String toString()
        Overrides:
        toString in class java.lang.Object