1 19 20 package org.apache.tools.ant.module.api.support; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.LinkedHashSet ; 26 import java.util.List ; 27 import java.util.Properties ; 28 import java.util.regex.Matcher ; 29 import java.util.regex.Pattern ; 30 import org.apache.tools.ant.module.api.AntProjectCookie; 31 import org.apache.tools.ant.module.api.AntTargetExecutor; 32 import org.openide.execution.ExecutorTask; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.loaders.DataObject; 36 import org.openide.util.Lookup; 37 38 44 public final class ActionUtils { 45 46 private ActionUtils() {} 47 48 57 public static ExecutorTask runTarget(FileObject buildXml, String [] targetNames, Properties properties) throws IOException , IllegalArgumentException { 58 if (buildXml == null) { 59 throw new NullPointerException ("Must pass non-null build script"); } 61 if (targetNames != null && targetNames.length == 0) { 62 throw new IllegalArgumentException ("No targets supplied"); } 64 AntProjectCookie apc = TargetLister.getAntProjectCookie(buildXml); 65 AntTargetExecutor.Env execenv = new AntTargetExecutor.Env(); 66 if (properties != null) { 67 Properties p = execenv.getProperties(); 68 p.putAll(properties); 69 execenv.setProperties(p); 70 } 71 return AntTargetExecutor.createTargetExecutor(execenv).execute(apc, targetNames); 72 } 73 74 102 public static FileObject[] findSelectedFiles(Lookup context, FileObject dir, String suffix, boolean strict) { 103 if (dir != null && !dir.isFolder()) { 104 throw new IllegalArgumentException ("Not a folder: " + dir); } 106 if (suffix != null && suffix.indexOf('/') != -1) { 107 throw new IllegalArgumentException ("Cannot includes slashes in suffix: " + suffix); } 109 Collection <FileObject> files = new LinkedHashSet <FileObject>(); for (DataObject d : context.lookupAll(DataObject.class)) { 112 FileObject f = d.getPrimaryFile(); 113 boolean matches = FileUtil.toFile(f) != null; 114 if (dir != null) { 115 matches &= (FileUtil.isParentOf(dir, f) || dir == f); 116 } 117 if (suffix != null) { 118 matches &= f.getNameExt().endsWith(suffix); 119 } 120 if (matches) { 124 files.add(f); 125 } else if (strict) { 126 return null; 127 } 128 } 129 if (files.isEmpty()) { 130 return null; 131 } 132 return files.toArray(new FileObject[files.size()]); 133 } 134 135 162 public static FileObject[] regexpMapFiles(FileObject[] fromFiles, FileObject fromDir, Pattern fromRx, FileObject toDir, String toSubst, boolean strict) throws IllegalArgumentException { 163 List <FileObject> files = new ArrayList <FileObject>(); 164 for (FileObject fromFile : fromFiles) { 165 String path = FileUtil.getRelativePath(fromDir, fromFile); 166 if (path == null) { 167 throw new IllegalArgumentException ("The file " + fromFile + " is not in " + fromDir); } 169 String toPath; 170 if (fromRx != null) { 171 Matcher m = fromRx.matcher(path); 172 toPath = m.replaceFirst(toSubst); 173 if (toPath.equals(path) && !m.find(0)) { 174 if (strict) { 176 return null; 177 } else { 178 continue; 179 } 180 } 181 } else { 182 toPath = path; 183 } 184 FileObject target = toDir.getFileObject(toPath); 185 if (target == null) { 186 if (strict) { 187 return null; 188 } else { 189 continue; 190 } 191 } 192 files.add(target); 193 } 194 return files.toArray(new FileObject[files.size()]); 195 } 196 197 208 public static String antIncludesList(FileObject[] files, FileObject dir) throws IllegalArgumentException { 209 return antIncludesList (files, dir, true); 210 } 211 212 225 public static String antIncludesList(FileObject[] files, FileObject dir, boolean recursive) throws IllegalArgumentException { 226 if (!dir.isFolder()) { 227 throw new IllegalArgumentException ("Not a folder: " + dir); } 229 StringBuffer b = new StringBuffer (); 230 for (int i = 0; i < files.length; i++) { 231 String path = FileUtil.getRelativePath(dir, files[i]); 232 if (path == null) { 233 throw new IllegalArgumentException ("The file " + files[i] + " is not in " + dir); } 235 if (i > 0) { 236 b.append(','); 237 } 238 b.append(path); 239 if (files[i].isFolder()) { 240 if (path.length() > 0) { 242 b.append('/'); } 244 b.append('*'); if (recursive) { 246 b.append('*'); } 248 } 249 } 250 return b.toString(); 251 } 252 253 } 254 | Popular Tags |