Search in sources :

Example 1 with PeripheralValidator

use of net.sourceforge.usbdm.deviceEditor.validators.PeripheralValidator in project usbdm-eclipse-plugins by podonoghue.

the class ParseMenuXML method parsePeriperalFile.

/**
 * Parses document from top element
 *
 * @param name         Name of peripheral (used for peripheral file name e.g. adc0_diff_a => adc0_diff_a.xml
 * @param peripheral   Provides the variables. New variables will be added to this peripheral
 *
 * @return Data from model
 * @throws Exception
 *
 * Looks for the file in the following locations in order:
 * <li>Relative path : Stationery/Packages/180.ARM_Peripherals/Hardware/peripherals
 * <li>Relative path : "USBDM Resource Path"/Stationery/Packages/180.ARM_Peripherals/Hardware/peripherals
 */
public static MenuData parsePeriperalFile(String name, PeripheralWithState peripheral) throws Exception {
    fName = name;
    MenuData fData;
    try {
        // For debug try local directory
        Path path = locateFile(name + ".xml");
        fData = parse(XML_BaseParser.parseXmlFile(path), peripheral, peripheral);
        fData.fRootModel.setToolTip(name);
    } catch (FileNotFoundException e) {
        // Some peripherals don't have templates yet - just warn
        throw new Exception("Failed to find peripheral file for " + name, e);
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Failed to process peripheral file for " + name, e);
    }
    for (ValidatorInformation v : fData.getValidators()) {
        try {
            // Get validator class
            Class<?> clazz = Class.forName(v.getClassName());
            int dimension = v.getDimension();
            PeripheralValidator validator;
            if (dimension > 0) {
                Constructor<?> constructor = clazz.getConstructor(PeripheralWithState.class, Integer.class, v.getParams().getClass());
                validator = (PeripheralValidator) constructor.newInstance(peripheral, dimension, v.getParams());
            } else {
                Constructor<?> constructor = clazz.getConstructor(PeripheralWithState.class, v.getParams().getClass());
                validator = (PeripheralValidator) constructor.newInstance(peripheral, v.getParams());
            }
            peripheral.addValidator(validator);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("Failed to add validator '" + v.getClassName() + "' for PeripheralWithState '" + peripheral.getName() + "'", e);
        }
    }
    return fData;
}
Also used : Path(java.nio.file.Path) FileNotFoundException(java.io.FileNotFoundException) PeripheralValidator(net.sourceforge.usbdm.deviceEditor.validators.PeripheralValidator) UsbdmException(net.sourceforge.usbdm.jni.UsbdmException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)1 Path (java.nio.file.Path)1 PeripheralValidator (net.sourceforge.usbdm.deviceEditor.validators.PeripheralValidator)1 UsbdmException (net.sourceforge.usbdm.jni.UsbdmException)1