1 package com.teamkonzept.lib.templates; 2 3 import java.io.*; 4 5 import com.teamkonzept.lib.*; 6 7 12 public class TKTemplateCache { 13 14 15 protected static TKHashtable syntaxes = new TKHashtable(); 16 17 18 protected static TKHashtable dates = new TKHashtable(); 19 20 21 protected static TKClassRegistry loaderRegistry = new TKClassRegistry(); 22 23 24 protected static TKHashtable loaders = new TKHashtable(); 25 26 static { 27 loaderRegistry.registerClass( TKTemplateFileLoader.CLASS_ID, 28 "com.teamkonzept.lib.templates.TKTemplateFileLoader" ); 29 } 30 31 protected static synchronized TKTemplateSourceLoader getLoader( String classId ) 32 { 33 TKTemplateSourceLoader loader = (TKTemplateSourceLoader) loaders.get( classId ); 34 if( loader == null ) { 35 try { 36 loader = (TKTemplateSourceLoader) loaderRegistry.get( classId ); 37 } 38 catch( Exception e ) { 39 throw new IncompatibleClassChangeError ( e.toString() ); 40 } 41 loaders.put( classId, loader ); 42 } 43 return loader; 44 } 45 46 public static final String loaderClassId( String location, String parentLocation ) 47 { 48 int loaderEnd = location.indexOf( ':' ); 49 return ( 50 loaderEnd < 0 51 ? parentLocation.substring( 0, parentLocation.indexOf(':') ) 52 : location.substring( 0, loaderEnd ) 53 ); 54 } 55 56 public static final boolean exists( String location, String parentLocation ) 57 { 58 location = location.replace ('/',File.separatorChar); 59 String loaderClassId = loaderClassId( location, parentLocation ); 60 61 TKTemplateSourceLoader loader = getLoader( loaderClassId ); 62 return loader.exists( loader.childLocation( location, parentLocation ) ); 63 } 64 65 68 public static String getSource(String location) throws IOException 69 { 70 location = location.replace ('/',File.separatorChar); 71 String loaderClassId = loaderClassId( location, "" ); 72 73 TKTemplateSourceLoader loader = getLoader( loaderClassId ); 74 75 String newLocation = loader.childLocation( location, "" ); 76 return loader.loadSource(newLocation); 77 } 78 79 87 public static synchronized TKTemplateSyntax getSyntax( String location, TKTemplateSyntax parent ) throws TKTemplateSyntaxException 88 { 89 location = location.replace ('/',File.separatorChar); 90 String parentLocation = ( parent == null ? "" : parent.getSource() ); String loaderClassId = loaderClassId( location, parentLocation ); 92 93 TKTemplateSourceLoader loader = getLoader( loaderClassId ); 94 95 String newLocation = loader.childLocation( location, parentLocation ); 96 97 101 long fileModified = loader.lastModified( newLocation ); 102 Long cacheModified = (Long ) dates.get( newLocation ); 103 104 if( cacheModified != null && cacheModified.longValue() == fileModified ) { 106 return (TKTemplateSyntax) syntaxes.get( newLocation ); 107 } 108 109 TKTemplateSyntax syntax = parent.newChild( loader.loadSource( newLocation ), newLocation ); 112 113 if( syntaxes.get( parentLocation ) != null ) { 115 syntaxes.put( newLocation, syntax ); 116 dates.put( newLocation, new Long ( fileModified ) ); 117 } 118 return syntax; 119 } 120 121 129 public static synchronized TKTemplateSyntax getSyntax( String location, TKTemplate template ) 130 throws TKTemplateSyntaxException, FileNotFoundException 131 { 132 location = location.replace ('/',File.separatorChar); 133 String loaderClassId = location.substring( 0, location.indexOf(':') ); 134 135 TKTemplateSourceLoader loader = getLoader( loaderClassId ); 136 137 String newLocation = loader.childLocation( location, "" ); 138 139 142 long fileModified = loader.lastModified( newLocation ); 143 Long cacheModified = (Long ) dates.get( newLocation ); 144 145 if( cacheModified != null && cacheModified.longValue() == fileModified ) { 147 return (TKTemplateSyntax) syntaxes.get( newLocation ); 148 } 149 150 153 TKTemplateSyntax syntax = template.syntax == null ? 154 template.newSyntax() : template.newSyntax (template.syntax.getTKTag()); 155 156 if (!loader.exists( newLocation )) { 157 throw new java.io.FileNotFoundException (newLocation); 158 } 159 160 syntax.init( loader.loadSource( newLocation ), newLocation ); 161 162 syntaxes.put( newLocation, syntax ); 163 dates.put( newLocation, new Long ( fileModified ) ); 164 return syntax; 165 } 166 167 public static void loadSyntax( String location, TKTemplateSyntax syntax ) throws TKTemplateSyntaxException 168 { 169 location = location.replace ('/',File.separatorChar); 170 String loaderClassId = location.substring( 0, location.indexOf(':') ); 171 172 TKTemplateSourceLoader loader = getLoader( loaderClassId ); 173 174 String newLocation = loader.childLocation( location, "" ); 175 176 syntax.init( loader.loadSource( newLocation ), newLocation ); 177 } 178 179 } 180 181 182 | Popular Tags |