001: /*
002: * $Id: AbstractDirective.java 474191 2006-11-13 08:30:40Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.velocity.components;
022:
023: import java.io.IOException;
024: import java.io.Writer;
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: import org.apache.struts2.ServletActionContext;
032: import org.apache.struts2.components.Component;
033: import org.apache.struts2.dispatcher.Dispatcher;
034: import org.apache.velocity.context.InternalContextAdapter;
035: import org.apache.velocity.exception.MethodInvocationException;
036: import org.apache.velocity.exception.ParseErrorException;
037: import org.apache.velocity.exception.ResourceNotFoundException;
038: import org.apache.velocity.runtime.directive.Directive;
039: import org.apache.velocity.runtime.parser.node.Node;
040:
041: import com.opensymphony.xwork2.inject.Container;
042: import com.opensymphony.xwork2.util.ValueStack;
043:
044: public abstract class AbstractDirective extends Directive {
045: public String getName() {
046: return "s" + getBeanName();
047: }
048:
049: public abstract String getBeanName();
050:
051: /**
052: * All components, unless otherwise stated, are LINE-level directives.
053: */
054: public int getType() {
055: return LINE;
056: }
057:
058: protected abstract Component getBean(ValueStack stack,
059: HttpServletRequest req, HttpServletResponse res);
060:
061: public boolean render(InternalContextAdapter ctx, Writer writer,
062: Node node) throws IOException, ResourceNotFoundException,
063: ParseErrorException, MethodInvocationException {
064: // get the bean
065: ValueStack stack = (ValueStack) ctx.get("stack");
066: HttpServletRequest req = (HttpServletRequest) stack
067: .getContext().get(ServletActionContext.HTTP_REQUEST);
068: HttpServletResponse res = (HttpServletResponse) stack
069: .getContext().get(ServletActionContext.HTTP_RESPONSE);
070: Component bean = getBean(stack, req, res);
071: Container container = Dispatcher.getInstance()
072: .getConfigurationManager().getConfiguration()
073: .getContainer();
074: container.inject(bean);
075: // get the parameters
076: Map params = createPropertyMap(ctx, node);
077: bean.copyParams(params);
078: //bean.addAllParameters(params);
079: bean.start(writer);
080:
081: if (getType() == BLOCK) {
082: Node body = node.jjtGetChild(node.jjtGetNumChildren() - 1);
083: body.render(ctx, writer);
084: }
085:
086: bean.end(writer, "");
087: return true;
088: }
089:
090: /**
091: * create a Map of properties that the user has passed in. for example,
092: * <pre>
093: * #xxx("name=hello" "value=world" "template=foo")
094: * </pre>
095: * would yield a params that contains {["name", "hello"], ["value", "world"], ["template", "foo"]}
096: *
097: * @param node the Node passed in to the render method
098: * @return a Map of the user specified properties
099: * @throws org.apache.velocity.exception.ParseErrorException
100: * if the was an error in the format of the property
101: */
102: protected Map createPropertyMap(
103: InternalContextAdapter contextAdapter, Node node)
104: throws ParseErrorException, MethodInvocationException {
105: Map propertyMap = new HashMap();
106:
107: int children = node.jjtGetNumChildren();
108: if (getType() == BLOCK) {
109: children--;
110: }
111:
112: for (int index = 0, length = children; index < length; index++) {
113: this .putProperty(propertyMap, contextAdapter, node
114: .jjtGetChild(index));
115: }
116:
117: return propertyMap;
118: }
119:
120: /**
121: * adds a given Node's key/value pair to the propertyMap. For example, if this Node contained the value "rows=20",
122: * then the key, rows, would be added to the propertyMap with the String value, 20.
123: *
124: * @param propertyMap a params containing all the properties that we wish to set
125: * @param node the parameter to set expressed in "name=value" format
126: */
127: protected void putProperty(Map propertyMap,
128: InternalContextAdapter contextAdapter, Node node)
129: throws ParseErrorException, MethodInvocationException {
130: // node.value uses the StrutsValueStack to evaluate the directive's value parameter
131: String param = node.value(contextAdapter).toString();
132:
133: int idx = param.indexOf("=");
134:
135: if (idx != -1) {
136: String property = param.substring(0, idx);
137:
138: String value = param.substring(idx + 1);
139: propertyMap.put(property, value);
140: } else {
141: throw new ParseErrorException(
142: "#"
143: + this .getName()
144: + " arguments must include an assignment operator! For example #tag( Component \"template=mytemplate\" ). #tag( TextField \"mytemplate\" ) is illegal!");
145: }
146: }
147: }
|