org.merlotxml.util.xml
Class DTDCache

java.lang.Object
  extended byorg.merlotxml.util.xml.DTDCache

public class DTDCache
extends java.lang.Object

This singleton class is responsible for loading and caching all DTD's required by the system. This manager can load DTD's from the filesystem, URL's, and zip/jar files (not currently implemented).

Apps should use this class to retrieve all their DTD's for valid documents (non-validating apps usually ignore the DTD anyway, so they don't really need to use this, but if they do get a DTD, it might be a good idea to call into this class.

Here's an example of getting a dtd:
DTDCacheEntry dtdentry = DTDCache.getSharedInstance().findDTD(publicId, systemId);
where publicId is the DOCTYPE's given public identifier (can be null), and systemId is a system designator (file path or URL)

This can also cache DTD's from other entity resolvers via the resolveDTD method.

Author:
Kelly A. Campbell, Frank Blecha

Field Summary
protected  java.util.Map _dtdEntries
          List of unique dtd entries.
protected  java.util.Map _filepathCache
          key is the file path (including a ! and the path within a jar), val is a DTDCacheEntry
protected static DTDCache _instance
          singleton instance
protected  java.util.Properties _properties
          Properties for getting dtd path, etc.
protected  java.util.Map _publicIdCache
          key is the public id, val is a DTDCacheEntry, e.g.
protected static java.lang.Object _synchronizer
          synch object for creating the instance
protected  java.util.Map _systemIdCache
          key is the system id, val is a DTDCacheEntry, e.g.
protected static java.lang.String FILE_PROTOCOL_NAME
          url prefix to know we're working with a local file
 
Constructor Summary
protected DTDCache()
          Constructor for the DTDCache object
 
Method Summary
 void checkCacheEntryTimestamp(DTDCacheEntry entry)
          Checks the timestamp associated with a cache entry and reloads the dtd file if it has changed.
protected  void debug(java.lang.String s)
          simple debugging print routine
protected  DTDCacheEntry findCacheEntryBySystemID(java.lang.String systemID)
           
 DTDCacheEntry findDTD(java.lang.String pubid, java.lang.String sysid, java.lang.String fileLocation)
          find a DTD based on the public id and system id
 DTDCacheEntry findDTDbyPublicId(java.lang.String publicId, java.lang.String systemId)
          Looks in our cache for a file with a given public ID
 DTDCacheEntry findDTDbySystemId(java.lang.String publicId, java.lang.String systemId, java.lang.String fileLocation)
          Finds a dtd given a system identifier.
protected  java.io.InputStream findDTDFromClassLoader(java.lang.String publicID, java.lang.String systemID)
           
protected  java.io.InputStream findDTDFromDTDPath(java.lang.String publicID, java.lang.String systemID)
           
protected  java.io.InputStream findDTDFromFile(java.lang.String systemID)
          see if the systemID exists as-is (is the absolute path embedded in the xml?)
protected  java.io.InputStream findDTDFromFile(java.lang.String systemID, java.lang.String fileLocation)
           
protected  java.io.InputStream findDTDFromURL(java.lang.String publicID, java.lang.String systemID)
           
protected  java.lang.String fixslashes(java.lang.String s)
          Deprecated. use fixSlashes instead
protected  java.lang.String fixSlashes(java.lang.String s)
          make all slashes forward slashes cause windows sucks
protected  java.lang.String formatFileName(java.lang.String originalFileName)
           
 java.util.Collection getCachedDTDEntries()
          Gets the cachedDTDEntries attribute of the DTDCache object
protected  java.lang.String getDTDPath()
          Gets the dTDPath attribute of the DTDCache object
static DTDCache getSharedInstance()
          gets the singleton instance.
 void loadDTDIntoCache(java.io.InputStream is, DTDCacheEntry entry)
          Loads a dtd into a DTDCacheEntry.
 void loadDTDIntoCache(java.io.Reader r, DTDCacheEntry entry)
          Loads a dtd into a DTDCacheEntry.
 void printCache()
          Description of the Method
 DTDCacheEntry resolveDTD(java.lang.String publicId, java.lang.String systemId, org.xml.sax.EntityResolver resolver, java.lang.String fileLocation)
          resolve a dtd from another resolver.
 void setProperties(java.util.Properties props)
          set the properties.
