/*
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.hatemile.util;

import org.hatemile.util.html.HTMLDOMElement;

/**
 * The CommonFuncionts class contains the used methods by HaTeMiLe classes.
 */
public final class CommonFunctions {

    /**
     * The name of attribute for not modify the elements.
     */
    public static final String DATA_IGNORE = "data-ignoreaccessibilityfix";

    /**
     * Count the number of ids created.
     */
    private static int count = 0;

    /**
     * The private constructor prevents that the class not can be initialized.
     */
    private CommonFunctions() {

    }

    /**
     * Generate a id for a element.
     * @param element The element.
     * @param prefix The prefix of id.
     * @deprecated Prefer use {@link org.hatemile.util.IDGenerator} instead this
     * constructor.
     */
    public static void generateId(final HTMLDOMElement element,
            final String prefix) {
        if (!element.hasAttribute("id")) {
            element.setAttribute("id", prefix + Integer.toString(count));
            count++;
        }
    }

    /**
     * Reset the count number of ids.
     */
    public static void resetCount() {
        count = 0;
    }

    /**
     * Copy a list of attributes of a element for other element.
     * @param element1 The element that have attributes copied.
     * @param element2 The element that copy the attributes.
     * @param attributes The list of attributes that will be copied.
     */
    public static void setListAttributes(final HTMLDOMElement element1,
            final HTMLDOMElement element2, final String[] attributes) {
        for (int i = 0, length = attributes.length; i < length; i++) {
            if (element1.hasAttribute(attributes[i])) {
                element2.setAttribute(attributes[i],
                        element1.getAttribute(attributes[i]));
            }
        }
    }

    /**
     * Increase a item in a HTML list.
     * @param list The list.
     * @param stringToIncrease The value of item.
     * @return The HTML list with the item added, if the item not was contained
     * in list.
     */
    public static String increaseInList(final String list,
            final String stringToIncrease) {
        if ((list != null) && (!list.isEmpty()) && (stringToIncrease != null)
                && (!stringToIncrease.isEmpty())) {
            if (inList(list, stringToIncrease)) {
                return list;
            } else {
                return list + " " + stringToIncrease;
            }
        } else if ((list != null) && (!list.isEmpty())) {
            return list;
        } else {
            return stringToIncrease;
        }
    }

    /**
     * Verify if the list contains the item.
     * @param list The list.
     * @param stringToSearch The value of item.
     * @return True if the list contains the item or false is not contains.
     */
    public static boolean inList(final String list,
            final String stringToSearch) {
        if ((list != null) && (!list.isEmpty()) && (stringToSearch != null)
                && (!stringToSearch.isEmpty())) {
            String[] elements = list.split("[ \n\t\r]+");
            for (int i = 0, length = elements.length; i < length; i++) {
                String element = elements[i];
                if (element.equals(stringToSearch)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Check that the element can be manipulated by HaTeMiLe.
     * @param element The element
     * @return True if element can be manipulated or false if element cannot be
     * manipulated.
     */
    public static boolean isValidElement(final HTMLDOMElement element) {
        if (element.hasAttribute(DATA_IGNORE)) {
            return false;
        } else {
            HTMLDOMElement parentElement = element.getParentElement();
            if (parentElement != null) {
                String tagName = parentElement.getTagName();
                if ((!tagName.equals("BODY")) && (!tagName.equals("HTML"))) {
                    return CommonFunctions.isValidElement(parentElement);
                } else {
                    return true;
                }
            } else {
                return true;
            }
        }
    }
}

@@ -624,12 +624,12 @@
 Func
+t
 ion
-t
 s cl

Set the correct name of class