View Javadoc

1   /*
2    * Copyright 2002,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  package org.apache.commons.jelly.impl;
17  
18  import java.util.Iterator;
19  import java.util.Map;
20  
21  import org.apache.commons.jelly.DynaTag;
22  import org.apache.commons.jelly.JellyContext;
23  import org.apache.commons.jelly.JellyException;
24  import org.apache.commons.jelly.JellyTagException;
25  import org.apache.commons.jelly.Tag;
26  import org.apache.commons.jelly.TagLibrary;
27  import org.apache.commons.jelly.XMLOutput;
28  import org.apache.commons.jelly.expression.Expression;
29  import org.xml.sax.SAXException;
30  
31  /***
32   * <p><code>StaticTagScript</code> is a script that evaluates a StaticTag, a piece of static XML
33   * though its attributes or element content may contain dynamic expressions.
34   * The first time this tag evaluates, it may have become a dynamic tag, so it will check that
35   * a new dynamic tag has not been generated.</p>
36   *
37   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
38   * @version $Revision: 1.8 $
39   */
40  public class StaticTagScript extends TagScript {
41  
42      public StaticTagScript() {
43      }
44  
45      public StaticTagScript(TagFactory tagFactory) {
46          super(tagFactory);
47      }
48  
49  
50      // Script interface
51      //-------------------------------------------------------------------------
52      public void run(JellyContext context, XMLOutput output) throws JellyTagException {
53  
54          try {
55              startNamespacePrefixes(output);
56          } catch (SAXException e) {
57              throw new JellyTagException("could not start namespace prefixes",e);
58          }
59  
60          Tag tag = null;
61          try {
62              tag = getTag();
63  
64              // lets see if we have a dynamic tag
65              if (tag instanceof StaticTag) {
66                  tag = findDynamicTag(context, (StaticTag) tag);
67              }
68  
69              setTag(tag);
70          } catch (JellyException e) {
71              throw new JellyTagException(e);
72          }
73  
74          try {
75              if ( tag == null ) {
76                  return;
77              }
78              tag.setContext(context);
79  
80              DynaTag dynaTag = (DynaTag) tag;
81  
82              // ### probably compiling this to 2 arrays might be quicker and smaller
83              for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) {
84                  Map.Entry entry = (Map.Entry) iter.next();
85                  String name = (String) entry.getKey();
86                  Expression expression = (Expression) entry.getValue();
87  
88                  Object value = null;
89  
90                  if ( Expression.class.isAssignableFrom( dynaTag.getAttributeType(name) ) ) {
91                      value = expression;
92                  } else {
93                      value = expression.evaluate(context);
94                  }
95  
96                  dynaTag.setAttribute(name, value);
97              }
98  
99              tag.doTag(output);
100         }
101         catch (JellyTagException e) {
102             handleException(e);
103         }
104         catch (RuntimeException e) {
105             handleException(e);
106         }
107 
108         try {
109             endNamespacePrefixes(output);
110         } catch (SAXException e) {
111             throw new JellyTagException("could not end namespace prefixes",e);
112         }
113     }
114 
115     /***
116      * Attempts to find a dynamically created tag that has been created since this
117      * script was compiled
118      */
119     protected Tag findDynamicTag(JellyContext context, StaticTag tag) throws JellyException {
120         // lets see if there's a tag library for this URI...
121         TagLibrary taglib = context.getTagLibrary( tag.getUri() );
122         if ( taglib != null ) {
123             Tag newTag = taglib.createTag( tag.getLocalName(), getSaxAttributes() );
124             if ( newTag != null ) {
125                 newTag.setParent( tag.getParent() );
126                 newTag.setBody( tag.getBody() );
127                 return newTag;
128             }
129         }
130         return tag;
131     }
132 }