Package com.openhtmltopdf.swing
Class NaiveUserAgent
- java.lang.Object
-
- com.openhtmltopdf.swing.NaiveUserAgent
-
- All Implemented Interfaces:
DocumentListener
,UserAgentCallback
- Direct Known Subclasses:
PdfBoxUserAgent
public class NaiveUserAgent extends java.lang.Object implements UserAgentCallback, DocumentListener
NaiveUserAgent is a simple implementation of
UserAgentCallback
which places no restrictions on what XML, CSS or images are loaded.The NaiveUserAgent has a simple per-run cache for images so that the same image is not embedded in a document multiple times.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
NaiveUserAgent.ByteStream
static class
NaiveUserAgent.DataUriFactory
static class
NaiveUserAgent.DefaultAccessController
static class
NaiveUserAgent.DefaultHttpStream
static class
NaiveUserAgent.DefaultHttpStreamFactory
static class
NaiveUserAgent.DefaultUriResolver
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<ExternalResourceControlPriority,java.util.function.BiPredicate<java.lang.String,ExternalResourceType>>
_accessControllers
protected java.lang.String
_baseUri
protected java.util.LinkedHashMap<java.lang.String,ImageResource>
_imageCache
a (simple) cache This is only useful for the one run.protected java.util.Map<java.lang.String,FSStreamFactory>
_protocolsStreamFactory
protected FSUriResolver
_resolver
protected FSUriResolver
DEFAULT_URI_RESOLVER
-
Constructor Summary
Constructors Constructor Description NaiveUserAgent()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
checkAccessAllowed(java.lang.String uriOrResolved, ExternalResourceType type, ExternalResourceControlPriority priority)
void
clearImageCache()
Deprecated.void
documentLoaded()
Deprecated.void
documentStarted()
Deprecated.protected java.lang.String
extractProtocol(java.lang.String uri)
java.lang.String
getBaseURL()
Returns the current baseUrl for this class.byte[]
getBinaryResource(java.lang.String uri, ExternalResourceType type)
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.CSSResource
getCSSResource(java.lang.String uri, ExternalResourceType type)
Retrieves the CSS located at the given URI.FSUriResolver
getDefaultUriResolver()
ImageResource
getImageResource(java.lang.String uri, ExternalResourceType type)
Retrieves the image located at the given URI.protected FSStreamFactory
getProtocolFactory(java.lang.String protocol)
XMLResource
getXMLResource(java.lang.String uri, ExternalResourceType type)
Retrieves the XML located at the given URI.protected boolean
hasProtocolFactory(java.lang.String protocol)
boolean
isVisited(java.lang.String uri)
Returns true if the given URI was visited, meaning it was requested at some point since initialization.void
onLayoutException(java.lang.Throwable t)
Deprecated.void
onRenderException(java.lang.Throwable t)
Deprecated.protected java.io.Reader
openReader(java.lang.String uri)
Gets a reader for the identified resource by a resolved URI.protected java.io.InputStream
openStream(java.lang.String uri)
Gets a InputStream for the resource identified by a resolved URI.protected java.lang.String
readAll(java.io.Reader reader)
java.lang.String
resolveUri(java.lang.String baseUri, java.lang.String uri)
java.lang.String
resolveURI(java.lang.String uri)
Used to find a uri that may be relative to the BaseURL.void
setAccessController(ExternalResourceControlPriority prio, java.util.function.BiPredicate<java.lang.String,ExternalResourceType> controller)
void
setBaseURL(java.lang.String uri)
URL relative to which URIs are resolved.void
setProtocolsStreamFactory(java.util.Map<java.lang.String,FSStreamFactory> protocolsStreamFactory)
void
setUriResolver(FSUriResolver resolver)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.openhtmltopdf.extend.UserAgentCallback
getBinaryResource, getCSSResource, getImageResource, getXMLResource
-
-
-
-
Field Detail
-
_imageCache
protected final java.util.LinkedHashMap<java.lang.String,ImageResource> _imageCache
a (simple) cache This is only useful for the one run.
-
DEFAULT_URI_RESOLVER
protected final FSUriResolver DEFAULT_URI_RESOLVER
-
_accessControllers
protected final java.util.Map<ExternalResourceControlPriority,java.util.function.BiPredicate<java.lang.String,ExternalResourceType>> _accessControllers
-
_resolver
protected FSUriResolver _resolver
-
_baseUri
protected java.lang.String _baseUri
-
_protocolsStreamFactory
protected java.util.Map<java.lang.String,FSStreamFactory> _protocolsStreamFactory
-
-
Method Detail
-
setProtocolsStreamFactory
public void setProtocolsStreamFactory(java.util.Map<java.lang.String,FSStreamFactory> protocolsStreamFactory)
-
setUriResolver
public void setUriResolver(FSUriResolver resolver)
-
getDefaultUriResolver
public FSUriResolver getDefaultUriResolver()
-
clearImageCache
@Deprecated public void clearImageCache()
Deprecated.Empties the image cache entirely.
-
getProtocolFactory
protected FSStreamFactory getProtocolFactory(java.lang.String protocol)
-
hasProtocolFactory
protected boolean hasProtocolFactory(java.lang.String protocol)
-
extractProtocol
protected java.lang.String extractProtocol(java.lang.String uri) throws java.net.URISyntaxException
- Throws:
java.net.URISyntaxException
-
openStream
protected java.io.InputStream openStream(java.lang.String uri)
Gets a InputStream for the resource identified by a resolved URI.
-
openReader
protected java.io.Reader openReader(java.lang.String uri)
Gets a reader for the identified resource by a resolved URI.
-
readAll
protected java.lang.String readAll(java.io.Reader reader) throws java.io.IOException
- Throws:
java.io.IOException
-
getCSSResource
public CSSResource getCSSResource(java.lang.String uri, ExternalResourceType type)
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:
getCSSResource
in interfaceUserAgentCallback
- Parameters:
uri
- Location of the CSS source.- Returns:
- A CSSResource containing the CSS reader or null if not available.
-
getImageResource
public ImageResource getImageResource(java.lang.String uri, ExternalResourceType type)
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:
getImageResource
in interfaceUserAgentCallback
- Parameters:
uri
- Location of the image source.- Returns:
- An ImageResource containing the image.
-
getXMLResource
public XMLResource getXMLResource(java.lang.String uri, ExternalResourceType type)
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:
getXMLResource
in interfaceUserAgentCallback
- Parameters:
uri
- Location of the XML source.type
- Either xhtml or svg.- Returns:
- An XMLResource containing the image.
-
getBinaryResource
public byte[] getBinaryResource(java.lang.String uri, ExternalResourceType type)
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
public boolean isVisited(java.lang.String uri)
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
public void setBaseURL(java.lang.String uri)
URL relative to which URIs are resolved.- Specified by:
setBaseURL
in interfaceUserAgentCallback
- Parameters:
uri
- A URI which anchors other, possibly relative URIs.
-
setAccessController
public void setAccessController(ExternalResourceControlPriority prio, java.util.function.BiPredicate<java.lang.String,ExternalResourceType> controller)
-
checkAccessAllowed
public boolean checkAccessAllowed(java.lang.String uriOrResolved, ExternalResourceType type, ExternalResourceControlPriority priority)
-
getBaseURL
public java.lang.String 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
@Deprecated public void documentStarted()
Deprecated.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
@Deprecated public void documentLoaded()
Deprecated.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
@Deprecated public void onLayoutException(java.lang.Throwable t)
Deprecated.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
@Deprecated public void onRenderException(java.lang.Throwable t)
Deprecated.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
-
resolveURI
public java.lang.String resolveURI(java.lang.String uri)
Description copied from interface:UserAgentCallback
Used 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:
resolveURI
in 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 java.lang.String resolveUri(java.lang.String baseUri, java.lang.String uri)
- Specified by:
resolveUri
in interfaceUserAgentCallback
-
-