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 * 半角文字+半角カタカナとは、X 属性に半角カタカナを加えた、
026 * 「c < 0x20 || ( c > 0x7e && c < 0xff64 ) || ( c >= 0xffa0 ) 以外」
027 * でのみ構成された文字列のことです。
028 *
029 * タイプチェックとして、以下の条件を判定します。
030 * ・文字列長は、Byte換算での文字数との比較
031 * ・半角文字+半角カタカナチェック
032 * ・文字パラメータの 正規表現チェック
033 * ・クロスサイトスクリプティングチェック
034 *
035 * @og.group データ属性
036 *
037 * @version  4.0
038 * @author   Kazuhiko Hasegawa
039 * @since    JDK5.0,
040 */
041public class DBType_XH extends AbstractDBType {
042        //* このプログラムのVERSION文字列を設定します。   {@value} */
043        private static final String VERSION = "5.2.2.0 (2010/11/01)" ;
044
045        /**
046         * データが登録可能かどうかをチェックします。
047         * データがエラーの場合は、そのエラー内容を返します。
048         *
049         * @og.rev 3.0.1.3 (2003/03/11) DBTypeCheckUtilクラスを利用するように修正
050         * @og.rev 3.6.0.0 (2004/09/22) dbType パラメータを引数に追加
051         * @og.rev 3.6.1.0 (2005/01/05) 半角カタカナに、『、』を含めます。(0xff65 以上 → 0xff64以上)
052         * @og.rev 5.2.2.0 (2010/11/01) 厳密にチェック(isStrict=true)するフラグを追加
053         *
054         * @param   key         キー
055         * @param   value       値
056         * @param   sizeX       整数部分の文字列の長さ
057         * @param   sizeY       少数部分の文字列の長さ
058         * @param   typeParam   dbType パラメータ
059         * @param   isStrict    厳密にチェックするかどうか[true:する/false:標準的]
060         *
061         * @return  エラー内容
062         */
063        @Override
064        public ErrorMessage valueCheck( final String key ,final String value ,
065                                                                        final int sizeX ,final int sizeY ,final String typeParam ,final boolean isStrict) {
066
067                ErrorMessage msg = new ErrorMessage();
068                if( value == null || value.length() == 0 ) { return msg; }
069
070                int len = (sizeY == 0) ? sizeX : sizeX + sizeY + 1;
071                String check = DBTypeCheckUtil.byteLengthCheck( value,len );
072                if( check != null ) {
073                        // 文字列の長さが指定の長さよりも長いです。
074                        msg.addMessage( 0,ErrorMessage.NG,"ERR0006",key,value,check,String.valueOf( len ) );
075                }
076
077                StringBuilder val = new StringBuilder();
078                boolean isError = false;
079                for( int i=0; i<value.length(); i++ ) {
080                        char ch = value.charAt( i );
081                        if( ch < 0x20 || ( ch > 0x7e && ch < 0xff64 ) || ( ch >= 0xffa0 ) ) {
082                                val.append( "<span class=\"NG\">" ).append( ch ).append( "</span>" );
083                                isError = true;
084                        }
085                        else {
086                                val.append( ch );
087                        }
088                }
089                if( isError ) {
090                        // 指定の文字以外の文字が使われています。
091                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,val.toString() );
092                }
093
094                // 3.6.0.0 (2004/09/22) dbType パラメータを使用したマッチチェック
095                check = DBTypeCheckUtil.matcheCheck( value,typeParam );
096                if( check != null ) {
097                        // 指定の文字以外の文字が使われています。
098                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,check );
099                }
100
101                // クロスサイトスクリプティング対策:'<', '>' は登録させない。
102                msg = xssCheck( key ,value, msg );
103
104                return msg;
105        }
106}