public static void ensureFormattingState(XHTMLContentHandler xhtml, EnumSet<Tag> desired,
                                             Deque<Tag> currentState) throws SAXException {
        EnumSet<FormattingUtils.Tag> undesired = EnumSet.complementOf(desired);

        while (!currentState.isEmpty() && currentState.stream().anyMatch(undesired::contains)) {
            xhtml.endElement(currentState.pop().tagName());
        }

        desired.removeAll(currentState);
        for (FormattingUtils.Tag tag : desired) {
            currentState.push(tag);
            xhtml.startElement(tag.tagName());
        }
    }
Closes all tags until {@code currentState} contains only tags from {@code desired} set,
then open all required tags to reach desired state.

@param xhtml        handler
@param desired      desired formatting state
@param currentState current formatting state (stack of open formatting tags)
@throws SAXException pass underlying handler exception
tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/main/java/org/apache/tika/parser/microsoft/FormattingUtils.java