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.net.URL;
019: import java.util.Iterator;
020: import java.util.Map;
021:
022: import org.apache.commons.jelly.DynaTag;
023: import org.apache.commons.jelly.JellyContext;
024: import org.apache.commons.jelly.JellyException;
025: import org.apache.commons.jelly.JellyTagException;
026: import org.apache.commons.jelly.Tag;
027: import org.apache.commons.jelly.TagLibrary;
028: import org.apache.commons.jelly.XMLOutput;
029: import org.apache.commons.jelly.expression.Expression;
030: import org.xml.sax.SAXException;
031:
032: /**
033: * <p><code>StaticTagScript</code> is a script that evaluates a StaticTag, a piece of static XML
034: * though its attributes or element content may contain dynamic expressions.
035: * The first time this tag evaluates, it may have become a dynamic tag, so it will check that
036: * a new dynamic tag has not been generated.</p>
037: *
038: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
039: * @version $Revision: 155420 $
040: */
041: public class StaticTagScript extends TagScript {
042:
043: public StaticTagScript() {
044: }
045:
046: public StaticTagScript(TagFactory tagFactory) {
047: super (tagFactory);
048: }
049:
050: // Script interface
051: //-------------------------------------------------------------------------
052: public void run(JellyContext context, XMLOutput output)
053: throws JellyTagException {
054: try {
055: startNamespacePrefixes(output);
056: } catch (SAXException e) {
057: throw new JellyTagException(
058: "could not start namespace prefixes", e);
059: }
060:
061: Tag tag = null;
062: try {
063: tag = getTag(context);
064:
065: // lets see if we have a dynamic tag
066: if (tag instanceof StaticTag) {
067: tag = findDynamicTag(context, (StaticTag) tag);
068: }
069:
070: setTag(tag, context);
071: } catch (JellyException e) {
072: throw new JellyTagException(e);
073: }
074:
075: URL rootURL = context.getRootURL();
076: URL currentURL = context.getCurrentURL();
077: try {
078: if (tag == null) {
079: return;
080: }
081: tag.setContext(context);
082: setContextURLs(context);
083:
084: DynaTag dynaTag = (DynaTag) tag;
085:
086: // ### probably compiling this to 2 arrays might be quicker and smaller
087: for (Iterator iter = attributes.entrySet().iterator(); iter
088: .hasNext();) {
089: Map.Entry entry = (Map.Entry) iter.next();
090: String name = (String) entry.getKey();
091: Expression expression = (Expression) entry.getValue();
092:
093: Object value = null;
094:
095: if (Expression.class.isAssignableFrom(dynaTag
096: .getAttributeType(name))) {
097: value = expression;
098: } else {
099: value = expression.evaluate(context);
100: }
101:
102: dynaTag.setAttribute(name, value);
103: }
104:
105: tag.doTag(output);
106: } catch (JellyTagException e) {
107: handleException(e);
108: } catch (RuntimeException e) {
109: handleException(e);
110: } finally {
111: context.setCurrentURL(currentURL);
112: context.setRootURL(rootURL);
113: }
114:
115: try {
116: endNamespacePrefixes(output);
117: } catch (SAXException e) {
118: throw new JellyTagException(
119: "could not end namespace prefixes", e);
120: }
121: }
122:
123: /**
124: * Attempts to find a dynamically created tag that has been created since this
125: * script was compiled
126: */
127: protected Tag findDynamicTag(JellyContext context, StaticTag tag)
128: throws JellyException {
129: // lets see if there's a tag library for this URI...
130: TagLibrary taglib = context.getTagLibrary(tag.getUri());
131: if (taglib != null) {
132: Tag newTag = taglib.createTag(tag.getLocalName(),
133: getSaxAttributes());
134: if (newTag != null) {
135: newTag.setParent(tag.getParent());
136: newTag.setBody(tag.getBody());
137: return newTag;
138: }
139: }
140: return tag;
141: }
142: }
|