1 package org.apache.velocity.runtime.directive;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "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(). <em>This is deprecated and will be
35 * removed in Velocity 2.0; please use #[[unparsed content]]#
36 * instead.</em>
37 *
38 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
39 * @version $Id: Literal.java 746438 2009-02-21 05:41:24Z nbubna $
40 * @deprecated Use the #[[unparsed content]]# syntax instead.
41 */
42 public class Literal extends Directive
43 {
44 String literalText;
45
46 /**
47 * Return name of this directive.
48 * @return The name of this directive.
49 */
50 public String getName()
51 {
52 return "literal";
53 }
54
55 /**
56 * Return type of this directive.
57 * @return The type of this directive.
58 */
59 public int getType()
60 {
61 return BLOCK;
62 }
63
64 /**
65 * Since there is no processing of content,
66 * there is never a need for an internal scope.
67 */
68 public boolean isScopeProvided()
69 {
70 return false;
71 }
72
73 /**
74 * Store the literal rendition of a node using
75 * the Node.literal().
76 * @param rs
77 * @param context
78 * @param node
79 * @throws TemplateInitException
80 */
81 public void init(RuntimeServices rs, InternalContextAdapter context,
82 Node node)
83 throws TemplateInitException
84 {
85 super.init( rs, context, node );
86
87 literalText = node.jjtGetChild(0).literal();
88 }
89
90 /**
91 * Throw the literal rendition of the block between
92 * #literal()/#end into the writer.
93 * @param context
94 * @param writer
95 * @param node
96 * @return True if the directive rendered successfully.
97 * @throws IOException
98 */
99 public boolean render( InternalContextAdapter context,
100 Writer writer, Node node)
101 throws IOException
102 {
103 writer.write(literalText);
104 return true;
105 }
106 }