<commit_before>/**
 * Copyright (C) 2009 Orbeon, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation; either version
 * 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.xforms.processor.handlers;

import org.apache.commons.lang.StringUtils;
import org.orbeon.oxf.xml.ContentHandlerHelper;
import org.orbeon.oxf.xml.ForwardingContentHandler;
import org.orbeon.oxf.xml.XMLConstants;
import org.orbeon.oxf.xml.XMLUtils;
import org.orbeon.saxon.om.FastStringBuffer;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

/**
 * Intercept SAX output and annotate resulting elements and/or text with classes and spans.
 */
public class OutputInterceptor extends ForwardingContentHandler {

    private Listener beginDelimiterListener;

    private String spanQName;

    private String delimiterNamespaceURI;
    private String delimiterPrefix;
    private String delimiterLocalName;

    private FastStringBuffer addedClasses;

    private boolean mustGenerateFirstDelimiters = true ;

    private int level;
    private boolean isCharacters;
    private StringBuffer currentCharacters = new StringBuffer();

    protected AttributesImpl reusableAttributes = new AttributesImpl();
    public OutputInterceptor(ContentHandler output, String spanQName, Listener beginDelimiterListener) {
        super(output);
        this.spanQName = spanQName;
        this.beginDelimiterListener = beginDelimiterListener;
    }

    public void startElement(String uri, String localname, String qName, Attributes attributes) throws SAXException {

        level++;
        final boolean topLevel = level == 1;

        flushCharacters(false, topLevel);

        // The first element received determines the type of separator
        checkDelimiters(uri, qName, topLevel);

        // Add or update classes on element if needed
        super.startElement(uri, localname, qName, topLevel ? getAttributesWithClass(attributes) : attributes);
    }

    public void endElement(String uri, String localname, String qName) throws SAXException {

        flushCharacters(false, false);
        super.endElement(uri, localname, qName);

        level--;
    }

    public void characters(char[] chars, int start, int length) {
        currentCharacters.append(chars, start, length);
        isCharacters = true;
    }

    public void flushCharacters(boolean finalFlush, boolean topLevel) throws SAXException {

        if (currentCharacters.length() > 0) {
`
            final String currentString = currentCharacters.toString();
            final char[] chars = currentString.toCharArray();
            if (StringUtils.isBlank(currentString) || !topLevel) {
                // Just output whitespace as is
                super.characters(chars, 0, chars.length);
            } else {

                // The first element received determines the type of separator
                checkDelimiters(XMLConstants.XHTML_NAMESPACE_URI, spanQName, topLevel);

                // Wrap any other text within an xhtml:span<commit_msg>Typo.<commit_after>/**
 * Copyright (C) 2009 Orbeon, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation; either version
 * 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.xforms.processor.handlers;

import org.apache.commons.lang.StringUtils;
import org.orbeon.oxf.xml.ContentHandlerHelper;
import org.orbeon.oxf.xml.ForwardingContentHandler;
import org.orbeon.oxf.xml.XMLConstants;
import org.orbeon.oxf.xml.XMLUtils;
import org.orbeon.saxon.om.FastStringBuffer;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

/**
 * Intercept SAX output and annotate resulting elements and/or text with classes and spans.
 */
public class OutputInterceptor extends ForwardingContentHandler {

    private Listener beginDelimiterListener;

    private String spanQName;

    private String delimiterNamespaceURI;
    private String delimiterPrefix;
    private String delimiterLocalName;

    private FastStringBuffer addedClasses;

    private boolean mustGenerateFirstDelimiters = true ;

    private int level;
    private boolean isCharacters;
    private StringBuffer currentCharacters = new StringBuffer();

    protected AttributesImpl reusableAttributes = new AttributesImpl();
    public OutputInterceptor(ContentHandler output, String spanQName, Listener beginDelimiterListener) {
        super(output);
        this.spanQName = spanQName;
        this.beginDelimiterListener = beginDelimiterListener;
    }

    public void startElement(String uri, String localname, String qName, Attributes attributes) throws SAXException {

        level++;
        final boolean topLevel = level == 1;

        flushCharacters(false, topLevel);

        // The first element received determines the type of separator
        checkDelimiters(uri, qName, topLevel);

        // Add or update classes on element if needed
        super.startElement(uri, localname, qName, topLevel ? getAttributesWithClass(attributes) : attributes);
    }

    public void endElement(String uri, String localname, String qName) throws SAXException {

        flushCharacters(false, false);
        super.endElement(uri, localname, qName);

        level--;
    }

    public void characters(char[] chars, int start, int length) {
        currentCharacters.append(chars, start, length);
        isCharacters = true;
    }

    public void flushCharacters(boolean finalFlush, boolean topLevel) throws SAXException {

        if (currentCharacters.length() > 0) {

            final String currentString = currentCharacters.toString();
            final char[] chars = currentString.toCharArray();
            if (StringUtils.isBlank(currentString) || !topLevel) {
                // Just output whitespace as is
                super.characters(chars, 0, chars.length);
            } else {

                // The first element received determines the type of separator
                checkDelimiters(XMLConstants.XHTML_NAMESPACE_URI, spanQName, topLevel);

                // Wrap any other text within an xhtml:span
{'fraction_non_alphanumeric': 0.0739402362751911, 'fraction_numerical': 0.002501737317581654, 'mean_word_length': 3.393162393162393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3958252', 'n_tokens_mistral': 1927, 'n_tokens_neox': 1849, 'n_words': 623}