1 11 package org.eclipse.ant.internal.core; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.IOException ; 16 import java.util.ArrayList ; 17 import java.util.Enumeration ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.core.runtime.Path; 26 import org.eclipse.core.variables.IStringVariableManager; 27 import org.eclipse.core.variables.VariablesPlugin; 28 import org.osgi.framework.BundleContext; 29 30 public class AntCoreUtil { 31 32 private static BundleContext fgContext= null; 33 34 public static void setBundleContext(BundleContext context) { 35 fgContext= context; 36 } 37 38 public static BundleContext getBundleContext() { 39 return fgContext; 40 } 41 42 45 public static ArrayList getArrayList(String [] args) { 46 if (args == null) { 47 return null; 48 } 49 ArrayList result = new ArrayList (args.length); 53 for (int i = 0; i < args.length; i++) { 54 result.add(args[i]); 55 } 56 return result; 57 } 58 59 66 public static String getArgument(List commands, String param) { 67 if (commands == null) { 68 return null; 69 } 70 int index = commands.indexOf(param); 71 if (index == -1) { 72 return null; 73 } 74 commands.remove(index); 75 if (index == commands.size()) { return ""; } 78 79 String command = (String ) commands.get(index); 80 if (command.startsWith("-")) { return ""; } 83 commands.remove(index); 84 return command; 85 } 86 87 public static void processMinusDProperties(List commands, Map userProperties) { 88 Iterator iter= commands.iterator(); 89 while (iter.hasNext()) { 90 String arg = (String ) iter.next(); 91 if (arg.startsWith("-D")) { String name = arg.substring(2, arg.length()); 93 String value = null; 94 int posEq = name.indexOf("="); if (posEq == 0) { 96 value= name.substring(1); 97 name= ""; } else if (posEq > 0 && posEq != name.length() - 1) { 99 value = name.substring(posEq + 1).trim(); 100 name = name.substring(0, posEq); 101 } 102 103 if (value == null) { 104 continue; 106 } 107 108 userProperties.put(name, value); 109 iter.remove(); 110 } 111 } 112 } 113 114 public static File getFileRelativeToBaseDir(String fileName, String base, String buildFileLocation) { 115 IPath path= new Path(fileName); 116 if (!path.isAbsolute()) { 117 if (base != null) { 118 File baseDir= new File (base); 119 path= new Path(baseDir.getAbsolutePath()); 121 } else { 122 path= new Path(buildFileLocation); 124 path= path.removeLastSegments(1); 125 } 126 path= path.addTrailingSeparator(); 127 path= path.append(fileName); 128 } 129 130 return path.toFile(); 131 } 132 133 136 public static List loadPropertyFiles(List fileNames, String base, String buildFileLocation) throws IOException { 137 List allProperties= new ArrayList (fileNames.size()); 138 for (int i = 0; i < fileNames.size(); i++) { 139 String filename = (String ) fileNames.get(i); 140 File file= getFileRelativeToBaseDir(filename, base, buildFileLocation); 141 Properties props = new Properties (); 142 FileInputStream fis = null; 143 try { 144 fis = new FileInputStream (file); 145 props.load(fis); 146 } finally { 147 if (fis != null) { 148 try { 149 fis.close(); 150 } catch (IOException e){ 151 } 152 } 153 } 154 Enumeration propertyNames = props.propertyNames(); 155 while (propertyNames.hasMoreElements()) { 156 String name = (String ) propertyNames.nextElement(); 157 158 String value= props.getProperty(name); 159 props.remove(name); 160 IStringVariableManager stringVariableManager = VariablesPlugin.getDefault().getStringVariableManager(); 161 try { 162 name= stringVariableManager.performStringSubstitution(name); 163 value= stringVariableManager.performStringSubstitution(value); 164 } catch (CoreException e) { 165 } 166 props.setProperty(name, value); 167 } 168 allProperties.add(props); 169 } 170 return allProperties; 171 } 172 } 173 | Popular Tags |