Resource.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package me.ag2s.epublib.domain;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.Reader;
  6. import java.io.Serializable;
  7. import me.ag2s.epublib.Constants;
  8. import me.ag2s.epublib.util.IOUtil;
  9. import me.ag2s.epublib.util.StringUtil;
  10. import me.ag2s.epublib.util.commons.io.XmlStreamReader;
  11. /**
  12. * Represents a resource that is part of the epub.
  13. * A resource can be a html file, image, xml, etc.
  14. *
  15. * @author paul
  16. *
  17. */
  18. public class Resource implements Serializable {
  19. private static final long serialVersionUID = 1043946707835004037L;
  20. private String id;
  21. private String title;
  22. private String href;
  23. private String properties;
  24. protected final String originalHref;
  25. private MediaType mediaType;
  26. private String inputEncoding;
  27. protected byte[] data;
  28. /**
  29. * Creates an empty Resource with the given href.
  30. *
  31. * Assumes that if the data is of a text type (html/css/etc) then the encoding will be UTF-8
  32. *
  33. * @param href The location of the resource within the epub. Example: "chapter1.html".
  34. */
  35. public Resource(String href) {
  36. this(null, new byte[0], href, MediaTypes.determineMediaType(href));
  37. }
  38. /**
  39. * Creates a Resource with the given data and MediaType.
  40. * The href will be automatically generated.
  41. *
  42. * Assumes that if the data is of a text type (html/css/etc) then the encoding will be UTF-8
  43. *
  44. * @param data The Resource's contents
  45. * @param mediaType The MediaType of the Resource
  46. */
  47. public Resource(byte[] data, MediaType mediaType) {
  48. this(null, data, null, mediaType);
  49. }
  50. /**
  51. * Creates a resource with the given data at the specified href.
  52. * The MediaType will be determined based on the href extension.
  53. *
  54. * Assumes that if the data is of a text type (html/css/etc) then the encoding will be UTF-8
  55. *
  56. * @see MediaTypes#determineMediaType(String)
  57. *
  58. * @param data The Resource's contents
  59. * @param href The location of the resource within the epub. Example: "chapter1.html".
  60. */
  61. public Resource(byte[] data, String href) {
  62. this(null, data, href, MediaTypes.determineMediaType(href),
  63. Constants.CHARACTER_ENCODING);
  64. }
  65. /**
  66. * Creates a resource with the data from the given Reader at the specified href.
  67. * The MediaType will be determined based on the href extension.
  68. *
  69. * @see MediaTypes#determineMediaType(String)
  70. *
  71. * @param in The Resource's contents
  72. * @param href The location of the resource within the epub. Example: "cover.jpg".
  73. */
  74. public Resource(Reader in, String href) throws IOException {
  75. this(null, IOUtil.toByteArray(in, Constants.CHARACTER_ENCODING), href,
  76. MediaTypes.determineMediaType(href),
  77. Constants.CHARACTER_ENCODING);
  78. }
  79. /**
  80. * Creates a resource with the data from the given InputStream at the specified href.
  81. * The MediaType will be determined based on the href extension.
  82. *
  83. * @see MediaTypes#determineMediaType(String)
  84. *
  85. * Assumes that if the data is of a text type (html/css/etc) then the encoding will be UTF-8
  86. *
  87. * It is recommended to us the {@link #Resource(Reader, String)} method for creating textual
  88. * (html/css/etc) resources to prevent encoding problems.
  89. * Use this method only for binary Resources like images, fonts, etc.
  90. *
  91. *
  92. * @param in The Resource's contents
  93. * @param href The location of the resource within the epub. Example: "cover.jpg".
  94. */
  95. public Resource(InputStream in, String href) throws IOException {
  96. this(null, IOUtil.toByteArray(in), href,
  97. MediaTypes.determineMediaType(href));
  98. }
  99. /**
  100. * Creates a resource with the given id, data, mediatype at the specified href.
  101. * Assumes that if the data is of a text type (html/css/etc) then the encoding will be UTF-8
  102. *
  103. * @param id The id of the Resource. Internal use only. Will be auto-generated if it has a null-value.
  104. * @param data The Resource's contents
  105. * @param href The location of the resource within the epub. Example: "chapter1.html".
  106. * @param mediaType The resources MediaType
  107. */
  108. public Resource(String id, byte[] data, String href, MediaType mediaType) {
  109. this(id, data, href, mediaType, Constants.CHARACTER_ENCODING);
  110. }
  111. public Resource(String id, byte[] data, String href, String originalHref, MediaType mediaType) {
  112. this(id, data, href, originalHref, mediaType, Constants.CHARACTER_ENCODING);
  113. }
  114. /**
  115. * Creates a resource with the given id, data, mediatype at the specified href.
  116. * If the data is of a text type (html/css/etc) then it will use the given inputEncoding.
  117. *
  118. * @param id The id of the Resource. Internal use only. Will be auto-generated if it has a null-value.
  119. * @param data The Resource's contents
  120. * @param href The location of the resource within the epub. Example: "chapter1.html".
  121. * @param mediaType The resources MediaType
  122. * @param inputEncoding If the data is of a text type (html/css/etc) then it will use the given inputEncoding.
  123. */
  124. public Resource(String id, byte[] data, String href, MediaType mediaType,
  125. String inputEncoding) {
  126. this.id = id;
  127. this.href = href;
  128. this.originalHref = href;
  129. this.mediaType = mediaType;
  130. this.inputEncoding = inputEncoding;
  131. this.data = data;
  132. }
  133. public Resource(String id, byte[] data, String href, String originalHref, MediaType mediaType,
  134. String inputEncoding) {
  135. this.id = id;
  136. this.href = href;
  137. this.originalHref = originalHref;
  138. this.mediaType = mediaType;
  139. this.inputEncoding = inputEncoding;
  140. this.data = data;
  141. }
  142. /**
  143. * Gets the contents of the Resource as an InputStream.
  144. *
  145. * @return The contents of the Resource.
  146. *
  147. * @throws IOException IOException
  148. */
  149. public InputStream getInputStream() throws IOException {
  150. return new ByteArrayInputStream(getData());
  151. }
  152. /**
  153. * The contents of the resource as a byte[]
  154. *
  155. * @return The contents of the resource
  156. */
  157. public byte[] getData() throws IOException {
  158. return data;
  159. }
  160. /**
  161. * Tells this resource to release its cached data.
  162. *
  163. * If this resource was not lazy-loaded, this is a no-op.
  164. */
  165. public void close() {
  166. }
  167. /**
  168. * Sets the data of the Resource.
  169. * If the data is a of a different type then the original data then make sure to change the MediaType.
  170. *
  171. * @param data the data of the Resource
  172. */
  173. public void setData(byte[] data) {
  174. this.data = data;
  175. }
  176. /**
  177. * Returns the size of this resource in bytes.
  178. *
  179. * @return the size.
  180. */
  181. public long getSize() {
  182. return data.length;
  183. }
  184. /**
  185. * If the title is found by scanning the underlying html document then it is cached here.
  186. *
  187. * @return the title
  188. */
  189. public String getTitle() {
  190. return title;
  191. }
  192. /**
  193. * Sets the Resource's id: Make sure it is unique and a valid identifier.
  194. *
  195. * @param id Resource's id
  196. */
  197. public void setId(String id) {
  198. this.id = id;
  199. }
  200. /**
  201. * The resources Id.
  202. *
  203. * Must be both unique within all the resources of this book and a valid identifier.
  204. * @return The resources Id.
  205. */
  206. public String getId() {
  207. return id;
  208. }
  209. /**
  210. * The location of the resource within the contents folder of the epub file.
  211. *
  212. * Example:<br/>
  213. * images/cover.jpg<br/>
  214. * content/chapter1.xhtml<br/>
  215. *
  216. * @return The location of the resource within the contents folder of the epub file.
  217. */
  218. public String getHref() {
  219. return href;
  220. }
  221. /**
  222. * Sets the Resource's href.
  223. *
  224. * @param href Resource's href.
  225. */
  226. public void setHref(String href) {
  227. this.href = href;
  228. }
  229. /**
  230. * The character encoding of the resource.
  231. * Is allowed to be null for non-text resources like images.
  232. *
  233. * @return The character encoding of the resource.
  234. */
  235. public String getInputEncoding() {
  236. return inputEncoding;
  237. }
  238. /**
  239. * Sets the Resource's input character encoding.
  240. *
  241. * @param encoding Resource's input character encoding.
  242. */
  243. public void setInputEncoding(String encoding) {
  244. this.inputEncoding = encoding;
  245. }
  246. /**
  247. * Gets the contents of the Resource as Reader.
  248. *
  249. * Does all sorts of smart things (courtesy of apache commons io XMLStreamREader) to handle encodings, byte order markers, etc.
  250. *
  251. * @return the contents of the Resource as Reader.
  252. * @throws IOException IOException
  253. */
  254. public Reader getReader() throws IOException {
  255. return new XmlStreamReader(new ByteArrayInputStream(getData()),
  256. getInputEncoding());
  257. }
  258. /**
  259. * Gets the hashCode of the Resource's href.
  260. *
  261. */
  262. public int hashCode() {
  263. return href.hashCode();
  264. }
  265. /**
  266. * Checks to see of the given resourceObject is a resource and whether its href is equal to this one.
  267. *
  268. * @return whether the given resourceObject is a resource and whether its href is equal to this one.
  269. */
  270. public boolean equals(Object resourceObject) {
  271. if (!(resourceObject instanceof Resource)) {
  272. return false;
  273. }
  274. return href.equals(((Resource) resourceObject).getHref());
  275. }
  276. /**
  277. * This resource's mediaType.
  278. *
  279. * @return This resource's mediaType.
  280. */
  281. public MediaType getMediaType() {
  282. return mediaType;
  283. }
  284. public void setMediaType(MediaType mediaType) {
  285. this.mediaType = mediaType;
  286. }
  287. public void setTitle(String title) {
  288. this.title = title;
  289. }
  290. public String getProperties() {
  291. return properties;
  292. }
  293. public void setProperties(String properties) {
  294. this.properties = properties;
  295. }
  296. @SuppressWarnings("NullableProblems")
  297. public String toString() {
  298. return StringUtil.toString("id", id,
  299. "title", title,
  300. "encoding", inputEncoding,
  301. "mediaType", mediaType,
  302. "href", href,
  303. "size", (data == null ? 0 : data.length));
  304. }
  305. }