protected  DTDCacheEntry setupCacheEntryFromClassLoader(java.lang.String publicID, java.lang.String systemID)
           
protected  DTDCacheEntry setupCacheEntryFromFile(java.lang.String publicID, java.lang.String systemID)
           
protected  DTDCacheEntry setupCacheEntryFromURL(java.lang.String publicID, java.lang.String systemID)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_publicIdCache

protected java.util.Map _publicIdCache
key is the public id, val is a DTDCacheEntry, e.g. (publicId, DTDCacheEntry)


_systemIdCache

protected java.util.Map _systemIdCache
key is the system id, val is a DTDCacheEntry, e.g. (systemId, DTDCacheEntry)


_filepathCache

protected java.util.Map _filepathCache
key is the file path (including a ! and the path within a jar), val is a DTDCacheEntry


_dtdEntries

protected java.util.Map _dtdEntries
List of unique dtd entries. key is the dtd entry, value is not used (null)


_properties

protected java.util.Properties _properties
Properties for getting dtd path, etc. from


_instance

protected static DTDCache _instance
singleton instance


_synchronizer

protected static final java.lang.Object _synchronizer
synch object for creating the instance


FILE_PROTOCOL_NAME

protected static final java.lang.String FILE_PROTOCOL_NAME
url prefix to know we're working with a local file

See Also:
Constant Field Values
Constructor Detail

DTDCache

protected DTDCache()
Constructor for the DTDCache object

Method Detail

getSharedInstance

public static DTDCache getSharedInstance()
gets the singleton instance.

Returns:
The sharedInstance value

setProperties

public void setProperties(java.util.Properties props)
set the properties. should really only be called once by some app initializer

Parameters:
props - The new properties value

findCacheEntryBySystemID

protected DTDCacheEntry findCacheEntryBySystemID(java.lang.String systemID)
Parameters:
systemID - ba
Returns:
DTDCacheEntry null if we're unable to find the entry, otherwise the entry

setupCacheEntryFromFile

protected DTDCacheEntry setupCacheEntryFromFile(java.lang.String publicID,
                                                java.lang.String systemID)
Parameters:
publicID -
systemID -
Returns:
DTDCacheEntry the cache entry or null if we couldn't find it or encoutered an error

setupCacheEntryFromClassLoader

protected DTDCacheEntry setupCacheEntryFromClassLoader(java.lang.String publicID,
                                                       java.lang.String systemID)
Parameters:
publicID -
systemID -
Returns:
DTDCacheEntry

setupCacheEntryFromURL

protected DTDCacheEntry setupCacheEntryFromURL(java.lang.String publicID,
                                               java.lang.String systemID)
Parameters:
publicID - Description of the Parameter
systemID - Description of the Parameter
Returns:
DTDCacheEntry the cache entry or null if we cannot set it up correctly.

findDTDFromFile

protected java.io.InputStream findDTDFromFile(java.lang.String systemID)
see if the systemID exists as-is (is the absolute path embedded in the xml?)

Parameters:
systemID - Description of the Parameter
Returns:
InputStream - the InputStream from dtdFile or null if we can't find it

findDTDFromFile

protected java.io.InputStream findDTDFromFile(java.lang.String systemID,
                                              java.lang.String fileLocation)
Parameters:
systemID - Description of the Parameter
fileLocation - Description of the Parameter
Returns:
Description of the Return Value

findDTDFromURL

protected java.io.InputStream findDTDFromURL(java.lang.String publicID,
                                             java.lang.String systemID)
Parameters:
publicID -
systemID -
Returns:
InputStream the InputStream for the DTD, or null we can't find it.

findDTDFromClassLoader

protected java.io.InputStream findDTDFromClassLoader(java.lang.String publicID,
                                                     java.lang.String systemID)
