Search in sources :

Example 6 with UsbdmException

use of net.sourceforge.usbdm.jni.UsbdmException in project usbdm-eclipse-plugins by podonoghue.

the class DevicePeripheralsProviderInterface method getProvider.

/**
 * Get provider from ID
 *
 * @param providerId
 * @return
 * @throws Exception
 */
public IPeripheralDescriptionProvider getProvider(final String providerId) throws UsbdmException {
    // System.err.println("IPeripheralDescriptionProvider.getProvider() looking for providerId = " + providerId);
    IExtensionRegistry registry = Platform.getExtensionRegistry();
    if (registry == null) {
        throw new UsbdmException("DevicePeripheralsProviderInterface.getProvider() failed to get registry ");
    }
    IConfigurationElement[] config = registry.getConfigurationElementsFor(PeripheralDescriptionProvider_ID);
    // Get provider
    final ArrayList<IPeripheralDescriptionProvider> peripheralDescriptionProvider = new ArrayList<IPeripheralDescriptionProvider>();
    IApplyTo listProviderNames = new IApplyTo(config) {

        @Override
        public void run(IPeripheralDescriptionProvider provider) {
            if (provider.getId().equals(providerId)) {
                peripheralDescriptionProvider.add(provider);
            }
        }
    };
    listProviderNames.foreach();
    if (peripheralDescriptionProvider.size() > 0) {
        return peripheralDescriptionProvider.get(0);
    }
    throw new UsbdmException("DevicePeripheralsProviderInterface.getProvider() failed to get provider for " + providerId);
}
Also used : ArrayList(java.util.ArrayList) UsbdmException(net.sourceforge.usbdm.jni.UsbdmException) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) IExtensionRegistry(org.eclipse.core.runtime.IExtensionRegistry)

Example 7 with UsbdmException

use of net.sourceforge.usbdm.jni.UsbdmException in project usbdm-eclipse-plugins by podonoghue.

the class Peripheral method writeHeaderFileDmaInformation.

/**
 * Write header file miscellaneous information
 *
 * @param writer
 * @throws UsbdmException
 */
