Top Description Inners Fields Constructors Methods
com.openhtmltopdf.swing

public Class NaiveUserAgent

extends Object
implements UserAgentCallback, DocumentListener
Class Inheritance
All Implemented Interfaces
com.openhtmltopdf.event.DocumentListener, com.openhtmltopdf.extend.UserAgentCallback
Imports
java.awt.image.BufferedImage, java.io.*, java.net.MalformedURLException, .URI, .URISyntaxException, .URL, .URLConnection, java.nio.charset.StandardCharsets, java.util.EnumMap, .HashMap, .LinkedHashMap, .Map, java.util.function.BiPredicate, java.util.logging.Level, javax.imageio.ImageIO, com.openhtmltopdf.event.DocumentListener, com.openhtmltopdf.extend.FSUriResolver, .FSStreamFactory, .FSStream, .UserAgentCallback, com.openhtmltopdf.outputdevice.helper.ExternalResourceControlPriority, .ExternalResourceType, com.openhtmltopdf.resource.CSSResource, .ImageResource, .XMLResource, com.openhtmltopdf.util.ImageUtil, .LogMessageId, .XRLog

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.

Author
Torbjoern Gannholm

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static class
public static class
public static class
public static class
public static class

Field Summary

Modifier and TypeField and Description
protected final Map<ExternalResourceControlPriority, BiPredicate<String, ExternalResourceType>>
protected String
protected final LinkedHashMap<String, ImageResource>
_imageCache

a (simple) cache This is only useful for the one run.

protected Map<String, FSStreamFactory>
protected FSUriResolver
protected final FSUriResolver

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
public void
clearImageCache()
Deprecated

Empties the image cache entirely.

public void
documentLoaded()

Implements com.openhtmltopdf.event.DocumentListener.documentLoaded.

Deprecated

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.

public void
documentStarted()

Implements com.openhtmltopdf.event.DocumentListener.documentStarted.

Deprecated

Indicates document has been requested (e.g. a new document is going to be loaded).

protected String
public String
getBaseURL()

Implements com.openhtmltopdf.extend.UserAgentCallback.getBaseURL.

Returns the current baseUrl for this class.

public byte[]
getBinaryResource(String uri, ExternalResourceType type)

Implements com.openhtmltopdf.extend.UserAgentCallback.getBinaryResource.

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.

public CSSResource

Returns:

A CSSResource containing the CSS reader or null if not available.
getCSSResource
(String
Location of the CSS source.
uri
,
ExternalResourceType type)

Implements com.openhtmltopdf.extend.UserAgentCallback.getCSSResource.

Retrieves the CSS located at the given URI.

public FSUriResolver
public ImageResource

Returns:

An ImageResource containing the image.
getImageResource
(String
Location of the image source.
uri
,
ExternalResourceType type)

Implements com.openhtmltopdf.extend.UserAgentCallback.getImageResource.

Retrieves the image located at the given URI.

protected FSStreamFactory
public XMLResource

Returns:

An XMLResource containing the image.
getXMLResource
(String
Location of the XML source.
uri
,
ExternalResourceType
Either xhtml or svg.
type
)

Implements com.openhtmltopdf.extend.UserAgentCallback.getXMLResource.

Retrieves the XML located at the given URI.

protected boolean
public boolean

Returns:

Always false; visits are not tracked in the NaiveUserAgent.
isVisited
(String
A URI which might have been visited.
uri
)

Implements com.openhtmltopdf.extend.UserAgentCallback.isVisited.

Returns true if the given URI was visited, meaning it was requested at some point since initialization.

public void
onLayoutException(Throwable t)

Implements com.openhtmltopdf.event.DocumentListener.onLayoutException.

Deprecated

Called when document layout failed with an exception.

public void
onRenderException(Throwable t)

Implements com.openhtmltopdf.event.DocumentListener.onRenderException.

Deprecated

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)

public String
resolveURI(String
an absolute or relative (to baseURL) uri to be resolved.
uri
)

Implements com.openhtmltopdf.extend.UserAgentCallback.resolveURI.

Used to find a uri that may be relative to the BaseURL.

public String
public void
public void
setBaseURL(String
A URI which anchors other, possibly relative URIs.
uri
)