Parameters:
publicID -
systemID -
Returns:
InputStream stream to read the dtd, or null if an error was encountered

findDTDFromDTDPath

protected java.io.InputStream findDTDFromDTDPath(java.lang.String publicID,
                                                 java.lang.String systemID)
Parameters:
publicID -
systemID -
Returns:
InputStream the stream to read the dtd from, or null if we got an error

findDTDbySystemId

public DTDCacheEntry findDTDbySystemId(java.lang.String publicId,
                                       java.lang.String systemId,
                                       java.lang.String fileLocation)
Finds a dtd given a system identifier. If it cannot be found, null is returned

Parameters:
publicId -
systemId - can be a URL, and absolute filepath, or a filepath relative to the current document
fileLocation - the location of the file which includes the given DTD
Returns:
Description of the Return Value

findDTDbyPublicId

public DTDCacheEntry findDTDbyPublicId(java.lang.String publicId,
                                       java.lang.String systemId)
Looks in our cache for a file with a given public ID

Parameters:
publicId - Description of the Parameter
systemId - Description of the Parameter
Returns:
Description of the Return Value

findDTD

public DTDCacheEntry findDTD(java.lang.String pubid,
                             java.lang.String sysid,
                             java.lang.String fileLocation)
find a DTD based on the public id and system id

Parameters:
pubid - Description of the Parameter
sysid - Description of the Parameter
fileLocation - Description of the Parameter
Returns:
Description of the Return Value

resolveDTD

public DTDCacheEntry resolveDTD(java.lang.String publicId,
                                java.lang.String systemId,
                                org.xml.sax.EntityResolver resolver,
                                java.lang.String fileLocation)
                         throws org.xml.sax.SAXException,
                                java.io.IOException
resolve a dtd from another resolver. This way we can cache it locally.

Parameters:
publicId - Description of the Parameter
systemId - Description of the Parameter
resolver - Description of the Parameter
fileLocation - Description of the Parameter
Returns:
Description of the Return Value
Throws:
org.xml.sax.SAXException - Description of the Exception
java.io.IOException - Description of the Exception

loadDTDIntoCache

public void loadDTDIntoCache(java.io.InputStream is,
                             DTDCacheEntry entry)
                      throws java.io.IOException
Loads a dtd into a DTDCacheEntry.

Parameters:
is - Description of the Parameter
entry - Description of the Parameter
Throws:
java.io.IOException - Description of the Exception

loadDTDIntoCache

public void loadDTDIntoCache(java.io.Reader r,
                             DTDCacheEntry entry)
                      throws java.io.IOException
Loads a dtd into a DTDCacheEntry. The public and system id's should be set on the dtd entry.

Parameters:
r - Description of the Parameter
entry - Description of the Parameter
Throws:
java.io.IOException - Description of the Exception

checkCacheEntryTimestamp

public void checkCacheEntryTimestamp(DTDCacheEntry entry)
Checks the timestamp associated with a cache entry and reloads the dtd file if it has changed.

Parameters:
entry - Description of the Parameter

getCachedDTDEntries

public java.util.Collection getCachedDTDEntries()
Gets the cachedDTDEntries attribute of the DTDCache object

Returns:
The cachedDTDEntries value

printCache

public void printCache()
Description of the Method


fixslashes

protected java.lang.String fixslashes(java.lang.String s)
Deprecated. use fixSlashes instead

Parameters:
s - Description of the Parameter
Returns:
Description of the Return Value

fixSlashes

protected java.lang.String fixSlashes(java.lang.String s)
make all slashes forward slashes cause windows sucks

Parameters:
s - Description of the Parameter
Returns:
Description of the Return Value

debug

protected void debug(java.lang.String s)
simple debugging print routine

Parameters:
s - Description of the Parameter

formatFileName

protected java.lang.String formatFileName(java.lang.String originalFileName)
Parameters:
originalFileName - - systemID
Returns:
String

getDTDPath

protected java.lang.String getDTDPath()
Gets the dTDPath attribute of the DTDCache object

Returns:
The dTDPath value