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. */
  17. package org.apache.tools.ant.taskdefs.optional.extension.resolvers;
  18. import java.io.File;
  19. import java.net.URL;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.Project;
  22. import org.apache.tools.ant.taskdefs.Get;
  23. import org.apache.tools.ant.taskdefs.optional.extension.Extension;
  24. import org.apache.tools.ant.taskdefs.optional.extension.ExtensionResolver;
  25. /**
  26. * Resolver that just returns s specified location.
  27. *
  28. * @version $Revision: 1.6.2.4 $ $Date: 2004/03/09 17:01:46 $
  29. */
  30. public class URLResolver
  31. implements ExtensionResolver {
  32. private File m_destfile;
  33. private File m_destdir;
  34. private URL m_url;
  35. public void setUrl(final URL url) {
  36. m_url = url;
  37. }
  38. public void setDestfile(final File destfile) {
  39. m_destfile = destfile;
  40. }
  41. public void setDestdir(final File destdir) {
  42. m_destdir = destdir;
  43. }
  44. public File resolve(final Extension extension,
  45. final Project project)
  46. throws BuildException {
  47. validate();
  48. final File file = getDest();
  49. final Get get = (Get) project.createTask("get");
  50. get.setDest(file);
  51. get.setSrc(m_url);
  52. get.execute();
  53. return file;
  54. }
  55. private File getDest() {
  56. if (null != m_destfile) {
  57. return m_destfile;
  58. } else {
  59. final String file = m_url.getFile();
  60. String filename = null;
  61. if (null == file || file.length() <= 1) {
  62. filename = "default.file";
  63. } else {
  64. int index = file.lastIndexOf('/');
  65. if (-1 == index) {
  66. index = 0;
  67. }
  68. filename = file.substring(index);
  69. }
  70. return new File(m_destdir, filename);
  71. }
  72. }
  73. private void validate() {
  74. if (null == m_url) {
  75. final String message = "Must specify URL";
  76. throw new BuildException(message);
  77. }
  78. if (null == m_destdir && null == m_destfile) {
  79. final String message = "Must specify destination file or directory";
  80. throw new BuildException(message);
  81. } else if (null != m_destdir && null != m_destfile) {
  82. final String message = "Must not specify both destination file or directory";
  83. throw new BuildException(message);
  84. }
  85. }
  86. public String toString() {
  87. return "URL[" + m_url + "]";
  88. }
  89. }