01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.velocity;
18:
19: import org.apache.wicket.markup.html.IHeaderResponse;
20: import org.apache.wicket.model.IModel;
21: import org.apache.wicket.util.lang.Packages;
22:
23: /**
24: * A derivation of VelocityContributor that uses
25: * {@link wicket.markup.html.IHeaderResponse#renderJavascript(CharSequence, String)}
26: */
27: public class VelocityJavascriptContributor extends VelocityContributor {
28: private final String id;
29:
30: /**
31: * Ctor
32: *
33: * Use this constructor if you have configured Velocity to use a
34: * ClasspathResourceLoader. The templatePath will then be relative to the
35: * package for clazz
36: *
37: * @param clazz
38: * @param templatePath
39: * @param model
40: * @param id
41: */
42: public VelocityJavascriptContributor(Class clazz,
43: String templatePath, IModel model, String id) {
44: super (Packages.absolutePath(clazz, templatePath), model);
45: this .id = id;
46: }
47:
48: /**
49: * Construct.
50: *
51: * Use this constructor when Velocity is configured with the
52: * FileResourceLoader. templatePath with then be relative to the loader path
53: * configured in velocity.properties
54: *
55: * @param templatePath
56: * @param model
57: * @param id
58: */
59: public VelocityJavascriptContributor(String templatePath,
60: IModel model, String id) {
61: super (templatePath, model);
62: this .id = id;
63: }
64:
65: /**
66: * @see org.apache.wicket.velocity.VelocityContributor#renderHead(org.apache.wicket.markup.html.IHeaderResponse)
67: */
68: public void renderHead(IHeaderResponse response) {
69: CharSequence s = evaluate();
70: if (s != null) {
71: response.renderJavascript(s, id);
72: }
73: }
74: }
|