%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.task.AntJellyContext |
|
|
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 | ||
17 | package org.apache.commons.jelly.task; |
|
18 | ||
19 | import java.util.HashMap; |
|
20 | import java.util.Iterator; |
|
21 | import java.util.Map; |
|
22 | ||
23 | import org.apache.commons.jelly.JellyContext; |
|
24 | import org.apache.commons.logging.Log; |
|
25 | import org.apache.commons.logging.LogFactory; |
|
26 | import org.apache.tools.ant.Project; |
|
27 | ||
28 | /** <p><code>AntJellyContext</code> represents the Jelly context from inside Ant.</p> |
|
29 | * |
|
30 | * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> |
|
31 | * @version $Revision: 1.10 $ |
|
32 | */ |
|
33 | ||
34 | 0 | public class AntJellyContext extends JellyContext { |
35 | ||
36 | /** The Ant project which contains the variables */ |
|
37 | private Project project; |
|
38 | ||
39 | /** The Log to which logging calls will be made. */ |
|
40 | 0 | private Log log = LogFactory.getLog(AntJellyContext.class); |
41 | ||
42 | public AntJellyContext(Project project, JellyContext parentJellyContext) { |
|
43 | 0 | super( parentJellyContext ); |
44 | 0 | this.project = project; |
45 | 0 | } |
46 | ||
47 | /** @return the value of the given variable name */ |
|
48 | public Object getVariable(String name) { |
|
49 | // look in parent first |
|
50 | 0 | Object answer = super.getVariable(name); |
51 | 0 | if (answer == null) { |
52 | 0 | answer = project.getProperty(name); |
53 | } |
|
54 | ||
55 | 0 | if ( log.isDebugEnabled() ) { |
56 | 0 | log.debug( "Looking up variable: " + name + " answer: " + answer ); |
57 | } |
|
58 | ||
59 | 0 | return answer; |
60 | } |
|
61 | ||
62 | /** Sets the value of the given variable name */ |
|
63 | public void setVariable(String name, Object value) { |
|
64 | 0 | if ( log.isDebugEnabled() ) { |
65 | 0 | log.debug( "Setting variable: " + name + " to: " + value ); |
66 | } |
|
67 | ||
68 | 0 | super.setVariable( name, value ); |
69 | ||
70 | // only export string values back to Ant? |
|
71 | 0 | if ( value instanceof String ) { |
72 | 0 | project.setProperty(name, (String) value); |
73 | } |
|
74 | 0 | } |
75 | ||
76 | /** Removes the given variable */ |
|
77 | public void removeVariable(String name) { |
|
78 | 0 | super.removeVariable( name ); |
79 | 0 | project.setProperty(name, null); |
80 | 0 | } |
81 | ||
82 | /** |
|
83 | * @return an Iterator over the current variable names in this |
|
84 | * context |
|
85 | */ |
|
86 | public Iterator getVariableNames() { |
|
87 | 0 | return getVariables().keySet().iterator(); |
88 | } |
|
89 | ||
90 | /** |
|
91 | * @return the Map of variables in this scope |
|
92 | */ |
|
93 | public Map getVariables() { |
|
94 | // we should add all the Project's properties |
|
95 | 0 | Map map = new HashMap( project.getProperties() ); |
96 | ||
97 | // override any local properties |
|
98 | 0 | map.putAll( super.getVariables() ); |
99 | 0 | return map; |
100 | } |
|
101 | ||
102 | /** |
|
103 | * Sets the Map of variables to use |
|
104 | */ |
|
105 | ||
106 | public void setVariables(Map variables) { |
|
107 | 0 | super.setVariables(variables); |
108 | ||
109 | // export any Ant properties |
|
110 | 0 | for ( Iterator iter = variables.entrySet().iterator(); iter.hasNext(); ) { |
111 | 0 | Map.Entry entry = (Map.Entry) iter.next(); |
112 | 0 | String key = (String) entry.getKey(); |
113 | 0 | Object value = entry.getValue(); |
114 | 0 | if ( value instanceof String ) { |
115 | 0 | project.setProperty(key, (String)value); |
116 | } |
|
117 | } |
|
118 | 0 | } |
119 | ||
120 | ||
121 | // Implementation methods |
|
122 | //------------------------------------------------------------------------- |
|
123 | ||
124 | /** |
|
125 | * Factory method to create a new child of this context |
|
126 | */ |
|
127 | protected JellyContext createChildContext() { |
|
128 | 0 | return new AntJellyContext(project, this); |
129 | } |
|
130 | ||
131 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |