Example usage for org.jdom2 Attribute getValue

List of usage examples for org.jdom2 Attribute getValue

Introduction

In this page you can find the example usage for org.jdom2 Attribute getValue.

Prototype

public String getValue() 

Source Link

Document

This will return the actual textual value of this Attribute.

Usage

From source file:org.tenje.jtrain.runnable.JTrainAccessories.java

License:Open Source License

private static void start(String[] args, Logger logger)
        throws FileNotFoundException, JDOMException, IOException, InterruptedException {
    DccppSocket socket;/*from   w  w  w  .j a v  a 2  s.com*/
    String[] addressParts;
    InetAddress address;
    int port;
    if (args.length < 1) {
        throw new IllegalArgumentException("no station address defined");
    }
    addressParts = args[0].split(":");
    if (addressParts.length < 2) {
        throw new IllegalArgumentException("no station port defined");
    }
    address = InetAddress.getByName(addressParts[0]);
    port = Integer.parseInt(addressParts[1]);
    // -

    PacketFactory packetFactory = new PacketFactoryImpl();
    PacketFactoryImpl.regiserDefaultPackets(packetFactory);
    PacketSensorRegistry sensorRegistry = null;
    PacketTurnoutRegistry turnoutRegistry = new PacketTurnoutRegistry();

    SAXBuilder builder = new SAXBuilder();
    Document document = builder.build(new FileInputStream("accessories.xml"));
    Attribute attribute;
    AccessoryDecoderAddress accessoryAddress;
    List<Sensor> sensors = new ArrayList<>();
    for (Element accessoryElem : document.getRootElement().getChildren()) {
        if (accessoryElem.getName().equals("lightSignal")) {
            Map<SignalAspect, GpioPinDigitalOutput> pins = new EnumMap<>(SignalAspect.class);
            Map<AccessoryDecoderAddress, SignalAspect> addresses = new HashMap<>();
            Signal signal;
            SignalAspect aspect;
            AccessoryDecoderAddress aspectAddress = null;
            GpioPinDigitalOutput pin;
            for (Element aspectElem : accessoryElem.getChildren()) {
                if ((attribute = aspectElem.getAttribute("color")) != null) {
                    aspect = SignalAspect.valueOf(attribute.getValue().toUpperCase());
                } else {
                    throw new MissingFormatArgumentException("no color defined: " + aspectElem);
                }
                if ((attribute = aspectElem.getAttribute("address")) != null) {
                    aspectAddress = new AccessoryDecoderAddress(Integer.parseInt(attribute.getValue()), 0);
                } else {
                    throw new MissingFormatArgumentException("no address defined: " + accessoryElem);
                }
                pin = JTrainXmlReader.getOutputPin(aspectElem, "pin");
                if (pins.put(aspect, pin) != null) {
                    throw new IllegalArgumentException("color already defined: " + aspect.name().toLowerCase());
                }
                if (addresses.put(aspectAddress, aspect) != null) {
                    throw new IllegalArgumentException(
                            "address already defined: " + aspectAddress.getMainAddress());
                }
            }
            if (!pins.isEmpty()) {
                signal = new RPiSignal(aspectAddress, pins);
                for (Entry<AccessoryDecoderAddress, SignalAspect> entry : addresses.entrySet()) {
                    turnoutRegistry
                            .register(new SignalAspectControlTurnout(entry.getKey(), signal, entry.getValue()));
                }
            }
        } else if (accessoryElem.getName().equals("scheduler")) {
            SwitchableScheduler scheduler = JTrainXmlReader.getScheduler(accessoryElem);
            if ((attribute = accessoryElem.getAttribute("address")) != null) {
                accessoryAddress = new AccessoryDecoderAddress(Integer.parseInt(attribute.getValue()), 0);
                throw new UnsupportedOperationException("Not supported, yet");
            }
            scheduler.setSwitched(true);
        } else {
            if ((attribute = accessoryElem.getAttribute("address")) != null) {
                accessoryAddress = new AccessoryDecoderAddress(Integer.parseInt(attribute.getValue()), 0);
                switch (accessoryElem.getName()) {
                case "sensor": {
                    Sensor sensor = new RPiSensor(accessoryAddress,
                            JTrainXmlReader.getInputPin(accessoryElem, "pin"));
                    sensors.add(sensor);
                }
                    break;
                case "turnout": {
                    int switchTime = 0;
                    if ((attribute = accessoryElem.getAttribute("switchTime")) != null) {
                        switchTime = Integer.parseInt(attribute.getValue());
                    }
                    Turnout turnout = new RPiTurnout(accessoryAddress,
                            JTrainXmlReader.getOutputPin(accessoryElem, "straightPin"),
                            JTrainXmlReader.getOutputPin(accessoryElem, "thrownPin"), switchTime);
                    turnoutRegistry.register(turnout);
                }
                    break;
                case "servoTurnout": {
                    int pin, switchTime = 0;
                    double straightTime, thrownTime;
                    if ((attribute = accessoryElem.getAttribute("pin")) != null) {
                        pin = Integer.parseInt(attribute.getValue());
                    } else {
                        throw new MissingFormatArgumentException("no pin defined: " + accessoryElem);
                    }
                    if ((attribute = accessoryElem.getAttribute("straightPwm")) != null) {
                        straightTime = Double.parseDouble(attribute.getValue());
                    } else {
                        throw new MissingFormatArgumentException("no straightPwm defined: " + accessoryElem);
                    }
                    if ((attribute = accessoryElem.getAttribute("thrownPwm")) != null) {
                        thrownTime = Double.parseDouble(attribute.getValue());
                    } else {
                        throw new MissingFormatArgumentException("no thrownPwm defined: " + accessoryElem);
                    }
                    if ((attribute = accessoryElem.getAttribute("switchTime")) != null) {
                        switchTime = Integer.parseInt(attribute.getValue());
                    }
                    Turnout turnout = new RPiServoTurnout(accessoryAddress, pin, straightTime, thrownTime,
                            switchTime);
                    turnoutRegistry.register(turnout);
                }
                    break;
                }
            } else {
                throw new MissingFormatArgumentException("no address defined: " + accessoryElem);
            }
        }
    }

    // --
    logger.info("Connecting to " + address + ":" + port + "...");
    while (true) {
        try {
            socket = new DccppSocket(address, port, packetFactory);
            logger.info("Connected");
        } catch (IOException ex) {
            Thread.sleep(1_000);
            logger.log(Level.WARNING, "Connection failed. Retrying...");
            continue; // Retry
        }
        if (sensorRegistry == null) {
            sensorRegistry = new PacketSensorRegistry(socket, socket.getConnectedBroker());
            for (Sensor sensor : sensors) {
                sensorRegistry.register(sensor);
            }
            sensors = null;
        } else {
            sensorRegistry.setReceiver(socket.getConnectedBroker());
        }
        socket.addPacketListener(sensorRegistry);
        socket.addPacketListener(turnoutRegistry);
        synchronized (socket) {
            socket.wait(); // Wait until connection lost
        }
        logger.log(Level.WARNING, "Connection lost. Trying to reconnect...");
    }
}

