Class Resources

  • All Implemented Interfaces:
    java.io.Serializable

    public class Resources
    extends java.lang.Object
    implements java.io.Serializable
    All the resources that make up the book. XHTML files, images and epub xml documents must be here.
    See Also:
    Serialized Form
    • Field Detail

      • lastId

        private int lastId
      • resources

        private java.util.Map<java.lang.String,​Resource> resources
    • Constructor Detail

      • Resources

        public 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