<commit_before>/*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2014, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */
package org.thymeleaf.aurora.engine;

/**
 *
 * @author <NAME>
 * @since 3.0.0
 * 
 */
public class AttributeName {

    private final String dialectPrefix;
    private final String attributeName;
    private final String completeNSAttributeName;
    private final String completeDataAttributeName;


    public AttributeName(final String dialectPrefix, final String attributeName) {

        super();

        if (attributeName == null || attributeName.length() == 0) {
            throw new IllegalArgumentException("Attribute name cannot be null or empty");
        }

        this.dialectPrefix = dialectPrefix;
        this.attributeName = attributeName;

        this.completeNSAttributeName = (dialectPrefix == null? attributeName : dialectPrefix + ":" + attributeName);

        // Note that, if prefix is null, we are not creating attribute names like "data-{name}" because the
        // fact tha prefix is null means that we want to act on the standard HTML/XML attributes themselves.
        this.completeDataAttributeName = (dialectPrefix == null? attributeName : "data-" + dialectPrefix + "-" + attributeName);

    }


    public String getDialectPrefix() {
        return this.dialectPrefix;
    }

    public String getAttributeName() {
        return this.attributeName;
    }


    public String getCompleteNSAttributeName() {
        return this.completeNSAttributeName;
    }

    public String getCompleteDataAttributeName() {
        return this.completeDataAttributeName;
    }

}<commit_msg>Added static factory methods for AttributeName.<commit_after>/*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2014, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */
package org.thymeleaf.aurora.engine;

import org.thymeleaf.aurora.util.TextUtil;

/**
 *
 * @author <NAME>
 * @since 3.0.0
 * 
 */
public class AttributeName {

    private final String dialectPrefix;
    private final String attributeName;
    private final String completeNSAttributeName;
    private final String completeDataAttributeName;




    public static AttributeName forXmlName(final char[] attributeNameBuffer, final int attributeNameOffset, final int attributeNameLen) {

        if (attributeNameBuffer == null) {
            throw new IllegalArgumentException("Attribute name buffer cannot be null or empty");
        }
        if (attributeNameOffset < 0 || attributeNameLen < 0) {
            throw new IllegalArgumentException("Atribute name offset and len must be equal or greater than zero");
        }


        char c;
        int i = attributeNameOffset;
        int n = attributeNameLen;
        while (n-- != 0) {

            c = attributeNameBuffer[i++];
            if (c != ':') {
                continue;
            }

            if (c == ':') {
                if (i == 1){
                    // ':' was the first char, no prefix there
                    return new AttributeName(
                            null, new String(attributeNameBuffer, attributeNameOffset, attributeNameLen), false);
                }

                return new AttributeName(
                        new String(attributeNameBuffer, attributeNameOffset, (i - (attributeNameOffset + 1))),
                        new String(attributeNameBuffer, i, attributeNameLen - i),
                        false);
            }

        }

        return new AttributeName(
                null, new String(attributeNameBuffer, attributeNameOffset, attributeNameLen), false);

    }



    public static AttributeName forHtmlName(final char[] attributeNameBuffer, final int attributeNameOffset, final int attributeNameLen) {

        if (attributeNameBuffer == null) {
            throw new IllegalArgumentException("Attribute name buffer cannot be null or empty");
        }
        if (attributeNameOffset < 0 || attributeNameLen < 0) {
            throw new IllegalArgumentException("Atribute name offset and len must be equal or greater than zero");
        }


        char c;
        int i = attributeNameOffset;
        int n = attributeNameLen;
        boolean inData = false;
        while (n-- != 0) {

            c = attributeNameBuffer[i++];
            if (c != ':' && c != '-') {
                continue;
            }

            if (!inData && c == ':') {
                if (i == 1){
                    // ':' was the first char, no prefix there
                    return new AttributeName(
                            null, new String(attributeNameBuffer, attributeNameOffset, attributeNameLen).toLowerCase(), true);
                }

                if (TextUtil.equals(false, "xml", 0, 3, attributeNameBuffer, attributeNameOffset, (i - (attributeNameOffset + 1)))) {
                    // 'xml' is not a valid dialect prefix in HTML mode
                    return new AttributeName(
                            null, new String(attributeNameBuffer, attributeNameOffset, attributeNameLen).toLowerCase(), true);
                }

                return new AttributeName(
                        new String(attributeNameBuffer, attributeNameOffset, (i - (attributeNameOffset + 1))).toLowerCase(),
                        new String(attributeNameBuffer, i, attributeNameLen - i).toLowerCase(),
                        true);
            }

            if (!inData && c == '-') {
                if (i == 5 && TextUtil.equals(false, "data", 0, 4, attributeNameBuffer, attributeNameOffset, 4)) {
                    inData = true;
                    continue;
                } else {
                    // this is just a normal, non-thymeleaf 'data-*' attribute
                    return new AttributeName(
                            null, new String(attributeNameBuffer, attributeNameOffset, attributeNameLen).toLowerCase(), true);
                }
            }

            if (inData && c == '-') {
                if (i == 6) {
                    // '-' was the first char after 'data-', no prefix there
                    return new AttributeName(
                            null, new String(attributeNameBuffer, attributeNameOffset, attributeNameLen).toLowerCase(), true);

                }
                return new AttributeName(
                        new String(attributeNameBuffer, attributeNameOffset + 5, (i - (attributeNameOffset + 6))).toLowerCase(),
                        new String(attributeNameBuffer, i, attributeNameLen - i).toLowerCase(),
                        true);
            }

        }

        return new AttributeName(
                null, new String(attributeNameBuffer, attributeNameOffset, attributeNameLen).toLowerCase(), true);

    }



