Package nl.siegmann.epublib.domain
Class Resource
- java.lang.Object
-
- nl.siegmann.epublib.domain.Resource
-
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
LazyResource
public class Resource extends java.lang.Object implements java.io.SerializableRepresents a resource that is part of the epub. A resource can be a html file, image, xml, etc.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]dataprivate java.lang.Stringhrefprivate java.lang.Stringidprivate java.lang.StringinputEncodingprivate MediaTypemediaTypeprotected java.lang.StringoriginalHrefprivate static longserialVersionUIDprivate java.lang.Stringtitle
-
Constructor Summary
Constructors Constructor Description Resource(byte[] data, java.lang.String href)Creates a resource with the given data at the specified href.Resource(byte[] data, MediaType mediaType)Creates a Resource with the given data and MediaType.Resource(java.io.InputStream in, java.lang.String href)Creates a resource with the data from the given InputStream at the specified href.Resource(java.io.Reader in, java.lang.String href)Creates a resource with the data from the given Reader at the specified href.Resource(java.lang.String href)Creates an empty Resource with the given href.Resource(java.lang.String id, byte[] data, java.lang.String href, MediaType mediaType)Creates a resource with the given id, data, mediatype at the specified href.Resource(java.lang.String id, byte[] data, java.lang.String href, MediaType mediaType, java.lang.String inputEncoding)Creates a resource with the given id, data, mediatype at the specified href.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Tells this resource to release its cached data.booleanequals(java.lang.Object resourceObject)Checks to see of the given resourceObject is a resource and whether its href is equal to this one.byte[]getData()The contents of the resource as a byte[]java.lang.StringgetHref()The location of the resource within the contents folder of the epub file.java.lang.StringgetId()The resources Id.java.lang.StringgetInputEncoding()The character encoding of the resource.java.io.InputStreamgetInputStream()Gets the contents of the Resource as an InputStream.MediaTypegetMediaType()This resource's mediaType.java.io.ReadergetReader()Gets the contents of the Resource as Reader.longgetSize()Returns the size of this resource in bytes.java.lang.StringgetTitle()If the title is found by scanning the underlying html document then it is cached here.inthashCode()Gets the hashCode of the Resource's href.voidsetData(byte[] data)Sets the data of the Resource.voidsetHref(java.lang.String href)Sets the Resource's href.voidsetId(java.lang.String id)Sets the Resource's id: Make sure it is unique and a valid identifier.voidsetInputEncoding(java.lang.String encoding)Sets the Resource's input character encoding.voidsetMediaType(MediaType mediaType)voidsetTitle(java.lang.String title)java.lang.StringtoString()
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
id
private java.lang.String id
-
title
private java.lang.String title
-
href
private java.lang.String href
-
originalHref
protected java.lang.String originalHref
-
mediaType
private MediaType mediaType
-
inputEncoding
private java.lang.String inputEncoding
-
data
protected byte[] data
-
-
Constructor Detail
-
Resource
public Resource(java.lang.String href)
Creates an empty Resource with the given href. Assumes that if the data is of a text type (html/css/etc) then the encoding will be UTF-8- Parameters:
href- The location of the resource within the epub. Example: "chapter1.html".
-
Resource
public Resource(byte[] data, MediaType mediaType)Creates a Resource with the given data and MediaType. The href will be automatically generated. Assumes that if the data is of a text type (html/css/etc) then the encoding will be UTF-8- Parameters:
data- The Resource's contentsmediaType- The MediaType of the Resource
-
Resource
public Resource(byte[] data, java.lang.String href)Creates a resource with the given data at the specified href. The MediaType will be determined based on the href extension. Assumes that if the data is of a text type (html/css/etc) then the encoding will be UTF-8- Parameters:
data- The Resource's contentshref- The location of the resource within the epub. Example: "chapter1.html".- See Also:
MediatypeService.determineMediaType(String)
-
Resource
public Resource(java.io.Reader in, java.lang.String href) throws java.io.IOExceptionCreates a resource with the data from the given Reader at the specified href. The MediaType will be determined based on the href extension.- Parameters:
in- The Resource's contentshref- The location of the resource within the epub. Example: "cover.jpg".- Throws:
java.io.IOException- See Also:
MediatypeService.determineMediaType(String)
-
Resource
public Resource(java.io.InputStream in, java.lang.String href) throws java.io.IOExceptionCreates a resource with the data from the given InputStream at the specified href. The MediaType will be determined based on the href extension.- Parameters:
in- The Resource's contentshref- The location of the resource within the epub. Example: "cover.jpg".- Throws:
java.io.IOException- See Also:
Assumes that if the data is of a text type (html/css/etc) then the encoding will be UTF-8 It is recommended to us the method for creating textual (html/css/etc) resources to prevent encoding problems. Use this method only for binary Resources like images, fonts, etc.
-
Resource
public Resource(java.lang.String id, byte[] data, java.lang.String href, MediaType mediaType)Creates a resource with the given id, data, mediatype at the specified href. Assumes that if the data is of a text type (html/css/etc) then the encoding will be UTF-8- Parameters:
id- The id of the Resource. Internal use only. Will be auto-generated if it has a null-value.data- The Resource's contentshref- The location of the resource within the epub. Example: "chapter1.html".mediaType- The resources MediaType
-
Resource
public Resource(java.lang.String id, byte[] data, java.lang.String href, MediaType mediaType, java.lang.String inputEncoding)Creates a resource with the given id, data, mediatype at the specified href. If the data is of a text type (html/css/etc) then it will use the given inputEncoding.- Parameters:
id- The id of the Resource. Internal use only. Will be auto-generated if it has a null-value.data- The Resource's contentshref- The location of the resource within the epub. Example: "chapter1.html".mediaType- The resources MediaTypeinputEncoding- If the data is of a text type (html/css/etc) then it will use the given inputEncoding.
-
-
Method Detail
-
getInputStream
public java.io.InputStream getInputStream() throws java.io.IOExceptionGets the contents of the Resource as an InputStream.- Returns:
- The contents of the Resource.
- Throws:
java.io.IOException
-
getData
public byte[] getData() throws java.io.IOExceptionThe contents of the resource as a byte[]- Returns:
- The contents of the resource
- Throws:
java.io.IOException
-
close
public void close()
Tells this resource to release its cached data. If this resource was not lazy-loaded, this is a no-op.
-
setData
public void setData(byte[] data)
Sets the data of the Resource. If the data is a of a different type then the original data then make sure to change the MediaType.- Parameters:
data-
-
getSize
public long getSize()
Returns the size of this resource in bytes.- Returns:
- the size.
-
getTitle
public java.lang.String getTitle()
If the title is found by scanning the underlying html document then it is cached here.- Returns:
- the title
-
setId
public void setId(java.lang.String id)
Sets the Resource's id: Make sure it is unique and a valid identifier.- Parameters:
id-
-
getId
public java.lang.String getId()
The resources Id. Must be both unique within all the resources of this book and a valid identifier.- Returns:
- The resources Id.
-
getHref
public java.lang.String getHref()
The location of the resource within the contents folder of the epub file. Example:
images/cover.jpg
content/chapter1.xhtml- Returns:
- The location of the resource within the contents folder of the epub file.
-
setHref
public void setHref(java.lang.String href)
Sets the Resource's href.- Parameters:
href-
-
getInputEncoding
public java.lang.String getInputEncoding()
The character encoding of the resource. Is allowed to be null for non-text resources like images.- Returns:
- The character encoding of the resource.
-
setInputEncoding
public void setInputEncoding(java.lang.String encoding)
Sets the Resource's input character encoding.- Parameters:
encoding-
-
getReader
public java.io.Reader getReader() throws java.io.IOExceptionGets the contents of the Resource as Reader. Does all sorts of smart things (courtesy of apache commons io XMLStreamREader) to handle encodings, byte order markers, etc.- Returns:
- the contents of the Resource as Reader.
- Throws:
java.io.IOException
-
hashCode
public int hashCode()
Gets the hashCode of the Resource's href.- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object resourceObject)
Checks to see of the given resourceObject is a resource and whether its href is equal to this one.- Overrides:
equalsin classjava.lang.Object- Returns:
- whether the given resourceObject is a resource and whether its href is equal to this one.
-
getMediaType
public MediaType getMediaType()
This resource's mediaType.- Returns:
- This resource's mediaType.
-
setMediaType
public void setMediaType(MediaType mediaType)
-
setTitle
public void setTitle(java.lang.String title)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-