01: package org.apache.velocity.runtime.directive;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21:
22: import java.io.IOException;
23: import java.io.Writer;
24:
25: import org.apache.velocity.context.InternalContextAdapter;
26: import org.apache.velocity.exception.TemplateInitException;
27: import org.apache.velocity.runtime.RuntimeServices;
28: import org.apache.velocity.runtime.parser.node.Node;
29:
30: /**
31: * A very simple directive that leverages the Node.literal()
32: * to grab the literal rendition of a node. We basically
33: * grab the literal value on init(), then repeatedly use
34: * that during render().
35: *
36: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
37: * @version $Id: Literal.java 471381 2006-11-05 08:56:58Z wglass $
38: */
39: public class Literal extends Directive {
40: String literalText;
41:
42: /**
43: * Return name of this directive.
44: * @return The name of this directive.
45: */
46: public String getName() {
47: return "literal";
48: }
49:
50: /**
51: * Return type of this directive.
52: * @return The type of this directive.
53: */
54: public int getType() {
55: return BLOCK;
56: }
57:
58: /**
59: * Store the literal rendition of a node using
60: * the Node.literal().
61: * @param rs
62: * @param context
63: * @param node
64: * @throws TemplateInitException
65: */
66: public void init(RuntimeServices rs,
67: InternalContextAdapter context, Node node)
68: throws TemplateInitException {
69: super .init(rs, context, node);
70:
71: literalText = node.jjtGetChild(0).literal();
72: }
73:
74: /**
75: * Throw the literal rendition of the block between
76: * #literal()/#end into the writer.
77: * @param context
78: * @param writer
79: * @param node
80: * @return True if the directive rendered successfully.
81: * @throws IOException
82: */
83: public boolean render(InternalContextAdapter context,
84: Writer writer, Node node) throws IOException {
85: writer.write(literalText);
86: return true;
87: }
88: }
|