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         *
079         * @og.rev 3.6.0.0 (2004/09/22) dbType パラメータ(文字パラメータ)を引数に追加
080         * @og.rev 5.2.2.0 (2010/11/01) 厳密にチェック(isStrict=true)するフラグを追加
081         *
082         * @param   key         キー
083         * @param   value       値
084         * @param   sizeX       整数部分の文字列の長さ
085         * @param   sizeY       小数部分の文字列の長さ
086         * @param   typeParam   dbType パラメータ(文字パラメータ)
087         * @param   isStrict    厳密にチェックするかどうか[true:する/false:標準的]
088         *
089         * @return  エラー内容
090         * @og.rtnNotNull
091         */
092        @Override
093        public ErrorMessage valueCheck( final String key ,final String value ,
094                                                                        final int sizeX ,final int sizeY ,final String typeParam ,final boolean isStrict) {
095
096                // 6.3.9.1 (2015/11/27) Found 'DD'-anomaly for variable(PMD)
097                final ErrorMessage msg = new ErrorMessage();
098                if( value == null || value.isEmpty() ) { return msg; }
099
100                final int len = (sizeY == 0) ? sizeX : sizeX + sizeY + 1;
101                String check = DBTypeCheckUtil.byteLengthCheck( value,len );
102                if( check != null ) {
103                        // 文字列の長さが指定の長さよりも長いです。
104                        msg.addMessage( 0,ErrorMessage.NG,"ERR0006",key,value,check,String.valueOf( len ) );
105                }
106
107                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
108                boolean isError = false;
109                for( int i=0; i<value.length(); i++ ) {
110                        final char ch = value.charAt( i );
111                        if( ch < 0x20 || ( ch >= 0xff61 && ch <= 0xff9f ) ) {
112                                buf.append( "<span class=\"NG\">" ).append( ch ).append( "</span>" );
113                                isError = true;
114                        }
115                        else {
116                                buf.append( ch );
117                        }
118                }
119                if( isError ) {
120                        // 指定の文字以外の文字が使われています。
121                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,buf.toString() );
122                }
123
124                // 3.6.0.0 (2004/09/22) dbType パラメータ(文字パラメータ)を使用したマッチチェック
125                check = DBTypeCheckUtil.matcheCheck( value,typeParam );
126                if( check != null ) {
127                        // 指定の文字以外の文字が使われています。
128                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,check );
129                }
130
131                // クロスサイトスクリプティング対策:'<', '>' は登録させない。
132                // 6.3.9.1 (2015/11/27) Found 'DD'-anomaly for variable(PMD)
133                return xssCheck( key ,value, msg );
134        }
135}