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 * 旧OASで実装していた、半角文字(カナ含む)名称用記号を扱う為の、カラム属性を定義します。
024 *
025 * 使用可能文字は、半角英数大小文字,スペース,半角カタカナ,・,+,-,(,),*,#,/,.,% です。(,は含みません)
026 * 半角カタカナのUnicode範囲は、(c >= 0xff65 && c <= 0xff9f ) を有効とします。
027 *
028 * タイプチェックとして、以下の条件を判定します。
029 * ・文字列長は、Byte換算での文字数との比較
030 * ・半角文字+半角カタカナ+特殊名称チェック
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_OASNM extends AbstractDBType {
041        //* このプログラムのVERSION文字列を設定します。   {@value} */
042        private static final String VERSION = "5.2.2.0 (2010/11/01)" ;
043
044        private static final String OAS_NAME = "+-()*#/.%" ;
045
046        /**
047         * データが登録可能かどうかをチェックします。
048         * データがエラーの場合は、そのエラー内容を返します。
049         *
050         * @og.rev 5.2.2.0 (2010/11/01) 厳密にチェック(isStrict=true)するフラグを追加
051         *
052         * @param   key         キー
053         * @param   value       値
054         * @param   sizeX       整数部分の文字列の長さ
055         * @param   sizeY       少数部分の文字列の長さ
056         * @param   typeParam   dbType パラメータ
057         * @param   isStrict    厳密にチェックするかどうか[true:する/false:標準的]
058         *
059         * @return  エラー内容
060         */
061        @Override
062        public ErrorMessage valueCheck( final String key ,final String value ,
063                                                                        final int sizeX ,final int sizeY ,final String typeParam ,final boolean isStrict) {
064
065                ErrorMessage msg = new ErrorMessage();
066                if( value == null || value.length() == 0 ) { return msg; }
067
068                int len = (sizeY == 0) ? sizeX : sizeX + sizeY + 1;
069                String check = DBTypeCheckUtil.byteLengthCheck( value,len );
070                if( check != null ) {
071                        // 文字列の長さが指定の長さよりも長いです。
072                        msg.addMessage( 0,ErrorMessage.NG,"ERR0006",key,value,check,String.valueOf( len ) );
073                }
074
075                StringBuilder val = new StringBuilder();
076                boolean isError = false;
077                for( int i=0; i<value.length(); i++ ) {
078                        char ch = value.charAt( i );
079                        boolean okFlag =        (ch >= '0' && ch <= '9' ) || (ch >= 'A' && ch <= 'Z' )
080                                                        ||      (ch >= 'a' && ch <= 'z' ) || ( OAS_NAME.indexOf( ch ) >= 0 )
081                                                        ||      (ch >= 0xff65 && ch <= 0xff9f ) || ch == ' ' ;
082
083                        if( okFlag ) {
084                                val.append( ch );
085                        }
086                        else {
087                                val.append( "<span class=\"NG\">" ).append( ch ).append( "</span>" );
088                                isError = true;
089                        }
090                }
091                if( isError ) {
092                        // 指定の文字以外の文字が使われています。
093                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,val.toString() );
094                }
095
096                // 3.6.0.0 (2004/09/22) dbType パラメータを使用したマッチチェック
097                check = DBTypeCheckUtil.matcheCheck( value,typeParam );
098                if( check != null ) {
099                        // 指定の文字以外の文字が使われています。
100                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,check );
101                }
102
103                return msg;
104        }
105}