001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.plugin.column;
017
018import org.opengion.fukurou.util.ErrorMessage;
019import org.opengion.hayabusa.db.AbstractDBType;
020import org.opengion.hayabusa.db.DBTypeCheckUtil;
021
022/**
023 * 半角英数字のみの文字列を扱う為の、カラム属性を定義します。
024 *
025 * 使用可能文字は、0-9,A-Z,a-z です。(,は含みません)
026 * 小数点、カンマ、半角スペース などを含みません。
027 *
028 * タイプチェックとして、以下の条件を判定します。
029 * ・文字列長は、直接計算で文字数との比較
030 * ・半角文字チェック「0-9,A-Z,a-z」の範囲
031 * ・文字パラメータの 正規表現チェック
032 *
033 * @og.rev 3.8.0.2 (2005/07/11) 新規作成
034 * @og.group データ属性
035 *
036 * @version  4.0
037 * @author   Kazuhiko Hasegawa
038 * @since    JDK5.0,
039 */
040public class DBType_XLU9 extends AbstractDBType {
041        /** このプログラムのVERSION文字列を設定します。   {@value} */
042        private static final String VERSION = "6.4.2.0 (2016/01/29)" ;
043
044        /**
045         * デフォルトコンストラクター
046         *
047         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
048         */
049        public DBType_XLU9() { super(); }               // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
050
051        /**
052         * データが登録可能かどうかをチェックします。
053         * データがエラーの場合は、そのエラー内容を返します。
054         *
055         * @og.rev 5.2.2.0 (2010/11/01) 厳密にチェック(isStrict=true)するフラグを追加
056         *
057         * @param   key         キー
058         * @param   value       値
059         * @param   sizeX       整数部分の文字列の長さ
060         * @param   sizeY       小数部分の文字列の長さ
061         * @param   typeParam   dbType パラメータ(文字パラメータ)
062         * @param   isStrict    厳密にチェックするかどうか[true:する/false:標準的]
063         *
064         * @return  エラー内容
065         * @og.rtnNotNull
066         */
067        @Override
068        public ErrorMessage valueCheck( final String key ,final String value ,
069                                                                        final int sizeX ,final int sizeY ,final String typeParam ,final boolean isStrict) {
070
071                final ErrorMessage msg = new ErrorMessage();
072                if( value == null || value.isEmpty() ) { return msg; }
073
074                final int len = (sizeY == 0) ? sizeX : sizeX + sizeY + 1;
075                if( value.length() > len ) {
076                        // 文字列の長さが指定の長さよりも長いです。
077                        msg.addMessage( 0,ErrorMessage.NG,"ERR0006", key,value, String.valueOf( value.length() ), String.valueOf( len ) );
078                }
079
080                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
081                boolean isError = false;
082                for( int i=0; i<value.length(); i++ ) {
083                        final char ch = value.charAt( i );
084                        final boolean okFlag =  (ch >= '0' && ch <= '9' )
085                                                        ||      (ch >= 'A' && ch <= 'Z' )
086                                                        ||      (ch >= 'a' && ch <= 'z' ) ;
087
088                        if( okFlag ) {
089                                buf.append( ch );
090                        }
091                        else {
092                                buf.append( "<span class=\"NG\">" ).append( ch ).append( "</span>" );
093                                isError = true;
094                        }
095                }
096                if( isError ) {
097                        // 指定の文字以外の文字が使われています。
098                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,buf.toString() );
099                }
100
101                // 3.6.0.0 (2004/09/22) dbType パラメータ(文字パラメータ)を使用したマッチチェック
102                final String check = DBTypeCheckUtil.matcheCheck( value,typeParam );
103                if( check != null ) {
104                        // 指定の文字以外の文字が使われています。
105                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,check );
106                }
107
108                return msg;
109        }
110}