From source file:org.tenje.jtrain.runnable.JTrainRPiTrain.java

License:Open Source License

private static TrainFunction getVolatileSoundFunction(Element functionElem)
        throws LineUnavailableException, IOException, UnsupportedAudioFileException {
    List<Element> soundElems = functionElem.getChildren("sound");
    List<Clip> clips = new ArrayList<>(soundElems.size());
    Attribute attribute = functionElem.getAttribute("order");
    Order order;//from  www  . j  a  v a2s .  co m
    if (attribute != null && soundElems.size() > 1) {
        order = Order.valueOf(attribute.getValue().toUpperCase());
    } else { // Not defined or only one element
        order = Order.RANDOM;
    }
    for (Element soundElem : soundElems) {
        Line.Info linfo = new Line.Info(Clip.class);
        Line line = AudioSystem.getLine(linfo);
        Clip clip = (Clip) line;
        clip.open(AudioSystem.getAudioInputStream(new File(soundElem.getTextNormalize())));
        clips.add(clip);
    }
    return new MultipleVolatileSoundFunction(clips, order);
}

From source file:org.tenje.jtrain.runnable.JTrainXmlReader.java

License:Open Source License

static SwitchableScheduler getScheduler(Element elem) {
    SwitchableSchedulerBuilder builder = new SwitchableSchedulerBuilder();
    Attribute attribute;
    boolean loop = true;
    if ((attribute = elem.getAttribute("loop")) != null) {
        String attributeValue = attribute.getValue().toLowerCase();
        if (attributeValue.equals("false")) {
            loop = false;//  w ww.  j a  va2  s .  co m
        } else if (!attributeValue.equals("true")) {
            throw new XmlReadException("state argument: " + attribute.getValue());
        }
    }
    for (Element child : elem.getChildren()) {
        switch (child.getName()) {
        case "pinState": {
            GpioPinDigitalOutput pin = getOutputPin(child, "pin");
            boolean state = true;
            attribute = child.getAttribute("state");
            if (attribute == null) {
                throw new XmlReadException("no state defined: " + child);
            }
            String attributeValue = attribute.getValue().toLowerCase();
            if (attributeValue.equals("low")) {
                state = false;
            } else if (!attributeValue.equals("low")) {
                throw new XmlReadException("illegal state argument: " + attribute.getValue());
            }
            builder.setState(new RPiOutputPin(pin), state);
        }
            break;
        case "sleep": {
            long time = getLong(child, "time");
            builder.sleep(time);
        }
            break;
        }
    }
    return builder.build(loop);
}