Implements com.openhtmltopdf.extend.UserAgentCallback.setBaseURL.

URL relative to which URIs are resolved.

public void
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_accessControllersback to summary
protected final Map<ExternalResourceControlPriority, BiPredicate<String, ExternalResourceType>> _accessControllers
_baseUriback to summary
protected String _baseUri
_imageCacheback to summary
protected final LinkedHashMap<String, ImageResource> _imageCache

a (simple) cache This is only useful for the one run.

_protocolsStreamFactoryback to summary
protected Map<String, FSStreamFactory> _protocolsStreamFactory
_resolverback to summary
protected FSUriResolver _resolver
DEFAULT_URI_RESOLVERback to summary
protected final FSUriResolver DEFAULT_URI_RESOLVER

Constructor Detail

NaiveUserAgentback to summary
public NaiveUserAgent()

Method Detail

checkAccessAllowedback to summary
public boolean checkAccessAllowed(String uriOrResolved, ExternalResourceType type, ExternalResourceControlPriority priority)
clearImageCacheback to summary
public void clearImageCache()

Deprecated

Empties the image cache entirely.

Annotations
@Deprecated
documentLoadedback to summary
public void documentLoaded()

Implements com.openhtmltopdf.event.DocumentListener.documentLoaded.

Doc from com.openhtmltopdf.event.DocumentListener.documentLoaded.

Deprecated

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).

Annotations
@Override
@Deprecated
documentStartedback to summary
public void documentStarted()

Implements com.openhtmltopdf.event.DocumentListener.documentStarted.

Doc from com.openhtmltopdf.event.DocumentListener.documentStarted.

Deprecated

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.

Annotations
@Override
@Deprecated
extractProtocolback to summary
protected String extractProtocol(String uri) throws URISyntaxException
getBaseURLback to summary
public String getBaseURL()

Implements com.openhtmltopdf.extend.UserAgentCallback.getBaseURL.

Returns the current baseUrl for this class.

Returns:String

Doc from com.openhtmltopdf.extend.UserAgentCallback.getBaseURL.

the base uri, possibly in the implementations private uri-space

Annotations
@Override
getBinaryResourceback to summary
public byte[] getBinaryResource(String uri, ExternalResourceType type)

Implements com.openhtmltopdf.extend.UserAgentCallback.getBinaryResource.

Doc from com.openhtmltopdf.extend.UserAgentCallback.getBinaryResource.

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.

Annotations
@Override
getCSSResourceback to summary
public CSSResource getCSSResource(String uri, ExternalResourceType type)

Implements com.openhtmltopdf.extend.UserAgentCallback.getCSSResource.

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.

Parameters
uri:String

Location of the CSS source.

Returns:CSSResource

A CSSResource containing the CSS reader or null if not available.

Annotations
@Override
getDefaultUriResolverback to summary
public FSUriResolver getDefaultUriResolver()
getImageResourceback to summary
public ImageResource getImageResource(String uri, ExternalResourceType type)

Implements com.openhtmltopdf.extend.UserAgentCallback.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 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.

Parameters
uri:String

Location of the image source.

Returns:ImageResource

An ImageResource containing the image.

Annotations
@Override
getProtocolFactoryback to summary
protected FSStreamFactory getProtocolFactory(String protocol)
getXMLResourceback to summary
public XMLResource getXMLResource(String uri, ExternalResourceType type)

Implements com.openhtmltopdf.extend.UserAgentCallback.getXMLResource.

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.

Parameters
uri:String

Location of the XML source.

type:ExternalResourceType

Doc from com.openhtmltopdf.extend.UserAgentCallback.getXMLResource.

Either xhtml or svg.

Returns:XMLResource

An XMLResource containing the image.

Annotations
@Override
hasProtocolFactoryback to summary
protected boolean hasProtocolFactory(String protocol)
isVisitedback to summary
public boolean isVisited(String uri)

Implements com.openhtmltopdf.extend.UserAgentCallback.isVisited.

Returns true if the given URI was visited, meaning it was requested at some point since initialization.

Parameters
uri:String

A URI which might have been visited.

Returns:boolean

Always false; visits are not tracked in the NaiveUserAgent.

