Class NaiveUserAgent
- java.lang.Object
-
- com.openhtmltopdf.swing.NaiveUserAgent
-
- All Implemented Interfaces:
DocumentListener,UserAgentCallback
- Direct Known Subclasses:
DelegatingUserAgent
public class NaiveUserAgent extends Object implements UserAgentCallback, DocumentListener
NaiveUserAgent is a simple implementation of
UserAgentCallbackwhich 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
clearImageCache()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 theDocumentListener.documentStarted()call and attempt to shrink its cache.- Author:
- Torbjoern Gannholm
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classNaiveUserAgent.DefaultHttpStreamstatic classNaiveUserAgent.DefaultHttpStreamFactorystatic classNaiveUserAgent.DefaultUriResolverprotected static classNaiveUserAgent.NullCache<T>static classNaiveUserAgent.NullFSCache
-
Field Summary
Fields Modifier and Type Field Description protected String_baseUriprotected FSMultiThreadCache<byte[]>_byteCacheprotected FSCache_externalCacheprotected LinkedHashMap<String,ImageResource>_imageCachea (simple) cache This is only useful for the one run.protected Map<String,FSStreamFactory>_protocolsStreamFactoryprotected FSUriResolver_resolverprotected FSMultiThreadCache<String>_textCacheprotected FSUriResolverDEFAULT_URI_RESOLVER
-
Constructor Summary
Constructors Constructor Description NaiveUserAgent()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearImageCache()Empties the image cache entirely.voiddocumentLoaded()Indicates document layout has complete, e.g.voiddocumentStarted()Indicates document has been requested (e.g.StringgetBaseURL()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 ornullif the resource could not be loaded.protected ReadergetCacheReader(String uri)protected InputStreamgetCacheStream(String uri)protected StringgetCacheText(String uri)CSSResourcegetCSSResource(String uri)Retrieves the CSS located at the given URI.FSUriResolvergetDefaultUriResolver()ImageResourcegetImageResource(String uri)Retrieves the image located at the given URI.protected FSStreamFactorygetProtocolFactory(String protocol)XMLResourcegetXMLResource(String uri)Retrieves the XML located at the given URI.protected booleanhasProtocolFactory(String protocol)booleanisVisited(String uri)Returns true if the given URI was visited, meaning it was requested at some point since initialization.voidonLayoutException(Throwable t)Called when document layout failed with an exception.voidonRenderException(Throwable t)Called when document render failed with an exception.protected ReaderopenReader(String uri)Gets a reader for the identified resource by a resolved URI.protected InputStreamopenStream(String uri)Gets a InputStream for the resource identified by a resolved URI.protected StringreadAll(Reader reader)StringresolveUri(String baseUri, String uri)StringresolveURI(String uri)Used to find a uri that may be relative to the BaseURL.voidsetBaseURL(String uri)URL relative to which URIs are resolved.voidsetExternalByteCache(FSMultiThreadCache<byte[]> byteCache)voidsetExternalCache(FSCache cache)voidsetExternalTextCache(FSMultiThreadCache<String> textCache)voidsetProtocolsStreamFactory(Map<String,FSStreamFactory> protocolsStreamFactory)voidsetUriResolver(FSUriResolver resolver)
-
-
-
Field Detail
-
_imageCache
protected final LinkedHashMap<String,ImageResource> _imageCache
a (simple) cache This is only useful for the one run. For more than one run, set an external cache with setFSCache.
-
DEFAULT_URI_RESOLVER
protected final FSUriResolver DEFAULT_URI_RESOLVER
-
_externalCache
protected FSCache _externalCache
-
_resolver
protected FSUriResolver _resolver
-
_baseUri
protected String _baseUri
-
_protocolsStreamFactory
protected Map<String,FSStreamFactory> _protocolsStreamFactory
-
_textCache
protected FSMultiThreadCache<String> _textCache
-
_byteCache
protected FSMultiThreadCache<byte[]> _byteCache
-
-
Method Detail
-
setProtocolsStreamFactory
public void setProtocolsStreamFactory(Map<String,FSStreamFactory> protocolsStreamFactory)
-
setExternalCache
public void setExternalCache(FSCache cache)
-
setUriResolver
public void setUriResolver(FSUriResolver resolver)
-
getDefaultUriResolver
public FSUriResolver getDefaultUriResolver()
-
clearImageCache
public void clearImageCache()
Empties the image cache entirely.
-
getProtocolFactory
protected FSStreamFactory getProtocolFactory(String protocol)
-
hasProtocolFactory
protected boolean hasProtocolFactory(String protocol)
-
openStream
protected InputStream openStream(String uri)
Gets a InputStream for the resource identified by a resolved URI.
-
openReader
protected Reader openReader(String uri)
Gets a reader for the identified resource by a resolved URI.
-
readAll
protected String readAll(Reader reader) throws IOException
- Throws:
IOException
-
getCacheStream
protected InputStream getCacheStream(String uri)
-
getCSSResource
public CSSResource getCSSResource(String uri)
Retrieves the CSS located at the given URI. It's assumed the URI does point to a CSS file--the URI will be resolved, accessed (using the set FSStreamFactory or URL::openStream), opened, read and then passed into the CSS parser. The result is packed up into an CSSResource for later consumption.- Specified by:
getCSSResourcein interfaceUserAgentCallback- Parameters:
uri- Location of the CSS source.- Returns:
- A CSSResource containing the CSS reader or null if not available.
-
getImageResource
public ImageResource getImageResource(String uri)
Retrieves the image located at the given URI. It's assumed the URI does point to an image--the URI will be accessed (using the set HttpStreamFactory or URL::openStream), opened, read and then passed into the JDK image-parsing routines. The result is packed up into an ImageResource for later consumption.- Specified by:
getImageResourcein interfaceUserAgentCallback- Parameters:
uri- Location of the image source.- Returns:
- An ImageResource containing the image.
-
getXMLResource
public XMLResource getXMLResource(String uri)
Retrieves the XML located at the given URI. It's assumed the URI does point to a XML--the URI will be accessed (using the set HttpStreamFactory or URL::openStream), 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:
getXMLResourcein interfaceUserAgentCallback- Parameters:
uri- Location of the XML source.- Returns:
- An XMLResource containing the image.
-
getBinaryResource
public byte[] getBinaryResource(String uri)
Description copied from interface:UserAgentCallbackRetrieves a binary resource located at a given URI and returns its contents as a byte array ornullif the resource could not be loaded.- Specified by:
getBinaryResourcein interfaceUserAgentCallback
-
isVisited
public boolean isVisited(String uri)
Returns true if the given URI was visited, meaning it was requested at some point since initialization.- Specified by:
isVisitedin interfaceUserAgentCallback- Parameters:
uri- A URI which might have been visited.- Returns:
- Always false; visits are not tracked in the NaiveUserAgent.
-
setBaseURL
public void setBaseURL(String uri)
URL relative to which URIs are resolved.- Specified by:
setBaseURLin interfaceUserAgentCallback- Parameters:
uri- A URI which anchors other, possibly relative URIs.
-
getBaseURL
public String getBaseURL()
Returns the current baseUrl for this class.- Specified by:
getBaseURLin interfaceUserAgentCallback- Returns:
- the base uri, possibly in the implementations private uri-space
-
documentStarted
public void documentStarted()
Description copied from interface:DocumentListenerIndicates 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:
documentStartedin interfaceDocumentListener
-
documentLoaded
public void documentLoaded()
Description copied from interface:DocumentListenerIndicates 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:
documentLoadedin interfaceDocumentListener
-
onLayoutException
public void onLayoutException(Throwable t)
Description copied from interface:DocumentListenerCalled when document layout failed with an exception. AllThrowableobjects thrown (except forThreadDeath) during layout and not otherwise handled will be provided to this method. If aDocumentListenerhas been defined an XHTML panel, the listener is entirely responsible for handling the exception. No other action will be taken.- Specified by:
onLayoutExceptionin interfaceDocumentListener
-
onRenderException
public void onRenderException(Throwable t)
Description copied from interface:DocumentListenerCalled when document render failed with an exception. AllThrowableobjects thrown (except forThreadDeath) during render and not otherwise handled will be provided to this method. If aDocumentListenerhas been defined an XHTML panel, the listener is entirely responsible for handling the exception. No other action will be taken.- Specified by:
onRenderExceptionin interfaceDocumentListener
-
resolveURI
public String resolveURI(String uri)
Description copied from interface:UserAgentCallbackUsed to find a uri that may be relative to the BaseURL. The returned value will always only be used via methods in the same implementation of this interface, therefore may be a private uri-space.- Specified by:
resolveURIin interfaceUserAgentCallback- Parameters:
uri- an absolute or relative (to baseURL) uri to be resolved.- Returns:
- the full uri in uri-spaces known to the current implementation.
-
resolveUri
public String resolveUri(String baseUri, String uri)
- Specified by:
resolveUriin interfaceUserAgentCallback
-
setExternalTextCache
public void setExternalTextCache(FSMultiThreadCache<String> textCache)
-
setExternalByteCache
public void setExternalByteCache(FSMultiThreadCache<byte[]> byteCache)
-
-