org.semwebcentral.sweetrules.util
Class SweetEntityAndNamespaceManager

java.lang.Object
  extended byorg.xml.sax.helpers.DefaultHandler
      extended byorg.semwebcentral.sweetrules.util.SweetEntityAndNamespaceManager
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.ext.DeclHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

public class SweetEntityAndNamespaceManager
extends org.xml.sax.helpers.DefaultHandler
implements org.xml.sax.ext.DeclHandler, org.xml.sax.ContentHandler

The XML namespace manager. This keeps track of all the namespaces and their prefixes, for all documents processed in one session. This is a singleton class.

Author:
Shashidhara K Ganjugunte, Dr. Benjamin Grosof, Dr. Said Tabet, Chitravanu Neogy

Field Summary
private  java.util.Hashtable namespaceToOriginalPrefixTable
          The namespace to original prefix map namespace -> set of original prefixes (can be due to redefinitions of the same ns in multiple files)
private  java.util.Hashtable namespaceToUniquePrefixTable
          Map from the namespace to the unique prefix
static java.lang.String RDF_NAMESPACE
          The RDF namespace
static java.lang.String RDF_NAMESPACE_PREFIX
          The prefix for RDF namespace
private  java.lang.String sweetMungedNamespaceSuffix
          SWEET suffix for the RDF oriented Namespace munging in SWeet
private static SweetEntityAndNamespaceManager sweetNamespaceManager
          The singleton instance
private  java.util.Hashtable uniquePrefixToNamespaceTable
          The map of namespaces and their unique prefixes
 
Constructor Summary
private SweetEntityAndNamespaceManager()
          The private constructor for the singleton class
 