Annotations
@Override
onLayoutExceptionback to summary
public void onLayoutException(Throwable t)

Implements com.openhtmltopdf.event.DocumentListener.onLayoutException.

Doc from com.openhtmltopdf.event.DocumentListener.onLayoutException.

Deprecated

Called when document layout failed with an exception. All Throwable 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.

Annotations
@Override
@Deprecated
onRenderExceptionback to summary
public void onRenderException(Throwable t)

Implements com.openhtmltopdf.event.DocumentListener.onRenderException.

Doc from com.openhtmltopdf.event.DocumentListener.onRenderException.

Deprecated

Called when document render failed with an exception. All Throwable 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.

Annotations
@Override
@Deprecated
openReaderback to summary
protected Reader openReader(String uri)

Gets a reader for the identified resource by a resolved URI.

openStreamback to summary
protected InputStream openStream(String uri)

Gets a InputStream for the resource identified by a resolved URI.

readAllback to summary
protected String readAll(Reader reader) throws IOException
resolveURIback to summary
public String resolveURI(String uri)

Implements com.openhtmltopdf.extend.UserAgentCallback.resolveURI.

Doc from com.openhtmltopdf.extend.UserAgentCallback.resolveURI.

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.

Parameters
uri:String

an absolute or relative (to baseURL) uri to be resolved.

Returns:String

the full uri in uri-spaces known to the current implementation.

Annotations
@Override
resolveUriback to summary
public String resolveUri(String baseUri, String uri)

Implements com.openhtmltopdf.extend.UserAgentCallback.resolveUri.

Annotations
@Override
setAccessControllerback to summary
public void setAccessController(ExternalResourceControlPriority prio, BiPredicate<String, ExternalResourceType> controller)
setBaseURLback to summary
public void setBaseURL(String uri)

Implements com.openhtmltopdf.extend.UserAgentCallback.setBaseURL.

URL relative to which URIs are resolved.

Parameters
uri:String

A URI which anchors other, possibly relative URIs.

Annotations
@Override
setProtocolsStreamFactoryback to summary
public void setProtocolsStreamFactory(Map<String, FSStreamFactory> protocolsStreamFactory)
setUriResolverback to summary
public void setUriResolver(FSUriResolver resolver)
com.openhtmltopdf.swing back to summary

private Class NaiveUserAgent.ByteStream

extends Object
implements FSStream
Class Inheritance
All Implemented Interfaces
com.openhtmltopdf.extend.FSStream

Field Summary

Modifier and TypeField and Description
private final byte[]

Constructor Summary

AccessConstructor and Description
pack-priv
ByteStream(byte[] input)

Method Summary

Modifier and TypeMethod and Description
public Reader
public InputStream
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

inputback to summary
private final byte[] input

Constructor Detail

ByteStreamback to summary
pack-priv ByteStream(byte[] input)

Method Detail

getReaderback to summary
public Reader getReader()

Implements com.openhtmltopdf.extend.FSStream.getReader.

Annotations
@Override
getStreamback to summary
public InputStream getStream()

Implements com.openhtmltopdf.extend.FSStream.getStream.

Annotations
@Override
com.openhtmltopdf.swing back to summary

public Class NaiveUserAgent.DataUriFactory

extends Object
implements FSStreamFactory
Class Inheritance
All Implemented Interfaces
com.openhtmltopdf.extend.FSStreamFactory

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public FSStream
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DataUriFactoryback to summary
public DataUriFactory()

Method Detail

getUrlback to summary
public FSStream getUrl(String url)

Implements com.openhtmltopdf.extend.FSStreamFactory.getUrl.

Annotations
@Override
com.openhtmltopdf.swing back to summary

public Class NaiveUserAgent.DefaultAccessController

extends Object
implements BiPredicate<String, ExternalResourceType>
Class Inheritance
  • java.lang.Object
  • com.openhtmltopdf.swing.NaiveUserAgent.DefaultAccessController
All Implemented Interfaces
java.util.function.BiPredicate

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
test(String
the first input argument
uri
,
ExternalResourceType
the second input argument
resourceType
)

Implements java.util.function.BiPredicate.test.

