KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > lib > templates > TKTemplateCache


1 package com.teamkonzept.lib.templates;
2
3 import java.io.*;
4
5 import com.teamkonzept.lib.*;
6
7 /**
8  * Falls noch kein TKTemplateSyntax-Objekt besteht, wird dieses erzeugt
9  * @author $Author: alex $
10  * @version $Revision: 1.11 $
11  */

12 public class TKTemplateCache {
13
14     /** die gecacheten Syntaxobjekte */
15     protected static TKHashtable syntaxes = new TKHashtable();
16     
17     /** Modifizierungsdaten der Templates */
18     protected static TKHashtable dates = new TKHashtable();
19     
20     /** die Class Objekte der Loader */
21     protected static TKClassRegistry loaderRegistry = new TKClassRegistry();
22     
23     /** die Instanzen der Loader */
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 JavaDoc 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 JavaDoc e ) {
39                 throw new IncompatibleClassChangeError JavaDoc( e.toString() );
40             }
41             loaders.put( classId, loader );
42         }
43         return loader;
44     }
45
46     public static final String JavaDoc loaderClassId( String JavaDoc location, String JavaDoc 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 JavaDoc location, String JavaDoc parentLocation )
57     {
58         location = location.replace ('/',File.separatorChar);
59         String JavaDoc loaderClassId = loaderClassId( location, parentLocation );
60         
61         TKTemplateSourceLoader loader = getLoader( loaderClassId );
62         return loader.exists( loader.childLocation( location, parentLocation ) );
63     }
64     
65     /**
66         liefert Source Text zurueck
67     */

68     public static String JavaDoc getSource(String JavaDoc location) throws IOException
69     {
70         location = location.replace ('/',File.separatorChar);
71         String JavaDoc loaderClassId = loaderClassId( location, "" );
72         
73         TKTemplateSourceLoader loader = getLoader( loaderClassId );
74         
75         String JavaDoc newLocation = loader.childLocation( location, "" );
76         return loader.loadSource(newLocation);
77     }
78     
79     /**
80      * Falls noch kein Syntaxbaum fuer ein Template erzeugt wurde, wird dieser
81      * aufgebaut. Der Vater eines Syntaxobjektes mit seinen Kindern wird
82      * zurueckgegeben
83      *
84      * @param File templateFile, ein Template als File-Objekt
85      * @return die Syntax eines Templates
86      */

87     public static synchronized TKTemplateSyntax getSyntax( String JavaDoc location, TKTemplateSyntax parent ) throws TKTemplateSyntaxException
88     {
89         location = location.replace ('/',File.separatorChar);
90         String JavaDoc parentLocation = ( parent == null ? "" : parent.getSource() ); // Abfrage auf null ueberfluessig!
91
String JavaDoc loaderClassId = loaderClassId( location, parentLocation );
92         
93         TKTemplateSourceLoader loader = getLoader( loaderClassId );
94         
95         String JavaDoc newLocation = loader.childLocation( location, parentLocation );
96                 
97         // TKLog.println ("TKTemplateCache.getSyntax/parentsyntax, location="+
98
// location+", loaderClassId="+loaderClassId+", newLocation="+
99
// newLocation+", parentLocation="+parentLocation);
100

101         long fileModified = loader.lastModified( newLocation );
102         Long JavaDoc cacheModified = (Long JavaDoc) dates.get( newLocation );
103     
104         //Ein TKTemplateSyntax-Objekt besteht bereits
105
if( cacheModified != null && cacheModified.longValue() == fileModified ) {
106             return (TKTemplateSyntax) syntaxes.get( newLocation );
107         }
108         
109         //Ein TKTemplateSyntax-Objekt wird erzeugt
110
//TKLib.slurpfile(templateFile) => das File wird in String umgewandelt
111
TKTemplateSyntax syntax = parent.newChild( loader.loadSource( newLocation ), newLocation );
112     
113         //Nur wenn der Vater schon im Cache war, kommt auch der Sohn rein!
114
if( syntaxes.get( parentLocation ) != null ) {
115             syntaxes.put( newLocation, syntax );
116             dates.put( newLocation, new Long JavaDoc( fileModified ) );
117         }
118         return syntax;
119     }
120     
121     /**
122      * Falls noch kein Syntaxbaum fuer ein Template erzeugt wurde, wird dieser
123      * aufgebaut. Der Vater eines Syntaxobjektes mit seinen Kindern wird
124      * zurueckgegeben
125      *
126      * @param File templateFile, ein Template als File-Objekt
127      * @return die Syntax eines Templates
128      */

129     public static synchronized TKTemplateSyntax getSyntax( String JavaDoc location, TKTemplate template )
130         throws TKTemplateSyntaxException, FileNotFoundException
131     {
132         location = location.replace ('/',File.separatorChar);
133         String JavaDoc loaderClassId = location.substring( 0, location.indexOf(':') );
134         
135         TKTemplateSourceLoader loader = getLoader( loaderClassId );
136         
137         String JavaDoc newLocation = loader.childLocation( location, "" );
138         
139         // TKLog.println ("TKTemplateCache.getSyntax/template, location="+location+", loaderClassId="+
140
// loaderClassId+", newLocation="+newLocation);
141

142         long fileModified = loader.lastModified( newLocation );
143         Long JavaDoc cacheModified = (Long JavaDoc) dates.get( newLocation );
144     
145         //Ein TKTemplateSyntax-Objekt besteht bereits
146
if( cacheModified != null && cacheModified.longValue() == fileModified ) {
147             return (TKTemplateSyntax) syntaxes.get( newLocation );
148         }
149         
150         //Ein TKTemplateSyntax-Objekt wird erzeugt
151
//TKLib.slurpfile(templateFile) => das File wird in String umgewandelt
152

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 JavaDoc(newLocation);
158         }
159         
160         syntax.init( loader.loadSource( newLocation ), newLocation );
161     
162         syntaxes.put( newLocation, syntax );
163         dates.put( newLocation, new Long JavaDoc( fileModified ) );
164         return syntax;
165     }
166     
167     public static void loadSyntax( String JavaDoc location, TKTemplateSyntax syntax ) throws TKTemplateSyntaxException
168     {
169         location = location.replace ('/',File.separatorChar);
170         String JavaDoc loaderClassId = location.substring( 0, location.indexOf(':') );
171         
172         TKTemplateSourceLoader loader = getLoader( loaderClassId );
173         
174         String JavaDoc newLocation = loader.childLocation( location, "" );
175         
176         syntax.init( loader.loadSource( newLocation ), newLocation );
177     }
178     
179 }
180
181
182
Popular Tags