private void outputStyle(final boolean applyStyle, final StringBuilder output) {
        // Output the start style element if the style has changed.
        if (applyStyle) {
            final Style style = getStyle(c, elementType);

            // If the style has changed we need to output the new style.
            if (style != lastStyle) {
                // We need to make sure that elements don't overlap so we need
                // to close the highlight element if it is open.
                if (inHighlight) {
                    if (!changedHighlight) {
                        // If we haven't only just switched into a highlight
                        // section then we must already be in one. In this case
                        // we need to close the previous highlight section.
                        write(Style.HIGHLIGHT.getEnd());
                    }
                } else if (changedHighlight) {
                    // We are moving out of the highlight state so output an end
                    // span.
                    write(Style.HIGHLIGHT.getEnd());
                }

                if (lastStyle != null) {
                    // Close the previous style.
                    write(lastStyle.getEnd());
                }
                // Output the appropriate style.
                write(style.getStart());

                if (inHighlight) {
                    // Output a highlight if we are in a highlight section.
                    write(Style.HIGHLIGHT.getStart());
                }
            } else {
                // This section will only be executed for the first char as all
                // subsequent calls to this method will have set the lastStyle.
                if (lastStyle == null) {
                    // Output the appropriate style.
                    write(style.getStart());
                }

                // Output the highlight style if the output is to be highlighted
                // from the start.
                outputHighlight(output);
            }

            // Record the style that was applied.
            lastStyle = style;

        } else {
            // Output a highlight element start/end if we are moving in/out of a
            // highlight section.
            outputHighlight(output);
        }

        // Set the flag to indicate highlighting hasn't just been turned on/off
        changedHighlight = false;
    }
Outputs style span elements based on the current element type and whether
we are currently in a highlighted section or not.

@param applyStyle Determines if element styles are to be output. This does not
                  affect the output of highlight styles as they are applied to
                  styled and non styled text.
@param output     The output <code>StringBuilder</code> to append the style span
                  elements to.
stroom-core-client-widget/src/main/java/stroom/editor/client/model/XMLStyler.java