View Javadoc

1   /*
2    * Copyright 2003-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.velocity.tools.generic.directive;
18  
19  import java.io.IOException;
20  import java.io.Writer;
21  
22  import org.apache.velocity.context.InternalContextAdapter;
23  import org.apache.velocity.exception.MethodInvocationException;
24  import org.apache.velocity.exception.ParseErrorException;
25  import org.apache.velocity.exception.ResourceNotFoundException;
26  import org.apache.velocity.runtime.directive.Directive;
27  import org.apache.velocity.runtime.parser.node.Node;
28  
29  /***
30   * @author <a href="mailto:shinobu@ieee.org">Shinobu Kawai</a>
31   * @version $Id: $
32   */
33  public class Ifnotnull extends Directive {
34  
35      /* (non-Javadoc)
36       * @see org.apache.velocity.runtime.directive.Directive#getName()
37       */
38      public String getName()
39      {
40          return "ifnotnull";
41      }
42  
43      /* (non-Javadoc)
44       * @see org.apache.velocity.runtime.directive.Directive#getType()
45       */
46      public int getType()
47      {
48          return BLOCK;
49      }
50  
51      /* (non-Javadoc)
52       * @see org.apache.velocity.runtime.directive.Directive#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer, org.apache.velocity.runtime.parser.node.Node)
53       */
54      public boolean render(InternalContextAdapter context, Writer writer,
55              Node node) throws IOException, ResourceNotFoundException,
56              ParseErrorException, MethodInvocationException
57      {
58          Object value = node.jjtGetChild(0).value(context);
59          if (value != null)
60          {
61              Node content = node.jjtGetChild(1);
62              content.render(context, writer);
63          }
64          return true;
65      }
66  
67  }