Evaluates this predicate on the given arguments.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DefaultAccessControllerback to summary
public DefaultAccessController()

Method Detail

testback to summary
public boolean test(String uri, ExternalResourceType resourceType)

Implements java.util.function.BiPredicate.test.

Doc from java.util.function.BiPredicate.test.

Evaluates this predicate on the given arguments.

Parameters
uri:String

the first input argument

resourceType:ExternalResourceType

the second input argument

Returns:boolean

true if the input arguments match the predicate, otherwise false

com.openhtmltopdf.swing back to summary

public Class NaiveUserAgent.DefaultHttpStream

extends Object
implements FSStream
Class Inheritance
All Implemented Interfaces
com.openhtmltopdf.extend.FSStream

Field Summary

Modifier and TypeField and Description
private InputStream

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Reader
public InputStream
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

strmback to summary
private InputStream strm

Constructor Detail

DefaultHttpStreamback to summary
public DefaultHttpStream(InputStream strm)

Method Detail

getReaderback to summary
public Reader getReader()

Implements com.openhtmltopdf.extend.FSStream.getReader.

Annotations
@Override
getStreamback to summary
public InputStream getStream()

Implements com.openhtmltopdf.extend.FSStream.getStream.

Annotations
@Override
com.openhtmltopdf.swing back to summary

public Class NaiveUserAgent.DefaultHttpStreamFactory

extends Object
implements FSStreamFactory
Class Inheritance
  • java.lang.Object
  • com.openhtmltopdf.swing.NaiveUserAgent.DefaultHttpStreamFactory
All Implemented Interfaces
com.openhtmltopdf.extend.FSStreamFactory

Field Summary

Modifier and TypeField and Description
pack-priv static final int
pack-priv final int
pack-priv static final int
pack-priv final int

Constructor Summary

AccessConstructor and Description
public
DefaultHttpStreamFactory(int connectTimeout, int readTimeout)

Create a FSStreamFactory for http, https with specified timeouts.

public
DefaultHttpStreamFactory()

Create a FSStreamFactory with 10 second connect timeout and 30 second read timeout.

Method Summary

Modifier and TypeMethod and Description
public FSStream
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CONNECTION_TIMEOUTback to summary
pack-priv static final int CONNECTION_TIMEOUT
connectTimeoutback to summary
pack-priv final int connectTimeout
READ_TIMEOUTback to summary
pack-priv static final int READ_TIMEOUT
readTimeoutback to summary
pack-priv final int readTimeout

Constructor Detail

DefaultHttpStreamFactoryback to summary
public DefaultHttpStreamFactory(int connectTimeout, int readTimeout)

Create a FSStreamFactory for http, https with specified timeouts. Uses URLConnection to perform requests. Zero value for timeout specifies no timeout.

DefaultHttpStreamFactoryback to summary
public DefaultHttpStreamFactory()

Create a FSStreamFactory with 10 second connect timeout and 30 second read timeout.

Method Detail

getUrlback to summary
public FSStream getUrl(String uri)

Implements com.openhtmltopdf.extend.FSStreamFactory.getUrl.

Annotations
@Override
com.openhtmltopdf.swing back to summary

public Class NaiveUserAgent.DefaultUriResolver

extends Object
implements FSUriResolver
Class Inheritance
All Implemented Interfaces
com.openhtmltopdf.extend.FSUriResolver

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

A URI as String, resolved, or null if there was an exception (for example if the URI is malformed).
resolveURI
(String baseUri, String
A URI, possibly relative.
uri
)

Implements com.openhtmltopdf.extend.FSUriResolver.resolveURI.

Resolves the URI; if absolute, leaves as is, if relative, returns an absolute URI based on the baseUrl for the agent.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DefaultUriResolverback to summary
public DefaultUriResolver()

Method Detail

resolveURIback to summary
public String resolveURI(String baseUri, String uri)

Implements com.openhtmltopdf.extend.FSUriResolver.resolveURI.

Resolves the URI; if absolute, leaves as is, if relative, returns an absolute URI based on the baseUrl for the agent.

Parameters
uri:String

A URI, possibly relative.

Returns:String

A URI as String, resolved, or null if there was an exception (for example if the URI is malformed).

Annotations
@Override