org.mulgara.jena.shared.impl
Class PrefixMappingImpl

java.lang.Object
  extended by org.mulgara.jena.shared.impl.PrefixMappingImpl
All Implemented Interfaces:
PrefixMapping

public class PrefixMappingImpl
extends Object
implements PrefixMapping

An implementation of PrefixMapping. The mappings are stored in a pair of hash tables, one per direction. The test for a legal prefix is left to xerces's XMLChar.isValidNCName() predicate.

Author:
kers

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.mulgara.jena.shared.PrefixMapping
PrefixMapping.Factory, PrefixMapping.IllegalPrefixException, PrefixMapping.JenaLockedException
 
Field Summary
protected  boolean locked
           
protected  Map<String,String> prefixToURI
           
protected  Map<String,String> URItoPrefix
           
 
Fields inherited from interface org.mulgara.jena.shared.PrefixMapping
Extended, Standard
 
Constructor Summary
PrefixMappingImpl()
           
 
Method Summary
protected  void checkUnlocked()
           
protected  boolean equals(PrefixMappingImpl other)
           
protected  boolean equalsByMap(PrefixMapping other)
           
 String expandPrefix(String prefixed)
          Expand a prefixed URI.
protected  String get(String prefix)
           
 Map<String,String> getNsPrefixMap()
          Return a copy of the internal mapping from names to URI strings.
 String getNsPrefixURI(String prefix)
          Get the URI bound to a specific prefix, null if there isn't one.
 String getNsURIPrefix(String uri)
          Answer the prefix for the given URI, or null if there isn't one.
static boolean isNiceURI(String uri)
           
 PrefixMapping lock()
          Lock the PrefixMapping so that changes can no longer be made to it.
static boolean notNameChar(char ch)
          answer true iff this is not a legal NCName character, ie, is a possible split-point start.
 String qnameFor(String uri)
          Answer the qname for uri which uses a prefix from this mapping, or null if there isn't one.
protected  void regenerateReverseMapping()
           
 PrefixMapping removeNsPrefix(String prefix)
          Remove any existing maplet with the given prefix name and answer this mapping.
protected  boolean sameAs(PrefixMappingImpl other)
           
 boolean samePrefixMappingAs(PrefixMapping other)
          Answer true iff this prefix-mappings are equal, that is, map the same prefixes to the same URIs; same as this.getNsPrefixMap().equals( other.getNsPrefixMap() ) except that it may avoid unnecessary Map creations.
protected  void set(String prefix, String uri)
           
 PrefixMapping setNsPrefix(String prefix, String uri)
          Specify the prefix name for a URI prefix string.
 PrefixMapping setNsPrefixes(Map<String,String> other)
          Add the bindings in the prefixToURI to our own.
 PrefixMapping setNsPrefixes(PrefixMapping other)
          Add the bindings of other to our own.
 String shortForm(String uri)
          Compress the URI using the prefix mapping.
 String toString()
          Answer a readable (we hope) representation of this prefix mapping.
 PrefixMapping withDefaultMappings(PrefixMapping other)
          Answer this PrefixMapping after updating it with the (p, u) mappings in other where neither p nor u appear in this mapping.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

prefixToURI

protected Map<String,String> prefixToURI

URItoPrefix

protected Map<String,String> URItoPrefix

locked

protected boolean locked
Constructor Detail

PrefixMappingImpl

public PrefixMappingImpl()
Method Detail

set

protected void set(String prefix,
                   String uri)

get

protected String get(String prefix)

lock

public PrefixMapping lock()
Description copied from interface: PrefixMapping
Lock the PrefixMapping so that changes can no longer be made to it. Primarily intended to lock Standard against mutation.

Specified by:
lock in interface PrefixMapping
Returns:
this mapping, locked against changes

setNsPrefix

public PrefixMapping setNsPrefix(String prefix,
                                 String uri)
Description copied from interface: PrefixMapping
Specify the prefix name for a URI prefix string. Any existing use of that prefix name is overwritten. The result is this same prefixMapping. (The earlier restriction that adding second prefix for the same URI caused the earlier binding to be deleted has been withdrawn.)

A prefix name must be a valid NCName, or the empty string. The empty string is reserved to mean "the default namespace".

Need not check the RFC2396 validity of the URI. Bad URIs are either silently ignored or behave as if they were good. The earlier restriction that the URI should end with a non-NCName character has been removed.

Specified by:
setNsPrefix in interface PrefixMapping
Parameters:
prefix - the string to be used for the prefix.
uri - the URI prefix to be named
Returns:
this PrefixMapping

removeNsPrefix

public PrefixMapping removeNsPrefix(String prefix)
Description copied from interface: PrefixMapping
Remove any existing maplet with the given prefix name and answer this mapping. If the prefix is the empty string, then this removes the default namespace. If the prefix is not a legal prefix string, or is not present in the mapping, nothing happens.

