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//      public ErrorMessage valueCheck( final String key ,final String value ,
064//                                                                      final int sizeX ,final int sizeY ,final String param ) {
065        @Override
066        public ErrorMessage valueCheck( final String key ,final String value ,
067                                                                        final int sizeX ,final int sizeY ,final String typeParam ,final boolean isStrict) {
068
069                ErrorMessage msg = new ErrorMessage();
070                if( value == null || value.length() == 0 ) { return msg; }
071
072                int len = (sizeY == 0) ? sizeX : sizeX + sizeY + 1;
073                String check = DBTypeCheckUtil.byteLengthCheck( value,len );
074                if( check != null ) {
075                        // 文字列の長さが指定の長さよりも長いです。
076                        msg.addMessage( 0,ErrorMessage.NG,"ERR0006",key,value,check,String.valueOf( len ) );
077                }
078
079                StringBuilder val = new StringBuilder();
080                boolean isError = false;
081                for( int i=0; i<value.length(); i++ ) {
082                        char ch = value.charAt( i );
083                        if( ch < 0x20 || ( ch > 0x7e && ch < 0xff64 ) || ( ch >= 0xffa0 ) ) {
084                                val.append( "<span class=\"NG\">" ).append( ch ).append( "</span>" );
085                                isError = true;
086                        }
087                        else {
088                                val.append( ch );
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                // クロスサイトスクリプティング対策:'<', '>' は登録させない。
104                msg = xssCheck( key ,value, msg );
105
106                return msg;
107        }
108}