View Javadoc

1   /*
2    * $Id: AbstractDirective.java 474191 2006-11-13 08:30:40Z mrdon $
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  package org.apache.struts2.views.velocity.components;
22  
23  import java.io.IOException;
24  import java.io.Writer;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.struts2.ServletActionContext;
32  import org.apache.struts2.components.Component;
33  import org.apache.struts2.dispatcher.Dispatcher;
34  import org.apache.velocity.context.InternalContextAdapter;
35  import org.apache.velocity.exception.MethodInvocationException;
36  import org.apache.velocity.exception.ParseErrorException;
37  import org.apache.velocity.exception.ResourceNotFoundException;
38  import org.apache.velocity.runtime.directive.Directive;
39  import org.apache.velocity.runtime.parser.node.Node;
40  
41  import com.opensymphony.xwork2.inject.Container;
42  import com.opensymphony.xwork2.util.ValueStack;
43  
44  public abstract class AbstractDirective extends Directive {
45      public String getName() {
46          return "s" + getBeanName();
47      }
48  
49      public abstract String getBeanName();
50  
51      /***
52       * All components, unless otherwise stated, are LINE-level directives.
53       */
54      public int getType() {
55          return LINE;
56      }
57  
58      protected abstract Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res);
59  
60      public boolean render(InternalContextAdapter ctx, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
61          // get the bean
62          ValueStack stack = (ValueStack) ctx.get("stack");
63          HttpServletRequest req = (HttpServletRequest) stack.getContext().get(ServletActionContext.HTTP_REQUEST);
64          HttpServletResponse res = (HttpServletResponse) stack.getContext().get(ServletActionContext.HTTP_RESPONSE);
65          Component bean = getBean(stack, req, res);
66          Container container = Dispatcher.getInstance().getConfigurationManager().getConfiguration().getContainer();
67          container.inject(bean);
68          // get the parameters
69          Map params = createPropertyMap(ctx, node);
70          bean.copyParams(params);
71          //bean.addAllParameters(params);
72          bean.start(writer);
73  
74          if (getType() == BLOCK) {
75              Node body = node.jjtGetChild(node.jjtGetNumChildren() - 1);
76              body.render(ctx, writer);
77          }
78  
79          bean.end(writer, "");
80          return true;
81      }
82  
83      /***
84       * create a Map of properties that the user has passed in.  for example,
85       * <pre>
86       * #xxx("name=hello" "value=world" "template=foo")
87       * </pre>
88       * would yield a params that contains {["name", "hello"], ["value", "world"], ["template", "foo"]}
89       *
90       * @param node the Node passed in to the render method
91       * @return a Map of the user specified properties
92       * @throws org.apache.velocity.exception.ParseErrorException
93       *          if the was an error in the format of the property
94       */
95      protected Map createPropertyMap(InternalContextAdapter contextAdapter, Node node) throws ParseErrorException, MethodInvocationException {
96          Map propertyMap = new HashMap();
97  
98          int children = node.jjtGetNumChildren();
99          if (getType() == BLOCK) {
100             children--;
101         }
102 
103         for (int index = 0, length = children; index < length; index++) {
104             this.putProperty(propertyMap, contextAdapter, node.jjtGetChild(index));
105         }
106 
107         return propertyMap;
108     }
109 
110     /***
111      * adds a given Node's key/value pair to the propertyMap.  For example, if this Node contained the value "rows=20",
112      * then the key, rows, would be added to the propertyMap with the String value, 20.
113      *
114      * @param propertyMap a params containing all the properties that we wish to set
115      * @param node        the parameter to set expressed in "name=value" format
116      */
117     protected void putProperty(Map propertyMap, InternalContextAdapter contextAdapter, Node node) throws ParseErrorException, MethodInvocationException {
118         // node.value uses the StrutsValueStack to evaluate the directive's value parameter
119         String param = node.value(contextAdapter).toString();
120 
121         int idx = param.indexOf("=");
122 
123         if (idx != -1) {
124             String property = param.substring(0, idx);
125 
126             String value = param.substring(idx + 1);
127             propertyMap.put(property, value);
128         } else {
129             throw new ParseErrorException("#" + this.getName() + " arguments must include an assignment operator!  For example #tag( Component \"template=mytemplate\" ).  #tag( TextField \"mytemplate\" ) is illegal!");
130         }
131     }
132 }