    public static AttributeName forXmlName(final String attributeName) {

        if (attributeName == null) {
            throw new IllegalArgumentException("Attribute name cannot be null or empty");
        }


        char c;
        int i = 0;
        int n = attributeName.length();
        while (n-- != 0) {

            c = attributeName.charAt(i++);
            if (c != ':') {
                continue;
            }

            if (c == ':') {
                if (i == 1){
                    // ':' was the first char, no prefix there
                    return new AttributeName(null, attributeName, false);
                }

                return new AttributeName(
                        attributeName.substring(0, i - 1),
                        attributeName.substring(i, attributeName.length()),
                        false);
            }

        }

        return new AttributeName(null, attributeName, false);

    }



    public static AttributeName forHtmlName(final String attributeName) {

        if (attributeName == null) {
            throw new IllegalArgumentException("Attribute name cannot be null or empty");
        }

        char c;
        int i = 0;
        int n = attributeName.length();
        boolean inData = false;
        while (n-- != 0) {

            c = attributeName.charAt(i++);
            if (c != ':' && c != '-') {
                continue;
            }

            if (!inData && c == ':') {
                if (i == 1){
                    // ':' was the first char, no prefix there
                    return new AttributeName(null, attributeName.toLowerCase(), true);
                }

                if (TextUtil.equals(false, "xml", 0, 3, attributeName, 0, 3)) {
                    // 'xml' is not a valid dialect prefix in HTML mode
                    return new AttributeName(null, attributeName.toLowerCase(), true);
                }

                return new AttributeName(
                        attributeName.substring(0, i - 1).toLowerCase(),
                        attributeName.substring(i,attributeName.length()).toLowerCase(),
                        true);
            }

            if (!inData && c == '-') {
                if (i == 5 && TextUtil.equals(false, "data", 0, 4, attributeName, 0, 4)) {
                    inData = true;
                    continue;
                } else {
                    // this is just a normal, non-thymeleaf 'data-*' attribute
                    return new AttributeName(null, attributeName.toLowerCase(), true);
                }
            }

            if (inData && c == '-') {
                if (i == 6) {
                    // '-' was the first char after 'data-', no prefix there
                    return new AttributeName(null, attributeName.toLowerCase(), true);

                }
                return new AttributeName(
                        attributeName.substring(5, i - 1).toLowerCase(),
                        attributeName.substring(i, attributeName.length()).toLowerCase(),
                        true);
            }

        }

        return new AttributeName(null, attributeName.toLowerCase(), true);

    }



    public static AttributeName forHtmlName(final String dialectPrefix, final String attributeName) {
        return new AttributeName(dialectPrefix, attributeName, true);
    }



    public static AttributeName forXmlName(final String dialectPrefix, final String attributeName) {
        return new AttributeName(dialectPrefix, attributeName, false);
    }





    AttributeName(final String dialectPrefix, final String attributeName, final boolean crateCompleteHtml5AttributeName) {

        super();

        if (attributeName == null || attributeName.length() == 0) {
            throw new IllegalArgumentException("Attribute name cannot be null or empty");
        }

        this.dialectPrefix = dialectPrefix;
        this.attributeName = attributeName;

        this.completeNSAttributeName = (dialectPrefix == null? attributeName : dialectPrefix + ":" + attributeName);

        if (crateCompleteHtml5AttributeName) {
            // Note that, if prefix is null, we are not creating attribute names like "data-{name}" because the
            // fact tha prefix is null means that we want to act on the standard HTML/XML attributes themselves.
            this.completeDataAttributeName = (dialectPrefix == null? attributeName : "data-" + dialectPrefix + "-" + attributeName);
        } else {
            this.completeDataAttributeName = null;
        }

    }


    public String getDialectPrefix() {
        return this.dialectPrefix;
    }

    public String getAttributeName() {
        return this.attributeName;
    }


    public String getCompleteNSAttributeName() {
        return this.completeNSAttributeName;
    }

    public String getCompleteDataAttributeName() {
        return this.completeDataAttributeName;
    }



    @Override
    public String toString() {
        // Reference equality is OK (and faster) in this case
        if (this.completeDataAttributeName == null || this.completeNSAttributeName == this.completeDataAttributeName) {
            return "{" + this.completeNSAttributeName + "}";
        }
        return "{" + this.completeNSAttributeName + "," + this.completeDataAttributeName + "}";
    }

}
{'fraction_non_alphanumeric': 0.11374407582938388, 'fraction_numerical': 0.005627962085308057, 'mean_word_length': 1.9687843482083975, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4625521', 'n_tokens_mistral': 3338, 'n_tokens_neox': 3118, 'n_words': 1011}