ASF Bugzilla – Attachment 19644 Details for
Bug 41701
[PATCH] <import> should be able to load .xmls from classpath
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Updated patch with formatting preserved
ant-import-task.patch (text/plain), 6.25 KB, created by
Gregory Mostizky
on 2007-02-27 05:33:43 UTC
(
hide
)
Description:
Updated patch with formatting preserved
Filename:
MIME Type:
Creator:
Gregory Mostizky
Created:
2007-02-27 05:33:43 UTC
Size:
6.25 KB
patch
obsolete
>Index: ImportTask.java >=================================================================== >--- ImportTask.java (revision 511767) >+++ ImportTask.java (working copy) >@@ -23,10 +23,15 @@ > import org.apache.tools.ant.Project; > import org.apache.tools.ant.Task; > import org.apache.tools.ant.util.FileUtils; >+import org.apache.tools.ant.taskdefs.Ant.Reference; >+import org.apache.tools.ant.types.Path; > > import java.io.File; >+import java.io.IOException; >+import java.net.URL; > import java.util.Vector; > >+ > /** > * Task to import another build file into the current project. > * <p> >@@ -48,6 +53,16 @@ > * <import file="${deploy-platform}.xml"/> > * </pre> > * <p>Import the project defined by the property <code>deploy-platform</code>.</p> >+ * <pre> >+ * <import resource="common-targets.xml" /> >+ * </pre> >+ * Import targets from ant classpath. >+ * <p> >+ * <pre> >+ * <import resource="common-targets.xml" classpath="example.jar" /> >+ * </pre> >+ * Import targets from a specified classpath. >+ * <p> > * > * @since Ant1.6 > * @ant.task category="control" >@@ -55,7 +70,9 @@ > public class ImportTask extends Task { > private String file; > private boolean optional; >- private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); >+ private String resource; >+ protected Path classpath; >+ private static final FileUtils FILE_UTILS = FileUtils.newFileUtils(); > > /** > * sets the optional attribute >@@ -78,14 +95,59 @@ > this.file = file; > } > >+ /** >+ * @param resource The resource to set. >+ */ >+ public void setResource(String resource) { >+ this.resource = resource; >+ } >+ >+ /** >+ * The classpath to use when looking up a resource. >+ * >+ * @param classpath to add to any existing classpath >+ */ >+ public void setClasspath(Path classpath) { >+ if (this.classpath == null) { >+ this.classpath = classpath; >+ } else { >+ this.classpath.append(classpath); >+ } >+ } >+ >+ /** >+ * The classpath to use when looking up a resource. >+ */ >+ public Path createClasspath() { >+ if (this.classpath == null) { >+ this.classpath = new Path(getProject()); >+ } >+ return this.classpath.createPath(); >+ } >+ >+ /** >+ * the classpath to use when looking up a resource, given as reference to a >+ * <path> defined elsewhere >+ */ >+ public void setClasspathRef(Reference r) { >+ createClasspath().setRefid(r); >+ } >+ >+ /** >+ * @since Ant 1.5 >+ */ >+ public Path getClasspath() { >+ return classpath; >+ } >+ > /** > * This relies on the task order model. > * > */ > public void execute() { >- if (file == null) { >- throw new BuildException("import requires file attribute"); >- } >+ if (file == null && resource == null) { >+ throw new BuildException("import requires file or resource attributes"); >+ } > if (getOwningTarget() == null > || !"".equals(getOwningTarget().getName())) { > throw new BuildException("import only allowed as a top-level task"); >@@ -114,40 +176,86 @@ > > File buildFile = new File(getLocation().getFileName()).getAbsoluteFile(); > >+ if(file!=null) >+ getProject().log("Importing file " + file + " from " >+ + buildFile.getAbsolutePath(), Project.MSG_VERBOSE); >+ >+ if(resource!=null) >+ getProject().log("Importing resource " + resource + " from " >+ + buildFile.getAbsolutePath(), Project.MSG_VERBOSE); >+ > // Paths are relative to the build file they're imported from, > // *not* the current directory (same as entity includes). > > File buildFileParent = new File(buildFile.getParent()); >- File importedFile = FILE_UTILS.resolveFile(buildFileParent, file); >+ Object importedObject = null; >+ >+ if (file != null) { >+ File importedFile = FILE_UTILS.resolveFile(buildFileParent, file); >+ if (!importedFile.exists()) { >+ String message = "Cannot find " + file + " imported from " >+ + buildFile.getAbsolutePath(); >+ if (optional) { >+ getProject().log(message, Project.MSG_VERBOSE); >+ return; >+ } else { >+ throw new BuildException(message); >+ } >+ } > >- getProject().log("Importing file " + importedFile + " from " >- + buildFile.getAbsolutePath(), Project.MSG_VERBOSE); >+ importedFile = new File(getPath(importedFile)); >+ importedObject = importedFile; >+ } else if (resource != null) { >+ log("Resource Loading " + resource, Project.MSG_VERBOSE); >+ URL importedURL = null; >+ ClassLoader cL = null; > >- if (!importedFile.exists()) { >- String message = >- "Cannot find " + file + " imported from " >- + buildFile.getAbsolutePath(); >- if (optional) { >- getProject().log(message, Project.MSG_VERBOSE); >- return; >- } else { >- throw new BuildException(message); >- } >- } >+ if (classpath != null) { >+ cL = getProject().createClassLoader(classpath); >+ } else { >+ cL = this.getClass().getClassLoader(); >+ } > >- if (importStack.contains(importedFile)) { >+ if (cL == null) { >+ importedURL = ClassLoader.getSystemResource(resource); >+ } else { >+ importedURL = cL.getResource(resource); >+ } >+ >+ if (importedURL == null) { >+ String message = "Cannot find " + resource + " imported from " >+ + buildFile.getAbsolutePath(); >+ if (optional) { >+ getProject().log(message, Project.MSG_VERBOSE); >+ return; >+ } else { >+ throw new BuildException(message); >+ } >+ } >+ importedObject = importedURL; >+ } >+ >+ if (importStack.contains(importedObject)) { > getProject().log( >- "Skipped already imported file:\n " >- + importedFile + "\n", Project.MSG_VERBOSE); >+ "Skipped already imported file/resource:\n " >+ + importedObject + "\n", Project.MSG_VERBOSE); > return; > } > > try { >- helper.parse(getProject(), importedFile); >+ helper.parse(getProject(), importedObject); > } catch (BuildException ex) { > throw ProjectHelper.addLocationToBuildException( > ex, getLocation()); > } > } >+ >+ private static String getPath(File file) { >+ try { >+ return file.getCanonicalPath(); >+ } catch (IOException e) { >+ return file.getAbsolutePath(); >+ } >+ } > > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 41701
:
19635
| 19644