package org.eclipse.ant.internal.core;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.eclipse.ant.core.AntCorePlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.variables.IStringVariableManager;
import org.eclipse.core.variables.VariablesPlugin;
import org.osgi.framework.BundleContext;
public class AntCoreUtil {
private static BundleContext fgContext = null;
public static void setBundleContext(BundleContext context) {
fgContext = context;
}
public static BundleContext getBundleContext() {
return fgContext;
}
public static ArrayList<String> getArrayList(String[] args) {
if (args == null) {
return null;
}
ArrayList<String> result = new ArrayList<>(args.length);
for (int i = 0; i < args.length; i++) {
result.add(args[i]);
}
return result;
}
public static String getArgument(List<String> commands, String param) {
if (commands == null) {
return null;
}
int index = commands.indexOf(param);
if (index == -1) {
return null;
}
commands.remove(index);
if (index == commands.size()) {
return IAntCoreConstants.EMPTY_STRING;
}
String command = commands.get(index);
if (command.startsWith("-")) {
return IAntCoreConstants.EMPTY_STRING;
}
commands.remove(index);
return command;
}
public static void processMinusDProperties(List<String> commands, Map<String, String> userProperties) {
Iterator<String> iter = commands.iterator();
while (iter.hasNext()) {
String arg = iter.next();
if (arg.startsWith("-D")) {
String name = arg.substring(2, arg.length());
String value = null;
int posEq = name.indexOf("=");
if (posEq == 0) {
value = name.substring(1);
name = IAntCoreConstants.EMPTY_STRING;
} else if (posEq > 0 && posEq != name.length() - 1) {
value = name.substring(posEq + 1).trim();
name = name.substring(0, posEq);
}
if (value == null) {
continue;
}
userProperties.put(name, value);
iter.remove();
}
}
}
public static File getFileRelativeToBaseDir(String fileName, String base, String buildFileLocation) {
IPath path = new Path(fileName);
if (!path.isAbsolute()) {
if (base != null) {
File baseDir = new File(base);
path = new Path(baseDir.getAbsolutePath());
} else {
path = new Path(buildFileLocation);
path = path.removeLastSegments(1);
}
path = path.addTrailingSeparator();
path = path.append(fileName);
}
return path.toFile();
}
public static List<Properties> loadPropertyFiles(List<String> fileNames, String base, String buildFileLocation) throws IOException {
ArrayList<Properties> allProperties = new ArrayList<>(fileNames.size());
for (int i = 0; i < fileNames.size(); i++) {
String filename = fileNames.get(i);
File file = getFileRelativeToBaseDir(filename, base, buildFileLocation);
Properties props = new Properties();
try (FileInputStream fis = new FileInputStream(file)) {
props.load(fis);
}
Enumeration<?> propertyNames = props.propertyNames();
while (propertyNames.hasMoreElements()) {
String name = (String) propertyNames.nextElement();
String value = props.getProperty(name);
props.remove(name);
IStringVariableManager stringVariableManager = VariablesPlugin.getDefault().getStringVariableManager();
try {
name = stringVariableManager.performStringSubstitution(name);
value = stringVariableManager.performStringSubstitution(value);
}
catch (CoreException e) {
AntCorePlugin.log(e);
}
props.setProperty(name, value);
}
allProperties.add(props);
}
return allProperties;
}
}