Package nl.siegmann.epublib.domain
Class Resources
- java.lang.Object
-
- nl.siegmann.epublib.domain.Resources
-
- All Implemented Interfaces:
java.io.Serializable
public class Resources extends java.lang.Object implements java.io.SerializableAll the resources that make up the book. XHTML files, images and epub xml documents must be here.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringIMAGE_PREFIXprivate static java.lang.StringITEM_PREFIXprivate intlastIdprivate java.util.Map<java.lang.String,Resource>resourcesprivate static longserialVersionUID
-
Constructor Summary
Constructors Constructor Description Resources()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Resourceadd(Resource resource)Adds a resource to the resources.voidaddAll(java.util.Collection<Resource> resources)Adds all resources from the given Collection of resources to the existing collection.booleancontainsByHref(java.lang.String href)Whether there exists a resource with the given hrefbooleancontainsId(java.lang.String id)Whether the map of resources already contains a resource with the given id.private java.lang.StringcreateHref(MediaType mediaType, int counter)private java.lang.StringcreateUniqueResourceId(Resource resource)Creates a new resource id that is guaranteed to be unique for this set of Resourcesstatic ResourcefindFirstResourceByMediaType(java.util.Collection<Resource> resources, MediaType mediaType)Gets the first resource (random order) with the give mediatype.ResourcefindFirstResourceByMediaType(MediaType mediaType)Gets the first resource (random order) with the give mediatype.private voidfixResourceHref(Resource resource)voidfixResourceId(Resource resource)Checks the id of the given resource and changes to a unique identifier if it isn't one already.java.util.Collection<Resource>getAll()java.util.Collection<java.lang.String>getAllHrefs()All resource hrefsResourcegetByHref(java.lang.String href)Gets the resource with the given href.ResourcegetById(java.lang.String id)Gets the resource with the given id.ResourcegetByIdOrHref(java.lang.String idOrHref)First tries to find a resource with as id the given idOrHref, if that fails it tries to find one with the idOrHref as href.private java.lang.StringgetResourceItemPrefix(Resource resource)java.util.Map<java.lang.String,Resource>getResourceMap()The resources that make up this book.java.util.List<Resource>getResourcesByMediaType(MediaType mediaType)All resources that have the given MediaType.java.util.List<Resource>getResourcesByMediaTypes(MediaType[] mediaTypes)All Resources that match any of the given list of MediaTypesbooleanisEmpty()private java.lang.StringmakeValidId(java.lang.String resourceId, Resource resource)Check if the id is a valid identifier.Resourceremove(java.lang.String href)Remove the resource with the given href.voidset(java.util.Collection<Resource> resources)Sets the collection of Resources to the given collection of resourcesvoidset(java.util.Map<java.lang.String,Resource> resources)Sets the collection of Resources to the given collection of resourcesintsize()The number of resources
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
IMAGE_PREFIX
private static final java.lang.String IMAGE_PREFIX
- See Also:
- Constant Field Values
-
ITEM_PREFIX
private static final java.lang.String ITEM_PREFIX
- See Also:
- Constant Field Values
-
lastId
private int lastId
-
resources
private java.util.Map<java.lang.String,Resource> resources
-
-
Method Detail
-
add
public Resource add(Resource resource)
Adds a resource to the resources. Fixes the resources id and href if necessary.- Parameters:
resource-- Returns:
- the newly added resource
-
fixResourceId
public void fixResourceId(Resource resource)
Checks the id of the given resource and changes to a unique identifier if it isn't one already.- Parameters:
resource-
-
makeValidId
private java.lang.String makeValidId(java.lang.String resourceId, Resource resource)Check if the id is a valid identifier. if not: prepend with valid identifier- Parameters:
resource-- Returns:
- a valid id
-
getResourceItemPrefix
private java.lang.String getResourceItemPrefix(Resource resource)
-
createUniqueResourceId
private java.lang.String createUniqueResourceId(Resource resource)
Creates a new resource id that is guaranteed to be unique for this set of Resources- Parameters:
resource-- Returns:
- a new resource id that is guaranteed to be unique for this set of Resources
-
containsId
public boolean containsId(java.lang.String id)
Whether the map of resources already contains a resource with the given id.- Parameters:
id-- Returns:
- Whether the map of resources already contains a resource with the given id.
-
getById
public Resource getById(java.lang.String id)
Gets the resource with the given id.- Parameters:
id-- Returns:
- null if not found
-
remove
public Resource remove(java.lang.String href)
Remove the resource with the given href.- Parameters:
href-- Returns:
- the removed resource, null if not found
-
fixResourceHref
private void fixResourceHref(Resource resource)
-
createHref
private java.lang.String createHref(MediaType mediaType, int counter)
-
isEmpty
public boolean isEmpty()
-
size
public int size()
The number of resources- Returns:
- The number of resources
-
getResourceMap
public java.util.Map<java.lang.String,Resource> getResourceMap()
The resources that make up this book. Resources can be xhtml pages, images, xml documents, etc.- Returns:
- The resources that make up this book.
-
getAll
public java.util.Collection<Resource> getAll()
-
containsByHref
public boolean containsByHref(java.lang.String href)
Whether there exists a resource with the given href- Parameters:
href-- Returns:
- Whether there exists a resource with the given href
-
set
public void set(java.util.Collection<Resource> resources)
Sets the collection of Resources to the given collection of resources- Parameters:
resources-
-
addAll
public void addAll(java.util.Collection<Resource> resources)
Adds all resources from the given Collection of resources to the existing collection.- Parameters:
resources-
-
set
public void set(java.util.Map<java.lang.String,Resource> resources)
Sets the collection of Resources to the given collection of resources- Parameters:
resources- A map with as keys the resources href and as values the Resources
-
getByIdOrHref
public Resource getByIdOrHref(java.lang.String idOrHref)
First tries to find a resource with as id the given idOrHref, if that fails it tries to find one with the idOrHref as href.- Parameters:
idOrHref-- Returns:
- the found Resource
-
getByHref
public Resource getByHref(java.lang.String href)
Gets the resource with the given href. If the given href contains a fragmentId then that fragment id will be ignored.- Parameters:
href-- Returns:
- null if not found.
-
findFirstResourceByMediaType
public Resource findFirstResourceByMediaType(MediaType mediaType)
Gets the first resource (random order) with the give mediatype. Useful for looking up the table of contents as it's supposed to be the only resource with NCX mediatype.- Parameters:
mediaType-- Returns:
- the first resource (random order) with the give mediatype.
-
findFirstResourceByMediaType
public static Resource findFirstResourceByMediaType(java.util.Collection<Resource> resources, MediaType mediaType)
Gets the first resource (random order) with the give mediatype. Useful for looking up the table of contents as it's supposed to be the only resource with NCX mediatype.- Parameters:
mediaType-- Returns:
- the first resource (random order) with the give mediatype.
-
getResourcesByMediaType
public java.util.List<Resource> getResourcesByMediaType(MediaType mediaType)
All resources that have the given MediaType.- Parameters:
mediaType-- Returns:
- All resources that have the given MediaType.
-
getResourcesByMediaTypes
public java.util.List<Resource> getResourcesByMediaTypes(MediaType[] mediaTypes)
All Resources that match any of the given list of MediaTypes- Parameters:
mediaTypes-- Returns:
- All Resources that match any of the given list of MediaTypes
-
getAllHrefs
public java.util.Collection<java.lang.String> getAllHrefs()
All resource hrefs- Returns:
- all resource hrefs
-
-