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 java.util.Locale;
019
020import org.opengion.fukurou.util.ErrorMessage;
021import org.opengion.fukurou.util.StringUtil;
022import org.opengion.hayabusa.db.AbstractDBType;
023import org.opengion.hayabusa.db.DBTypeCheckUtil;
024
025/**
026 * 今までのカラム属性は昔からのルールに従っているため、
027 * 品番情報の文字列を扱う為のカラム属性を新たに定義します。
028 *
029 * 品番で使用可能な文字化どうか(例えばI等)は考慮しません。
030 *
031 * タイプチェックとして、以下の条件を判定します。
032 * ・文字列長は、Byte換算での文字数との比較
033 * ・文字列チェックはXUと同じ
034 * ・文字パラメータの 正規表現チェック
035 * ・クロスサイトスクリプティングチェック
036 * 
037 * ハイフンは、
038 * ・入力文字数が13または14桁
039 * ・4桁目&10桁目が"-"
040 * ・他にハイフンがない
041 * を全て満たした場合に削除を行います。
042 *
043 * @og.group データ属性
044 *
045 * @version  5.0
046 * @author   Takahashi Masakazu
047 * @since    JDK5.0,
048 */
049public class DBType_PN2 extends AbstractDBType {
050        //* このプログラムのVERSION文字列を設定します。   {@value} */
051        private static final String VERSION = "6.4.3.2 (2016/02/19)" ;
052
053        /**
054         * デフォルトコンストラクター
055         *
056         * @og.rev 6.4.3.2 (2016/02/19) PMD refactoring. Each class should declare at least one constructor.
057         */
058        public DBType_PN2() { super(); }                // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
059
060        /**
061         * エディターで編集されたデータを登録する場合に、データそのものを
062         * 変換して、実登録データを作成します。
063         * データの表示用文字列を返します。
064         * 特定の条件の場合、XXX-XXXXX-XXX 形式で入力された情報を、XXXXXXXXXXX 形式で表示します。
065         * カット&ペースト対策です。
066         * ※入力文字数が12または13桁
067         *  かつ、4桁目&10桁目が"-"
068         *  かつ、他にハイフンがない
069         * 場合にハイフンの削除を行います。
070         *
071         * @og.rev 6.4.3.1 (2016/02/12) newValの求め方が、おかしい。
072         * @og.rev 5.9.7.1 (2016/04/06) スペースは前後を取るようにする
073         *
074         * @param       value   (一般に編集データとして登録されたデータ)
075         *
076         * @return  修正後の文字列(一般にデータベースに登録するデータ)
077         */
078        @Override
079        public String valueSet( final String value ) {
080
081                String newVal = value;
082                if( newVal != null ) {
083                        newVal = newVal.trim().toUpperCase(Locale.JAPAN);
084                        if( ( newVal.length()==12 || newVal.length()==13 )
085                                        && newVal.indexOf( '-' ) == 3
086                                        && newVal.lastIndexOf( '-' ) == 9
087                                        && newVal.indexOf( '-' , 4 ) == 9 ) {
088                                newVal = StringUtil.replace( newVal,"-","" );
089                        }
090                }
091
092                return newVal;
093
094        }
095
096        /**
097         * データが登録可能かどうかをチェックします。
098         * データがエラーの場合は、そのエラー内容を返します。
099         *
100         * @param   key         キー
101         * @param   value       値
102         * @param   sizeX       整数部分の文字列の長さ
103         * @param   sizeY       少数部分の文字列の長さ
104         * @param   typeParam   dbType パラメータ(文字パラメータ)
105         * @param   isStrict    厳密にチェックするかどうか[true:する/false:標準的]
106         *
107         * @return  エラー内容
108         */
109        @Override
110        public ErrorMessage valueCheck( final String key ,final String value ,
111                                                                        final int sizeX ,final int sizeY ,final String typeParam ,final boolean isStrict) {
112
113                ErrorMessage msg = new ErrorMessage();
114                if( value == null || value.length() == 0 ) { return msg; }
115
116                final int len = (sizeY == 0) ? sizeX : sizeX + sizeY + 1;
117                if( value.length() > len ) {
118                        // 文字列の長さが指定の長さよりも長いです。
119                        msg.addMessage( 0,ErrorMessage.NG,"ERR0006", key,value, String.valueOf( value.length() ), String.valueOf( len ) );
120                }
121
122                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
123                boolean isError = false;
124                for( int i=0; i<value.length(); i++ ) {
125                        final char ch = value.charAt( i );
126                        if( ch < 0x20 || ch > 0x7e || 'a' <= ch && ch <= 'z' ) {                        // 6.9.7.0 (2018/05/14) PMD Useless parentheses.
127                                buf.append( "<span class=\"NG\">" ).append( ch ).append( "</span>" );
128                                isError = true;
129                        }
130                        else {
131                                buf.append( ch );
132                        }
133                }
134                if( isError ) {
135                        // 指定の文字以外の文字が使われています。
136                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,buf.toString() );
137                }
138
139                // 3.6.0.0 (2004/09/22) dbType パラメータ(文字パラメータ)を使用したマッチチェック
140                final String check = DBTypeCheckUtil.matcheCheck( value,typeParam );
141                if( check != null ) {
142                        // 指定の文字以外の文字が使われています。
143                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,check );
144                }
145
146                // クロスサイトスクリプティング対策:'<', '>' は登録させない。
147                msg = xssCheck( key ,value, msg );
148                return msg;
149        }
150}