1 18 package com.teamkonzept.lib; 19 20 import java.io.*; 21 import com.oroinc.text.regex.*; 22 import javax.servlet.http.HttpServletRequest ; 23 import com.teamkonzept.lib.TKException; 24 import com.teamkonzept.lib.templates.*; 25 import org.apache.log4j.Category; 26 27 33 public class TKTemplate implements TemplateBasic, TemplateTypes 34 { 35 private static final Category CAT = Category.getInstance(TKTemplate.class); 36 37 static protected final String PAT_PRETAG = "TK[_:]"; 38 static protected final String PAT_REFTAG = "(SRC|HREF|ACTION|BACKGROUND)"; 39 40 static Pattern patCopyRight; 41 static Pattern patCleanEmpty; 42 static Pattern patBaseURL; 43 static public Pattern patTKTag; 45 static boolean initialized = false; 46 47 private static String NEW_LINE = System.getProperty("line.separator"); 48 49 private String currAppName = "TKTemplate"; 50 51 52 public TKTemplateSyntax syntax = null; 53 54 55 public TKTemplateData td = null; 56 57 58 public String text; 59 60 64 public TKTemplateSyntax newSyntax() 65 { 66 return new TKTemplateSyntax(); 67 } 68 69 73 public TKTemplateSyntax newSyntax (Pattern patTKTag) 74 { 75 return new TKTemplateSyntax (patTKTag); 76 } 77 78 public String getType() 79 { 80 return TK_TEMPLATE; 81 } 82 83 89 public TKTemplate( String location ) throws TKTemplateSyntaxException, FileNotFoundException 90 { 91 init( location ); 92 } 93 94 102 public TKTemplate( TKTemplateSyntax ts ) 103 { 104 init( ts ); 105 } 106 107 public DOMTemplateData getDOMData() 108 { 109 return td.getDOMData(); 110 } 111 112 protected void init( TKTemplateSyntax ts ) 113 { 114 if( !initialized ) initStatic(); syntax = ts; 116 td = ts.getNewTemplateData(); 117 } 118 119 protected void init( String location ) throws TKTemplateSyntaxException, FileNotFoundException 120 { 121 init( TKTemplateCache.getSyntax( location, this ) ); 122 } 123 124 125 132 public void setListIterator(TKListIterator aIterator){ 133 td.setListIterator(aIterator); 134 } 135 136 143 public TKListIterator getListIterator(){ 144 return td.getListIterator(); 145 } 146 public void setOriginalRequest(HttpServletRequest request) 148 { 149 } 150 151 159 160 public void setEnumerationContext( Object key, Object value ) 161 { 162 td.setEnumerationContext( key, value ); 163 } 164 165 168 169 public Object getEnumerationContext( Object key ) 170 { 171 return td.getEnumerationContext( key ); 172 } 173 174 175 176 183 public void set( TKHashtable aSubst ) 184 { 185 td.mergeVariables( aSubst ); 186 } 187 188 195 public void set( String key, Object val ) 196 { 197 td.setLocalVariable( key, val ); 198 } 199 200 209 public void add( TKHashtable aSubst ) 210 { 211 td.concatVariables( aSubst ); 212 } 213 214 224 public void add( String key, Object val ) 225 { 226 td.extendVariable( key, val ); 227 } 228 229 242 243 public String getText() 244 { 245 return text; 246 } 247 248 253 public String setAppName( String appName ) 254 { 255 return currAppName = appName; 256 } 257 258 269 public void printTemplate( PrintStream out ){ 270 out.print(text); 271 } 272 273 284 public void printTemplate( Writer writer ) throws IOException 285 { 286 writer.write(text); 287 } 288 289 300 public void printTemplate() 301 { 302 printTemplate( System.out ); 303 } 304 305 318 public void printTemplate(String file) throws IOException { 319 FileOutputStream f = new FileOutputStream( file); 320 PrintStream out = new PrintStream(f); 321 322 printTemplate( out ); 323 } 324 325 334 public void doTagSubstitution() throws TKTemplateSyntaxException 335 { 336 doMainSubstitutions(); 337 } 338 339 347 public void doMainSubstitutions() throws TKTemplateSyntaxException 348 { 349 td.prepareApply(); 350 351 355 try { 357 StringWriter writer = new StringWriter(); 358 syntax.apply( td, writer ); 359 text = writer.toString(); 360 } 361 catch (IOException e) 362 { 363 CAT.error("Catched IOException in TKTemplate.doMainSubstition() thrown by StringWriter", e); 364 } 365 } 366 367 376 public void doCleanup(){ 377 text = Util.substitute( TKReg.getMatcher(), patCleanEmpty, NEW_LINE, text, Util.SUBSTITUTE_ALL ); 378 } 379 380 384 static void initStatic() { 385 if( initialized ) return; 386 387 PatternCompiler compiler = TKReg.getCompiler(); 388 389 try { 390 patCopyRight = compiler.compile( "<HTML>", Perl5Compiler.CASE_INSENSITIVE_MASK ); 391 patBaseURL = compiler.compile(" "+PAT_REFTAG+"\\s*\\=\\s*\"(?!(/|#|\\w+?(:|:)))", Perl5Compiler.CASE_INSENSITIVE_MASK); 392 patTKTag = compiler.compile("</?"+PAT_PRETAG, Perl5Compiler.CASE_INSENSITIVE_MASK); 393 patCleanEmpty = compiler.compile(NEW_LINE + "\\s*" + NEW_LINE); 394 } catch(MalformedPatternException e) { 395 CAT.error("Bad pattern.", e); 396 } 397 398 initialized = true; 399 } 400 401 static { 402 initStatic(); 403 } 404 } 405 406 407 | Popular Tags |