1 18 package org.apache.tools.ant.taskdefs; 19 20 import java.io.File ; 21 import java.io.UnsupportedEncodingException ; 22 23 import org.apache.tools.ant.Task; 24 import org.apache.tools.ant.types.Path; 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.launch.Locator; 27 import org.apache.tools.ant.util.FileUtils; 28 29 36 public class ManifestClassPath extends Task { 37 38 39 private String name; 40 41 42 private File dir; 43 44 45 private int maxParentLevels = 2; 46 47 48 private Path path; 49 50 55 public void execute() { 56 if (name == null) { 57 throw new BuildException("Missing 'property' attribute!"); 58 } 59 if (dir == null) { 60 throw new BuildException("Missing 'jarfile' attribute!"); 61 } 62 if (getProject().getProperty(name) != null) { 63 throw new BuildException("Property '" + name + "' already set!"); 64 } 65 if (path == null) { 66 throw new BuildException("Missing nested <classpath>!"); 67 } 68 69 final FileUtils fileUtils = FileUtils.getFileUtils(); 71 dir = fileUtils.normalize(dir.getAbsolutePath()); 72 73 File currDir = dir; 76 String [] dirs = new String [maxParentLevels + 1]; 77 for (int i = 0; i < maxParentLevels + 1; ++i) { 78 dirs[i] = currDir.getAbsolutePath() + File.separatorChar; 79 currDir = currDir.getParentFile(); 80 if (currDir == null) { 81 maxParentLevels = i + 1; 82 break; 83 } 84 } 85 86 String [] elements = path.list(); 87 StringBuffer buffer = new StringBuffer (); 88 StringBuffer element = new StringBuffer (); 89 for (int i = 0; i < elements.length; ++i) { 90 File pathEntry = new File (elements[i]); 92 pathEntry = fileUtils.normalize(pathEntry.getAbsolutePath()); 93 String fullPath = pathEntry.getAbsolutePath(); 94 95 String relPath = null; 98 for (int j = 0; j <= maxParentLevels; ++j) { 99 String dir = dirs[j]; 100 if (!fullPath.startsWith(dir)) { 101 continue; 102 } 103 104 element.setLength(0); 107 for (int k = 0; k < j; ++k) { 108 element.append(".."); 109 element.append(File.separatorChar); 110 } 111 element.append(fullPath.substring(dir.length())); 112 relPath = element.toString(); 113 break; 114 } 115 116 if (relPath == null) { 118 throw new BuildException( 119 "No suitable relative path from " 120 + dir + " to " + fullPath); 121 } 122 123 if (File.separatorChar != '/') { 127 relPath = relPath.replace(File.separatorChar, '/'); 128 } 129 if (pathEntry.isDirectory()) { 130 relPath = relPath + '/'; 131 } 132 try { 133 relPath = Locator.encodeURI(relPath); 134 } catch (UnsupportedEncodingException exc) { 135 throw new BuildException(exc); 136 } 137 buffer.append(relPath); 138 buffer.append(' '); 139 } 140 141 getProject().setNewProperty(name, buffer.toString().trim()); 143 } 144 145 150 public void setProperty(String name) { 151 this.name = name; 152 } 153 154 160 public void setJarFile(File jarfile) { 161 File parent = jarfile.getParentFile(); 162 if (!parent.isDirectory()) { 163 throw new BuildException("Jar's directory not found: " + parent); 164 } 165 this.dir = parent; 166 } 167 168 174 public void setMaxParentLevels(int levels) { 175 this.maxParentLevels = levels; 176 } 177 178 183 public void addClassPath(Path path) { 184 this.path = path; 185 } 186 187 } 188 | Popular Tags |