<commit_before>/**
 * Copyright (C) 2010 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.xhtml;

import org.orbeon.oxf.pipeline.api.XMLReceiver;
import org.orbeon.oxf.xforms.XFormsConstants;
import org.orbeon.oxf.xforms.control.XFormsControl;
import org.orbeon.oxf.xforms.control.controls.XFormsOutputControl;
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.SAXException;
import org.xml.sax.helpers.AttributesImpl;

/**
 * Handler for xf:output[@appearance = 'xxf:download'].
 */
public class XFormsOutputDownloadHandler extends XFormsOutputHandler {

    @Override
    protected void handleLabel() {
        // NOP because the label is output as the text within <a>
    }

    @Override
    protected void handleControlStart(String uri, String localname, String qName, Attributes attributes, String effectiveId, XFormsControl control) throws SAXException {

        final XFormsOutputControl outputControl = (XFormsOutputControl) control;
        final XMLReceiver xmlReceiver = handlerContext.getController().getOutput();
        final String xhtmlPrefix = handlerContext.findXHTMLPrefix();

        final AttributesImpl containerAttributes = getContainerAttributes(uri, localname, attributes, effectiveId, outputControl);

        {
            final AttributesImpl aAttributes = getAnchorAttributes(outputControl, containerAttributes);

            // Handle accessibility attributes on <a>
            handleAccessibilityAttributes(attributes, aAttributes);

            final String aQName = XMLUtils.buildQName(xhtmlPrefix, "a");
            xmlReceiver.startElement(XMLConstants.XHTML_NAMESPACE_URI, "a", aQName, aAttributes);
            {
                final String labelValue = (control != null) ? control.getLabel() : null;
                final boolean mustOutputHTMLFragment = control != null && control.isHTMLLabel();
                outputLabelText(xmlReceiver, control, labelValue, xhtmlPrefix, mustOutputHTMLFragment);
            }
            xmlReceiver.endElement(XMLConstants.XHTML_NAMESPACE_URI, "a", aQName);
        }
    }

    private AttributesImpl getAnchorAttributes(XFormsOutputControl outputControl, AttributesImpl containerAttributes) {
        final String hrefValue = XFormsOutputControl.getExternalValueOrDefault(outputControl, null);

        if (hrefValue == null || hrefValue.trim().equals("")) {
            // No URL so make sure a click doesn't cause navigation, and add class
            containerAttributes.addAttribute("", "href", "href", ContentHandlerHelper.CDATA, "#");
            XMLUtils.addOrAppendToAttribute(containerAttributes, "class", "xforms-readonly");
        } else {
            // URL value
            containerAttributes.addAttribute("", "href", "href", ContentHandlerHelper.CDATA, hrefValue);
        }

        // Add _blank target in order to prevent:
        // 1. The browser replacing the current page, and
        // 2. The browser displaying the "Are you sure you want to navigate away from this page?" warning dialog
        // This, as of 2009-05, seems to be how most sites handle this
        containerAttributes.addAttribute("", "target", "target", ContentHandlerHelper.CDATA, "_blank");

        // Output xxf:* extension attributes
        if (outputControl != null)
            outputControl.addExtensionAttributes(containerAttributes, XFormsConstants.XXFORMS_NAMESPACE_URI);

        return containerAttributes;
    }
}<commit_msg>Fix #559 "Error downloading attachment with proxy portlet"<commit_after>/**
 * Copyright (C) 2010 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.xhtml;

import org.orbeon.oxf.pipeline.api.XMLReceiver;
import org.orbeon.oxf.xforms.XFormsConstants;
import org.orbeon.oxf.xforms.control.XFormsControl;
import org.orbeon.oxf.xforms.control.controls.XFormsOutputControl;
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.SAXException;
import org.xml.sax.helpers.AttributesImpl;

/**
 * Handler for xf:output[@appearance = 'xxf:download'].
 */
public class XFormsOutputDownloadHandler extends XFormsOutputHandler {

    @Override
    protected void handleLabel() {
        // NOP because the label is output as the text within <a>
    }

    @Override
    protected void handleControlStart(String uri, String localname, String qName, Attributes attributes, String effectiveId, XFormsControl control) throws SAXException {

        final XFormsOutputControl outputControl = (XFormsOutputControl) control;
        final XMLReceiver xmlReceiver = handlerContext.getController().getOutput();
        final String xhtmlPrefix = handlerContext.findXHTMLPrefix();

        final AttributesImpl containerAttributes = getContainerAttributes(uri, localname, attributes, effectiveId, outputControl);

        {
            // For f:url-type="resource"
            final String formattingPrefix = handlerContext.findFormattingPrefixDeclare();

            final AttributesImpl aAttributes = getAnchorAttributes(outputControl, containerAttributes, formattingPrefix);

            // Handle accessibility attributes on <a>
            handleAccessibilityAttributes(attributes, aAttributes);

            final String aQName = XMLUtils.buildQName(xhtmlPrefix, "a");
            xmlReceiver.startElement(XMLConstants.XHTML_NAMESPACE_URI, "a", aQName, aAttributes);
            {
                final String labelValue = (control != null) ? control.getLabel() : null;
                final boolean mustOutputHTMLFragment = control != null && control.isHTMLLabel();
                outputLabelText(xmlReceiver, control, labelValue, xhtmlPrefix, mustOutputHTMLFragment);
            }
            xmlReceiver.endElement(XMLConstants.XHTML_NAMESPACE_URI, "a", aQName);

            handlerContext.findFormattingPrefixUndeclare(formattingPrefix);
        }
    }

    private AttributesImpl getAnchorAttributes(XFormsOutputControl outputControl, AttributesImpl containerAttributes, String formattingPrefix) {
        final String hrefValue = XFormsOutputControl.getExternalValueOrDefault(outputControl, null);

        if (hrefValue == null || hrefValue.trim().equals("")) {
            // No URL so make sure a click doesn't cause navigation, and add class
            containerAttributes.addAttribute("", "href", "href", ContentHandlerHelper.CDATA, "#");
            XMLUtils.addOrAppendToAttribute(containerAttributes, "class", "xforms-readonly");
        } else {
            // URL value
            containerAttributes.addAttribute("", "href", "href", ContentHandlerHelper.CDATA, hrefValue);
        }

        // Specify resource URL type for proxy portlet
        containerAttributes.addAttribute(
            XMLConstants.FORMATTING_URL_TYPE_QNAME.getNamespaceURI(),
            XMLConstants.FORMATTING_URL_TYPE_QNAME.getName(),
            XMLUtils.buildQName(formattingPrefix, XMLConstants.FORMATTING_URL_TYPE_QNAME.getName()),
            ContentHandlerHelper.CDATA, "resource");

        // Add _blank target in order to prevent:
        // 1. The browser replacing the current page, and
        // 2. The browser displaying the "Are you sure you want to navigate away from this page?" warning dialog
        // This, as of 2009-05, seems to be how most sites handle this
        containerAttributes.addAttribute("", "target", "target", ContentHandlerHelper.CDATA, "_blank");

        // Output xxf:* extension attributes
        if (outputControl != null)
            outputControl.addExtensionAttributes(containerAttributes, XFormsConstants.XXFORMS_NAMESPACE_URI);

        return containerAttributes;
    }
}
{'fraction_non_alphanumeric': 0.08613608613608614, 'fraction_numerical': 0.003441003441003441, 'mean_word_length': 3.505, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '987297', 'n_tokens_mistral': 2337, 'n_tokens_neox': 2261, 'n_words': 685}