Class TableOfContents

  • All Implemented Interfaces:
    java.io.Serializable

    public class TableOfContents
    extends java.lang.Object
    implements java.io.Serializable
    The table of contents of the book. The TableOfContents is a tree structure at the root it is a list of TOCReferences, each if which may have as children another list of TOCReferences. The table of contents is used by epub as a quick index to chapters and sections within chapters. It may contain duplicate entries, may decide to point not to certain chapters, etc. See the spine for the complete list of sections in the order in which they should be read.
    See Also:
    Spine, Serialized Form
    • Constructor Detail

      • TableOfContents

        public TableOfContents()
      • TableOfContents

        public TableOfContents​(java.util.List<TOCReference> tocReferences)
    • Method Detail

      • getTocReferences

        public java.util.List<TOCReference> getTocReferences()
      • setTocReferences

        public void setTocReferences​(java.util.List<TOCReference> tocReferences)
      • addSection

        public TOCReference addSection​(Resource resource,
                                       java.lang.String path)
        Calls addTOCReferenceAtLocation after splitting the path using the DEFAULT_PATH_SEPARATOR.
        Returns:
        the new TOCReference
      • addSection

        public TOCReference addSection​(Resource resource,
                                       java.lang.String path,
                                       java.lang.String pathSeparator)
        Calls addTOCReferenceAtLocation after splitting the path using the given pathSeparator.
        Parameters:
        resource -
        path -
        pathSeparator -
        Returns:
        the new TOCReference
      • findTocReferenceByTitle

        private static TOCReference findTocReferenceByTitle​(java.lang.String title,
                                                            java.util.List<TOCReference> tocReferences)
        Finds the first TOCReference in the given list that has the same title as the given Title.
        Parameters:
        title -
        tocReferences -
        Returns:
        null if not found.
      • addSection

        public TOCReference addSection​(Resource resource,
                                       java.lang.String[] pathElements)
        Adds the given Resources to the TableOfContents at the location specified by the pathElements. Example: Calling this method with a Resource and new String[] {"chapter1", "paragraph1"} will result in the following:
        • a TOCReference with the title "chapter1" at the root level.
          If this TOCReference did not yet exist it will have been created and does not point to any resource
        • A TOCReference that has the title "paragraph1". This TOCReference will be the child of TOCReference "chapter1" and will point to the given Resource
        Parameters:
        resource -
        pathElements -
        Returns:
        the new TOCReference
      • addSection

        public TOCReference addSection​(Resource resource,
                                       int[] pathElements,
                                       java.lang.String sectionTitlePrefix,
                                       java.lang.String sectionNumberSeparator)
        Adds the given Resources to the TableOfContents at the location specified by the pathElements. Example: Calling this method with a Resource and new int[] {0, 0} will result in the following:
        • a TOCReference at the root level.
          If this TOCReference did not yet exist it will have been created with a title of "" and does not point to any resource
        • A TOCReference that points to the given resource and is a child of the previously created TOCReference.
          If this TOCReference didn't exist yet it will be created and have a title of ""
        Parameters:
        resource -
        pathElements -
        Returns:
        the new TOCReference
      • paddTOCReferences

        private void paddTOCReferences​(java.util.List<TOCReference> currentTocReferences,
                                       int[] pathElements,
                                       int pathPos,
                                       java.lang.String sectionPrefix,
                                       java.lang.String sectionNumberSeparator)
      • createSectionTitle

        private java.lang.String createSectionTitle​(int[] pathElements,
                                                    int pathPos,
                                                    int lastPos,
                                                    java.lang.String sectionPrefix,
                                                    java.lang.String sectionNumberSeparator)
      • getAllUniqueResources

        public java.util.List<Resource> getAllUniqueResources()
        All unique references (unique by href) in the order in which they are referenced to in the table of contents.
        Returns:
        All unique references (unique by href) in the order in which they are referenced to in the table of contents.
      • getAllUniqueResources

        private static void getAllUniqueResources​(java.util.Set<java.lang.String> uniqueHrefs,
                                                  java.util.List<Resource> result,
                                                  java.util.List<TOCReference> tocReferences)
      • size

        public int size()
        The total number of references in this table of contents.
        Returns:
        The total number of references in this table of contents.
      • getTotalSize

        private static int getTotalSize​(java.util.Collection<TOCReference> tocReferences)
      • calculateDepth

        public int calculateDepth()
        The maximum depth of the reference tree
        Returns:
        The maximum depth of the reference tree
      • calculateDepth

        private int calculateDepth​(java.util.List<TOCReference> tocReferences,
                                   int currentDepth)