%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.impl.StaticTagScript |
|
|
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 | 0 | public StaticTagScript() { |
43 | 0 | } |
44 | ||
45 | public StaticTagScript(TagFactory tagFactory) { |
|
46 | 40 | super(tagFactory); |
47 | 40 | } |
48 | ||
49 | ||
50 | // Script interface |
|
51 | //------------------------------------------------------------------------- |
|
52 | public void run(JellyContext context, XMLOutput output) throws JellyTagException { |
|
53 | ||
54 | try { |
|
55 | 26 | startNamespacePrefixes(output); |
56 | 26 | } catch (SAXException e) { |
57 | 0 | throw new JellyTagException("could not start namespace prefixes",e); |
58 | } |
|
59 | ||
60 | 26 | Tag tag = null; |
61 | try { |
|
62 | 26 | tag = getTag(); |
63 | ||
64 | // lets see if we have a dynamic tag |
|
65 | 26 | if (tag instanceof StaticTag) { |
66 | 26 | tag = findDynamicTag(context, (StaticTag) tag); |
67 | } |
|
68 | ||
69 | 26 | setTag(tag); |
70 | 26 | } catch (JellyException e) { |
71 | 0 | throw new JellyTagException(e); |
72 | } |
|
73 | ||
74 | try { |
|
75 | 26 | if ( tag == null ) { |
76 | 0 | return; |
77 | } |
|
78 | 26 | tag.setContext(context); |
79 | ||
80 | 26 | DynaTag dynaTag = (DynaTag) tag; |
81 | ||
82 | // ### probably compiling this to 2 arrays might be quicker and smaller |
|
83 | 80 | for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) { |
84 | 36 | Map.Entry entry = (Map.Entry) iter.next(); |
85 | 28 | String name = (String) entry.getKey(); |
86 | 28 | Expression expression = (Expression) entry.getValue(); |
87 | ||
88 | 28 | Object value = null; |
89 | ||
90 | 28 | if ( Expression.class.isAssignableFrom( dynaTag.getAttributeType(name) ) ) { |
91 | 0 | value = expression; |
92 | } else { |
|
93 | 28 | value = expression.evaluate(context); |
94 | } |
|
95 | ||
96 | 28 | dynaTag.setAttribute(name, value); |
97 | } |
|
98 | ||
99 | 26 | tag.doTag(output); |
100 | 26 | } |
101 | catch (JellyTagException e) { |
|
102 | 0 | handleException(e); |
103 | 0 | } |
104 | catch (RuntimeException e) { |
|
105 | 0 | handleException(e); |
106 | } |
|
107 | ||
108 | try { |
|
109 | 26 | endNamespacePrefixes(output); |
110 | 26 | } catch (SAXException e) { |
111 | 0 | throw new JellyTagException("could not end namespace prefixes",e); |
112 | } |
|
113 | 26 | } |
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 | 26 | TagLibrary taglib = context.getTagLibrary( tag.getUri() ); |
122 | 26 | if ( taglib != null ) { |
123 | 12 | Tag newTag = taglib.createTag( tag.getLocalName(), getSaxAttributes() ); |
124 | 12 | if ( newTag != null ) { |
125 | 0 | newTag.setParent( tag.getParent() ); |
126 | 0 | newTag.setBody( tag.getBody() ); |
127 | 0 | return newTag; |
128 | } |
|
129 | } |
|
130 | 26 | return tag; |
131 | } |
|
132 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |