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     */
016    package org.opengion.hayabusa.taglib;
017    
018    import org.opengion.hayabusa.common.HybsSystem;
019    import org.opengion.hayabusa.common.HybsSystemException;
020    import org.opengion.fukurou.util.LogWriter;
021    import org.opengion.fukurou.util.FileString;
022    
023    import javax.servlet.ServletRequest ;
024    import javax.servlet.http.HttpServletRequest ;
025    
026    /**
027     * BODY部に記述されたエンジン固有ã?æ–?­—å?({@XXXX}など)をã?
028     *  ユーザーæƒ??のエンコーãƒ?‚£ングに変換するタグですã?
029     *
030     * XML形式で 日本語コードã?パã?スがã?JSPエンジン(Tomcat)でサポã?ãƒ?
031     * されるまでの、暫定的なタグですã?
032     * なおã?こã?タグのå†?ƒ¨に存在するカスタãƒ?‚¿グは、å?に実行されるため
033     * 漢字コードã?、変換されませんã€?
034     *
035     * @og.formSample
036     * ●形式ï¼?lt;og:text >?¥?¥?¥</og:text>
037     * ●body?šあã‚?EVAL_BODY_BUFFERED:BODYを評価しã?{@XXXX} を解析しまã�?
038     *
039     * ●Tag定義??
040     *   <og:text
041     *       value              【TAG】value 値に直接書かれた漢字コードをShift_JIS に変換しまã�?
042     *       include            【TAG】動çš?�«ファイルã‚?include しまã�?
043     *       debug              【TAG】デバッグæƒ??をå?力するかどã�?�‹[true/false]を指定しまã�?初期値:false)
044     *   >   ... Body ...
045     *   </og:text>
046     *
047     * ●使用ä¾?
048     *      ・<og:text >
049     *            <p>あいおえおï¼?lt;input name="PN" value="{@PN}" /> </p>
050     *        </og:text>
051     *      ・<og:text value="あいã�?�ˆã�? />
052     *
053     *     動的にファイルã‚?include することがå?来ますã?
054     *      ・<og:text include="{@query}.txt" />
055     *
056     * @og.group 画面部å“?
057     *
058     * @version  4.0
059     * @author   Kazuhiko Hasegawa
060     * @since    JDK5.0,
061     */
062    public class TextTag extends CommonTagSupport {
063            //* こã?プログラãƒ??VERSIONæ–?­—å?を設定しますã?       {@value} */
064            private static final String VERSION = "4.0.0.0 (2007/11/30)" ;
065    
066            private static final long serialVersionUID = 400020071130L ;
067    
068            private String  value   = null;
069            private boolean useInclude = false;
070    
071            /**
072             * Taglibの開始タグが見つかったときに処ç�?�™ã‚?doStartTag() ã‚?オーバã?ライドしますã?
073             *
074             * @return      後続å?ç�??æŒ?¤º( EVAL_BODY_BUFFERED )
075             */
076            @Override
077            public int doStartTag() {
078                    return( EVAL_BODY_BUFFERED );   // Body を評価するã€? extends BodyTagSupport æ™?
079            }
080    
081            /**
082             * Taglibのタグ本体を処ç�?�™ã‚?doAfterBody() ã‚?オーバã?ライドしますã?
083             *
084             * @og.rev 2.2.0.0 (2002/12/17) 中国èª?国際化)対å¿?エンコードã?取得方法変更
085             * @og.rev 3.0.0.0 (2002/12/25) StringUtil#changeString å»?­¢
086             * @og.rev 3.1.1.0 (2003/03/28) ボディのå†?®¹を取得するå?ç�?‚’、CommonTagSupport で行うã€?
087             * @og.rev 4.0.0.0 (2007/10/12) 処ç�?¸­にエラーを発生させなã�?‚ˆã�?�«しするã?
088             *
089             * @return      後続å?ç�??æŒ?¤º(SKIP_BODY)
090             */
091            @Override
092            public int doAfterBody() {
093                    if( !useInclude && value == null ) {
094                            // 4.0.0.0 (2007/10/12) 処ç�?¸­にエラーを発生させなã�?
095                            try {
096                                    value = getBodyString();
097                            }
098                            catch( HybsSystemException ex ) {       // 主に、UserInfo が見つからなã�??å�?
099                                    value = getBodyContent().getString() ;
100                            }
101    
102                            if( value != null && value.indexOf( "<og:" ) >= 0 ) {
103                                    String errMsg = "こã?タグの BODY部に opengion タグが記述されてã�?�¾すã?"
104                                                            + "BODY部ではopengion タグは使用できませんã€?;
105                                    throw new HybsSystemException( errMsg );
106                            }
107                    }
108    
109                    return(SKIP_BODY);
110            }
111    
112            /**
113             * Taglibの終äº?‚¿グが見つかったときに処ç�?�™ã‚?doEndTag() ã‚?オーバã?ライドしますã?
114             *
115             * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応ã?release2() ã‚?doEndTag()で呼ぶã€?
116             *
117             * @return      後続å?ç�??æŒ?¤º
118             */
119            @Override
120            public int doEndTag() {
121                    debugPrint();           // 4.0.0 (2005/02/28)
122                    jspPrint( value );
123    
124                    return(EVAL_PAGE);
125            }
126    
127            /**
128             * タグリブオブジェクトをリリースしますã?
129             * キャãƒ?‚·ュされて再利用されるã?で、フィールドã?初期設定を行いますã?
130             *
131             * @og.rev 2.0.0.4 (2002/09/27) カスタãƒ?‚¿グの release() メソãƒ?ƒ‰をã?追åŠ?
132             * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応ã?release2() ã‚?doEndTag()で呼ぶã€?
133             *
134             */
135            @Override
136            protected void release2() {
137                    super.release2();
138                    value           = null;
139                    useInclude      = false;
140            }
141    
142            /**
143             * 【TAG】value 値に設定しますã?
144             *
145             * @og.tag
146             * ここで、value に設定したå?合ã?、BODY 部は無視されますã?
147             * なおã?こã?タグでは、エラー発生時でも継続して処ç�?‚’続けられるよã�?�«しますã?
148             * error.jsp などのエラー処ç�?”»面で、このタグを使用するケースがある為ですã?
149             *
150             *  &lt;og:text value="あいã�?�ˆã�? /&gt;
151             *
152             * @og.rev 2.2.0.0 (2002/12/17) 中国èª?国際化)対å¿?エンコードã?取得方法変更
153             * @og.rev 3.0.0.0 (2002/12/25) StringUtil#changeString å»?­¢
154             * @og.rev 4.0.0.0 (2005/12/31) エラー発生時でも異常終äº?�•せずに処ç�?‚’続けますã?
155             *
156             * @param   val 設定å?
157             */
158            public void setValue( final String val ) {
159                    if( !useInclude ) {
160                            try {
161                                    value = getRequestParameter( val );
162                            }
163                            catch( HybsSystemException ex ) {
164                                    value = val ;
165                                    LogWriter.log( "val=" + val + " [" + ex.getMessage() + "]" );
166                            }
167                    }
168            }
169    
170            /**
171             * 【TAG】動çš?�«ファイルã‚?include しますã?
172             *
173             * @og.tag
174             * æŒ?®šã?ファイル名ã?、è?身のãƒ?‚£レクトリからの相対パスで表されますã?
175             *
176             * @og.rev 4.0.0.0 (2007/05/25) 新規追åŠ?
177             *
178             * @param   file ファイルå�?
179             */
180            public void setInclude( final String file ) {
181                    useInclude = true;
182    
183                    String relativePath = getRequestParameter( file );
184                    String resourcePath = getContextRelativePath(getRequest(), relativePath);
185                    String realPath = HybsSystem.url2dir( resourcePath.substring(1) );
186    
187                    FileString fs = new FileString();
188                    fs.setFilename( realPath );
189                    value = fs.getValue();
190            }
191    
192            /**
193             * こã?オブジェクトã?æ–?­—å?表現を返しますã?
194             * 基本çš?�«ãƒ?ƒ�ãƒ?‚°目çš?�«使用しますã?
195             *
196             * @return こã?クラスのæ–?­—å?表現
197             */
198            @Override
199            public String toString() {
200                    return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
201                                    .println( "VERSION"             ,VERSION        )
202                                    .println( "value"               ,value          )
203                                    .println( "Other..."    ,getAttributes().getAttribute() )
204                                    .fixForm().toString() ;
205            }
206    
207            /**
208             * 動的にファイルã‚?include する為の、コンãƒ?‚­ストパスを求めますã?
209             *
210             * æŒ?®šã?ファイル名ã?、è?身のãƒ?‚£レクトリからの相対パスで表されますã?
211             *
212             * @og.rev 4.0.0.0 (2007/05/25) 新規追åŠ?
213             * @og.rev 4.0.0.0 (2007/11/30) if の評価方法を変更しますã?
214             *
215             * @param       request                 ServletRequestオブジェクãƒ?
216             * @param       relativePath    ファイルå�?
217             *
218             * @return      コンãƒ?‚­ストパス
219             */
220            private String getContextRelativePath( final ServletRequest request,
221                                                                                            final String relativePath) {
222                    if(relativePath.startsWith("/")) {
223                            return (relativePath);
224                    }
225                    if(!(request instanceof HttpServletRequest)) {
226                            return (relativePath);
227                    }
228                    HttpServletRequest hrequest = (HttpServletRequest) request;
229                    String uri = (String)request.getAttribute("javax.servlet.include.servlet_path");
230    //              if(uri != null) {
231    //                      String pathInfo = (String)request.getAttribute("javax.servlet.include.path_info");
232    //                      if(pathInfo == null) {
233    //                              if(uri.lastIndexOf('/') >= 0) {
234    //                                      uri = uri.substring(0, uri.lastIndexOf('/'));
235    //                              }
236    //                      }
237    //              }
238                    if( uri != null && uri.lastIndexOf('/') >= 0 ) {
239                            String pathInfo = (String)request.getAttribute("javax.servlet.include.path_info");
240                            if(pathInfo == null) {
241                                    uri = uri.substring(0, uri.lastIndexOf('/'));
242                            }
243                    }
244                    else {
245                            uri = hrequest.getServletPath();
246                            if(uri.lastIndexOf('/') >= 0) {
247                                    uri = uri.substring(0, uri.lastIndexOf('/'));
248                            }
249                    }
250                    return uri + '/' + relativePath;
251            }
252    }