use of net.sourceforge.usbdm.packageParser.ProjectConstant in project usbdm-eclipse-plugins by podonoghue.
the class ProcessProjectActions method process.
public static void process(final Wizard wizard, final IProject projectHandle, final Device device, final ProjectActionList actionList, final Map<String, String> variableMap, final IProgressMonitor monitor) throws Exception {
// System.err.println("ProcessProjectActions.process("+device.getName()+") =============================================");
if (actionList == null) {
return;
}
final ApplyOptions applyOptions = new ApplyOptions(projectHandle);
class MyVisitor implements ProjectActionList.Visitor {
@Override
public Result applyTo(ProjectAction action, Value result, IProgressMonitor monitor) {
// System.err.println("ProjectCustomAction: "+action.toString());
try {
if (action instanceof FileAction) {
new AddTargetFiles().process(projectHandle, variableMap, (FileAction) action, monitor);
} else if (action instanceof DeleteResourceAction) {
new DeleteResource().process(projectHandle, variableMap, (DeleteResourceAction) action, monitor);
} else if (action instanceof CreateFolderAction) {
ProjectUtilities.createFolder(projectHandle, variableMap, (CreateFolderAction) action, monitor);
} else if (action instanceof ProjectOption) {
applyOptions.process(variableMap, (ProjectOption) action, monitor);
} else if (action instanceof ExcludeAction) {
ProjectUtilities.excludeItem(projectHandle, (ExcludeAction) action, monitor);
} else if (action instanceof ProjectActionList) {
ProjectActionList projectActionList = (ProjectActionList) action;
return new Result(projectActionList.appliesTo(device, variableMap) ? Status.CONTINUE : Status.PRUNE);
} else if (action instanceof ProjectCustomAction) {
ProjectCustomAction customAction = (ProjectCustomAction) action;
Class<?> actionClass = Class.forName(customAction.getclassName());
Object clsInstance = actionClass.newInstance();
if (!(clsInstance instanceof CustomAction)) {
throw new Exception("Custom action does not implement required interface");
}
((CustomAction) clsInstance).action(projectHandle, variableMap, monitor, customAction.getValue());
} else if (action instanceof ProjectConstant) {
// Ignore as already added to paramMap
} else if (action instanceof WizardGroup) {
// Ignore as already added to paramMap
} else if (action instanceof ProjectVariable) {
// Ignore as already added to paramMap
} else if (action instanceof WizardPageInformation) {
// Ignore as only applicable to wizard dialogues
} else {
throw new Exception("Unexpected action class: " + action.getClass());
}
} catch (Exception e) {
// e.printStackTrace();
StringBuffer sb = new StringBuffer();
sb.append("Unable to process Action " + action.toString() + "\n");
sb.append("Action id = " + action.getId() + "\n");
sb.append("Action owned by = " + action.getOwnerId() + "\n");
sb.append(e.getMessage());
return new Result(new Exception(sb.toString()));
}
return new Result(Status.CONTINUE);
}
}
;
MyVisitor visitor = new MyVisitor();
Result res = actionList.visit(visitor, null, monitor);
if (res.getStatus() == Status.EXCEPTION) {
throw res.getException();
}
}
use of net.sourceforge.usbdm.packageParser.ProjectConstant in project usbdm-eclipse-plugins by podonoghue.
the class ParseMenuXML method parseControlItem.
/**
* Parse element: <ul>
* <li> <fragment> referencing only elements below
* <li> <validate>
* <li> <template>
* <li> <projectActionList>
*</ul>
*
* Items found are recorded
*
* @param menuElement Menu element to parse
*
* @throws Exception
*/
private void parseControlItem(Element element) throws Exception {
String tagName = element.getTagName();
if (tagName == "fragment") {
for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
if (node.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
parseControlItem((Element) node);
}
} else if (tagName == "validate") {
fValidators.add(parseValidate(element));
} else if (tagName == "template") {
/**
* namespace:
* class - Template is available in
*/
String name = element.getAttribute("name");
String namespace = element.getAttribute("namespace");
if (namespace.isEmpty()) {
throw new Exception("Template is missing namespace, name='" + name + "'");
}
if (!name.isEmpty() && !namespace.equals("all")) {
throw new Exception("Named templates must have 'all' namespace, name='" + name + "'");
}
int dimension = getIntAttribute(element, "dim");
addTemplate(name, namespace, dimension, element.getTextContent().replaceAll("^\n\\s*", "").replaceAll("(\\\\n|\\n)\\s*", "\n").replaceAll("\\\\t", " "));
// System.err.println(fTemplate.toString().substring(0, 40)+"\n");
} else if (tagName == "projectActionList") {
ProjectActionList pal = PackageParser.parseRestrictedProjectActionList(element, RESOURCE_PATH);
pal.visit(new Visitor() {
@Override
public Result applyTo(ProjectAction action, Value result, IProgressMonitor monitor) {
if (action instanceof ProjectConstant) {
ProjectConstant constant = (ProjectConstant) action;
Variable var = new StringVariable(constant.getId(), constant.getId());
var.setValue(constant.getValue());
System.err.println("Adding " + var);
fProvider.addVariable(var);
}
return Visitor.CONTINUE;
}
}, null);
fProjectActionList.addProjectAction(pal);
} else {
throw new Exception("Unexpected field in parseControlItem(), value = \'" + tagName + "\'");
}
}
use of net.sourceforge.usbdm.packageParser.ProjectConstant in project usbdm-eclipse-plugins by podonoghue.
the class UsbdmNewProjectWizard method updateMapConstants.
/**
* Visits the nodes of the projectActionList and add constants found
*
* @param paramMap Map to add constants to
* @param projectActionList Action list to visit
*/
void updateMapConstants(final Map<String, String> paramMap, ProjectActionList projectActionList) {
Visitor visitor = new Visitor() {
@Override
public Result applyTo(ProjectAction action, Value result, IProgressMonitor monitor) {
try {
if (action instanceof ProjectActionList) {
ProjectActionList projectActionList = (ProjectActionList) action;
return projectActionList.appliesTo(fUsbdmProjectParametersPage_2.getDevice(), paramMap) ? CONTINUE : PRUNE;
} else if (action instanceof ProjectConstant) {
ProjectConstant projectConstant = (ProjectConstant) action;
// System.err.println(String.format("updateMapConstants(): Found constant %s => %s", projectConstant.getId(), projectConstant.getValue()));
String value = paramMap.get(projectConstant.getId());
if (value != null) {
if (projectConstant.isWeak()) {
// Ignore - assume constant is a default that has been overwritten
return CONTINUE;
}
if (!projectConstant.doOverwrite() && !value.equals(projectConstant.getValue())) {
return new Result(new Exception("paramMap already contains constant " + projectConstant.getId()));
}
}
paramMap.put(projectConstant.getId(), projectConstant.getValue());
}
return CONTINUE;
} catch (Exception e) {
return new Result(e);
}
}
};
// Visit all enabled actions and collect constants
Result result = fProjectActionList.visit(visitor, null);
if (result.getStatus() == Status.EXCEPTION) {
result.getException().printStackTrace();
}
}
use of net.sourceforge.usbdm.packageParser.ProjectConstant in project usbdm-eclipse-plugins by podonoghue.
the class UsbdmOptionsPanel method getPageData.
/**
* Add settings to paramMap
*
* @param paramMap
* @throws Exception
*/
public void getPageData(final Map<String, String> paramMap, ProjectActionList projectActionLists) {
Visitor visitor = new Visitor() {
@Override
public Result applyTo(ProjectAction action, Value result, IProgressMonitor monitor) {
try {
if (action instanceof ProjectActionList) {
ProjectActionList projectActionList = (ProjectActionList) action;
return projectActionList.appliesTo(fDevice, paramMap) ? CONTINUE : PRUNE;
} else if (action instanceof ProjectVariable) {
ProjectVariable projectVariable = (ProjectVariable) action;
Button button = fButtonMap.get(projectVariable.getId());
if (button == null) {
return new Result(new Exception("Can't find button for var : " + projectVariable + " from " + action.getId()));
}
Boolean value = button.getSelection();
// System.err.println("UsbdmOptionsPanel.getPageData() projectVariable = " + projectVariable.toString() + ", value = " + value);
paramMap.put(projectVariable.getId(), value.toString());
} else if (action instanceof ProjectConstant) {
ProjectConstant projectConstant = (ProjectConstant) action;
// System.err.println(String.format("UsbdmOptionsPanel.getPageData(): Adding constant %s => %s", projectConstant.getId(), projectConstant.getValue()));
String value = paramMap.get(projectConstant.getId());
if (value != null) {
if (projectConstant.isWeak()) {
// Ignore - assume constant is a default that has been overwritten
return CONTINUE;
}
if (!projectConstant.doOverwrite() && !value.equals(projectConstant.getValue())) {
return new Result(new Exception("paramMap already contains constant " + projectConstant.getId()));
}
}
paramMap.put(projectConstant.getId(), projectConstant.getValue());
}
return CONTINUE;
} catch (Exception e) {
return new Result(e);
}
}
};
// Visit all enabled actions and collect variables and constants
Result result = fProjectActionList.visit(visitor, null);
if (result.getStatus() == Status.EXCEPTION) {
result.getException().printStackTrace();
}
}
use of net.sourceforge.usbdm.packageParser.ProjectConstant in project usbdm-eclipse-plugins by podonoghue.
the class ProcessProjectActions method process.
/**
* Process a project action list
*
* @param projectHandle Project being manipulated
* @param actionList Actions to do
* @param variableMap Variables that may be needed
* @param monitor Progress monitor
*
* @throws Exception
*/
public void process(final IProject projectHandle, final ProjectActionList actionList, final Map<String, String> variableMap, final IProgressMonitor monitor) throws Exception {
if (actionList == null) {
return;
}
System.err.println("ProcessProjectActions.process " + actionList.getId());
final ApplyOptions applyOptions = new ApplyOptions(projectHandle);
class MyVisitor implements ProjectActionList.Visitor {
@Override
public Result applyTo(ProjectAction action, Value result, IProgressMonitor monitor) {
SubMonitor subMonitor = SubMonitor.convert(monitor);
subMonitor.subTask(action.toString());
// System.err.println("ProjectCustomAction: "+action.toString());
try {
if (action instanceof FileAction) {
new AddTargetFiles().process(projectHandle, variableMap, (FileAction) action, subMonitor);
} else if (action instanceof DeleteResourceAction) {
new DeleteResource().process(projectHandle, variableMap, (DeleteResourceAction) action, subMonitor);
} else if (action instanceof CreateFolderAction) {
// ProjectUtilities.createFolder(projectHandle, variableMap, (CreateFolderAction)action, subMonitor);
} else if (action instanceof ProjectOption) {
applyOptions.process(variableMap, (ProjectOption) action, subMonitor);
} else if (action instanceof ExcludeAction) {
ProjectUtilities.excludeItem(projectHandle, (ExcludeAction) action, subMonitor);
} else if (action instanceof ProjectActionList) {
ProjectActionList projectActionList = (ProjectActionList) action;
if (projectActionList.isDoOnceOnly()) {
if (previousActions.contains(projectActionList.getId())) {
// Don't repeat action
System.err.println("ProcessProjectActions.process - not repeating action " + projectActionList.getId());
return new Result(Status.PRUNE);
}
previousActions.add(projectActionList.getId());
}
return new Result(projectActionList.applies(variableMap) ? Status.CONTINUE : Status.PRUNE);
} else if (action instanceof ProjectCustomAction) {
ProjectCustomAction customAction = (ProjectCustomAction) action;
Class<?> actionClass = Class.forName(customAction.getclassName());
Object clsInstance = actionClass.newInstance();
if (!(clsInstance instanceof CustomAction)) {
throw new Exception("Custom action does not implement required interface");
}
((CustomAction) clsInstance).action(projectHandle, variableMap, subMonitor, customAction.getValue());
} else if (action instanceof ProjectConstant) {
// Ignore as already added to paramMap
} else if (action instanceof WizardGroup) {
// Ignore as already added to paramMap
} else if (action instanceof ProjectVariable) {
// Ignore as already added to paramMap
} else if (action instanceof WizardPageInformation) {
// Ignore as only applicable to wizard dialogues
} else {
throw new Exception("Unexpected action class: " + action.getClass());
}
} catch (Exception e) {
// e.printStackTrace();
StringBuffer sb = new StringBuffer();
sb.append("Unable to process Action " + action.toString() + "\n");
sb.append("Action id = " + action.getId() + "\n");
sb.append("Action owned by = " + action.getOwnerId() + "\n");
sb.append(e.getMessage());
return new Result(new Exception(sb.toString(), e));
}
return new Result(Status.CONTINUE);
}
}
;
MyVisitor visitor = new MyVisitor();
Result res = actionList.visit(visitor, null, monitor);
if (res.getStatus() == Status.EXCEPTION) {
throw res.getException();
}
}
Aggregations