- change the ProjectHelper API to start using Resource instead of URL or File git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@829947 13f79535-47bb-0310-9956-ffa450edef68master
@@ -18,11 +18,12 @@ | |||
package org.apache.tools.ant; | |||
import java.io.File; | |||
import java.net.URL; | |||
import java.util.Hashtable; | |||
import java.util.Locale; | |||
import java.util.Vector; | |||
import org.apache.tools.ant.types.Resource; | |||
import org.apache.tools.ant.types.resources.FileResource; | |||
import org.apache.tools.ant.util.LoaderUtils; | |||
import org.xml.sax.AttributeList; | |||
@@ -64,16 +65,17 @@ public class ProjectHelper { | |||
public static final String PROJECTHELPER_REFERENCE = MagicNames.REFID_PROJECT_HELPER; | |||
/** | |||
* Configures the project with the contents of the specified XML file. | |||
* Configures the project with the contents of the specified build file. | |||
* | |||
* @param project The project to configure. Must not be <code>null</code>. | |||
* @param buildFile An XML file giving the project's configuration. | |||
* @param buildFile A build file giving the project's configuration. | |||
* Must not be <code>null</code>. | |||
* | |||
* @exception BuildException if the configuration is invalid or cannot be read | |||
*/ | |||
public static void configureProject(Project project, File buildFile) throws BuildException { | |||
ProjectHelper helper = ProjectHelperRepository.getInstance().getProjectHelper(buildFile); | |||
FileResource resource = new FileResource(buildFile); | |||
ProjectHelper helper = ProjectHelperRepository.getInstance().getProjectHelperForBuildFile(resource); | |||
project.addReference(PROJECTHELPER_REFERENCE, helper); | |||
helper.parse(project, buildFile); | |||
} | |||
@@ -498,7 +500,7 @@ public class ProjectHelper { | |||
* | |||
* @since Ant 1.8.0 | |||
*/ | |||
public boolean canParseAntlibDescriptor(URL url) { | |||
public boolean canParseAntlibDescriptor(Resource r) { | |||
return false; | |||
} | |||
@@ -509,7 +511,7 @@ public class ProjectHelper { | |||
* @since ant 1.8.0 | |||
*/ | |||
public UnknownElement parseAntlibDescriptor(Project containingProject, | |||
URL source) { | |||
Resource source) { | |||
throw new BuildException("can't parse antlib descriptors"); | |||
} | |||
@@ -522,7 +524,7 @@ public class ProjectHelper { | |||
* @return true if the helper supports it | |||
* @since Ant 1.8.0 | |||
*/ | |||
public boolean supportsBuildFile(File buildFile) { | |||
public boolean canParseBuildFile(Resource buildFile) { | |||
return true; | |||
} | |||
@@ -18,7 +18,6 @@ | |||
package org.apache.tools.ant; | |||
import java.io.BufferedReader; | |||
import java.io.File; | |||
import java.io.InputStream; | |||
import java.io.InputStreamReader; | |||
import java.lang.reflect.Constructor; | |||
@@ -29,6 +28,7 @@ import java.util.Iterator; | |||
import java.util.List; | |||
import org.apache.tools.ant.helper.ProjectHelper2; | |||
import org.apache.tools.ant.types.Resource; | |||
import org.apache.tools.ant.util.LoaderUtils; | |||
/** | |||
@@ -206,20 +206,20 @@ public class ProjectHelperRepository { | |||
} | |||
/** | |||
* Get the helper that will be able to parse the specified file. The helper | |||
* Get the helper that will be able to parse the specified build file. The helper | |||
* will be chosen among the ones found in the classpath | |||
* | |||
* @return the first ProjectHelper that fit the requirement (never <code>null</code>). | |||
*/ | |||
public ProjectHelper getProjectHelper(File buildFile) throws BuildException { | |||
public ProjectHelper getProjectHelperForBuildFile(Resource buildFile) throws BuildException { | |||
Iterator it = getHelpers(); | |||
while (it.hasNext()) { | |||
ProjectHelper helper = (ProjectHelper) it.next(); | |||
if (helper.supportsBuildFile(buildFile)) { | |||
if (helper.canParseBuildFile(buildFile)) { | |||
if (DEBUG) { | |||
System.out.println("ProjectHelper " | |||
+ helper.getClass().getName() | |||
+ " selected for the file " | |||
+ " selected for the build file " | |||
+ buildFile); | |||
} | |||
return helper; | |||
@@ -229,6 +229,30 @@ public class ProjectHelperRepository { | |||
+ "have supported the file " + buildFile); | |||
} | |||
/** | |||
* Get the helper that will be able to parse the specified antlib. The helper | |||
* will be chosen among the ones found in the classpath | |||
* | |||
* @return the first ProjectHelper that fit the requirement (never <code>null</code>). | |||
*/ | |||
public ProjectHelper getProjectHelperForAntlib(Resource antlib) throws BuildException { | |||
Iterator it = getHelpers(); | |||
while (it.hasNext()) { | |||
ProjectHelper helper = (ProjectHelper) it.next(); | |||
if (helper.canParseAntlibDescriptor(antlib)) { | |||
if (DEBUG) { | |||
System.out.println("ProjectHelper " | |||
+ helper.getClass().getName() | |||
+ " selected for the antlib " | |||
+ antlib); | |||
} | |||
return helper; | |||
} | |||
} | |||
throw new RuntimeException("BUG: at least the ProjectHelper2 should " | |||
+ "have supported the file " + antlib); | |||
} | |||
/** | |||
* Get an iterator on the list of project helpers configured. The iterator | |||
* will always return at least one element as there will always be the | |||
@@ -27,6 +27,8 @@ import org.apache.tools.ant.Target; | |||
import org.apache.tools.ant.TargetGroup; | |||
import org.apache.tools.ant.Task; | |||
import org.apache.tools.ant.UnknownElement; | |||
import org.apache.tools.ant.types.Resource; | |||
import org.apache.tools.ant.types.resources.URLResource; | |||
import org.apache.tools.ant.util.FileUtils; | |||
import org.apache.tools.ant.util.JAXPUtils; | |||
import org.xml.sax.Attributes; | |||
@@ -83,7 +85,7 @@ public class ProjectHelper2 extends ProjectHelper { | |||
* | |||
* @since Ant 1.8.0 | |||
*/ | |||
public boolean canParseAntlibDescriptor(URL url) { | |||
public boolean canParseAntlibDescriptor(Resource resource) { | |||
return true; | |||
} | |||
@@ -97,8 +99,11 @@ public class ProjectHelper2 extends ProjectHelper { | |||
* @since ant 1.8.0 | |||
*/ | |||
public UnknownElement parseAntlibDescriptor(Project containingProject, | |||
URL source) { | |||
return parseUnknownElement(containingProject, source); | |||
Resource resource) { | |||
if (!(resource instanceof URLResource)) { | |||
throw new BuildException("Unsupported resource type: " + resource); | |||
} | |||
return parseUnknownElement(containingProject, ((URLResource)resource).getURL()); | |||
} | |||
/** | |||
@@ -28,10 +28,11 @@ import org.apache.tools.ant.BuildException; | |||
import org.apache.tools.ant.ComponentHelper; | |||
import org.apache.tools.ant.Project; | |||
import org.apache.tools.ant.ProjectHelper; | |||
import org.apache.tools.ant.ProjectHelperRepository; | |||
import org.apache.tools.ant.Task; | |||
import org.apache.tools.ant.TaskContainer; | |||
import org.apache.tools.ant.UnknownElement; | |||
import org.apache.tools.ant.helper.ProjectHelper2; | |||
import org.apache.tools.ant.types.resources.URLResource; | |||
/** | |||
@@ -70,6 +71,7 @@ public class Antlib extends Task implements TaskContainer { | |||
ComponentHelper helper = | |||
ComponentHelper.getComponentHelper(project); | |||
helper.enterAntLib(uri); | |||
URLResource antlibResource = new URLResource(antlibUrl); | |||
try { | |||
// Should be safe to parse | |||
ProjectHelper parser = null; | |||
@@ -77,18 +79,17 @@ public class Antlib extends Task implements TaskContainer { | |||
project.getReference(ProjectHelper.PROJECTHELPER_REFERENCE); | |||
if (p instanceof ProjectHelper) { | |||
parser = (ProjectHelper) p; | |||
if (!parser.canParseAntlibDescriptor(antlibUrl)) { | |||
project.log("ProjectHelper class " + p.getClass().getName() | |||
+ " can't parse Antlib descriptors, falling back" | |||
+ " to ProjectHelper2."); | |||
if (!parser.canParseAntlibDescriptor(antlibResource)) { | |||
parser = null; | |||
} | |||
} | |||
if (parser == null) { | |||
parser = new ProjectHelper2(); | |||
ProjectHelperRepository helperRepository = | |||
ProjectHelperRepository.getInstance(); | |||
parser = helperRepository.getProjectHelperForAntlib(antlibResource); | |||
} | |||
UnknownElement ue = | |||
parser.parseAntlibDescriptor(project, antlibUrl); | |||
parser.parseAntlibDescriptor(project, antlibResource); | |||
// Check name is "antlib" | |||
if (!(ue.getTag().equals(TAG))) { | |||
throw new BuildException( | |||