/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (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.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */
package com.icesoft.net.messaging;

import com.icesoft.util.ServerUtility;
import com.icesoft.util.Validator;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletContext;

public class IdentifierValidator
implements Validator<String> {
    private static final Logger LOGGER = Logger.getLogger(IdentifierValidator.class.getName());

    private static final String[] ILLEGAL_MESSAGE_SELECTOR_IDENTIFIERS =
        new String[] {
            "NULL",
            "TRUE",
            "FALSE",
            "NOT",
            "AND",
            "OR",
            "BETWEEN",
            "LIKE",
            "IN",
            "IS",
            "ESCAPE"
        };

    private ServletContext servletContext;

    public IdentifierValidator() {
        // Do nothing.
    }

    public IdentifierValidator(final ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    public boolean isValid(final String value) {
        if (value == null || value.trim().length() == 0) {
            return false;
        }
        /*
         * - Identifiers cannot be the names NULL, TRUE, and FALSE.
         * - Identifiers cannot be NOT, AND, OR, BETWEEN, LIKE, IN, IS, or ESCAPE.
         */
        for (String _illegalMessageSelectorIdentifier : ILLEGAL_MESSAGE_SELECTOR_IDENTIFIERS) {
            if (value.equalsIgnoreCase(_illegalMessageSelectorIdentifier)) {
                return false;
            }
        }
        char[] _charArray = value.toCharArray();
        /*
         * - An identifier is an unlimited-length sequence of letters and digits, the first of which must be a letter.
         *   A letter is any character for which the method Character.isJavaLetter(char) returns true.  This includes
         *   '_' and '$'.  A letter or digit is any character for which the method Character.isJavaLetterOrDigit(char)
         *   returns true.
         *
         * Note: Character.isJavaLetter(char) is deprecated and replaced by Character.isJavaIdentifierStart(char).
         *
         * Note: Character.isJavaLetterOrDigit(char) is deprecated and replaced by Character.isJavaIdentifierPart(char).
         */
        if (_charArray.length == 0) {
            return false;
        } else if (!Character.isJavaIdentifierStart(_charArray[0])) {
            return false;
        } else {
            for (int i = 1; i < _charArray.length; i++) {
                if (!Character.isJavaIdentifierPart(_charArray[i]) &&
                    (
                        (_charArray[i] != '.' && _charArray[i] != '-') ||
                        (servletContext != null && !ServerUtility.isJBoss7(servletContext))
                    )) {

                    return false;
                }
            }
            return true;
        }
    }
}
