public class NaiveUserAgent extends Object implements UserAgentCallback, DocumentListener
NaiveUserAgent is a simple implementation of UserAgentCallback which places no restrictions on what
XML, CSS or images are loaded, and reports visited links without any filtering. The most straightforward process
available in the JDK is used to load the resources in question--either using java.io or java.net classes.
The NaiveUserAgent has a small cache for images,
the size of which (number of images) can be passed as a constructor argument. There is no automatic cleaning of
the cache; call #shrinkImageCache() to remove the least-accessed elements--for example, you might do this
when a new document is about to be loaded. The NaiveUserAgent is also a DocumentListener; if registered with a
source of document events (like the panel hierarchy), it will respond to the
DocumentListener.documentStarted() call and attempt to shrink its cache.
| Modifier and Type | Class and Description |
|---|---|
static class |
NaiveUserAgent.DefaultHttpStream |
static class |
NaiveUserAgent.DefaultHttpStreamFactory |
static class |
NaiveUserAgent.DefaultUriResolver |
protected static class |
NaiveUserAgent.NullCache<T> |
static class |
NaiveUserAgent.NullFSCache |
| Modifier and Type | Field and Description |
|---|---|
protected String |
_baseUri |
protected FSMultiThreadCache<byte[]> |
_byteCache |
protected FSCache |
_externalCache |
protected LinkedHashMap<String,ImageResource> |
_imageCache
a (simple) cache
This is only useful for the one run.
|
protected Map<String,FSStreamFactory> |
_protocolsStreamFactory |
protected FSUriResolver |
_resolver |
protected FSMultiThreadCache<String> |
_textCache |
protected FSUriResolver |
DEFAULT_URI_RESOLVER |
| Constructor and Description |
|---|
NaiveUserAgent() |
| Modifier and Type | Method and Description |
|---|---|
void |
clearImageCache()
Empties the image cache entirely.
|
void |
documentLoaded()
Indicates document layout has complete, e.g.
|
void |
documentStarted()
Indicates document has been requested (e.g.
|
String |
getBaseURL()
Returns the current baseUrl for this class.
|
byte[] |
getBinaryResource(String uri)
Retrieves a binary resource located at a given URI and returns its contents
as a byte array or
null if the resource could not be loaded. |
protected Reader |
getCacheReader(String uri) |
protected InputStream |
getCacheStream(String uri) |
protected String |
getCacheText(String uri) |
CSSResource |
getCSSResource(String uri)
Retrieves the CSS located at the given URI.
|
FSUriResolver |
getDefaultUriResolver() |
ImageResource |
getImageResource(String uri)
Retrieves the image located at the given URI.
|
protected FSStreamFactory |
getProtocolFactory(String protocol) |
XMLResource |
getXMLResource(String uri)
Retrieves the XML located at the given URI.
|
protected boolean |
hasProtocolFactory(String protocol) |
boolean |
isVisited(String uri)
Returns true if the given URI was visited, meaning it was requested at some point since initialization.
|
void |
onLayoutException(Throwable t)
Called when document layout failed with an exception.
|
void |
onRenderException(Throwable t)
Called when document render failed with an exception.
|
protected Reader |
openReader(String uri)
Gets a reader for the identified resource by a resolved URI.
|
protected InputStream |
openStream(String uri)
Gets a InputStream for the resource identified by a resolved URI.
|
protected String |
readAll(Reader reader) |
String |
resolveURI(String uri)
Used to find a uri that may be relative to the BaseURL.
|
String |
resolveUri(String baseUri,
String uri) |
void |
setBaseURL(String uri)
URL relative to which URIs are resolved.
|
void |
setExternalByteCache(FSMultiThreadCache<byte[]> byteCache) |
void |
setExternalCache(FSCache cache) |
void |
setExternalTextCache(FSMultiThreadCache<String> textCache) |
void |
setProtocolsStreamFactory(Map<String,FSStreamFactory> protocolsStreamFactory) |
void |
setUriResolver(FSUriResolver resolver) |
protected final LinkedHashMap<String,ImageResource> _imageCache
protected final FSUriResolver DEFAULT_URI_RESOLVER
protected FSCache _externalCache
protected FSUriResolver _resolver
protected String _baseUri
protected Map<String,FSStreamFactory> _protocolsStreamFactory
protected FSMultiThreadCache<String> _textCache
protected FSMultiThreadCache<byte[]> _byteCache
public void setProtocolsStreamFactory(Map<String,FSStreamFactory> protocolsStreamFactory)
public void setExternalCache(FSCache cache)
public void setUriResolver(FSUriResolver resolver)
public FSUriResolver getDefaultUriResolver()
public void clearImageCache()
protected FSStreamFactory getProtocolFactory(String protocol)
protected boolean hasProtocolFactory(String protocol)
protected InputStream openStream(String uri)
protected Reader openReader(String uri)
protected String readAll(Reader reader) throws IOException
IOExceptionprotected InputStream getCacheStream(String uri)
public CSSResource getCSSResource(String uri)
getCSSResource in interface UserAgentCallbackuri - Location of the CSS source.public ImageResource getImageResource(String uri)
getImageResource in interface UserAgentCallbackuri - Location of the image source.public XMLResource getXMLResource(String uri)
getXMLResource in interface UserAgentCallbackuri - Location of the XML source.public byte[] getBinaryResource(String uri)
UserAgentCallbacknull if the resource could not be loaded.getBinaryResource in interface UserAgentCallbackpublic boolean isVisited(String uri)
isVisited in interface UserAgentCallbackuri - A URI which might have been visited.public void setBaseURL(String uri)
setBaseURL in interface UserAgentCallbackurl - A URI which anchors other, possibly relative URIs.public String getBaseURL()
getBaseURL in interface UserAgentCallbackpublic void documentStarted()
DocumentListenerdocumentStarted in interface DocumentListenerpublic void documentLoaded()
DocumentListenerdocumentLoaded in interface DocumentListenerpublic void onLayoutException(Throwable t)
DocumentListenerThrowable objects thrown (except for
ThreadDeath) during layout and not otherwise handled will
be provided to this method. If a DocumentListener has been
defined an XHTML panel, the listener is entirely responsible for
handling the exception. No other action will be taken.onLayoutException in interface DocumentListenerpublic void onRenderException(Throwable t)
DocumentListenerThrowable objects thrown (except for
ThreadDeath) during render and not otherwise handled will
be provided to this method. If a DocumentListener has been
defined an XHTML panel, the listener is entirely responsible for
handling the exception. No other action will be taken.onRenderException in interface DocumentListenerpublic String resolveURI(String uri)
UserAgentCallbackresolveURI in interface UserAgentCallbackuri - an absolute or relative (to baseURL) uri to be resolved.public String resolveUri(String baseUri, String uri)
resolveUri in interface UserAgentCallbackpublic void setExternalTextCache(FSMultiThreadCache<String> textCache)
public void setExternalByteCache(FSMultiThreadCache<byte[]> byteCache)
Copyright © 2018. All rights reserved.