Search in sources :

Example 6 with Visitor

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) Visitor(net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor) HashMap(java.util.HashMap) Document(org.w3c.dom.Document) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) FileFilter(java.io.FileFilter) File(java.io.File)

Example 7 with Visitor

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 &lt;appliesWhen&gt; 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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) Visitor(net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor) HashMap(java.util.HashMap) Document(org.w3c.dom.Document) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) FileFilter(java.io.FileFilter) File(java.io.File)

Aggregations

Visitor (net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor)7 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)7 ProjectAction (net.sourceforge.usbdm.packageParser.ProjectAction)5 Value (net.sourceforge.usbdm.packageParser.ProjectActionList.Value)5 ProjectActionList (net.sourceforge.usbdm.packageParser.ProjectActionList)4 HashMap (java.util.HashMap)3 ProjectConstant (net.sourceforge.usbdm.packageParser.ProjectConstant)3 File (java.io.File)2 FileFilter (java.io.FileFilter)2 Result (net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor.Result)2 ProjectVariable (net.sourceforge.usbdm.packageParser.ProjectVariable)2 WizardPageInformation (net.sourceforge.usbdm.packageParser.WizardPageInformation)2 IPath (org.eclipse.core.runtime.IPath)2 Path (org.eclipse.core.runtime.Path)2 Button (org.eclipse.swt.widgets.Button)2 Document (org.w3c.dom.Document)2 FileNotFoundException (java.io.FileNotFoundException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 BitmaskVariable (net.sourceforge.usbdm.deviceEditor.information.BitmaskVariable)1