1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.ant;
17
18 import org.apache.commons.beanutils.ConvertUtils;
19 import org.apache.commons.beanutils.Converter;
20 import org.apache.commons.grant.GrantProject;
21 import org.apache.commons.jelly.JellyContext;
22 import org.apache.commons.jelly.JellyException;
23 import org.apache.commons.jelly.Tag;
24 import org.apache.commons.jelly.TagLibrary;
25 import org.apache.commons.jelly.impl.TagFactory;
26 import org.apache.commons.jelly.impl.TagScript;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import org.apache.tools.ant.BuildLogger;
31 import org.apache.tools.ant.NoBannerLogger;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.taskdefs.optional.junit.FormatterElement;
34 import org.apache.tools.ant.types.EnumeratedAttribute;
35 import org.apache.tools.ant.types.Reference;
36
37 import org.xml.sax.Attributes;
38
39 /***
40 * A Jelly custom tag library that allows Ant tasks to be called from inside Jelly.
41 *
42 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
43 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
44 * @version $Revision: 1.6 $
45 */
46 public class AntTagLibrary extends TagLibrary {
47
48 /*** The Log to which logging calls will be made. */
49 private static final Log log = LogFactory.getLog(AntTagLibrary.class);
50
51 public static final String PROJECT_CONTEXT_HANDLE = "org.apache.commons.jelly.ant.Project";
52
53 static {
54
55
56
57
58 ConvertUtils.register(
59 new Converter() {
60 public Object convert(Class type, Object value) {
61 if ( value instanceof Reference ) {
62 return (Reference) value;
63 }
64 else if ( value != null ) {
65 String text = value.toString();
66 return new Reference( text );
67 }
68 return null;
69 }
70 },
71 Reference.class
72 );
73
74 ConvertUtils.register(
75 new Converter() {
76 public Object convert(Class type, Object value) {
77 if ( value instanceof EnumeratedAttribute ) {
78 return (EnumeratedAttribute) value;
79 }
80 else if ( value instanceof String ) {
81 FormatterElement.TypeAttribute attr = new FormatterElement.TypeAttribute();
82 attr.setValue( (String) value );
83 return attr;
84 }
85 return null;
86 }
87
88 },
89 FormatterElement.TypeAttribute.class
90 );
91 }
92
93
94 /***
95 * A helper method which will attempt to find a project in the current context
96 * or install one if need be.
97 *
98 * #### this method could move to an AntUtils class.
99 */
100 public static Project getProject(JellyContext context) {
101 Project project = (Project) context.findVariable( PROJECT_CONTEXT_HANDLE );
102 if ( project == null ) {
103 project = createProject(context);
104 context.setVariable( PROJECT_CONTEXT_HANDLE , project );
105 }
106 return project;
107 }
108
109 /***
110 * Sets the Ant Project to be used for this JellyContext.
111 *
112 * #### this method could move to an AntUtils class.
113 */
114 public static void setProject(JellyContext context, Project project) {
115 context.setVariable( PROJECT_CONTEXT_HANDLE, project );
116 }
117
118 /***
119 * A helper method to create a new project
120 *
121 * #### this method could move to an AntUtils class.
122 */
123 public static Project createProject(JellyContext context) {
124 GrantProject project = new GrantProject();
125 project.setPropsHandler(new JellyPropsHandler(context));
126
127 BuildLogger logger = new NoBannerLogger();
128
129 logger.setMessageOutputLevel( org.apache.tools.ant.Project.MSG_INFO );
130 logger.setOutputPrintStream( System.out );
131 logger.setErrorPrintStream( System.err);
132
133 project.addBuildListener( logger );
134
135 project.init();
136 project.getBaseDir();
137 if (context.getCurrentURL() != null) {
138 project.setProperty("ant.file",
139 context.getCurrentURL().toExternalForm());
140 }
141
142 return project;
143 }
144
145
146 /*** Creates a new script to execute the given tag name and attributes */
147 public TagScript createTagScript(String name, Attributes attributes) throws JellyException {
148 TagScript answer = createCustomTagScript(name, attributes);
149 if ( answer == null ) {
150 answer = new TagScript(
151 new TagFactory() {
152 public Tag createTag(String name, Attributes attributes) throws JellyException {
153 return AntTagLibrary.this.createTag(name, attributes);
154 }
155 }
156 );
157 }
158 return answer;
159 }
160
161 /***
162 * @return a new TagScript for any custom, statically defined tags, like 'fileScanner'
163 */
164 public TagScript createCustomTagScript(String name, Attributes attributes) throws JellyException {
165
166 if ( name.equals("fileScanner") ) {
167 return new TagScript(
168 new TagFactory() {
169 public Tag createTag(String name, Attributes attributes) throws JellyException {
170 return new FileScannerTag(new FileScanner());
171 }
172 }
173 );
174 }
175 if ( name.equals("setProperty") ) {
176 return new TagScript(
177 new TagFactory() {
178 public Tag createTag(String name, Attributes attributes) throws JellyException {
179 return new SetPropertyTag();
180 }
181 }
182 );
183 }
184 return null;
185 }
186
187 /***
188 * A helper method which creates an AntTag instance for the given element name
189 */
190 public Tag createTag(String name, Attributes attributes) throws JellyException {
191 AntTag tag = new AntTag( name );
192 if ( name.equals( "echo" ) ) {
193 tag.setTrim(false);
194 }
195 return tag;
196 }
197
198
199 }