Class DelegatingUserAgent
- All Implemented Interfaces:
DocumentListener
,UserAgentCallback
- Direct Known Subclasses:
PanelManager
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.
This class is meant as a starting point--it will work out of the box, but you should really implement your own, tuned to your application's needs.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ImageResourceLoader
private final UriResolver
private static final org.slf4j.Logger
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance of NaiveUserAgent with a max image cache of 16 images.DelegatingUserAgent
(ImageResourceLoader imageResourceLoader) -
Method Summary
Modifier and TypeMethodDescriptionvoid
Empties the image cache entirely.void
Indicates document layout has complete, e.g.void
Indicates document has been requested (e.g.@Nullable String
Returns the current baseUrl for this class.byte @Nullable []
getBinaryResource
(String uri) Retrieves a binary resource located at a given URI and returns its contents as a byte array ornull
if the resource could not be loaded.getCSSResource
(String uri) Retrieves the CSS located at the given URI.getImageResource
(String uri) Retrieves the image located at the given URI.@Nullable XMLResource
getXMLResource
(String uri) Retrieves the XML located at the given URI.boolean
Returns true if the given URI was visited, meaning it was requested at some point since initialization.void
Called when document layout failed with an exception.void
Called when document render failed with an exception.protected @Nullable InputStream
Gets a Reader for the resource identified@Nullable String
resolveURI
(@Nullable String uri) Resolves the URI; if absolute, leaves as is, if relative, returns an absolute URI based on the baseUrl for the agent.void
setBaseURL
(@Nullable String uri) URL relative to which URIs are resolved.void
Deprecated.Pass loader right to the constructor (instead of using setter)void
setRepaintListener
(RepaintListener listener) void
If the image cache has more items than the limit specified for this class, the least-recently used will be dropped from cache until it reaches the desired size.
-
Field Details
-
log
private static final org.slf4j.Logger log -
_uriResolver
-
_imageResourceLoader
-
-
Constructor Details
-
DelegatingUserAgent
public DelegatingUserAgent()Creates a new instance of NaiveUserAgent with a max image cache of 16 images. -
DelegatingUserAgent
-
-
Method Details
-
setImageResourceLoader
Deprecated.Pass loader right to the constructor (instead of using setter) -
shrinkImageCache
public void shrinkImageCache()If the image cache has more items than the limit specified for this class, the least-recently used will be dropped from cache until it reaches the desired size. -
clearImageCache
public void clearImageCache()Empties the image cache entirely. -
resolveAndOpenStream
Gets a Reader for the resource identified -
getCSSResource
Retrieves the CSS located at the given URI. It's assumed the URI does point to a CSS file--the URI will be accessed (using java.io or java.net), opened, read and then passed into the CSS parser. The result is packed up into an CSSResource for later consumption.- Specified by:
getCSSResource
in interfaceUserAgentCallback
- Parameters:
uri
- Location of the CSS source.- Returns:
- A CSSResource containing the parsed CSS.
-
getImageResource
Retrieves the image located at the given URI. It's assumed the URI does point to an image--the URI will be accessed (using java.io or java.net), opened, read and then passed into the JDK image-parsing routines. The result is packed up into an ImageResource for later consumption.- Specified by:
getImageResource
in interfaceUserAgentCallback
- Parameters:
uri
- Location of the image source.- Returns:
- An ImageResource containing the image.
-
getXMLResource
Retrieves the XML located at the given URI. It's assumed the URI does point to XML--the URI will be accessed (using java.io or java.net), opened, read and then passed into the XML parser (XMLReader) configured for Flying Saucer. The result is packed up into an XMLResource for later consumption.- Specified by:
getXMLResource
in interfaceUserAgentCallback
- Parameters:
uri
- Location of the XML source.- Returns:
- An XMLResource containing the image.
-
getBinaryResource
Description copied from interface:UserAgentCallback
Retrieves a binary resource located at a given URI and returns its contents as a byte array ornull
if the resource could not be loaded.- Specified by:
getBinaryResource
in interfaceUserAgentCallback
-
isVisited
Returns true if the given URI was visited, meaning it was requested at some point since initialization.- Specified by:
isVisited
in interfaceUserAgentCallback
- Parameters:
uri
- A URI which might have been visited.- Returns:
- Always false; visits are not tracked in the NaiveUserAgent.
-
setBaseURL
URL relative to which URIs are resolved.- Specified by:
setBaseURL
in interfaceUserAgentCallback
- Parameters:
uri
- A URI which anchors other, possibly relative URIs.
-
resolveURI
Resolves the URI; if absolute, leaves as is, if relative, returns an absolute URI based on the baseUrl for the agent.- Specified by:
resolveURI
in interfaceUserAgentCallback
- Parameters:
uri
- A URI, possibly relative.- Returns:
- A URI as String, resolved, or null if there was an exception (for example if the URI is malformed).
-
getBaseURL
Returns the current baseUrl for this class.- Specified by:
getBaseURL
in interfaceUserAgentCallback
- Returns:
- the base uri, possibly in the implementations private uri-space
-
documentStarted
public void documentStarted()Description copied from interface:DocumentListener
Indicates document has been requested (e.g. a new document is going to be loaded). This will be called before any activity takes place for the document.- Specified by:
documentStarted
in interfaceDocumentListener
-
documentLoaded
public void documentLoaded()Description copied from interface:DocumentListener
Indicates document layout has complete, e.g. document is fully "loaded" for display; this is not a callback for the document source (e.g. XML) being loaded. This method will be called on every layout run (including, for example, after panel resizes).- Specified by:
documentLoaded
in interfaceDocumentListener
-
onLayoutException
Description copied from interface:DocumentListener
Called when document layout failed with an exception. AllThrowable
objects thrown (except forThreadDeath
) during layout and not otherwise handled will be provided to this method. If aDocumentListener
has been defined an XHTML panel, the listener is entirely responsible for handling the exception. No other action will be taken.- Specified by:
onLayoutException
in interfaceDocumentListener
-
onRenderException
Description copied from interface:DocumentListener
Called when document render failed with an exception. AllThrowable
objects thrown (except forThreadDeath
) during render and not otherwise handled will be provided to this method. If aDocumentListener
has been defined an XHTML panel, the listener is entirely responsible for handling the exception. No other action will be taken.- Specified by:
onRenderException
in interfaceDocumentListener
-
setRepaintListener
-