Method Summary
 void addNamespaceprefixPair(java.lang.String fullNamespace, java.lang.String prefix)
          Adds the specified namespace and it's prefix to the manager's database
 void attributeDecl(java.lang.String eName, java.lang.String aName, java.lang.String type, java.lang.String valueDefault, java.lang.String value)
          The event handler for attribute declaration
 java.lang.String demungeNamespacePrefix(java.lang.String namespacePrefix)
          Demunges a given NS prefix if it is munged, else the original is returned
 void elementDecl(java.lang.String name, java.lang.String model)
           
 void externalEntityDecl(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
          The event handler for external entity declaration
static SweetEntityAndNamespaceManager getInstance()
          Retriever method for the singleton instance
 java.lang.String getLongestMatchingFullNamespace(java.lang.String uri)
          Returns the longest matching namespace for a given URI
 java.lang.String[] getOriginalPrefix(java.lang.String fullNamespace)
          Retrieves the original prefix for the namespace
 java.lang.String getUniquePrefix(java.lang.String fullNamespace)
          Returns a unique prefix (among the prefixes present in the database) for the given namespace
 void internalEntityDecl(java.lang.String name, java.lang.String value)
          The event handler for internal entity declaration
 boolean isMungedNamespacePrefix(java.lang.String namespacePrefix)
          Determines if the given namespace prefix is munged
 void loadAllNamespacesAndEntities(java.lang.String sourceData)
          Loads all the namespaces from the given source
static void main(java.lang.String[] args)
           
 java.lang.String mungeNamespacePrefix(java.lang.String namespacePrefix)
          Munges a given NS prefix if it is not munged, else the original is returned
 java.lang.String serializeAsEntityDeclarations(java.lang.String docType)
          Serializes all the current namespace to prefix maps as DTD Entity declarations embedded in a DTD DOCTYPE declaration
 void startPrefixMapping(java.lang.String prefix, java.lang.String uri)
           
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
characters, endDocument, endElement, endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, startDocument, startElement, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.xml.sax.ContentHandler
characters, endDocument, endElement, endPrefixMapping, ignorableWhitespace, processingInstruction, setDocumentLocator, skippedEntity, startDocument, startElement
 

Field Detail

sweetNamespaceManager

private static SweetEntityAndNamespaceManager sweetNamespaceManager
The singleton instance


namespaceToOriginalPrefixTable

private java.util.Hashtable namespaceToOriginalPrefixTable
The namespace to original prefix map namespace -> set of original prefixes (can be due to redefinitions of the same ns in multiple files)


namespaceToUniquePrefixTable

private java.util.Hashtable namespaceToUniquePrefixTable
Map from the namespace to the unique prefix


uniquePrefixToNamespaceTable

private java.util.Hashtable uniquePrefixToNamespaceTable
The map of namespaces and their unique prefixes


sweetMungedNamespaceSuffix

private final java.lang.String sweetMungedNamespaceSuffix
SWEET suffix for the RDF oriented Namespace munging in SWeet

See Also:
Constant Field Values

RDF_NAMESPACE

public static final java.lang.String RDF_NAMESPACE
The RDF namespace

See Also:
Constant Field Values

RDF_NAMESPACE_PREFIX

public static final java.lang.String RDF_NAMESPACE_PREFIX
The prefix for RDF namespace

See Also:
Constant Field Values
Constructor Detail

SweetEntityAndNamespaceManager

private SweetEntityAndNamespaceManager()
The private constructor for the singleton class

Method Detail

loadAllNamespacesAndEntities

public void loadAllNamespacesAndEntities(java.lang.String sourceData)
                                  throws SweetException
Loads all the namespaces from the given source

Parameters:
sourceData - The XML source (as a string) from which all the namespaces are to be loaded
Throws:
SweetException - if an error occurs

getLongestMatchingFullNamespace

public java.lang.String getLongestMatchingFullNamespace(java.lang.String uri)
Returns the longest matching namespace for a given URI

Parameters:
uri - The URI whose namespace is to be determined
Returns:
The longest matching namespace for a given URI

serializeAsEntityDeclarations

public java.lang.String serializeAsEntityDeclarations(java.lang.String docType)
Serializes all the current namespace to prefix maps as DTD Entity declarations embedded in a DTD DOCTYPE declaration

Parameters:
docType - Name of the DTD DOCTYPE declaration
Returns:
The serialized declaration

addNamespaceprefixPair

public void addNamespaceprefixPair(java.lang.String fullNamespace,
                                   java.lang.String prefix)
Adds the specified namespace and it's prefix to the manager's database

Parameters:
fullNamespace - The namespace to be added
prefix - The prefix of the namespace

isMungedNamespacePrefix

public boolean isMungedNamespacePrefix(java.lang.String namespacePrefix)
Determines if the given namespace prefix is munged

Parameters:
namespacePrefix - The namespace prefix to check
Returns:
true if the namespace prefix is munged

demungeNamespacePrefix

public java.lang.String demungeNamespacePrefix(java.lang.String namespacePrefix)
Demunges a given NS prefix if it is munged, else the original is returned

Parameters:
namespacePrefix - The prefix to be demunged
Returns:
The demunged prefix

mungeNamespacePrefix

public java.lang.String mungeNamespacePrefix(java.lang.String namespacePrefix)
Munges a given NS prefix if it is not munged, else the original is returned

Parameters:
namespacePrefix - The prefix to be munged
Returns:
The munged prefix

getUniquePrefix

public java.lang.String getUniquePrefix(java.lang.String fullNamespace)
Returns a unique prefix (among the prefixes present in the database) for the given namespace

Parameters:
fullNamespace - The namespace for which the unique prefix is required
Returns:
A unique prefix for the namespace (which may be different from the original prefix)

getOriginalPrefix

public java.lang.String[] getOriginalPrefix(java.lang.String fullNamespace)
Retrieves the original prefix for the namespace

Parameters:
fullNamespace - The namespace whose original prefix is required
Returns:
The original prefixes for the namespace

getInstance

public static SweetEntityAndNamespaceManager getInstance()
Retriever method for the singleton instance

Returns:
The singleton instance of the namespace manager

attributeDecl

public void attributeDecl(java.lang.String eName,
                          java.lang.String aName,
                          java.lang.String type,
                          java.lang.String valueDefault,
                          java.lang.String value)
The event handler for attribute declaration

Specified by:
attributeDecl in interface org.xml.sax.ext.DeclHandler

main

public static void main(java.lang.String[] args)
                 throws SweetException
Throws:
SweetException

externalEntityDecl

public void externalEntityDecl(java.lang.String name,
                               java.lang.String publicId,
                               java.lang.String systemId)
The event handler for external entity declaration

Specified by:
externalEntityDecl in interface org.xml.sax.ext.DeclHandler

internalEntityDecl

public void internalEntityDecl(java.lang.String name,
                               java.lang.String value)
The event handler for internal entity declaration

Specified by:
internalEntityDecl in interface org.xml.sax.ext.DeclHandler

elementDecl

public void elementDecl(java.lang.String name,
                        java.lang.String model)
                 throws org.xml.sax.SAXException
Specified by:
elementDecl in interface org.xml.sax.ext.DeclHandler
Throws:
org.xml.sax.SAXException

startPrefixMapping

public void startPrefixMapping(java.lang.String prefix,
                               java.lang.String uri)
                        throws org.xml.sax.SAXException
Specified by:
startPrefixMapping in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException