<commit_before>/**
 *  Copyright (C) 2005 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.orbeon.oxf.xforms.XFormsConstants;
import org.orbeon.oxf.xforms.XFormsControls;
import org.orbeon.oxf.xml.ContentHandlerHelper;
import org.orbeon.oxf.xml.XMLConstants;
import org.orbeon.oxf.xml.XMLUtils;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

/**
 * Handle xforms:output.
 */
public class XFormsOutputHandler extends XFormsValueControlHandler {

    private Attributes elementAttributes;

    public XFormsOutputHandler() {
        super(false);
    }

    public void start(String uri, String localname, String qName, Attributes attributes) throws SAXException {
        elementAttributes = new AttributesImpl(attributes);
        super.start(uri, localname, qName, attributes);
    }

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

        final ContentHandler contentHandler = handlerContext.getController().getOutput();
        final String effectiveId = handlerContext.getEffectiveId(elementAttributes);
        final XFormsControls.OutputControlInfo controlInfo = handlerContext.isGenerateTemplate()
                ? null : (XFormsControls.OutputControlInfo) containingDocument.getObjectById(pipelineContext, effectiveId);

        // xforms:label
        handleLabelHintHelpAlert(effectiveId, "label", controlInfo);

        final AttributesImpl newAttributes;
        final boolean isImage;
        final boolean isDateOrTime;
        final StringBuffer classes = new StringBuffer("xforms-control xforms-output");
        if (!handlerContext.isGenerateTemplate()) {

            final String appearanceValue = elementAttributes.getValue("appearance");
            final String appearanceLocalname = (appearanceValue == null) ? null : XMLUtils.localNameFromQName(appearanceValue);
            final String appearanceURI = (appearanceValue == null) ? null : uriFromQName(appearanceValue);

            final String mediaType = controlInfo.getMediaTypeAttribute();

            final boolean isHTML = appearanceValue != null && XFormsConstants.XXFORMS_NAMESPACE_URI.equals(appearanceURI) && "html".equals(appearanceLocalname);
            isImage = mediaType != null && mediaType.startsWith("image/");

            // Find classes to add

            if (isHTML) {
                classes.append(" xforms-output-html");
            } else if (isImage) {
                classes.append(" xforms-output-image");
            }
            isDateOrTime = isDateOrTime(controlInfo.getType());
            if (isDateOrTime) {
                classes.append(" xforms-date");
            }
            handleReadOnlyClass(classes, controlInfo);
            handleRelevantClass(classes, controlInfo);

            newAttributes = getAttributes(elementAttributes, classes.toString(), effectiveId);
            handleReadOnlyAttribute(newAttributes, controlInfo);
        } else {
            isImage = false;
            isDateOrTime = false;

            // Find classes to add
            newAttributes = getAttributes(elementAttributes, classes.toString(), effectiveId);
        }

        // Create xhtml:span
        final String xhtmlPrefix = handlerContext.findXHTMLPrefix();
        final String spanQName = XMLUtils.buildQName(xhtmlPrefix, "span");

        contentHandler.startElement(XMLConstants.XHTML_NAMESPACE_URI, "span", spanQName, newAttributes);
        if (!handlerContext.isGenerateTemplate()) {
            if (isImage) {
                // Case of image media type with URI
                final String imgQName = XMLUtils.buildQName(xhtmlPrefix, "img");
                final AttributesImpl imgAttributes = new AttributesImpl();
                // @src="..."
                imgAttributes.addAttribute("", "src", "src", ContentHandlerHelper.CDATA, controlInfo.getValue());<commit_msg>Fixed class for HTML output.<commit_after>/**
 *  Copyright (C) 2005 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.orbeon.oxf.xforms.XFormsConstants;
import org.orbeon.oxf.xforms.XFormsControls;
import org.orbeon.oxf.xml.ContentHandlerHelper;
import org.orbeon.oxf.xml.XMLConstants;
import org.orbeon.oxf.xml.XMLUtils;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

/**
 * Handle xforms:output.
 */
public class XFormsOutputHandler extends XFormsValueControlHandler {

    private Attributes elementAttributes;

    public XFormsOutputHandler() {
        super(false);
    }

    public void start(String uri, String localname, String qName, Attributes attributes) throws SAXException {
        elementAttributes = new AttributesImpl(attributes);
        super.start(uri, localname, qName, attributes);
    }

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

        final ContentHandler contentHandler = handlerContext.getController().getOutput();
        final String effectiveId = handlerContext.getEffectiveId(elementAttributes);
        final XFormsControls.OutputControlInfo controlInfo = handlerContext.isGenerateTemplate()
                ? null : (XFormsControls.OutputControlInfo) containingDocument.getObjectById(pipelineContext, effectiveId);

        // xforms:label
        handleLabelHintHelpAlert(effectiveId, "label", controlInfo);

        final AttributesImpl newAttributes;
        final boolean isDateOrTime;
        final StringBuffer classes = new StringBuffer("xforms-control xforms-output");

        final String appearanceValue = elementAttributes.getValue("appearance");
        final String appearanceLocalname = (appearanceValue == null) ? null : XMLUtils.localNameFromQName(appearanceValue);
        final String appearanceURI = (appearanceValue == null) ? null : uriFromQName(appearanceValue);

        final String mediatypeValue = elementAttributes.getValue("mediatype");
        final boolean isImage = mediatypeValue != null && mediatypeValue.startsWith("image/");
        final boolean isHTML = ( mediatypeValue != null && mediatypeValue.equals("text/html"))
                || (appearanceValue != null && XFormsConstants.XXFORMS_NAMESPACE_URI.equals(appearanceURI) && "html".equals(appearanceLocalname));

        if (isHTML) {
            classes.append(" xforms-output-html");
        } else if (isImage) {
            classes.append(" xforms-output-image");
        }

        if (!handlerContext.isGenerateTemplate()) {

            // Find classes to add
            isDateOrTime = isDateOrTime(controlInfo.getType());
            if (isDateOrTime) {
                classes.append(" xforms-date");
            }
            handleReadOnlyClass(classes, controlInfo);
            handleRelevantClass(classes, controlInfo);

            newAttributes = getAttributes(elementAttributes, classes.toString(), effectiveId);
            handleReadOnlyAttribute(newAttributes, controlInfo);
        } else {
            isDateOrTime = false;

            // Find classes to add
            newAttributes = getAttributes(elementAttributes, classes.toString(), effectiveId);
        }

        // Create xhtml:span
        final String xhtmlPrefix = handlerContext.findXHTMLPrefix();
        final String spanQName = XMLUtils.buildQName(xhtmlPrefix, "span");

        contentHandler.startElement(XMLConstants.XHTML_NAMESPACE_URI, "span", spanQName, newAttributes);
        if (!handlerContext.isGenerateTemplate()) {
            if (isImage) {
                // Case of image media type with URI
                final String imgQName = XMLUtils.buildQName(xhtmlPrefix, "img");
                final AttributesImpl imgAttributes = new AttributesImpl();
                // @src="..."
                imgAttributes.addAttribute("", "src", "src", ContentHandlerHelper.CDATA, controlInfo.getValue());
{'fraction_non_alphanumeric': 0.09078045063676934, 'fraction_numerical': 0.001306193534342005, 'mean_word_length': 3.148081264108352, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 14}, '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': '2655766', 'n_tokens_mistral': 2366, 'n_tokens_neox': 2314, 'n_words': 629}