1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.IOException ; 22 import java.net.URL ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import org.apache.tools.ant.TaskContainer; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.ComponentHelper; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.Task; 32 import org.apache.tools.ant.helper.ProjectHelper2; 33 import org.apache.tools.ant.UnknownElement; 34 35 36 43 public class Antlib extends Task implements TaskContainer { 44 48 49 public static final String TAG = "antlib"; 50 51 60 public static Antlib createAntlib(Project project, URL antlibUrl, 61 String uri) { 62 try { 64 antlibUrl.openConnection().connect(); 65 } catch (IOException ex) { 66 throw new BuildException( 67 "Unable to find " + antlibUrl, ex); 68 } 69 ComponentHelper helper = 70 ComponentHelper.getComponentHelper(project); 71 helper.enterAntLib(uri); 72 try { 73 ProjectHelper2 parser = new ProjectHelper2(); 75 UnknownElement ue = 76 parser.parseUnknownElement(project, antlibUrl); 77 if (!(ue.getTag().equals(TAG))) { 79 throw new BuildException( 80 "Unexpected tag " + ue.getTag() + " expecting " 81 + TAG, ue.getLocation()); 82 } 83 Antlib antlib = new Antlib(); 84 antlib.setProject(project); 85 antlib.setLocation(ue.getLocation()); 86 antlib.setTaskName("antlib"); 87 antlib.init(); 88 ue.configure(antlib); 89 return antlib; 90 } finally { 91 helper.exitAntLib(); 92 } 93 } 94 95 96 private ClassLoader classLoader; 100 private String uri = ""; 101 private List tasks = new ArrayList (); 102 103 110 protected void setClassLoader(ClassLoader classLoader) { 111 this.classLoader = classLoader; 112 } 113 114 118 protected void setURI(String uri) { 119 this.uri = uri; 120 } 121 122 private ClassLoader getClassLoader() { 123 if (classLoader == null) { 124 classLoader = Antlib.class.getClassLoader(); 125 } 126 return classLoader; 127 } 128 129 134 public void addTask(Task nestedTask) { 135 tasks.add(nestedTask); 136 } 137 138 142 public void execute() { 143 for (Iterator i = tasks.iterator(); i.hasNext();) { 144 UnknownElement ue = (UnknownElement) i.next(); 145 setLocation(ue.getLocation()); 146 ue.maybeConfigure(); 147 Object configuredObject = ue.getRealThing(); 148 if (configuredObject == null) { 149 continue; 150 } 151 if (!(configuredObject instanceof AntlibDefinition)) { 152 throw new BuildException( 153 "Invalid task in antlib " + ue.getTag() 154 + " " + configuredObject.getClass() + " does not " 155 + "extend org.apache.tools.ant.taskdefs.AntlibDefinition"); 156 } 157 AntlibDefinition def = (AntlibDefinition) configuredObject; 158 def.setURI(uri); 159 def.setAntlibClassLoader(getClassLoader()); 160 def.init(); 161 def.execute(); 162 } 163 } 164 165 } 166 | Popular Tags |