KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > engine > DefaultScriptSource


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.engine;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.ClassResolver;
22 import org.apache.hivemind.Resource;
23 import org.apache.tapestry.IScript;
24 import org.apache.tapestry.Tapestry;
25 import org.apache.tapestry.coerce.ValueConverter;
26 import org.apache.tapestry.event.ResetEventListener;
27 import org.apache.tapestry.script.ScriptParser;
28 import org.apache.tapestry.services.ExpressionEvaluator;
29 import org.apache.tapestry.util.xml.DocumentParseException;
30
31 /**
32  * Provides basic access to scripts available on the classpath. Scripts are cached in memory once
33  * parsed.
34  *
35  * @author Howard Lewis Ship
36  * @since 1.0.2
37  */

38
39 public class DefaultScriptSource implements IScriptSource, ResetEventListener
40 {
41     private ClassResolver _classResolver;
42
43     /** @since 4.0 */
44     private ExpressionEvaluator _expressionEvaluator;
45
46     /** @since 4.0 */
47     private ValueConverter _valueConverter;
48
49     private Map JavaDoc _cache = new HashMap JavaDoc();
50
51     public synchronized void resetEventDidOccur()
52     {
53         _cache.clear();
54     }
55
56     public synchronized IScript getScript(Resource resource)
57     {
58         IScript result = (IScript) _cache.get(resource);
59
60         if (result != null)
61             return result;
62
63         result = parse(resource);
64
65         _cache.put(resource, result);
66
67         return result;
68     }
69
70     private IScript parse(Resource resource)
71     {
72         ScriptParser parser = new ScriptParser(_classResolver, _expressionEvaluator,
73                 _valueConverter);
74
75         try
76         {
77             return parser.parse(resource);
78         }
79         catch (DocumentParseException ex)
80         {
81             throw new ApplicationRuntimeException(Tapestry.format(
82                     "DefaultScriptSource.unable-to-parse-script",
83                     resource), ex);
84         }
85     }
86
87     public void setClassResolver(ClassResolver classResolver)
88     {
89         _classResolver = classResolver;
90     }
91
92     /** @since 4.0 */
93     public void setExpressionEvaluator(ExpressionEvaluator expressionEvaluator)
94     {
95         _expressionEvaluator = expressionEvaluator;
96     }
97
98     /** @since 4.0 */
99     public void setValueConverter(ValueConverter valueConverter)
100     {
101         _valueConverter = valueConverter;
102     }
103 }
Popular Tags