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 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
public class AntTagLibrary extends TagLibrary { |
47 |
|
|
48 |
|
|
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 |
2 |
public Object convert(Class type, Object value) { |
61 |
0 |
if ( value instanceof Reference ) { |
62 |
0 |
return (Reference) value; |
63 |
|
} |
64 |
0 |
else if ( value != null ) { |
65 |
0 |
String text = value.toString(); |
66 |
0 |
return new Reference( text ); |
67 |
|
} |
68 |
0 |
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 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
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 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
public static void setProject(JellyContext context, Project project) { |
115 |
|
context.setVariable( PROJECT_CONTEXT_HANDLE, project ); |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
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 |
|
|
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 |
|
|
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(class="keyword">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 |
|
|
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 |
|
} |