View Javadoc

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().
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  {
41      String literalText;
42  
43      /**
44       * Return name of this directive.
45       * @return The name of this directive.
46       */
47      public String getName()
48      {
49          return "literal";
50      }
51  
52      /**
53       * Return type of this directive.
54       * @return The type of this directive.
55       */
56      public int getType()
57      {
58          return BLOCK;
59      }
60  
61      /**
62       * Store the literal rendition of a node using
63       * the Node.literal().
64       * @param rs
65       * @param context
66       * @param node
67       * @throws TemplateInitException
68       */
69      public void init(RuntimeServices rs, InternalContextAdapter context,
70                       Node node)
71          throws TemplateInitException
72      {
73          super.init( rs, context, node );
74  
75          literalText = node.jjtGetChild(0).literal();
76      }
77  
78      /**
79       * Throw the literal rendition of the block between
80       * #literal()/#end into the writer.
81       * @param context
82       * @param writer
83       * @param node
84       * @return True if the directive rendered successfully.
85       * @throws IOException
86       */
87      public boolean render( InternalContextAdapter context,
88                             Writer writer, Node node)
89          throws IOException
90      {
91          writer.write(literalText);
92          return true;
93      }
94  }