| 1 | package org.apache.velocity.runtime.directive; |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2000-2001,2004 The Apache Software Foundation. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | import java.io.*; |
| 20 | |
| 21 | import org.apache.velocity.context.InternalContextAdapter; |
| 22 | import org.apache.velocity.runtime.parser.node.Node; |
| 23 | |
| 24 | import org.apache.velocity.runtime.RuntimeServices; |
| 25 | |
| 26 | /** |
| 27 | * A very simple directive that leverages the Node.literal() |
| 28 | * to grab the literal rendition of a node. We basically |
| 29 | * grab the literal value on init(), then repeatedly use |
| 30 | * that during render(). |
| 31 | * |
| 32 | * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> |
| 33 | * @version $Id: Literal.java,v 1.7.4.1 2004/03/03 23:22:56 geirm Exp $ |
| 34 | */ |
| 35 | public class Literal extends Directive |
| 36 | { |
| 37 | String literalText; |
| 38 | |
| 39 | /** |
| 40 | * Return name of this directive. |
| 41 | */ |
| 42 | public String getName() |
| 43 | { |
| 44 | return "literal"; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Return type of this directive. |
| 49 | */ |
| 50 | public int getType() |
| 51 | { |
| 52 | return BLOCK; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Store the literal rendition of a node using |
| 57 | * the Node.literal(). |
| 58 | */ |
| 59 | public void init(RuntimeServices rs, InternalContextAdapter context, |
| 60 | Node node) |
| 61 | throws Exception |
| 62 | { |
| 63 | super.init( rs, context, node ); |
| 64 | |
| 65 | literalText = node.jjtGetChild(0).literal(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Throw the literal rendition of the block between |
| 70 | * #literal()/#end into the writer. |
| 71 | */ |
| 72 | public boolean render( InternalContextAdapter context, |
| 73 | Writer writer, Node node) |
| 74 | throws IOException |
| 75 | { |
| 76 | writer.write(literalText); |
| 77 | return true; |
| 78 | } |
| 79 | } |