From source file:org.tenje.jtrain.runnable.JTrainXmlReader.java

License:Open Source License

static TrainFunction getPinFunction(Element elem, RPiTrain train) {
    Attribute attribute;
    GpioController gpio = GpioFactory.getInstance();
    GpioPinDigitalOutput pin;/*  w  w w .  j a v  a2 s .c  o m*/
    if ((attribute = elem.getAttribute("pin")) != null) {
        pin = gpio.provisionDigitalOutputPin(RaspiPin.getPinByAddress(getInt(elem, "pin")));
        if (pin != null) {
            return new RPiPinTrainFunction(pin);
        } else {
            throw new XmlReadException("pin does not exist: " + attribute.getValue());
        }
    } else {
        int forwardPin = -1;
        int reversePin = -1;
        if ((attribute = elem.getAttribute("forwardPin")) != null) {
            forwardPin = Integer.parseInt(attribute.getValue());
        }
        if ((attribute = elem.getAttribute("reversePin")) != null) {
            reversePin = Integer.parseInt(attribute.getValue());
        }
        if (forwardPin == -1 && reversePin == -1) {
            throw new XmlReadException("no pin defined in gpioFunction");
        }
        GpioPinDigitalOutput forward = gpio.provisionDigitalOutputPin(RaspiPin.getPinByAddress(forwardPin));
        if (forward == null) {
            throw new XmlReadException("pin does not exist: " + forwardPin);
        }
        GpioPinDigitalOutput reverse = gpio.provisionDigitalOutputPin(RaspiPin.getPinByAddress(reversePin));
        if (reverse == null) {
            throw new XmlReadException("pin does not exist: " + reversePin);
        }
        return new RPiPinTrainFunctionDirectionDepend(forward, reverse, train);
    }
}

From source file:org.tenje.jtrain.runnable.JTrainXmlReader.java

License:Open Source License

static TrainFunction getVolatileSoundFunction(Element functionElem)
        throws LineUnavailableException, IOException, UnsupportedAudioFileException {
    List<Element> soundElems = functionElem.getChildren("sound");
    List<Clip> clips = new ArrayList<>(soundElems.size());
    Attribute attribute = functionElem.getAttribute("order");
    Order order;/*from  w ww .  jav  a2 s  . c om*/
    if (attribute != null && soundElems.size() > 1) {
        order = Order.valueOf(attribute.getValue().toUpperCase());
    } else { // Not defined or only one element
        order = Order.RANDOM;
    }
    for (Element soundElem : soundElems) {
        Line.Info linfo = new Line.Info(Clip.class);
        Line line = AudioSystem.getLine(linfo);
        Clip clip = (Clip) line;
        clip.open(AudioSystem.getAudioInputStream(new File(soundElem.getTextNormalize())));
        clips.add(clip);
    }
    return new MultipleVolatileSoundFunction(clips, order);
}

From source file:org.tenje.jtrain.runnable.JTrainXmlReader.java

License:Open Source License

static int getInt(Element elem, String name) {
    Attribute attribute = elem.getAttribute(name);
    if (attribute != null) {
        try {//  ww w.  java  2  s . c  o  m
            return Integer.parseInt(attribute.getValue());
        } catch (NumberFormatException ex) {
            throw new XmlReadException(
                    name + " attribute of " + elem + " is not a valid integer: " + attribute.getValue());
        }
    }
    throw new XmlReadException(name + " attribute missing for " + elem);
}

From source file:org.tenje.jtrain.runnable.JTrainXmlReader.java

License:Open Source License

static long getLong(Element elem, String name) {
    Attribute attribute = elem.getAttribute(name);
    if (attribute != null) {
        try {/*w  ww.  ja  v a  2 s  . c  om*/
            return Long.parseLong(attribute.getValue());
        } catch (NumberFormatException ex) {
            throw new XmlReadException(
                    name + " attribute of " + elem + " is not a valid integer: " + attribute.getValue());
        }
    }
    throw new XmlReadException(name + " attribute missing for " + elem);
}

