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.fukurou.xml;
017
018 import java.io.File;
019 import java.io.IOException;
020 import java.io.BufferedReader;
021
022 import org.opengion.fukurou.util.FileUtil;
023 import org.opengion.fukurou.util.Closer;
024 import org.opengion.fukurou.util.LogWriter;
025
026 /**
027 * こã?クラスは、jspファイルのXSLT変換に特化したã?Readerオブジェクトを作æ?するクラスですã?
028 * jspファイル に記述されるã?jsp:directive.include を見つけて、そのファイル属æ?に
029 * 記述されてã�?‚‹ファイルをã?インクルードしますã?
030 * Tomcat の特性上ã?インクルード時のファイルはã€?¼?‰ã?エスケープを処ç�?�—ておく
031 * å¿?¦�がありますã?
032 * <del>(つまりã?オリジナルは、エスケープå?ç�?�ªしで、インクルードされるファイルはã€?
033 * 属æ?部åˆ??み、エスケープå?ç�?�Œå¿?¦�ですã?)</del>
034 * エスケープã?前å?ç�??、jsp:root タグのあるなしで判定しますã?
035 * 現時点ではã€?& , < , <= , > , >= を前処ç�?�—ますã?
036 *
037 * <del>タグのBODY部に記述されてã�?‚‹ ??などもã?パã?スの対象になりますがã€?
038 * 擬似çš?�«処ç�?�—てã�?�¾すã?
039 * それは、ã? 』ã?< 』ã?<=』につã�?�¦、å?ç�?�—てã�?�¾すã?
040 * こã?形以外ã?処ç�??、今ã?入れてã�?�¾せんã€?/del>
041 *
042 * JSP では、og:head タグでã€?html> をå?力したり、htmlend.jsp インクルードで
043 * </body></html> をå?力してã�?�¾すが、フレーãƒ?‚„、フォワードなど、整合æ?が取れなã�?
044 * ケースがありますã?で、XML処ç�?”¨としてã€?html> をå?力してã�?�¾せんã€?
045 * 変換結果をã?正式な HTML ファイルとして再利用されるå?合ã?、ご注意くã�?�•ã�??
046 *
047 * @og.rev 4.0.0.2 (2007/12/10) 新規追åŠ?
048 *
049 * @version 4.0
050 * @author Kazuhiko Hasegawa
051 * @since JDK5.0,
052 */
053 public class JspIncludeReader {
054 private static final String CR = System.getProperty("line.separator");
055
056 // タグの属æ?の値のみを抜きå?してã�?�¾すã?特にã€?>& を含ã‚??合ã?
057 // 5.2.1.0 (2010/10/01) 仮å»?¢
058 // private static final Pattern ptn = Pattern.compile( "=[ \t]*\"([^\"]*[<>&].[^\"]*)\"" );
059
060 /**
061 * JSP のインクルードをè€??した、Reader オブジェクトを返しますã?
062 *
063 * @param file File
064 * @param encode String
065 *
066 * @return Reader
067 */
068 // public Reader getReader( final File file,final String encode ) {
069 // return new StringReader( getString( file,encode,false ) );
070 // }
071
072 /**
073 * JSP のインクルードをè€??した、JSPファイルをã?String で返しますã?
074 * こã?メソãƒ?ƒ‰は、å?部で再帰定義されてã�?�¾すã?つまりã?jsp:directive.include
075 * æ–?—å?が見つかったå?合ã?、その代わりに、ファイル名を取å?して、もã�?¸?º¦
076 * こã?メソãƒ?ƒ‰を呼び出しますã?
077 * <del>Tomcat の特性上ã?インクルード時のファイルはã€?¼?‰ã?エスケープを処ç�?�—ておく
078 * å¿?¦�がありますが、オリジナルは、エスケープå?ç�?�—ませんã€?
079 * そã?åˆ?‚Š替えを、第?“引数の isEscape で制御してã�?�¾すã?</del>
080 * Ver4 以降で、インクルードファイルに、XML宣è¨?�¨、jsp:root を付与するケースがありますã?
081 * 擬似çš?�«取り込ã‚?�¨きには、XML宣è¨??削除しますã?
082 *
083 * @og.rev 5.2.1.0 (2010/10/01) directive.include で、XMLタグとroot タグは取り込まなã�??
084 * @og.rev 5.2.1.0 (2010/10/01) エスケープå?ç�??引数をå»?¢しますã?
085 * @og.rev 5.6.5.2 (2013/06/21) 小細工å†?®¹の変更。replaceAll にするのと、スペã?スまたã?タブを使用しますã?
086 *
087 * @param file JSPファイル
088 * @param encode ファイルのエンコーãƒ?
089 *
090 * @return インクルードをè€??した、JSPファイル
091 */
092 // public String getString( final File file,final String encode,final boolean isEscape ) {
093 public String getString( final File file,final String encode ) {
094 StringBuilder buf = new StringBuilder() ;
095 BufferedReader reader = FileUtil.getBufferedReader( file,encode );
096
097 // ファイルがã?jsp 直下かどã�?�‹を判断しますã?
098 String parentFile = file.getParent() ;
099 boolean isUnder = parentFile.endsWith( "\\jsp" );
100
101 int cmntIn = -1;
102 int cmntOut = -1;
103 boolean isCmnt = false;
104 boolean isEscape = true; // エスケープするかどã�?�‹(true:する/false:しなã�?
105 try {
106 String line ;
107 while((line = reader.readLine()) != null) {
108 // 5.2.1.0 (2010/10/01) directive.include で、XMLタグは取り込まなã�??
109 if( line.indexOf( "<?xml" ) >= 0 && line.indexOf( "?>" ) >= 0 ) { continue; }
110 // jsp:root があれã?、エスケープå?ç�?‚’行わなã�?
111 if( line.indexOf( "<jsp:root" ) >= 0 ) { isEscape = false; }
112
113 // コメントã?削除
114 cmntIn = line.indexOf( "<!--" );
115 cmntOut = line.indexOf( "-->" );
116 if( cmntIn >= 0 && cmntOut >= 0 ) {
117 line = line.substring( 0,cmntIn ) + line.substring( cmntOut );
118 }
119 else if( cmntIn >= 0 && cmntOut < 0 ) {
120 line = line.substring( 0,cmntIn );
121 isCmnt = true;
122 }
123 else if( cmntIn < 0 && cmntOut >= 0 ) {
124 line = line.substring( cmntOut );
125 isCmnt = false;
126 }
127 else if( isCmnt && cmntIn < 0 && cmntOut < 0 ) { continue; }
128
129 // 特殊å?ç�?¼šog:head で html タグをå?力してã�?‚‹ã€?
130 // if( line.indexOf( "<og:head" ) >= 0 ) {
131 // buf.append( "<html>" );
132 // }
133
134 if( isEscape ) {
135 // 5.6.5.2 (2013/06/21) 小細工å†?®¹の変更。replaceAll にするのと、スペã?スまたã?タブを使用しますã?
136 // & , < , <= , > , >= を前処ç�?�—ますã?
137 // line = line.replace( "&" ,"&" ); // ちã‚?�£と小細工
138 // line = line.replace( " < "," < " ); // ちã‚?�£と小細工
139 // line = line.replace( " > "," > " ); // ちã‚?�£と小細工
140 // line = line.replace( " <="," <=" ); // ちã‚?�£と小細工
141 // line = line.replace( " >="," >=" ); // ちã‚?�£と小細工
142 line = line.replaceAll( "&" ,"&" ); // ちã‚?�£と小細工
143 line = line.replaceAll( "[ \\t]<[ \\t]"," < " ); // ちã‚?�£と小細工
144 line = line.replaceAll( "[ \\t]>[ \\t]"," > " ); // ちã‚?�£と小細工
145 line = line.replaceAll( "[ \\t]<="," <=" ); // ちã‚?�£と小細工
146 line = line.replaceAll( "[ \\t]>="," >=" ); // ちã‚?�£と小細工
147 // 5.2.1.0 (2010/10/01) 仮å»?¢
148 // Matcher mtch = ptn.matcher( line );
149 // int adrs = 0;
150 // StringBuilder buf2 = new StringBuilder();
151 // while( mtch.find(adrs) ) {
152 // String grp = mtch.group(1);
153 // String htm = StringUtil.htmlFilter( grp );
154 // int in = mtch.start(1);
155 // buf2.append( line.substring( adrs,in ) ).append( htm );
156 // adrs = mtch.end(1);
157 // }
158 // buf2.append( line.substring( adrs ) );
159 // line = buf2.toString();
160 }
161
162 int st = line.indexOf( "<jsp:directive.include" );
163 if( st < 0 ) { buf.append( line ); } // include が無ければ、そのまま追åŠ?
164 else {
165 buf.append( line.substring( 0,st ) );
166 int fin = line.indexOf( '\"',st ); // ファイルのæœ??
167 int fout= line.indexOf( '\"',fin+1 ); // ファイルのæœ?¾?
168 String fname = line.substring( fin+1,fout ); // ファイルå�?
169
170 // htmlend.jsp の インクルードã?行わなã�??
171 if( fname.endsWith( "htmlend.jsp" ) ) {
172 if( buf.indexOf( "<body" ) >= 0 && buf.indexOf( "</body>" ) < 0 ) {
173 buf.append( "</body>" );
174 }
175
176 // if( buf.indexOf( "<html" ) >= 0 ) {
177 // buf.append( "</html>" );
178 // }
179 }
180 else {
181 // ちã‚?�£と小細工
182 // include するファイルはã€?jsp/ からの絶対パスã€?
183 // jsp 直下ã?場合ã?ã€?/ 、それ以外ã?ã€?./ と置き換えますã?
184 if( isUnder ) { fname = fname.replace( "/jsp/","./" ); }
185 else { fname = fname.replace( "/jsp/","../" ); }
186 File newfile = new File( parentFile,fname );
187 buf.append( getString( newfile,encode ) );
188 }
189 int tagout = line.indexOf( "/>",fout+1 ); // タグのæœ?¾?
190
191 buf.append( line.substring( tagout+2 ) );
192 }
193
194 // og:commonForward を見つけた場合ã?、最後に html タグをå?力するã?
195 // if( line.indexOf( "<og:commonForward" ) >= 0 ) {
196 // buf.append( "</html>" );
197 // }
198
199 buf.append( CR );
200 }
201 }
202 catch( IOException ex ) {
203 LogWriter.log( ex );
204 }
205 finally {
206 Closer.ioClose( reader );
207 }
208 return buf.toString();
209 }
210
211 /**
212 * ãƒ?‚¹ト用の main メソãƒ?ƒ‰
213 *
214 * @param args コマンド引数配å?
215 */
216 public static void main( final String[] args ) {
217 JspIncludeReader reader = new JspIncludeReader();
218 // String xml = reader.getString( new File( args[0] ),"UTF-8",false );
219 String xml = reader.getString( new File( args[0] ),"UTF-8" );
220
221 if( args.length > 1 ) {
222 java.io.PrintWriter writer = FileUtil.getPrintWriter( new File( args[1] ),"UTF-8" );
223 writer.print( xml );
224 Closer.ioClose( writer );
225 }
226 else {
227 System.out.println( xml );
228 }
229 }
230 }