<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.pipeline.api.ExternalContext;
import org.orbeon.oxf.pipeline.api.PipelineContext;
import org.orbeon.oxf.xforms.XFormsConstants;
import org.orbeon.oxf.xforms.XFormsContainingDocument;
import org.orbeon.oxf.xforms.XFormsControls;
import org.orbeon.oxf.xforms.control.XFormsControl;
import org.orbeon.oxf.xforms.control.XFormsValueControl;
import org.orbeon.oxf.xml.ContentHandlerHelper;
import org.orbeon.oxf.xml.ElementHandlerNew;
import org.orbeon.oxf.xml.XMLConstants;
import org.orbeon.oxf.xml.XMLUtils;
import org.orbeon.oxf.common.OXFException;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.dom4j.QName;
import org.dom4j.Namespace;

/**
 *
 */
public abstract class HandlerBase extends ElementHandlerNew {

    private int level = 0;

    private Attributes labelAttributes;
    private Attributes helpAttributes;
    private Attributes hintAttributes;
    private Attributes alertAttributes;

    // NOTE: the XForms schema seems to indicates that "style", "onchange", and others
    // cannot be used; those should probably be in the XHTML namespace
    private static final String[] XHTML_ATTRIBUTES_TO_COPY = {"style", "onchange"};

    private boolean repeating;
    private boolean forwarding;

    protected HandlerContext handlerContext;
    protected PipelineContext pipelineContext;
    protected XFormsContainingDocument containingDocument;
    protected ExternalContext externalContext;

    protected AttributesImpl reusableAttributes = new AttributesImpl();

    protected HandlerBase(boolean repeating, boolean forwarding) {
        this.repeating = repeating;
        this.forwarding = forwarding;
    }

    public void setContext(Object context) {
        this.handlerContext = (HandlerContext) context;

        this.pipelineContext = handlerContext.getPipelineContext();
        this.containingDocument = handlerContext.getContainingDocument();
        this.externalContext = handlerContext.getExternalContext();

        super.setContext(context);
    }

    public boolean isRepeating() {
        return repeating;
    }

    public boolean isForwarding() {
        return forwarding;
    }

    public void start(String uri, String localname, String qName, Attributes attributes) throws SAXException {
        // Reset state, as this handler may be reused
        level = 0;
        labelAttributes = null;
        helpAttributes = null;
        hintAttributes = null;
        alertAttributes = null;
    }

    public void startElement(String uri, String localname, String qName, Attributes attributes) throws SAXException {
        level++;
        if (level == 1 && XFormsConstants.XFORMS_NAMESPACE_URI.equals(uri)) {
            // Handle direct children only
            if ("label".equals(localname)) {
                labelAttributes = new AttributesImpl(attributes);
            } else if ("hint".equals(localname)) {
                hintAttributes = new AttributesImpl(attributes);
            } else if ("help".equals(localname)) {
                helpAttributes = new AttributesImpl(attributes);
            } else if ("alert".equals(localname)) {
                alertAttributes = new AttributesImpl(attributes);
            }
        }
        super.startElement(uri, localname, qName, attributes);
    }

    public void endElement(String uri, String localname, String qName) throws SAXException {
        super.endElement(uri, localname, qName);
        level--;
    }

    public static void handleReadOnlyAttribute(AttributesImpl newAttributes, XFormsControl XFormsControl) {
        if (XFormsControl != null && XFormsControl.isReadonly()) {
            // @disabled="disabled"
            newAttributes.addAttribute("", "disabled", "disabled", ContentHandlerHelper.CDATA, "disabled");
        }
    }

    public static void handleMIPClasses(StringBuffer sb, XFormsControl xformsControl) {
        if (xformsControl != null) {// TEMP, controlInfo should not be null
            if (!xformsControl.isRelevant()) {
                if (sb.length() > 0)
                    sb.append(' ');
                sb.append("xforms-disabled");
            }
            if (!xformsControl.isValid()) {
                if (sb.length() > 0)
                    sb.append(' ');
                sb.append("xforms-invalid");
            }
            if (xformsControl != null && xformsControl.isReadonly()) {
                if (sb.length() > 0)
                    sb.append(' ');
                sb.append("xforms-readonly");
            }
            if (xformsControl != null && xformsControl instanceof XFormsValueControl && xformsControl.isRequired()) {
                if (sb.length() > 0)
                    sb.append(' ');
                sb.append("xforms-required");
                if ("".equals(((XFormsValueControl) xformsControl).getValue()))
                    sb.append(" xforms-required-empty");
                else
                    sb.append(" xforms-required-filled");
            }
        }
    }

    public static boolean isDateOrTime(String type) {
        return isDate(type)
                || "{http://www.w3.org/2001/XMLSchema}dateTime".equals(type)
                || "{http://www.w3.org/2001/XMLSchema}time".equals(type);
    }

    public static boolean isDate(String type) {
        return "{http://www.w3.org/2001/XMLSchema}date".equals(type);
    }

