use of net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor in project usbdm-eclipse-plugins by podonoghue.
the class PackageParser method getKDSPackageList.
/**
* Locates all package lists
*
* @param variableMap Variables to use when evaluation conditions for inclusion
*
* @return ArrayList of ProjectActionLists (an empty list if none)
* @throws Exception
*/
public static ProjectActionList getKDSPackageList(final Map<String, String> variableMap) throws Exception {
ProjectActionList projectActionList = new ProjectActionList("---KDSPackageList---");
IPath packagesDirectoryPath = Usbdm.getResourcePath().append("Stationery/KSDK_Libraries");
File[] packageDirectories = packagesDirectoryPath.toFile().listFiles();
Arrays.sort(packageDirectories);
if (packageDirectories == null) {
System.err.println("No packages found at " + packagesDirectoryPath.toOSString());
return projectActionList;
}
HashMap<String, ProjectVariable> projectVariables = new HashMap<String, ProjectVariable>();
for (File packageDirectory : packageDirectories) {
if (packageDirectory.isDirectory()) {
// Get all XML files
File[] files = packageDirectory.listFiles(new FileFilter() {
public boolean accept(File arg0) {
return arg0.getName().matches(".*\\.xml$");
}
});
Arrays.sort(files);
Path path = new Path(packageDirectory.toPath().toAbsolutePath().toString());
for (File f : files) {
if (f.isFile()) {
try {
PackageParser packParser = new PackageParser(path, projectVariables);
Document document = packParser.parseXmlFile(f.toString());
ProjectActionList newProjectActionList = packParser.parseDocument(document);
if (newProjectActionList.applies(variableMap)) {
// System.err.println("===============================================");
// System.err.println("projectAction = " + newProjectActionList.toString());
// System.err.println("projectAction applyWhen = " + newProjectActionList.getApplyWhenCondition().toString());
/*
* Collect constants for action-list
*/
final Visitor visitor = new ProjectActionList.Visitor() {
@Override
public Result applyTo(ProjectAction action, ProjectActionList.Value result, IProgressMonitor monitor) {
try {
// System.err.println(String.format("PackageParser.getKDSPackageList(): applyTo(), %s", action.toString()));
if (action instanceof ProjectActionList) {
ProjectActionList projectActionList = (ProjectActionList) action;
Result rc = projectActionList.applies(variableMap) ? CONTINUE : PRUNE;
// System.err.println(String.format("PackageParser.getDevicePackageList(): Found ProjectActionList, %d, \'%s\'", depth++, rc.toString()));
return rc;
} else if (action instanceof ProjectConstant) {
ProjectConstant projectConstant = (ProjectConstant) action;
// System.err.println(String.format("PackageParser.getDevicePackageList(): Adding constant %s => %s", projectConstant.getId(), projectConstant.getValue()));
if (!projectConstant.doOverwrite()) {
String value = variableMap.get(projectConstant.getId());
if ((value != null) && !value.equals(projectConstant.getValue())) {
return new Result(new Exception("Repeated constant"));
}
}
variableMap.put(projectConstant.getId(), projectConstant.getValue());
}
return PRUNE;
} catch (Exception e) {
return new Result(e);
}
}
};
newProjectActionList.visit(visitor, null);
// Add applicable actions
projectActionList.addProjectAction(newProjectActionList);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
// listOfProjectLists.setfVariableMap(projectVariables);
return projectActionList;
}
use of net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor in project usbdm-eclipse-plugins by podonoghue.
the class PackageParser method getDevicePackageList.
/**
* Locates actions that apply to this device. <br>
* Only evaluates the top-level <appliesWhen> clause when deciding inclusion
*
* @param device Device to investigate
* @param variableMap Variables to use when evaluation conditions
*
* @return Applicable actions
*/
public static ProjectActionList getDevicePackageList(final Device device, final Map<String, String> variableMap) {
// System.err.println(String.format("PackageParser.getDevicePackageList(): Device = %s", device.getName()));
ProjectActionList projectActionList = new ProjectActionList("---root " + device.getName() + " ---");
IPath packagesDirectoryPath = Usbdm.getResourcePath().append("Stationery/Packages");
File[] packageDirectories = packagesDirectoryPath.toFile().listFiles();
Arrays.sort(packageDirectories);
if (packageDirectories == null) {
System.err.println("No packages found at " + packagesDirectoryPath.toOSString());
return projectActionList;
}
final HashMap<String, ProjectVariable> projectVariables = new HashMap<String, ProjectVariable>();
for (File packageDirectory : packageDirectories) {
if (packageDirectory.isDirectory()) {
// Get all XML files
File[] files = packageDirectory.listFiles(new FileFilter() {
public boolean accept(File arg0) {
return arg0.getName().matches(".*\\.xml$");
}
});
Arrays.sort(files);
Path path = new Path(packageDirectory.toPath().toAbsolutePath().toString());
for (File f : files) {
if (f.isFile()) {
try {
PackageParser packParser = new PackageParser(path, projectVariables);
Document document = packParser.parseXmlFile(f.toString());
ProjectActionList newProjectActionList = packParser.parseDocument(document);
if (newProjectActionList.appliesTo(device, variableMap)) {
/*
* Add new project variables
*/
for (String key : packParser.getCurrentVariables().keySet()) {
projectVariables.put(key, packParser.getCurrentVariables().get(key));
}
/*
* Collect constants for action-list
*/
final Visitor visitor = new ProjectActionList.Visitor() {
@Override
public Result applyTo(ProjectAction action, ProjectActionList.Value result, IProgressMonitor monitor) {
try {
// System.err.println(String.format("PackageParser.getDevicePackageList(): applyTo(), %s", action.toString()));
if (action instanceof ProjectActionList) {
ProjectActionList projectActionList = (ProjectActionList) action;
Result rc = projectActionList.appliesTo(device, variableMap) ? CONTINUE : PRUNE;
// System.err.println(String.format("PackageParser.getDevicePackageList(): Found ProjectActionList, %d, \'%s\'", depth++, rc.toString()));
return rc;
} else if (action instanceof ProjectConstant) {
ProjectConstant projectConstant = (ProjectConstant) action;
// System.err.println(String.format("PackageParser.getDevicePackageList(): Adding constant %s => %s", projectConstant.getId(), projectConstant.getValue()));
if (!projectConstant.doOverwrite()) {
String value = variableMap.get(projectConstant.getId());
if ((value != null) && !value.equals(projectConstant.getValue())) {
return new Result(new Exception("Repeated constant"));
}
}
variableMap.put(projectConstant.getId(), projectConstant.getValue());
}
return PRUNE;
} catch (Exception e) {
return new Result(e);
}
}
};
newProjectActionList.visit(visitor, null);
// Add applicable actions
projectActionList.addProjectAction(newProjectActionList);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
// listOfProjectLists.setfVariableMap(projectVariables);
return projectActionList;
}
Aggregations