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.fukurou.util.StringUtil;
020import org.opengion.hayabusa.db.AbstractDBType;
021import org.opengion.hayabusa.db.DBTypeCheckUtil;
022
023/**
024 * 全角+改行C(CR,LF)のみで構成される文字列を扱う為の、カラム属性を定義します。
025 *
026 * 全角文字とは、「c < 0x7f || ( 0xff65 <= c && c < 0xffa0 ) 以外」
027 * の文字で構成される文字列のことです。
028 *
029 * タイプチェックとして、以下の条件を判定します。
030 * ・全角文字チェック「c < 0x7f || ( 0xff65 <= c && c < 0xffa0 ) 以外」
031 *   かつ改行C以外「c != 0x0a && c != 0x0d以外」エラー
032 * ・文字列長は、Byte換算での文字数との比較
033 * ・文字パラメータの 正規表現チェック
034 *
035 * @og.group データ属性
036 *
037 * @version  4.0
038 * @author       Hiroki Nakamura
039 * @since    JDK5.0,
040 */
041public class DBType_KCL extends AbstractDBType {
042        //* このプログラムのVERSION文字列を設定します。   {@value} */
043        private static final String VERSION = "5.2.2.0 (2010/11/01)" ;
044
045        /**
046         * 半角スペースで固定長(半角換算の数)に変換した文字列を返します。
047         * 半角スペース埋めは、文字が半角、全角混在でもかまいません。
048         * なお、エラーチェックは行われません。
049         * 実行前に、必ず valueCheck( String value ,int len ) が行われる必要があります。
050         *
051         * @param       value    FILL埋めする文字列
052         * @param       sizeX    整数部分の文字列の長さ
053         * @param       sizeY    少数部分の文字列の長さ
054         * @param   encode   固定長で変換する文字エンコード
055         *
056         * @return      FILL埋めした新しい文字列
057         */
058        @Override
059        public String valueFill( final String value ,final int sizeX ,final int sizeY,final String encode ) {
060                int len = (sizeY == 0) ? sizeX : sizeX + sizeY + 1;
061
062                return StringUtil.stringKFill( value,len,encode );
063        }
064
065        /**
066         * データが登録可能かどうかをチェックします。
067         * データがエラーの場合は、そのエラー内容を返します。
068         *
069         * @og.rev 5.2.2.0 (2010/11/01) 厳密にチェック(isStrict=true)するフラグを追加
070         *
071         * @param   key         キー
072         * @param   value       値
073         * @param   sizeX       整数部分の文字列の長さ
074         * @param   sizeY       少数部分の文字列の長さ
075         * @param   typeParam   dbType パラメータ
076         * @param   isStrict    厳密にチェックするかどうか[true:する/false:標準的]
077         *
078         * @return  エラー内容
079         */
080//      public ErrorMessage valueCheck( final String key ,final String value ,
081//                                                                      final int sizeX ,final int sizeY ,final String param ) {
082        @Override
083        public ErrorMessage valueCheck( final String key ,final String value ,
084                                                                        final int sizeX ,final int sizeY ,final String typeParam ,final boolean isStrict) {
085
086                ErrorMessage msg = new ErrorMessage();
087                if( value == null || value.length() == 0 ) { return msg; }
088
089                int len = (sizeY == 0) ? sizeX : sizeX + sizeY + 1;
090                String check = DBTypeCheckUtil.byteLengthCheck( value,len );
091                if( check != null ) {
092                        // 文字列の長さが指定の長さよりも長いです。
093                        msg.addMessage( 0,ErrorMessage.NG,"ERR0006",key,value,check,String.valueOf( len ) );
094                }
095
096                char[] chs = value.toCharArray() ;
097                for( int i=0; i<chs.length; i++ ) {
098                        if( chs[i] < 0x7f || ( 0xff65 <= chs[i] && chs[i] < 0xffa0 ) ) {
099                                if( chs[i] != 0x0a && chs[i] != 0x0d ) {
100                                        // 全てが全角文字ではありません。
101                                        msg.addMessage( 0,ErrorMessage.NG,"ERR0007",key,value );
102                                        break;
103                                }
104                        }
105                }
106
107                // 3.6.0.0 (2004/09/22) dbType パラメータを使用したマッチチェック
108                check = DBTypeCheckUtil.matcheCheck( value,typeParam );
109                if( check != null ) {
110                        // 指定の文字以外の文字が使われています。
111                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,check );
112                }
113
114                return msg;
115        }
116}