public class DocumentCache extends Object implements DocumentManager
This class is thread-safe.
Modifier and Type | Class and Description |
---|---|
static class |
DocumentCache.Entry
An entry in the document cache.
|
static class |
DocumentCache.Event
Rather low-level event detailing what happened to the document cache.
|
static class |
DocumentCache.EventType
The type of a document cache event.
|
static interface |
DocumentCache.Listener
Implemented by client code interested in learning what happens
to this document cache.
|
Modifier and Type | Field and Description |
---|---|
protected int |
capacity |
static int |
DEFAULT_CAPACITY
Default capacity (that is, the maximum number of cached documents)
of a document cache.
|
static int |
DEFAULT_LOAD_OPTIONS
Default load document options.
|
protected int |
defaultLoadOptions |
protected HashMap<URL,DocumentCache.Entry> |
entries |
protected DocumentCache.Event |
event |
protected DocumentCache.Entry |
first |
protected DocumentCache.Entry |
last |
protected DocumentCache.Listener[] |
listeners |
protected int |
size |
Constructor and Description |
---|
DocumentCache()
Constructs a document cache having the default capacity and using
the normal default load options.
|
DocumentCache(int capacity,
int defaultLoadOptions)
Constructs a document cache having specified capacity and using
specified default load options.
|
Modifier and Type | Method and Description |
---|---|
protected DocumentCache.Entry |
accessEntry(URL url,
boolean load,
int loadOptions) |
protected void |
addEntry(DocumentCache.Entry e) |
void |
addListener(DocumentCache.Listener listener)
Register specified event listener.
|
protected void |
attachEntry(DocumentCache.Entry e) |
protected boolean |
checkedAddEntry(DocumentCache.Entry e) |
void |
clear()
Make this document cache empty.
|
protected DocumentCache.Entry |
createEntry(URL url,
Document document,
int loadOptions,
File file,
long lastModified,
long size) |
protected void |
detachEntry(DocumentCache.Entry e) |
protected DocumentCache.Entry |
doAccessEntry(URL url,
boolean load,
int loadOptions) |
protected void |
doAddEntry(DocumentCache.Entry e) |
protected void |
doClear() |
protected Document |
doLoadDocument(InputSource source,
URL url,
int loadOptions,
List<LoadError> loadErrors,
List<LoadError> loadWarnings) |
protected void |
doRemoveEntry(DocumentCache.Entry e) |
Document |
fetch(URL url)
Just fetch -- do not load -- specified document from this cache.
|
DocumentCache.Entry |
fetchEntry(URL url)
Just fetch -- do not load -- specified document from this cache.
|
Document |
get(URL url)
Equivalent to
get(url, defaultLoadOptions) . |
Document |
get(URL url,
int loadOptions)
Fetch specified document from this cache; load it if needed to.
|
static DocumentCache |
getCache()
Returns the application-wide document cache.
|
int |
getCapacity()
Returns the maximum number of cached documents.
|
int |
getDefaultLoadOptions()
Returns an OR-ed mask containing the default options of the
document loader used by this cache.
|
Document |
getDocument(URL url)
Returns specified document.
|
DocumentCache.Entry |
getEntry(URL url)
Equivalent to
getEntry(url, defaultLoadOptions) . |
DocumentCache.Entry |
getEntry(URL url,
int loadOptions)
Fetch specified document from this cache; load it if needed to.
|
DocumentCache.Listener[] |
getListeners()
Returns an array containing all registered event listeners.
|
int |
getSize()
Returns the number of documents currently cached in this cache.
|
protected Document |
loadDocument(InputStream in,
URL url,
int loadOptions) |
protected DocumentCache.Entry |
loadEntry(URL url,
int loadOptions) |
protected void |
notifyListeners() |
Document |
remove(URL url)
Remove specified document from this cache.
|
protected void |
removeEntry(DocumentCache.Entry e) |
DocumentCache.Entry |
removeEntry(URL url)
Remove specified document from this cache.
|
void |
removeListener(DocumentCache.Listener listener)
Unregister specified event listener.
|
static void |
setCache(DocumentCache cache)
Specifies which cache to use as the application-wide
document cache.
|
void |
setCapacity(int capacity)
Specifies the maximum number of cached documents.
|
void |
setDefaultLoadOptions(int defaultLoadOptions)
Changes the default options of the document loader used by this cache.
|
public static final int DEFAULT_CAPACITY
public static final int DEFAULT_LOAD_OPTIONS
LoadOptions.ADD_ANY_DOCUMENT_TYPE
. Needed to
support xml:id.
LoadOptions.DONT_INCLUDE
. Document cache client code
is almost always interested in the intrinsic contents of a document.
setDefaultLoadOptions(int)
,
Constant Field Valuesprotected int capacity
protected int defaultLoadOptions
protected HashMap<URL,DocumentCache.Entry> entries
protected DocumentCache.Entry first
protected DocumentCache.Entry last
protected int size
protected DocumentCache.Listener[] listeners
protected DocumentCache.Event event
public DocumentCache()
DEFAULT_CAPACITY
,
DEFAULT_LOAD_OPTIONS
public DocumentCache(int capacity, int defaultLoadOptions)
capacity
- the maximum number of cached documentsdefaultLoadOptions
- load options used by default.
The getEntry(java.net.URL)
method allows to specify a different value
when needed to.public void setCapacity(int capacity)
getCapacity()
public int getCapacity()
setCapacity(int)
public void setDefaultLoadOptions(int defaultLoadOptions)
Note that changing these options does not automatically clear
the cache. You may want to do that manually by using clear()
.
defaultLoadOptions
- an OR-ed mask containing the default options
of the document loader used by this cachegetDefaultLoadOptions()
,
LoadOptions
public int getDefaultLoadOptions()
setDefaultLoadOptions(int)
,
LoadOptions
public void addListener(DocumentCache.Listener listener)
public void removeListener(DocumentCache.Listener listener)
public DocumentCache.Listener[] getListeners()
protected void notifyListeners()
public Document getDocument(URL url) throws IOException
Fetch specified document from this cache; load it if needed to.
getDocument
in interface DocumentManager
url
- URL of documentnull
instead of throwing an IOExceptionIOException
- if specified document cannot be loadedpublic Document get(URL url) throws IOException
get(url, defaultLoadOptions)
.IOException
getEntry(URL)
public Document get(URL url, int loadOptions) throws IOException
url
- URL of the document to be fetched or loadedloadOptions
- load options; used when the document needs
to be loaded from diskIOException
- if an I/O error occurs when the document
is accessed on diskgetEntry(URL, int)
,
fetch(java.net.URL)
public DocumentCache.Entry getEntry(URL url) throws IOException
getEntry(url, defaultLoadOptions)
.IOException
get(URL)
public DocumentCache.Entry getEntry(URL url, int loadOptions) throws IOException
url
- URL of the document to be fetched or loadedloadOptions
- load options; used when the document needs
to be loaded from diskIOException
- if an I/O error occurs when the document
is accessed on diskgetEntry(URL, int)
,
fetchEntry(java.net.URL)
public Document fetch(URL url)
url
- URL of the document to be fetchednull
fetchEntry(java.net.URL)
,
get(URL)
public DocumentCache.Entry fetchEntry(URL url)
url
- URL of the document to be fetchednull
fetch(java.net.URL)
,
getEntry(URL)
protected DocumentCache.Entry accessEntry(URL url, boolean load, int loadOptions) throws IOException
IOException
protected DocumentCache.Entry doAccessEntry(URL url, boolean load, int loadOptions) throws IOException
IOException
protected DocumentCache.Entry createEntry(URL url, Document document, int loadOptions, File file, long lastModified, long size)
protected DocumentCache.Entry loadEntry(URL url, int loadOptions) throws IOException
IOException
protected Document loadDocument(InputStream in, URL url, int loadOptions) throws IOException
IOException
protected Document doLoadDocument(InputSource source, URL url, int loadOptions, List<LoadError> loadErrors, List<LoadError> loadWarnings) throws SAXException, IOException
SAXException
IOException
protected boolean checkedAddEntry(DocumentCache.Entry e)
protected void addEntry(DocumentCache.Entry e)
protected void doAddEntry(DocumentCache.Entry e)
protected void attachEntry(DocumentCache.Entry e)
public Document remove(URL url)
url
- URL of the document to be removed from this cachenull
if specified document
was not cachedremoveEntry(java.net.URL)
public DocumentCache.Entry removeEntry(URL url)
url
- URL of the document to be removed from this cachenull
if specified document was not cachedremove(java.net.URL)
protected void removeEntry(DocumentCache.Entry e)
protected void doRemoveEntry(DocumentCache.Entry e)
protected void detachEntry(DocumentCache.Entry e)
public void clear()
protected void doClear()
public int getSize()
public static void setCache(DocumentCache cache)
This initial application-wide document cache is automatically
created using new DocumentCache()
.
getCache()
public static DocumentCache getCache()
null
.