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