From source file:org.universaal.tools.configurationEditor.editors.ConfigurationEditor.java

License:Apache License

private void createMainBar(String xml, ExpandBar mainBar) {

    Composite mainComp = new Composite(mainBar, SWT.NONE);
    GridLayout mainLayout = new GridLayout(2, false);
    mainComp.setLayout(mainLayout);//from   www  .j a  v  a  2 s  . co m

    try {

        this.doc = new SAXBuilder().build(new StringReader(xml));
        Element config = doc.getRootElement();

        Label l1;
        Text t1;

        ExpandItem itemMain = new ExpandItem(mainBar, SWT.NONE, 0);
        itemMain.setText("Configuration");
        itemMain.setControl(mainComp);

        List<Attribute> ats = config.getAttributes();

        for (Attribute at : ats) {

            l1 = new Label(mainComp, SWT.NONE);
            l1.setText(at.getName() + ":");

            t1 = new Text(mainComp, SWT.SINGLE | SWT.BORDER);
            t1.setEditable(true);
            t1.setText(at.getValue());

            // ------------- <Listener> --------------

            WidgetMapping.put(t1, at);
            t1.addModifyListener(widgetListener);

            // ------------- </Listener> --------------

            GridData gd = new GridData();
            gd.horizontalAlignment = GridData.FILL;
            gd.grabExcessHorizontalSpace = true;
            t1.setLayoutData(gd);

        }

        itemMain.setHeight(mainComp.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
        itemMain.setExpanded(true);

        createCategoryList(config);

    } catch (JDOMException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.universaal.tools.configurationEditor.editors.ConfigurationEditor.java

License:Apache License

private void showCategory(Element el) {

    for (Control wi : selectedItemGroup.getChildren()) {
        wi.dispose();/*from   www.  j  av  a2  s. c  o m*/
    }

    Label l1;
    Text t1;

    List<Attribute> cAts = el.getAttributes();

    for (Attribute at : cAts) {

        l1 = new Label(selectedItemGroup, SWT.NONE);
        l1.setText(at.getName() + ":");

        t1 = new Text(selectedItemGroup, SWT.SINGLE | SWT.BORDER);
        t1.setEditable(true);
        t1.setText(at.getValue());

        // ------------- <Listener> --------------

        WidgetMapping.put(t1, at);
        t1.addModifyListener(widgetListener);

        // ------------- </Listener> --------------

        GridData gd = new GridData();
        gd.horizontalAlignment = GridData.FILL;
        gd.grabExcessHorizontalSpace = true;
        t1.setLayoutData(gd);

    }

}

From source file:org.universaal.tools.configurationEditor.editors.ConfigurationEditor.java

License:Apache License

private void showSimpleConfigItem(Element el) {
    for (Control wi : selectedItemGroup.getChildren()) {
        wi.dispose();/*w  w w. j  a  v  a 2 s  .  c  om*/
    }

    List<Attribute> tiAtts = el.getAttributes();
    Label l1;
    Text t1;

    for (Attribute att : tiAtts) {
        l1 = new Label(selectedItemGroup, SWT.NONE);
        l1.setText(att.getName() + ":");

        t1 = new Text(selectedItemGroup, SWT.SINGLE | SWT.BORDER);
        t1.setEditable(true);
        t1.setText(att.getValue());

        // ------------- <Listener> --------------

        WidgetMapping.put(t1, att);
        t1.addModifyListener(widgetListener);

        // ------------- </Listener> --------------

        GridData gd = new GridData();
        gd.horizontalAlignment = GridData.FILL;
        gd.grabExcessHorizontalSpace = true;
        t1.setLayoutData(gd);
    }

    // TI's children
    List<Element> tiEls = el.getChildren();

    for (Element tiEl : tiEls) {

        if (tiEl.getName().equals("validators")) {
            //DO NOTHING!
        } else {

            l1 = new Label(selectedItemGroup, SWT.NONE);
            l1.setText(tiEl.getName() + ":");

            t1 = new Text(selectedItemGroup, SWT.SINGLE | SWT.BORDER);
            t1.setEditable(true);
            t1.setText(tiEl.getValue());

            // ------------- <Listener> --------------

            WidgetMapping.put(t1, tiEl);
            t1.addModifyListener(widgetListener);

            // ------------- </Listener> --------------

            GridData gd = new GridData();
            gd.horizontalAlignment = GridData.FILL;
            gd.grabExcessHorizontalSpace = true;
            t1.setLayoutData(gd);
        }
    }

}