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 import java.util.Map;
022 import java.util.WeakHashMap;
023
024 import org.opengion.fukurou.util.FileUtil;
025 import org.opengion.fukurou.util.Closer;
026 import org.opengion.fukurou.util.LogWriter;
027
028 /**
029 * ã“ã?クラスã¯ã€jspファイルã®XSLT変æ›ã«ç‰¹åŒ–ã—ãŸã?Readerオブジェクトを作æ?ã™ã‚‹ã‚¯ãƒ©ã‚¹ã§ã™ã?
030 * jspファイル ã«è¨˜è¿°ã•れるã?jsp:directive.include を見ã¤ã‘ã¦ã€ãã®ãƒ•ァイル属æ?ã«
031 * 記述ã•れã¦ã?‚‹ãƒ•ァイルをã?インクルードã—ã¾ã™ã?
032 * Tomcat ã®ç‰¹æ€§ä¸Šã?インクルード時ã®ãƒ•ァイルã¯ã€?¼?‰ã?エスケープを処ç?—ã¦ãŠã
033 * å¿?¦ãŒã‚りã¾ã™ã?
034 * <del>(ã¤ã¾ã‚Šã?オリジナルã¯ã€ã‚¨ã‚¹ã‚±ãƒ¼ãƒ—å?ç?ªã—ã§ã€ã‚¤ãƒ³ã‚¯ãƒ«ãƒ¼ãƒ‰ã•れるファイルã¯ã€?
035 * 属æ?部åˆ??ã¿ã€ã‚¨ã‚¹ã‚±ãƒ¼ãƒ—å?ç?Œå¿?¦ã§ã™ã?)</del>
036 * エスケープã?å‰å?ç??ã€jsp:root ã‚¿ã‚°ã®ã‚ã‚‹ãªã—ã§åˆ¤å®šã—ã¾ã™ã?
037 * ç¾æ™‚点ã§ã¯ã€?& , < , <= , > , >= ã‚’å‰å‡¦ç?—ã¾ã™ã?
038 *
039 * <del>ã‚¿ã‚°ã®BODY部ã«è¨˜è¿°ã•れã¦ã?‚‹ ??ãªã©ã‚‚ã?パã?スã®å¯¾è±¡ã«ãªã‚Šã¾ã™ãŒã€?
040 * 擬似çš?«å‡¦ç?—ã¦ã?¾ã™ã?
041 * ãれã¯ã€ã? ã€ã?< ã€ã?<=ã€ã«ã¤ã?¦ã€å?ç?—ã¦ã?¾ã™ã?
042 * ã“ã?形以外ã?処ç??ã€ä»Šã?入れã¦ã?¾ã›ã‚“ã€?/del>
043 *
044 * JSP ã§ã¯ã€og:head ã‚¿ã‚°ã§ã€?lt;html> ã‚’å?力ã—ãŸã‚Šã€htmlend.jsp インクルードã§
045 * </body></html> ã‚’å?力ã—ã¦ã?¾ã™ãŒã€ãƒ•レーãƒ?‚„ã€ãƒ•ォワードãªã©ã€æ•´åˆæ?ã?
046 * å–れãªã?‚±ãƒ¼ã‚¹ãŒã‚りã¾ã™ã?ã§ã€XML処ç?”¨ã¨ã—ã¦ã€?lt;html> ã‚’å?力ã—ã¦ã?¾ã›ã‚“ã€?
047 * 変æ›çµæžœã‚’ã?æ£å¼ãª HTML ファイルã¨ã—ã¦å†åˆ©ç”¨ã•れるå?åˆã?ã€ã”注æ„ãã?•ã??
048 *
049 * ãªãŠã?ã“ã?クラスã¯ã€ã?ルãƒã‚¹ãƒ¬ãƒ?ƒ‰å¯¾å¿œã•れã¦ã?¾ã›ã‚“ã€?
050 *
051 * @og.rev 4.0.0.2 (2007/12/10) æ–°è¦è¿½åŠ?
052 *
053 * @version 4.0
054 * @author Kazuhiko Hasegawa
055 * @since JDK5.0,
056 */
057 public class JspIncludeReader {
058 private static final String CR = System.getProperty("line.separator");
059
060 // 5.6.7.1 (2013/08/09) includeã—ãŸãƒ•ァイルをã‚ャãƒ?‚·ãƒ¥ã—ã¦ãŠãã¾ã™ã?
061 private static final Map<String,String> includeFiles = new WeakHashMap<String,String>();
062
063 // 5.6.7.1 (2013/08/09) ãƒ?ƒãƒ?‚°ç”¨ã«includeã—ãŸãƒ•ァイルをä¿å˜ã—ã¦ãŠãã¾ã™ã?
064 private StringBuilder incFiles = new StringBuilder();
065
066 // ã‚¿ã‚°ã®å±žæ?ã®å€¤ã®ã¿ã‚’抜ãå?ã—ã¦ã?¾ã™ã?特ã«ã€?>& ã‚’å«ã‚??åˆã?
067 // 5.2.1.0 (2010/10/01) ä»®å»?¢
068 // private static final Pattern ptn = Pattern.compile( "=[ \t]*\"([^\"]*[<>&].[^\"]*)\"" );
069
070 /**
071 * JSP ã®ã‚¤ãƒ³ã‚¯ãƒ«ãƒ¼ãƒ‰ã‚’è€??ã—ãŸã€Reader オブジェクトを返ã—ã¾ã™ã?
072 *
073 * @param file File
074 * @param encode String
075 *
076 * @return Reader
077 */
078 // public Reader getReader( final File file,final String encode ) {
079 // return new StringReader( getString( file,encode,false ) );
080 // }
081
082 /**
083 * JSP ã®ã‚¤ãƒ³ã‚¯ãƒ«ãƒ¼ãƒ‰ã‚’è€??ã—ãŸã€JSPファイルをã?String ã§è¿”ã—ã¾ã™ã?
084 * ã“ã?メソãƒ?ƒ‰ã¯ã€å?部ã§å†å¸°å®šç¾©ã•れã¦ã?¾ã™ã?ã¤ã¾ã‚Šã?jsp:directive.include
085 * æ–?—å?ãŒè¦‹ã¤ã‹ã£ãŸå?åˆã?ã€ãã®ä»£ã‚りã«ã€ãƒ•ァイルåã‚’å–å?ã—ã¦ã€ã‚‚ã?¸?º¦
086 * ã“ã?メソãƒ?ƒ‰ã‚’呼ã³å‡ºã—ã¾ã™ã?インクルードファイルã¨ã®é–¢é€£ã‚’ãƒã‚§ãƒ?‚¯ã™ã‚‹ç‚ºã«
087 * ãƒ?ƒŸãƒ¼ã®spanã‚¿ã‚°ã‚’å?れã¦ãŠãã¾ã™ã?
088 * <span type="jsp:directive" include="ファイルå?><!-- --></span>
089 * ãŸã ã—ã?ソースãƒã‚§ãƒ?‚¯æ™‚ã«ã€?
090 * <del>Tomcat ã®ç‰¹æ€§ä¸Šã?インクルード時ã®ãƒ•ァイルã¯ã€?¼?‰ã?エスケープを処ç?—ã¦ãŠã
091 * å¿?¦ãŒã‚りã¾ã™ãŒã€ã‚ªãƒªã‚¸ãƒŠãƒ«ã¯ã€ã‚¨ã‚¹ã‚±ãƒ¼ãƒ—å?ç?—ã¾ã›ã‚“ã€?
092 * ãã?åˆ?‚Šæ›¿ãˆã‚’ã€ç¬¬?“引数㮠isEscape ã§åˆ¶å¾¡ã—ã¦ã?¾ã™ã?</del>
093 * Ver4 以é™ã§ã€ã‚¤ãƒ³ã‚¯ãƒ«ãƒ¼ãƒ‰ãƒ•ァイルã«ã€XML宣è¨?¨ã€jsp:root を付与ã™ã‚‹ã‚±ãƒ¼ã‚¹ãŒã‚りã¾ã™ã?
094 * 擬似çš?«å–り込ã‚?¨ãã«ã¯ã€XML宣è¨??削除ã—ã¾ã™ã?
095 *
096 * @og.rev 5.2.1.0 (2010/10/01) directive.include ã§ã€XMLã‚¿ã‚°ã¨root ã‚¿ã‚°ã¯å–り込ã¾ãªã??
097 * @og.rev 5.2.1.0 (2010/10/01) エスケープå?ç??引数をå»?¢ã—ã¾ã™ã?
098 * @og.rev 5.6.5.2 (2013/06/21) å°ç´°å·¥å†?®¹ã®å¤‰æ›´ã€‚replaceAll ã«ã™ã‚‹ã®ã¨ã€ã‚¹ãƒšã?スã¾ãŸã?タブを使用ã—ã¾ã™ã?
099 * @og.rev 5.6.7.1 (2013/08/09) コメントã?処ç??ãƒã‚°ä¿®æ£ã€‚includeファイルåä¿å˜ã?
100 * @og.rev 5.6.7.1 (2013/08/09) includeファイルãŒå˜åœ¨ã—ãªã??åˆã?ã€gf共有ã‹ã‚‰å–å¾—ã™ã‚‹ã?
101 * @og.rev 5.6.7.2 (2013/08/16) includeファイルをå–り込ã‚??åˆã?代ã‚りã®spanã‚¿ã‚°ã‚’å?力ã—ã¦ãŠãã¾ã™ã?
102 * @og.rev 5.6.7.4 (2013/08/30) includeファイルã®å…ˆé?ã®pageEncodingæŒ?®šã?ãƒã‚§ãƒ?‚¯ç”¨ span ã‚¿ã‚°ã®å‡ºåŠ?
103 *
104 * @param file JSPファイル
105 * @param encode ファイルã®ã‚¨ãƒ³ã‚³ãƒ¼ãƒ?
106 *
107 * @return インクルードをè€??ã—ãŸã€JSPファイル
108 */
109 // public String getString( final File file,final String encode,final boolean isEscape ) {
110 public String getString( final File file,final String encode ) {
111 StringBuilder buf = new StringBuilder() ;
112 BufferedReader reader = FileUtil.getBufferedReader( file,encode );
113
114 // ファイルãŒã?jsp 直下ã‹ã©ã?‹ã‚’判æ–ã—ã¾ã™ã?
115 String parentFile = file.getParent() ;
116 boolean isUnder = parentFile.endsWith( "\\jsp" );
117
118 int cmntIn = -1;
119 int cmntOut = -1;
120 boolean isCmnt = false;
121 boolean isEscape = true; // エスケープã™ã‚‹ã‹ã©ã?‹(true:ã™ã‚‹/false:ã—ãªã?
122 try {
123 String line ;
124 while((line = reader.readLine()) != null) {
125 // 5.2.1.0 (2010/10/01) directive.include ã§ã€XMLã‚¿ã‚°ã¯å–り込ã¾ãªã??
126 if( line.indexOf( "<?xml" ) >= 0 && line.indexOf( "?>" ) >= 0 ) { continue; }
127 // jsp:root ãŒã‚れã?ã€ã‚¨ã‚¹ã‚±ãƒ¼ãƒ—å?ç?‚’行ã‚ãªã?
128 if( line.indexOf( "<jsp:root" ) >= 0 ) { isEscape = false; }
129
130 // コメントã?削除
131 cmntIn = line.indexOf( "<!--" );
132 cmntOut = line.indexOf( "-->" );
133 if( cmntIn >= 0 && cmntOut >= 0 ) {
134 // line = line.substring( 0,cmntIn ) + line.substring( cmntOut );
135 line = line.substring( 0,cmntIn ) + line.substring( cmntOut+3 ); // 5.6.7.1 (2013/08/09) コメントã?処ç??ãƒã‚°ä¿®æ£
136 }
137 else if( cmntIn >= 0 && cmntOut < 0 ) {
138 line = line.substring( 0,cmntIn );
139 isCmnt = true;
140 }
141 else if( cmntIn < 0 && cmntOut >= 0 ) {
142 // line = line.substring( cmntOut );
143 line = line.substring( cmntOut+3 ); // 5.6.7.1 (2013/08/09) コメントã?処ç??ãƒã‚°ä¿®æ£
144 isCmnt = false;
145 }
146 else if( isCmnt && cmntIn < 0 && cmntOut < 0 ) { continue; }
147
148 // 特殊å?ç?¼šog:head ã§ html ã‚¿ã‚°ã‚’å?力ã—ã¦ã?‚‹ã€?
149 // if( line.indexOf( "<og:head" ) >= 0 ) {
150 // buf.append( "<html>" );
151 // }
152
153 if( isEscape ) {
154 // 5.6.5.2 (2013/06/21) å°ç´°å·¥å†?®¹ã®å¤‰æ›´ã€‚replaceAll ã«ã™ã‚‹ã®ã¨ã€ã‚¹ãƒšã?スã¾ãŸã?タブを使用ã—ã¾ã™ã?
155 // & , < , <= , > , >= ã‚’å‰å‡¦ç?—ã¾ã™ã?
156 // line = line.replace( "&" ,"&" ); // ã¡ã‚?£ã¨å°ç´°å·¥
157 // line = line.replace( " < "," < " ); // ã¡ã‚?£ã¨å°ç´°å·¥
158 // line = line.replace( " > "," > " ); // ã¡ã‚?£ã¨å°ç´°å·¥
159 // line = line.replace( " <="," <=" ); // ã¡ã‚?£ã¨å°ç´°å·¥
160 // line = line.replace( " >="," >=" ); // ã¡ã‚?£ã¨å°ç´°å·¥
161 line = line.replaceAll( "&" ,"&" ); // ã¡ã‚?£ã¨å°ç´°å·¥
162 line = line.replaceAll( "[ \\t]<[ \\t]"," < " ); // ã¡ã‚?£ã¨å°ç´°å·¥
163 line = line.replaceAll( "[ \\t]>[ \\t]"," > " ); // ã¡ã‚?£ã¨å°ç´°å·¥
164 line = line.replaceAll( "[ \\t]<="," <=" ); // ã¡ã‚?£ã¨å°ç´°å·¥
165 line = line.replaceAll( "[ \\t]>="," >=" ); // ã¡ã‚?£ã¨å°ç´°å·¥
166 // 5.2.1.0 (2010/10/01) ä»®å»?¢
167 // Matcher mtch = ptn.matcher( line );
168 // int adrs = 0;
169 // StringBuilder buf2 = new StringBuilder();
170 // while( mtch.find(adrs) ) {
171 // String grp = mtch.group(1);
172 // String htm = StringUtil.htmlFilter( grp );
173 // int in = mtch.start(1);
174 // buf2.append( line.substring( adrs,in ) ).append( htm );
175 // adrs = mtch.end(1);
176 // }
177 // buf2.append( line.substring( adrs ) );
178 // line = buf2.toString();
179 }
180
181 int st = line.indexOf( "<jsp:directive.include" );
182 if( st < 0 ) { buf.append( line ); } // include ãŒç„¡ã‘れã°ã€ãã®ã¾ã¾è¿½åŠ?
183 else {
184 buf.append( line.substring( 0,st ) );
185 int fin = line.indexOf( '\"',st ); // ãƒ•ã‚¡ã‚¤ãƒ«ã®æœ??
186 int fout= line.indexOf( '\"',fin+1 ); // ãƒ•ã‚¡ã‚¤ãƒ«ã®æœ?¾?
187 String fname = line.substring( fin+1,fout ); // ファイルå?
188
189 // 5.6.7.2 (2013/08/16) includeファイルをå–り込ã‚??åˆã?代ã‚りã®spanã‚¿ã‚°ã‚’å?力ã—ã¦ãŠãã¾ã™ã?
190 buf.append( "<span type=\"jsp:directive\"" )
191 .append( " include=\"" ).append( fname ).append( "\" ><!-- --></span>" ) ;
192
193 // htmlend.jsp 㮠インクルードã?行ã‚ãªã??
194 if( fname.endsWith( "htmlend.jsp" ) ) {
195 if( buf.indexOf( "<body" ) >= 0 && buf.indexOf( "</body>" ) < 0 ) {
196 buf.append( "</body>" );
197 }
198
199 // if( buf.indexOf( "<html" ) >= 0 ) {
200 // buf.append( "</html>" );
201 // }
202 }
203 else {
204 // 5.6.7.1 (2013/08/09) ãƒ?ƒãƒ?‚°ç”¨ã«includeã—ãŸãƒ•ァイルをä¿å˜ã—ã¦ãŠãã¾ã™ã?
205 if( incFiles.length() > 0 ) { incFiles.append( " , " ); }
206 incFiles.append( fname );
207
208 // 5.6.7.1 (2013/08/09) includeã—ãŸãƒ•ァイルをã‚ャãƒ?‚·ãƒ¥ã‹ã‚‰æ¤œç´¢ã—ã¾ã™ã?
209 String fileData = includeFiles.get( fname ); // ã‚ャãƒ?‚·ãƒ¥ã‚’検索(fname ãŒã‚ー)
210 if( fileData == null ) {
211 // ã¡ã‚?£ã¨å°ç´°å·¥
212 String fname2 = fname ;
213 // include ã™ã‚‹ãƒ•ァイルã¯ã€?jsp/ ã‹ã‚‰ã®çµ¶å¯¾ãƒ‘スã€?
214 // jsp 直下ã?å ´åˆã?ã€?/ ã€ãれ以外ã?ã€?./ ã¨ç½®ãæ›ãˆã¾ã™ã?
215 if( isUnder ) { fname2 = fname2.replace( "/jsp/","./" ); }
216 else { fname2 = fname2.replace( "/jsp/","../" ); }
217 // 5.6.7.1 (2013/08/09) includeファイルãŒå˜åœ¨ã—ãªã??åˆã?ã€gf共有ã‹ã‚‰å–å¾—ã™ã‚‹ã?
218 File newfile = new File( parentFile,fname2 );
219 if( !newfile.exists() ) {
220 if( fname2.contains( "/common/" ) || fname2.contains( "/menu/" ) ) {
221 // 本当ã? classPathã‹ã‚‰ã€å–å¾—ã™ã¹ãã?
222 // 今ã?ã€å®Ÿè¡Œç’°å¢??相対パスã®ä½ç½®ã«ã€gf/jsp/common,menu ã®ãƒ•ァイルãŒå¿?¦ã?
223 fname2 = ( isUnder )
224 ? "./../../gf/jsp/" + fname2.substring( 2 )
225 : "../../../gf/jsp/" + fname2.substring( 3 ) ;
226 newfile = new File( parentFile,fname2 ); // ã“ã“ã§ãªã‘れã°ã€ã‚¨ãƒ©ãƒ¼ã«ãªã‚‹ã?
227 }
228 }
229 fileData = getString( newfile,encode );
230 // 5.6.7.4 (2013/08/30) includeファイルã®å…ˆé?ã®pageEncodingæŒ?®šã?ãƒã‚§ãƒ?‚¯ç”¨ span ã‚¿ã‚°ã®å‡ºåŠ?
231 // インクルードファイルã®å…ˆé?ã«ã¯ã€pageEncoding="UTF-8" 宣è¨?Œå¿?¦?UTF-8ã‹ã©ã?‹ã¯æœªãƒã‚§ãƒ?‚¯)
232 if( ! fileData.startsWith( "<jsp:directive.page pageEncoding" ) ) {
233 // ãƒã‚§ãƒ?‚¯ç”¨ã®spanã‚¿ã‚°ã‚’å?力ã—ã¦ãŠãã¾ã™ã?
234 buf.append( "<span type=\"jsp:directive\"" )
235 .append( " pageEncoding=\"non\" file=\"" ).append( fname ).append( "\" ><!-- --></span>" ) ;
236 }
237
238 // 5.6.7.1 (2013/08/09) includeã—ãŸãƒ•ァイルをã‚ャãƒ?‚·ãƒ¥ã—ã¦ãŠãã¾ã™ã?
239 includeFiles.put( fname,fileData ); // includeファイルをã‚ャãƒ?‚·ãƒ¥(fname ãŒã‚ー)
240 }
241
242 // buf.append( getString( newfile,encode ) );
243 buf.append( fileData );
244 }
245 int tagout = line.indexOf( "/>",fout+1 ); // ã‚¿ã‚°ã®æœ?¾?
246
247 buf.append( line.substring( tagout+2 ) );
248 }
249
250 // og:commonForward を見ã¤ã‘ãŸå ´åˆã?ã€æœ€å¾Œã« html ã‚¿ã‚°ã‚’å?力ã™ã‚‹ã?
251 // if( line.indexOf( "<og:commonForward" ) >= 0 ) {
252 // buf.append( "</html>" );
253 // }
254
255 buf.append( CR );
256 }
257 }
258 catch( IOException ex ) {
259 LogWriter.log( ex );
260 }
261 finally {
262 Closer.ioClose( reader );
263 }
264 return buf.toString();
265 }
266
267 /**
268 * インクルードã—ãŸãƒ•ァイルå?相対パス)ã®ãƒªã‚¹ãƒˆæ–‡å—å?ã‚’è¿”ã—ã¾ã™ã?
269 * 通常ã¯ã€XSLT変æ›å‡¦ç?§ã‚¨ãƒ©ãƒ¼ãŒç™ºç”Ÿã—ãŸå?åˆã?ã€includeãƒ•ã‚¡ã‚¤ãƒ«ã®æ•´åˆæ?ã?
270 * ãŠã‹ã—ã„å ´åˆãŒå¤šã„ã®ã§ã€ãƒ‡ãƒãƒƒã‚°æƒ??ã¨ã—ã¦åˆ©ç”¨ã—ã¾ã™ã?
271 * ãŸã ã—ã?エラー発生時ã®ä½ç½®ç‰¹å®šã¾ã§ã¯ã§ãã¾ã›ã‚“ã€?
272 *
273 * ã“ã?å†?ƒ¨å¤‰æ•°ã¯ã€ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹å¤‰æ•°ã§ã™ã?ã§ã€includeファイルã®ã‚ャãƒ?‚·ãƒ¥ã¨ã¯å¯¿å‘½ãŒç•°ãªã‚Šã¾ã™ã?
274 *
275 * @og.rev 5.6.7.1 (2013/08/09) æ–°è¦è¿½åŠ?
276 *
277 * @return includeファイルåã?リスト文å—å?
278 */
279 public String getIncludeFiles() {
280 return incFiles.toString();
281 }
282
283 /**
284 * インクルードã—ãŸãƒ•ァイルã®ã‚ャãƒ?‚·ãƒ¥ã‚’クリアã—ã¾ã™ã?
285 * ã‚ャãƒ?‚·ãƒ¥ã¯ã€ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã§ã¯ãªãã?スタãƒ?‚£ãƒ?‚¯å¤‰æ•°ã§ç®¡ç?—ã¦ã?¾ã™ã?
286 * よã£ã¦ã€ä¸??ã®å‡¦ç??åˆã‚ã¨æœ?¾Œã«ã‚¯ãƒªã‚¢ã—ã¦ãŠã„ã¦ãã ã•ã„ã€?
287 *
288 * @og.rev 5.6.7.1 (2013/08/09) æ–°è¦è¿½åŠ?
289 */
290 public static void cacheClear() {
291 includeFiles.clear();
292 }
293
294 /**
295 * ãƒ?‚¹ãƒˆç”¨ã® main メソãƒ?ƒ‰
296 *
297 * @param args コマンド引数é…å?
298 */
299 public static void main( final String[] args ) {
300 JspIncludeReader reader = new JspIncludeReader();
301 // String xml = reader.getString( new File( args[0] ),"UTF-8",false );
302 String xml = reader.getString( new File( args[0] ),"UTF-8" );
303
304 if( args.length > 1 ) {
305 java.io.PrintWriter writer = FileUtil.getPrintWriter( new File( args[1] ),"UTF-8" );
306 writer.print( xml );
307 Closer.ioClose( writer );
308 }
309 else {
310 System.out.println( xml );
311 }
312 }
313 }