    protected void handleAccessibilityAttributes(Attributes srcAttributes, AttributesImpl destAttributes) {<commit_msg>Add xforms-required class on groups.<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.pipeline.api.ExternalContext;
import org.orbeon.oxf.pipeline.api.PipelineContext;
import org.orbeon.oxf.xforms.XFormsConstants;
import org.orbeon.oxf.xforms.XFormsContainingDocument;
import org.orbeon.oxf.xforms.XFormsControls;
import org.orbeon.oxf.xforms.control.XFormsControl;
import org.orbeon.oxf.xforms.control.XFormsValueControl;
import org.orbeon.oxf.xml.ContentHandlerHelper;
import org.orbeon.oxf.xml.ElementHandlerNew;
import org.orbeon.oxf.xml.XMLConstants;
import org.orbeon.oxf.xml.XMLUtils;
import org.orbeon.oxf.common.OXFException;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.dom4j.QName;
import org.dom4j.Namespace;

/**
 *
 */
public abstract class HandlerBase extends ElementHandlerNew {

    private int level = 0;

    private Attributes labelAttributes;
    private Attributes helpAttributes;
    private Attributes hintAttributes;
    private Attributes alertAttributes;

    // NOTE: the XForms schema seems to indicates that "style", "onchange", and others
    // cannot be used; those should probably be in the XHTML namespace
    private static final String[] XHTML_ATTRIBUTES_TO_COPY = {"style", "onchange"};

    private boolean repeating;
    private boolean forwarding;

    protected HandlerContext handlerContext;
    protected PipelineContext pipelineContext;
    protected XFormsContainingDocument containingDocument;
    protected ExternalContext externalContext;

    protected AttributesImpl reusableAttributes = new AttributesImpl();

    protected HandlerBase(boolean repeating, boolean forwarding) {
        this.repeating = repeating;
        this.forwarding = forwarding;
    }

    public void setContext(Object context) {
        this.handlerContext = (HandlerContext) context;

        this.pipelineContext = handlerContext.getPipelineContext();
        this.containingDocument = handlerContext.getContainingDocument();
        this.externalContext = handlerContext.getExternalContext();

        super.setContext(context);
    }

    public boolean isRepeating() {
        return repeating;
    }

    public boolean isForwarding() {
        return forwarding;
    }

    public void start(String uri, String localname, String qName, Attributes attributes) throws SAXException {
        // Reset state, as this handler may be reused
        level = 0;
        labelAttributes = null;
        helpAttributes = null;
        hintAttributes = null;
        alertAttributes = null;
    }

    public void startElement(String uri, String localname, String qName, Attributes attributes) throws SAXException {
        level++;
        if (level == 1 && XFormsConstants.XFORMS_NAMESPACE_URI.equals(uri)) {
            // Handle direct children only
            if ("label".equals(localname)) {
                labelAttributes = new AttributesImpl(attributes);
            } else if ("hint".equals(localname)) {
                hintAttributes = new AttributesImpl(attributes);
            } else if ("help".equals(localname)) {
                helpAttributes = new AttributesImpl(attributes);
            } else if ("alert".equals(localname)) {
                alertAttributes = new AttributesImpl(attributes);
            }
        }
        super.startElement(uri, localname, qName, attributes);
    }

    public void endElement(String uri, String localname, String qName) throws SAXException {
        super.endElement(uri, localname, qName);
        level--;
    }

    public static void handleReadOnlyAttribute(AttributesImpl newAttributes, XFormsControl XFormsControl) {
        if (XFormsControl != null && XFormsControl.isReadonly()) {
            // @disabled="disabled"
            newAttributes.addAttribute("", "disabled", "disabled", ContentHandlerHelper.CDATA, "disabled");
        }
    }

    public static void handleMIPClasses(StringBuffer sb, XFormsControl xformsControl) {
        if (xformsControl != null) {// TEMP, xformsControl should not be null
            if (!xformsControl.isRelevant()) {
                if (sb.length() > 0)
                    sb.append(' ');
                sb.append("xforms-disabled");
            }
            if (!xformsControl.isValid()) {
                if (sb.length() > 0)
                    sb.append(' ');
                sb.append("xforms-invalid");
            }
            if (xformsControl != null && xformsControl.isReadonly()) {
                if (sb.length() > 0)
                    sb.append(' ');
                sb.append("xforms-readonly");
            }
            if (xformsControl != null && xformsControl.isRequired()) {
                if (sb.length() > 0)
                    sb.append(' ');
                sb.append("xforms-required");
                if (xformsControl instanceof XFormsValueControl) {
                    if ("".equals(((XFormsValueControl) xformsControl).getValue()))
                        sb.append(" xforms-required-empty");
                    else
                        sb.append(" xforms-required-filled");
                }
            }
        }
    }

    public static boolean isDateOrTime(String type) {
        return isDate(type)
                || "{http://www.w3.org/2001/XMLSchema}dateTime".equals(type)
                || "{http://www.w3.org/2001/XMLSchema}time".equals(type);
    }

    public static boolean isDate(String type) {
        return "{http://www.w3.org/2001/XMLSchema}date".equals(type);
    }

    protected void handleAccessibilityAttributes(Attributes srcAttributes, AttributesImpl destAttributes) {
{'fraction_non_alphanumeric': 0.09716407299154751, 'fraction_numerical': 0.004739710877636464, 'mean_word_length': 2.6813027042744983, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, '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': '5563658', 'n_tokens_mistral': 3455, 'n_tokens_neox': 3298, 'n_words': 873}