void writeHeaderFileDmaInformation(PrintWriter writer) throws UsbdmException {
    final Pattern peripheralPattern = Pattern.compile("DMAMUX(\\d+)");
    final Matcher peripheralMatcher = peripheralPattern.matcher(getName());
    if (!peripheralMatcher.matches()) {
        return;
    }
    boolean doneBraces = false;
    // System.err.println("writeHeaderFileDmaInformation() - creating enum for "+ this);
    HashSet<String> usedEnums = null;
    String instance = peripheralMatcher.group(1);
    for (Cluster cluster : getRegisters()) {
        if (cluster instanceof Register) {
            Register register = (Register) cluster;
            final Pattern registerPattern = Pattern.compile("CHCFG_?(LOW|HIGH)?.*");
            final Matcher registerMatcher = registerPattern.matcher(register.getName());
            for (Field field : register.getFields()) {
                if (registerMatcher.matches()) {
                    // System.err.println("writeHeaderFileDmaInformation() - matched "+register);
                    String description = "";
                    String modifier = "";
                    String offset = "";
                    if (registerMatcher.group(1) != null) {
                        if (registerMatcher.group(1).equals("LOW")) {
                            description = " - low channels 0-15";
                            modifier = "Low";
                        } else if (registerMatcher.group(1).equals("HIGH")) {
                            description = " - high channels 16-31";
                            modifier = "High";
                            offset = "0x800|";
                        }
                    }
                    if (field.getName().equals("SOURCE")) {
                        for (Enumeration e : field.getEnumerations()) {
                            if (e.getName().startsWith("Reserved")) {
                                continue;
                            }
                            String identifier = "Dma" + instance + "Slot" + modifier + "_" + e.getName();
                            if (!Utiltity.isCIdentifier(identifier)) {
                                throw new UsbdmException("Invalid name for C identifier in DMAMUX names \'" + identifier + "\'");
                            }
                            if (!doneBraces) {
                                usedEnums = new HashSet<String>();
                                writer.print(String.format(DMA_ENUM_OPENING, description));
                                doneBraces = true;
                            }
                            if (usedEnums.contains(identifier)) {
                                continue;
                            // throw new UsbdmException("Repeated enum value for DMA slot" + identifier);
                            }
                            usedEnums.add(identifier);
                            writer.print(String.format(DMA_FORMAT, identifier, offset + e.getValue(), e.getDescription() + description));
                        }
                    }
                }
            }
        }
    }
    if (doneBraces) {
        writer.print(String.format(DMA_ENUM_CLOSING));
        doneBraces = false;
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) UsbdmException(net.sourceforge.usbdm.jni.UsbdmException)

Example 8 with UsbdmException

use of net.sourceforge.usbdm.jni.UsbdmException in project usbdm-eclipse-plugins by podonoghue.

the class ExampleList method parseExampleList.

/**
 * Parse exampleList
 *
 * @param exampleListElement <exampleList> element
 *
 * @return list of project elements
 *
 * @throws UsbdmException
 */
private ArrayList<ProjectInformation> parseExampleList(Element exampleListElement) throws UsbdmException {
    ArrayList<ProjectInformation> projectList = new ArrayList<ProjectInformation>();
    for (Node node = exampleListElement.getFirstChild(); node != null; node = node.getNextSibling()) {
        // element node for <example>
        if (node.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        Element element = (Element) node;
        if (element.getTagName() != "example") {
            throw new UsbdmException("parseExampleList(" + node.getNodeName() + ") - not example element");
        }
        String description = element.getAttribute("description");
        String longDescription = element.getTextContent().trim();
        IPath path = new Path(exampleFolderPath + "/" + element.getAttribute("path"));
        String buildTool = element.getAttribute("buildTool");
        // File    file       = path.toFile();
        // if (!file.isFile() || !file.canRead()) {
        // throw new UsbdmException("Failed to find Example Project file: " + path.toString());
        // }
        String family = element.getAttribute("family");
        ProjectInformation projectInfo = new ProjectInformation(description, longDescription, path, family, buildTool);
        projectList.add(projectInfo);
        parseVariables(projectInfo, element);
    }
    return projectList;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) UsbdmException(net.sourceforge.usbdm.jni.UsbdmException)

Example 9 with UsbdmException

use of net.sourceforge.usbdm.jni.UsbdmException in project usbdm-eclipse-plugins by podonoghue.

the class ExampleList method parseXmlFile.

/**
 * @param iPath path to example list XML file
 *
 * @throws UsbdmException
 */
void parseXmlFile(IPath iPath) throws UsbdmException {
    // Get the factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        // Using factory get an instance of document builder
        DocumentBuilder db = dbf.newDocumentBuilder();
        // Parse using builder to get DOM representation of the XML file
        dom = db.parse(iPath.toFile());
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
        throw new UsbdmException(pce.getMessage());
    } catch (SAXException se) {
        se.printStackTrace();
        throw new UsbdmException(se.getMessage());
    } catch (IOException ioe) {
        ioe.printStackTrace();
        throw new UsbdmException(ioe.getMessage());
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) UsbdmException(net.sourceforge.usbdm.jni.UsbdmException) SAXException(org.xml.sax.SAXException)

Example 10 with UsbdmException

use of net.sourceforge.usbdm.jni.UsbdmException in project usbdm-eclipse-plugins by podonoghue.

the class UsbdmExampleProjectsWizard method performFinish.

@Override
public boolean performFinish() {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    ZipFile zipFile = null;
    String internalProjectDirectoryName = null;
    if (!usbdmSelectProjectWizardPage.validate()) {
        return false;
    }
    // Unzip example into workspace
    try {
        usbdmSelectProjectWizardPage.saveSetting();
        ProjectInformation projectInformation = usbdmSelectProjectWizardPage.getProjectInformation();
        zipFile = new ZipFile(projectInformation.getPath().toFile());
        ArrayList<Attribute> attributes = projectInformation.getAttributes();
        attributes.add(new Attribute("projectName", projectInformation.getDescription()));
        addCommonAttributes(attributes);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        if (!entries.hasMoreElements()) {
            throw new UsbdmException("Example Zip file is empty");
        }
        IPath workspaceRootPath = workspace.getRoot().getLocation();
        IPath projectPath = null;
        // Do pass through zip file creating directories & files
        entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            // System.err.println("performFinish() entry = " + entry.getName());
            IPath internalPath = new Path(entry.getName());
            if (internalPath.segmentCount() < 1) {
                throw new UsbdmException("Example project has invalid structure, \'" + entry.toString() + "\' is missing project directory)");
            }
            if (internalProjectDirectoryName == null) {
                // Haven't got project directory yet - determine from root of first entry
                internalProjectDirectoryName = internalPath.segment(0);
                projectPath = workspaceRootPath.append(internalProjectDirectoryName);
                // System.err.println("performFinish() Project Path: " + projectPath.toString());
                File directory = projectPath.toFile();
                if (directory.exists()) {
                    // Project directory should not already exist
                    throw new UsbdmException("Project Directory already exists (" + internalProjectDirectoryName + ")");
                }
                // System.err.println("performFinish() Creating project directory:   " + directory.toString());
                directory.mkdir();
            }
            if (!internalProjectDirectoryName.equals(internalPath.segment(0))) {
                // Entry is not a child of project directory
                throw new UsbdmException("Example project has invalid structure, \'" + entry.toString() + "\' is outside project directory)");
            }
            if ((internalPath.segmentCount() == 1) && !entry.isDirectory()) {
                // Only 1 segment => must be project directory
                throw new UsbdmException("Example project has invalid structure, \'" + entry.toString() + "\' is outside project directory)");
            }
            IPath filePath = workspaceRootPath.append(internalPath);
            // Create each directory in path (excluding last segment which may be a file or directory)
            File directory = filePath.uptoSegment(filePath.segmentCount() - 1).toFile();
            if (!directory.exists()) {
                // System.err.println("performFinish() Creating directories in path: " + directory.toString());
                directory.mkdirs();
            }
            if (entry.isDirectory()) {
                // Make the directory
                directory = filePath.toFile();
                if (!directory.exists()) {
                    // System.err.println("performFinish() Creating directory:           " + filePath.toString());
                    directory.mkdir();
                }
            } else {
                // System.err.println("performFinish() Creating file:                " + filePath.toString());
                String fileExtension = filePath.getFileExtension();
                String[] textFiles = { "project", "cproject", "h", "hpp", "c", "cpp", "s", "asm", "ld", "launch", "mk", "rc", "d" };
                boolean doConversion = false;
                if ((fileExtension != null) && (fileExtension.length() > 0)) {
                    for (String textExtension : textFiles) {
                        if (fileExtension.equalsIgnoreCase(textExtension)) {
                            doConversion = true;
                            break;
                        }
                    }
                } else {
                    String filename = filePath.lastSegment();
                    String[] specialFiles = { "makefile", "timestamp" };
                    if ((filename != null) && (filename.length() > 0)) {
                        for (String specialFile : specialFiles) {
                            if (filename.equalsIgnoreCase(specialFile)) {
                                doConversion = true;
                                break;
                            }
                        }
                    }
                }
                if (doConversion) {
                    // System.err.println("performFinish() Converting file:                " + filePath.toString());
                    convertTextStream(zipFile.getInputStream(entry), new FileOutputStream(filePath.toFile()), attributes);
                } else {
                    // System.err.println("performFinish() Copying file:                   " + filePath.toString());
                    copyBinaryStream(zipFile.getInputStream(entry), new FileOutputStream(filePath.toFile()));
                }
            }
        }
        ;
        if (projectPath == null) {
            throw new UsbdmException("No project Folder found");
        }
        IProjectDescription description = workspace.loadProjectDescription(projectPath.append(".project"));
        IProject newProject = workspace.getRoot().getProject(description.getName());
        newProject.create(description, null);
        newProject.open(null);
        String additionalProjectNature = null;
        if ((usbdmSelectProjectWizardPage.getBuildToolsId().equals(UsbdmSharedConstants.CODESOURCERY_ARM_BUILD_ID)) || (usbdmSelectProjectWizardPage.getBuildToolsId().equals(UsbdmSharedConstants.ARMLTD_ARM_BUILD_ID))) {
            additionalProjectNature = "net.sourceforge.usbdm.cdt.tools.ArmProjectNature";
        } else if (usbdmSelectProjectWizardPage.getBuildToolsId().equals(UsbdmSharedConstants.CODESOURCERY_COLDFIRE_BUILD_ID)) {
            additionalProjectNature = "net.sourceforge.usbdm.cdt.tools.ColdfireProjectNature";
        }
        if (additionalProjectNature != null) {
            IProjectDescription projectDescription = newProject.getDescription();
            String[] currentIds = projectDescription.getNatureIds();
            String[] newIds = new String[currentIds.length + 1];
            int index;
            for (index = 0; index < currentIds.length; index++) {
                newIds[index] = currentIds[index];
            // System.err.println("AddNature.process() Copying nature : " + newIds[index]);
            }
            newIds[index] = additionalProjectNature;
            // System.err.println("AddNature.process() Adding nature : " + newIds[index]);
            projectDescription.setNatureIds(newIds);
            newProject.setDescription(projectDescription, null);
            ManagedBuildManager.saveBuildInfo(newProject, true);
        }
    } catch (Exception e) {
        e.printStackTrace();
        MessageBox errBox = new MessageBox(getShell(), SWT.ERROR);
        errBox.setText("Error Creating Project");
        errBox.setMessage("Reason:\n\n    " + e.getMessage());
        errBox.open();
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
            }
        }
    }
    return true;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) Attribute(net.sourceforge.usbdm.cdt.ui.examplewizard.ExampleList.Attribute) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ProjectInformation(net.sourceforge.usbdm.cdt.ui.examplewizard.ExampleList.ProjectInformation) IProject(org.eclipse.core.resources.IProject) UsbdmException(net.sourceforge.usbdm.jni.UsbdmException) IOException(java.io.IOException) MessageBox(org.eclipse.swt.widgets.MessageBox) ZipFile(java.util.zip.ZipFile) IWorkspace(org.eclipse.core.resources.IWorkspace) FileOutputStream(java.io.FileOutputStream) IProjectDescription(org.eclipse.core.resources.IProjectDescription) UsbdmException(net.sourceforge.usbdm.jni.UsbdmException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

UsbdmException (net.sourceforge.usbdm.jni.UsbdmException)11 IPath (org.eclipse.core.runtime.IPath)3 Path (org.eclipse.core.runtime.Path)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Peripheral (net.sourceforge.usbdm.deviceEditor.information.Peripheral)2 DevicePeripherals (net.sourceforge.usbdm.peripheralDatabase.DevicePeripherals)2 MessageBox (org.eclipse.swt.widgets.MessageBox)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1