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.fukurou.util.KanaFilter;
021import org.opengion.hayabusa.db.AbstractDBType;
022import org.opengion.hayabusa.db.DBTypeCheckUtil;
023
024/**
025 * 半角/全角混在のクラスですが、半角カタカナのみを通さない文字列を扱う為の、カラム属性を定義します。
026 *
027 * 半角カタカナの定義は、\uFF61 から \uFF9F までです。
028 * 半角カタカナを入力すると、全角カタカナに変換して、登録しています。
029 * この変換処理に、KanaFilter#han2zen を使用しています。これは、
030 * 『CJKV日中韓越情報処理』のフィルターアルゴリズムで濁点や半濁点の正しい処理も含まれています。
031 *
032 * タイプチェックとして、以下の条件を判定します。
033 * ・文字列長は、Byte換算での文字数との比較
034 * ・半角文字+全角カタカナチェック「c < 0x20 || ( c >= 0xff61 && c <= 0xff9f )以外」エラー
035 * ・文字パラメータの 正規表現チェック
036 * ・クロスサイトスクリプティングチェック
037 *
038 * @og.rev 3.4.0.0 (2003/09/01) 新規作成
039 * @og.group データ属性
040 *
041 * @version  4.0
042 * @author   Kazuhiko Hasegawa
043 * @since    JDK5.0,
044 */
045public class DBType_XKZ extends AbstractDBType {
046        /** このプログラムのVERSION文字列を設定します。   {@value} */
047        private static final String VERSION = "6.4.2.0 (2016/01/29)" ;
048
049        /**
050         * デフォルトコンストラクター
051         *
052         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
053         */
054        public DBType_XKZ() { super(); }                // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
055
056        /**
057         * エディターで編集されたデータを登録する場合に、データそのものを
058         * 変換して、実登録データを作成します。
059         * 例えば,大文字のみのフィールドなら、大文字化します。
060         * 実登録データの作成は、DBType オブジェクトを利用しますので,
061         * これと Editor とがアンマッチの場合は、うまくデータ変換
062         * されない可能性がありますので、注意願います。
063         *
064         * @param       value   (一般に編集データとして登録されたデータ)
065         *
066         * @return  修正後の文字列(一般にデータベースに登録するデータ)
067         */
068        @Override
069        public String valueSet( final String value ) {
070                // 6.4.1.1 (2016/01/16) PMD refactoring. A method should have only one exit point, and that should be the last statement in the method
071                return value == null ? null : KanaFilter.han2zen( StringUtil.rTrim( value ) );
072        }
073
074        /**
075         * データが登録可能かどうかをチェックします。
076         * データがエラーの場合は、そのエラー内容を返します。
077         *
078         * @og.rev 3.6.0.0 (2004/09/22) dbType パラメータ(文字パラメータ)を引数に追加
079         * @og.rev 5.2.2.0 (2010/11/01) 厳密にチェック(isStrict=true)するフラグを追加
080         *
081         * @param   key         キー
082         * @param   value       値
083         * @param   sizeX       整数部分の文字列の長さ
084         * @param   sizeY       小数部分の文字列の長さ
085         * @param   typeParam   dbType パラメータ(文字パラメータ)
086         * @param   isStrict    厳密にチェックするかどうか[true:する/false:標準的]
087         *
088         * @return  エラー内容
089         * @og.rtnNotNull
090         */
091        @Override
092        public ErrorMessage valueCheck( final String key ,final String value ,
093                                                                        final int sizeX ,final int sizeY ,final String typeParam ,final boolean isStrict) {
094
095                // 6.3.9.1 (2015/11/27) Found 'DD'-anomaly for variable(PMD)
096                final ErrorMessage msg = new ErrorMessage();
097                if( value == null || value.isEmpty() ) { return msg; }
098
099                final int len = (sizeY == 0) ? sizeX : sizeX + sizeY + 1;
100                String check = DBTypeCheckUtil.byteLengthCheck( value,len );
101                if( check != null ) {
102                        // 文字列の長さが指定の長さよりも長いです。
103                        msg.addMessage( 0,ErrorMessage.NG,"ERR0006",key,value,check,String.valueOf( len ) );
104                }
105
106                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
107                boolean isError = false;
108                for( int i=0; i<value.length(); i++ ) {
109                        final char ch = value.charAt( i );
110                        if( ch < 0x20 || ch >= 0xff61 && ch <= 0xff9f ) {                                                       // 6.9.7.0 (2018/05/14) PMD Useless parentheses.
111                                buf.append( "<span class=\"NG\">" ).append( ch ).append( "</span>" );
112                                isError = true;
113                        }
114                        else {
115                                buf.append( ch );
116                        }
117                }
118                if( isError ) {
119                        // 指定の文字以外の文字が使われています。
120                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,buf.toString() );
121                }
122
123                // 3.6.0.0 (2004/09/22) dbType パラメータ(文字パラメータ)を使用したマッチチェック
124                check = DBTypeCheckUtil.matcheCheck( value,typeParam );
125                if( check != null ) {
126                        // 指定の文字以外の文字が使われています。
127                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,check );
128                }
129
130                // クロスサイトスクリプティング対策:'<', '>' は登録させない。
131                // 6.3.9.1 (2015/11/27) Found 'DD'-anomaly for variable(PMD)
132                return xssCheck( key ,value, msg );
133        }
134}