The reverse URI-to-prefix mapping is updated, but if there are multiple prefixes for the removed URI it is unspecified which of them will be chosen.

Specified by:
removeNsPrefix in interface PrefixMapping
Parameters:
prefix - the prefix string to remove
Returns:
this PrefixMapping

regenerateReverseMapping

protected void regenerateReverseMapping()

checkUnlocked

protected void checkUnlocked()

isNiceURI

public static boolean isNiceURI(String uri)

setNsPrefixes

public PrefixMapping setNsPrefixes(PrefixMapping other)
Add the bindings of other to our own. We defer to the general case because we have to ensure the URIs are checked.

Specified by:
setNsPrefixes in interface PrefixMapping
Parameters:
other - the PrefixMapping whose bindings we are to add to this.
Returns:
this PrefixMapping

withDefaultMappings

public PrefixMapping withDefaultMappings(PrefixMapping other)
Answer this PrefixMapping after updating it with the (p, u) mappings in other where neither p nor u appear in this mapping.

Specified by:
withDefaultMappings in interface PrefixMapping

setNsPrefixes

public PrefixMapping setNsPrefixes(Map<String,String> other)
Add the bindings in the prefixToURI to our own. This will fail with a ClassCastException if any key or value is not a String; we make no guarantees about order or completeness if this happens. It will fail with an IllegalPrefixException if any prefix is illegal; similar provisos apply.

Specified by:
setNsPrefixes in interface PrefixMapping
Parameters:
other - the Map whose bindings we are to add to this.
Returns:
this PrefixMapping

getNsPrefixURI

public String getNsPrefixURI(String prefix)
Description copied from interface: PrefixMapping
Get the URI bound to a specific prefix, null if there isn't one.

Specified by:
getNsPrefixURI in interface PrefixMapping
Parameters:
prefix - the prefix name to be looked up
Returns:
the most recent URI bound to that prefix name, null if none

getNsPrefixMap

public Map<String,String> getNsPrefixMap()
Description copied from interface: PrefixMapping
Return a copy of the internal mapping from names to URI strings. Updating this copy will have no effect on the PrefixMap.

Specified by:
getNsPrefixMap in interface PrefixMapping
Returns:
a copy of the internal String -> String mapping

getNsURIPrefix

public String getNsURIPrefix(String uri)
Description copied from interface: PrefixMapping
Answer the prefix for the given URI, or null if there isn't one. If there is more than one, one of them will be picked. If possible, it will be the most recently added prefix. (The cases where it's not possible is when a binding has been removed.)

Specified by:
getNsURIPrefix in interface PrefixMapping
Parameters:
uri - the uri whose prefix is to be found
Returns:
the prefix mapped to that uri, or null if there isn't one

expandPrefix

public String expandPrefix(String prefixed)
Expand a prefixed URI. There's an assumption that any URI of the form Head:Tail is subject to mapping if Head is in the prefix mapping. So, if someone takes it into their heads to define eg "http" or "ftp" we have problems.

Specified by:
expandPrefix in interface PrefixMapping
Parameters:
prefixed - a QName or URI
Returns:
the expanded string if possible, otherwise the original string

toString

public String toString()
Answer a readable (we hope) representation of this prefix mapping.

Overrides:
toString in class Object

qnameFor

public String qnameFor(String uri)
Answer the qname for uri which uses a prefix from this mapping, or null if there isn't one.

Relies on splitNamespace to carve uri into namespace and localname components; this ensures that the localname is legal and we just have to (reverse-)lookup the namespace in the prefix table.

Specified by:
qnameFor in interface PrefixMapping
See Also:
PrefixMapping.qnameFor(java.lang.String)

shortForm

public String shortForm(String uri)
Compress the URI using the prefix mapping. This version of the code looks through all the maplets and checks each candidate prefix URI for being a leading substring of the argument URI. There's probably a much more efficient algorithm available, preprocessing the prefix strings into some kind of search table, but for the moment we don't need it.

Specified by:
shortForm in interface PrefixMapping
Parameters:
uri - the URI string to try and prefix-compress
Returns:
the shortened form if possible, otherwise the unchanged argument

samePrefixMappingAs

public boolean samePrefixMappingAs(PrefixMapping other)
Description copied from interface: PrefixMapping
Answer true iff this prefix-mappings are equal, that is, map the same prefixes to the same URIs; same as
this.getNsPrefixMap().equals( other.getNsPrefixMap() )
except that it may avoid unnecessary Map creations.

Specified by:
samePrefixMappingAs in interface PrefixMapping

equals

protected boolean equals(PrefixMappingImpl other)

sameAs

protected boolean sameAs(PrefixMappingImpl other)

equalsByMap

protected final boolean equalsByMap(PrefixMapping other)

notNameChar

public static boolean notNameChar(char ch)
answer true iff this is not a legal NCName character, ie, is a possible split-point start.



Copyright © 2011. All Rights Reserved.