Class IndexingSink

  • All Implemented Interfaces:
    java.lang.AutoCloseable, Markup, Sink
    Direct Known Subclasses:
    CreateAnchorsForIndexEntries

    public class IndexingSink
    extends SinkWrapper
    A sink wrapper for populating an index tree for particular elements in a document. Currently this only generates IndexEntry objects for sections.
    • Field Detail

      • stack

        private final java.util.Stack<IndexEntry> stack
        The stack.
      • usedIds

        private final java.util.Map<java.lang.String,​java.util.concurrent.atomic.AtomicInteger> usedIds
        A map containing all used ids of index entries as key and how often they are used as value (0-based, i.e. 0 means used 1 time). AtomicInteger is only used here as it implements a mutable integer (not for its atomicity).
      • isComplete

        private boolean isComplete
        Is true once the sink has been closed.
      • isTitle

        private boolean isTitle
      • hasOpenEntry

        private boolean hasOpenEntry
        Is true if the sink is currently populating entry data (i.e. metadata about the current entry is not completely captured yet)
    • Constructor Detail

      • IndexingSink

        public IndexingSink​(Sink delegate)
      • IndexingSink

        private IndexingSink​(IndexEntry rootEntry,
                             Sink delegate)
        Default constructor.
    • Method Detail

      • getRootEntry

        public IndexEntry getRootEntry()
        This should only be called once the sink is closed. Before that the tree might not be complete.
        Returns:
        the tree of entries starting from the root
        Throws:
        java.lang.IllegalStateException - in case the sink was not closed yet
      • title_

        public void title_()
        Description copied from interface: Sink
        Ends the title element.
        Specified by:
        title_ in interface Sink
        Overrides:
        title_ in class SinkWrapper
      • section

        public void section​(int level,
                            SinkEventAttributes attributes)
        Description copied from interface: Sink
        Start a new section at the given level.

        Sections with higher level have to be entirely contained within sections of lower level.

        Supported attributes are the base attributes.

        Specified by:
        section in interface Sink
        Overrides:
        section in class SinkWrapper
        Parameters:
        level - the section level (must be a value between 1 and 6).
        attributes - A set of SinkEventAttributes, may be null.
      • section_

        public void section_​(int level)
        Description copied from interface: Sink
        Ends a section at the given level.
        Specified by:
        section_ in interface Sink
        Overrides:
        section_ in class SinkWrapper
        Parameters:
        level - the section level (must be a value between 1 and 6).
      • sectionTitle_

        public void sectionTitle_​(int level)
        Description copied from interface: Sink
        Ends a section title at the given level.
        Specified by:
        sectionTitle_ in interface Sink
        Overrides:
        sectionTitle_ in class SinkWrapper
        Parameters:
        level - the section title level (must be a value between 1 and 6).
      • text

        public void text​(java.lang.String text,
                         SinkEventAttributes attributes)
        Description copied from interface: Sink
        Adds a text.

        The text parameter should contain only real content, ie any ignorable/collapsable whitespace/EOLs or other pretty-printing should be removed/normalized by a parser.

        If text contains any variants of line terminators, they should be normalized to the System EOL by an implementing Sink.

        Supported attributes are the base attributes plus

        SEMANTICS (values "emphasis", "strong", "small", "line-through", "citation", "quote", "definition", "abbreviation", "italic", "bold", "monospaced", "variable", "sample", "keyboard", "superscript", "subscript", "annotation", "highlight", "ruby", "rubyBase", "rubyText", "rubyTextContainer", "rubyParentheses", "bidirectionalIsolation", "bidirectionalOverride", "phrase", "insert", "delete").

        The following attributes are deprecated:

        VALIGN (values "sub", "sup"), DECORATION (values "underline", "overline", "line-through"), STYLE (values "italic", "bold", "monospaced").
        Specified by:
        text in interface Sink
        Overrides:
        text in class SinkWrapper
        Parameters:
        text - The text to write.
        attributes - A set of SinkEventAttributes, may be null.
      • anchor

        public void anchor​(java.lang.String name,
                           SinkEventAttributes attributes)
        Description copied from interface: Sink
        Starts an element which defines an anchor.

        The name parameter has to be a valid SGML NAME token. According to the HTML 4.01 specification section 6.2 SGML basic types:

        ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

        Supported attributes are the base attributes. If NAME is specified in the SinkEventAttributes, it will be overwritten by the name parameter.

        Specified by:
        anchor in interface Sink
        Overrides:
        anchor in class SinkWrapper
        Parameters:
        name - the name of the anchor. This has to be a valid SGML NAME token.
        attributes - A set of SinkEventAttributes, may be null.
      • parseAnchor

        private boolean parseAnchor​(java.lang.String name)
      • setEntryId

        private void setEntryId​(IndexEntry entry,
                                java.lang.String id)
      • getUniqueId

        java.lang.String getUniqueId​(java.lang.String id)
        Converts the given id into a unique one by potentially suffixing it with an index value.
        Parameters:
        id -
        Returns:
        the unique id
      • indexEntryComplete

        void indexEntryComplete()
      • onIndexEntry

        protected void onIndexEntry​(IndexEntry entry)
        Called at the beginning of each entry (once all metadata about it is collected). The events for the metadata are buffered and only flushed after this method was called.
        Parameters:
        entry - the newly collected entry
      • pushNewEntry

        private void pushNewEntry​(IndexEntry.Type type)
        Creates and pushes a new IndexEntry onto the top of this stack.
      • push

        public void push​(IndexEntry entry)
        Pushes an IndexEntry onto the top of this stack.
        Parameters:
        entry - to put.
      • pop

        public void pop()
        Removes the IndexEntry at the top of this stack.
      • peek

        public IndexEntry peek()

        peek.

        Returns:
        Looks at the IndexEntry at the top of this stack.
      • close

        public void close()
        Description copied from interface: Sink
        Close the writer or the stream, if needed. Closing a previously-closed Sink has no effect.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface Sink
        Overrides:
        close in class SinkWrapper