Package com.openhtmltopdf.swing
Class NaiveUserAgent
java.lang.Object
com.openhtmltopdf.swing.NaiveUserAgent
- All Implemented Interfaces:
DocumentListener,UserAgentCallback
- Direct Known Subclasses:
PdfBoxUserAgent
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 ClassesModifier and TypeClassDescriptionprivate static classstatic classstatic classstatic classstatic classstatic class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Map<ExternalResourceControlPriority, BiPredicate<String, ExternalResourceType>> protected Stringprotected final LinkedHashMap<String, ImageResource> a (simple) cache This is only useful for the one run.protected Map<String, FSStreamFactory> protected FSUriResolverprotected final FSUriResolver -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancheckAccessAllowed(String uriOrResolved, ExternalResourceType type, ExternalResourceControlPriority priority) voidDeprecated.voidDeprecated.voidDeprecated.protected StringextractProtocol(String uri) Returns the current baseUrl for this class.byte[]getBinaryResource(String uri, ExternalResourceType type) Retrieves a binary resource located at a given URI and returns its contents as a byte array ornullif the resource could not be loaded.getCSSResource(String uri, ExternalResourceType type) Retrieves the CSS located at the given URI.getImageResource(String uri, ExternalResourceType type) Retrieves the image located at the given URI.protected FSStreamFactorygetProtocolFactory(String protocol) getXMLResource(String uri, ExternalResourceType type) Retrieves the XML located at the given URI.protected booleanhasProtocolFactory(String protocol) booleanReturns true if the given URI was visited, meaning it was requested at some point since initialization.voidDeprecated.voidDeprecated.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 StringresolveUri(String baseUri, String uri) resolveURI(String uri) Used to find a uri that may be relative to the BaseURL.voidsetAccessController(ExternalResourceControlPriority prio, BiPredicate<String, ExternalResourceType> controller) voidsetBaseURL(String uri) URL relative to which URIs are resolved.voidsetProtocolsStreamFactory(Map<String, FSStreamFactory> protocolsStreamFactory) voidsetUriResolver(FSUriResolver resolver) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.openhtmltopdf.extend.UserAgentCallback
getBinaryResource, getCSSResource, getImageResource, getXMLResource
-
Field Details
-
_imageCache
a (simple) cache This is only useful for the one run. -
DEFAULT_URI_RESOLVER
-
_accessControllers
protected final Map<ExternalResourceControlPriority,BiPredicate<String, _accessControllersExternalResourceType>> -
_resolver
-
_baseUri
-
_protocolsStreamFactory
-
-
Constructor Details
-
NaiveUserAgent
public NaiveUserAgent()
-
-
Method Details
-
setProtocolsStreamFactory
-
setUriResolver
-
getDefaultUriResolver
-
clearImageCache
Deprecated.Empties the image cache entirely. -
getProtocolFactory
-
hasProtocolFactory
-
extractProtocol
- Throws:
URISyntaxException
-
openStream
Gets a InputStream for the resource identified by a resolved URI. -
openReader
Gets a reader for the identified resource by a resolved URI. -
readAll
- Throws:
IOException
-
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.- Specified by:
getCSSResourcein interfaceUserAgentCallback- Parameters:
uri- Location of the CSS source.- Returns:
- A CSSResource containing the CSS reader or null if not available.
-
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.- Specified by:
getImageResourcein 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 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.type- Either xhtml or svg.- Returns:
- An XMLResource containing the image.
-
getBinaryResource
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
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
URL relative to which URIs are resolved.- Specified by:
setBaseURLin interfaceUserAgentCallback- Parameters:
uri- A URI which anchors other, possibly relative URIs.
-
setAccessController
public void setAccessController(ExternalResourceControlPriority prio, BiPredicate<String, ExternalResourceType> controller) -
checkAccessAllowed
public boolean checkAccessAllowed(String uriOrResolved, ExternalResourceType type, ExternalResourceControlPriority priority) -
getBaseURL
Returns the current baseUrl for this class.- Specified by:
getBaseURLin interfaceUserAgentCallback- Returns:
- the base uri, possibly in the implementations private uri-space
-
documentStarted
Deprecated.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
Deprecated.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
Deprecated.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
Deprecated.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
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
- Specified by:
resolveUriin interfaceUserAgentCallback
-