001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.impl;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020: import java.util.Iterator;
021:
022: import org.apache.commons.jelly.DynaTagSupport;
023: import org.apache.commons.jelly.JellyContext;
024: import org.apache.commons.jelly.JellyTagException;
025: import org.apache.commons.jelly.Script;
026: import org.apache.commons.jelly.XMLOutput;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: /**
031: * <p><code>DynamicTag</code> is a tag that is created from
032: * inside a Jelly script as a Jelly template and will invoke a
033: * given script, passing in its instantiation attributes
034: * as variables and will allow the template to invoke its instance body.</p>
035: *
036: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
037: * @version $Revision: 155420 $
038: */
039: public class DynamicTag extends DynaTagSupport {
040:
041: /** The Log to which logging calls will be made. */
042: private static final Log log = LogFactory.getLog(DynamicTag.class);
043:
044: /** The template script */
045: private Script template;
046:
047: /** The instance attributes */
048: private Map attributes = new HashMap();
049:
050: public DynamicTag() {
051: }
052:
053: public DynamicTag(Script template) {
054: this .template = template;
055: }
056:
057: // Tag interface
058: //-------------------------------------------------------------------------
059: public void doTag(XMLOutput output) throws JellyTagException {
060: if (log.isDebugEnabled()) {
061: log.debug("Invoking dynamic tag with attributes: "
062: + attributes);
063: }
064: attributes.put("org.apache.commons.jelly.body", getBody());
065:
066: // create new context based on current attributes
067: JellyContext newJellyContext = context
068: .newJellyContext(attributes);
069: Map attrMap = new HashMap();
070: for (Iterator keyIter = this .attributes.keySet().iterator(); keyIter
071: .hasNext();) {
072: String key = (String) keyIter.next();
073: if (key.endsWith("Attr")) {
074: Object value = this .attributes.get(key);
075: attrMap.put(key, value);
076: attrMap.put(key.substring(0, key.length() - 4), value);
077: }
078: }
079: newJellyContext.setVariable("attrs", attrMap);
080: getTemplate().run(newJellyContext, output);
081: }
082:
083: // DynaTag interface
084: //-------------------------------------------------------------------------
085: public void setAttribute(String name, Object value) {
086: attributes.put(name, value);
087: attributes.put(name + "Attr", value);
088: }
089:
090: // Properties
091: //-------------------------------------------------------------------------
092: /** The template to be executed by this tag which may well
093: * invoke this instances body from inside the template
094: */
095: public Script getTemplate() {
096: return template;
097: }
098:
099: public void setTemplate(Script template) {
100: this.template = template;
101: }
102: }
|