Example usage for org.jdom2 Element getChildText

List of usage examples for org.jdom2 Element getChildText

Introduction

In this page you can find the example usage for org.jdom2 Element getChildText.

Prototype

public String getChildText(final String cname) 

Source Link

Document

Returns the textual content of the named child element, or null if there's no such child.

Usage

From source file:neon.systems.conversation.DialogLoader.java

License:Open Source License

private CreatureNode loadCreatureNode(Element node, RDialog dialog) {
    ArrayList<String> nodes = new ArrayList<>();
    for (Element child : node.getChildren("pnode")) {
        nodes.add(loadPlayerNode(child, dialog).id);
    }//from  ww  w . j a v  a2 s.  c o m

    CreatureNode cnode = new CreatureNode(node.getAttributeValue("id"), node.getChildText("text"), nodes);
    dialog.addNode(cnode);
    return cnode;
}

From source file:net.FriendsUnited.Services.FileClient.java

License:Open Source License

private void parseResponse(FriendPacket response, PrintWriter out) {
    log.debug("{}", response);
    ByteConverter responseBc = new ByteConverter(response.getPayload());
    byte type = responseBc.getByte();
    log.debug("Response Type : {}", type);
    switch (type) {
    case FileServer.FAILURE:
        String errorMsg = responseBc.getString();
        if (true == "OK".equals(errorMsg)) {
            out.println("OK");
        } else {// w  w w.ja v  a 2s  .  c o  m
            out.println("ERROR in Response Packet:" + errorMsg);
        }
        break;

    case FileServer.DIRECTORY_LISTING:
        out.println("Listing  of " + RemoteServer + " : " + RemotePath);
        String xmlData = responseBc.getString();
        log.info("Response String : {}", xmlData);
        final SAXBuilder builder = new SAXBuilder();
        Document doc;
        try {
            doc = builder.build(new StringReader(xmlData));
            Element root = null;
            root = doc.getRootElement();
            List<Element> le = root.getChildren();
            for (int i = 0; i < le.size(); i++) {
                Element curentElement = le.get(i);
                String date = curentElement.getChildText("lastModified");
                long ms = Long.parseLong(date);
                Date d = new Date(ms);
                out.printf("%29s", d);
                out.print("|");
                String size = curentElement.getChildText("Size");
                if (null == size) {
                    size = "";
                }
                out.printf("%12s", size);
                out.print("|");
                if (true == "File".equals(curentElement.getName())) {
                    out.print("F");
                } else if (true == "Directory".equals(curentElement.getName())) {
                    out.print("D");
                } else {
                    out.print("X");
                }
                out.print("|");
                out.println(curentElement.getText()); // File Name
            }
        } catch (JDOMException e) {
            log.error(Tool.fromExceptionToString(e));
        } catch (IOException e) {
            log.error(Tool.fromExceptionToString(e));
        }
        break;

    default:
        out.println("ERROR: Receive Response of invalid Type ! "
                + Tool.fromByteBufferToHexString(response.getPayload()));
        break;
    }
}

From source file:net.kleditzsch.AVM.FritzBox.SmartHome.FritzBoxSmarthome.java

License:Open Source License

/**
 * gibt eine Liste der bekannte SmartHome Gerte zurck
 *
 * @return Liste der SmartHome Gerte/*from   w  w  w  . ja  v a 2s  .c o m*/
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws JDOMException
 */
public List<SmarthomeDevice> listDevices() throws IOException, NoSuchAlgorithmException, JDOMException {

    List<SmarthomeDevice> smartHomeDevices = new ArrayList<>();
    String response = fritzBoxHandler
            .sendHttpRequest("/webservices/homeautoswitch.lua?switchcmd=getdevicelistinfos");

    Document doc = new SAXBuilder().build(new StringReader(response));
    Element deviceList = doc.getRootElement();

    for (Element device : deviceList.getChildren()) {

        SmarthomeDevice smarthomeDevice = new SmarthomeDevice();

        //Allgemeine Daten
        smarthomeDevice.setIdentifier(device.getAttributeValue("identifier").replaceAll("\\s", ""));
        smarthomeDevice.setId(device.getAttributeValue("id"));
        smarthomeDevice.setFunctionBitmask(Integer.parseInt(device.getAttributeValue("functionbitmask")));
        smarthomeDevice.setFirmwareVersion(device.getAttributeValue("fwversion"));
        smarthomeDevice.setManufacturer(device.getAttributeValue("manufacturer"));
        smarthomeDevice.setProductName(device.getAttributeValue("productname"));
        smarthomeDevice.setPresent((device.getChildText("present").trim().equals("1")));
        smarthomeDevice.setName(device.getChildText("name"));

        if (smarthomeDevice.isCometDectRadiatorThermostat()) {

            //TODO comming soon
        }
        if (smarthomeDevice.isEnergyMeter()) {

            //Energiemesse
            EnergyMeter energyMeter = new EnergyMeter();
            Element powermeter = device.getChild("powermeter");
            energyMeter.setPower(Long.parseLong(powermeter.getChildText("power")));
            energyMeter.setEnergy(Long.parseLong(powermeter.getChildText("energy")));
            smarthomeDevice.setEnergyMeter(energyMeter);
        }
        if (smarthomeDevice.isTemperatureSensor()) {

            //Temperatur Sensor
            TemperatureSensor temperatureSensor = new TemperatureSensor();
            Element temperature = device.getChild("temperature");
            temperatureSensor.setThemperature(Integer.parseInt(temperature.getChildText("celsius")) / 10);
            temperatureSensor.setOffset(Integer.parseInt(temperature.getChildText("offset")) / 10);
            smarthomeDevice.setTemperatureSensor(temperatureSensor);
        }
        if (smarthomeDevice.isSwitchableSocket()) {

            //schaltbare Steckdose
            Switch aSwitch = new Switch(fritzBoxHandler, smarthomeDevice.getIdentifier());
            Element switchElement = device.getChild("switch");
            aSwitch.setState(Integer.parseInt(switchElement.getChildText("state")));
            aSwitch.setMode(switchElement.getChildText("mode"));
            aSwitch.setLocked(Integer.parseInt(switchElement.getChildText("lock")) == 1);
            smarthomeDevice.setSwitch(aSwitch);
        }
        if (smarthomeDevice.isDectRepeater()) {

            //DECT Repeater
        }
        smartHomeDevices.add(smarthomeDevice);
    }

    return smartHomeDevices;
}

From source file:net.kleditzsch.Edimax.SmartPug.SP2101.java

License:Open Source License

/**
 * gibt die Messdaten der Steckdose zurck
 *
 * @return Energiedaten//  w w w  .ja  v a  2s  .c o  m
 * @throws IOException
 */
public EnergyData getEnergyData() throws IOException {

    String request = "<?xml version=\"1.0\" encoding=\"UTF8\"?>" + "               <SMARTPLUG id=\"edimax\">"
            + "                   <CMD id=\"get\"> " + "                       <NOW_POWER></NOW_POWER> "
            + "                   </CMD> " + "               </SMARTPLUG>";

    String response = sendHttpCommand(request);
    Document doc = null;
    try {
        doc = new SAXBuilder().build(new StringReader(response));
        Element root = doc.getRootElement();
        Element cmd = root.getChild("CMD");
        Element data = cmd.getChild("NOW_POWER");

        EnergyData energyData = new EnergyData();

        String lastToggleTime = data.getChildText("Device.System.Power.LastToggleTime");
        LocalDateTime lastSwitchTime = LocalDateTime.parse(lastToggleTime, formatter);
        energyData.setLastSwitchTime(lastSwitchTime);
        energyData.setNowCurrent(Double.parseDouble(data.getChildText("Device.System.Power.NowCurrent")));
        energyData.setNowPower(Double.parseDouble(data.getChildText("Device.System.Power.NowPower")));
        energyData.setDayEnergy(Double.parseDouble(data.getChildText("Device.System.Power.NowEnergy.Day")));
        energyData.setWeekEnergy(Double.parseDouble(data.getChildText("Device.System.Power.NowEnergy.Week")));
        energyData.setMonthEnergy(Double.parseDouble(data.getChildText("Device.System.Power.NowEnergy.Month")));

        return energyData;
    } catch (JDOMException e) {
    }
    return null;
}

From source file:net.psexton.libuti.UtiDb.java

License:Open Source License

/**
 * Adds UTI mappings to the DB from an XML source
 * @param in InputStream containing XML data
 * @throws IOException if there was a problem reading the InputStream
 * @throws JDOMException if there was a problem parsing the XML
 */// w  ww  .j  a  v  a 2s . c  o m
public final void importXmlData(InputStream in) throws IOException, JDOMException {
    // Parse the input stream and rip out a usable Element from the Document
    Document doc = new SAXBuilder().build(in);
    Element root = doc.detachRootElement();

    // root element is <uti-list>
    // Iterate over all <uti> children
    for (Object o : root.getChildren("uti")) {
        Element uti = (Element) o;
        // UTI's name is in a <name> child
        String name = uti.getChildText("name");
        conformances.addVertex(name); // Add UTI to graph
        // File suffixes are in <suffix> children
        // Iterate over them
        for (Object o2 : uti.getChildren("suffix")) {
            Element suffix = (Element) o2;
            if (suffix.getAttribute("preferred") != null
                    && suffix.getAttribute("preferred").getBooleanValue()) {
                reverseSuffixTable.put(name, suffix.getText()); // Add UTI->suffix to reverseSuffixTable
            }
            suffixTable.put(suffix.getText(), name); // Add suffix->UTI to suffixTable
        }
        // Conformances are in <conforms-to> children
        // Iterate over them
        for (Object o2 : uti.getChildren("conforms-to")) {
            Element conformsTo = (Element) o2;
            String parentUtiName = conformsTo.getText();
            String edgeName = name + "->" + parentUtiName;
            conformances.addEdge(edgeName, name, parentUtiName, EdgeType.DIRECTED);
        }
    }
}

From source file:net.visualillusionsent.tellplugin.xml.TellSaveHandler.java

License:Open Source License

public void deleteData(Tell tell) {
    ArrayList<Element> toremove = new ArrayList<Element>();

    for (Element element : getDocument().getRootElement().getChildren()) {
        VIBotX.log.info(element.getChildText("receiver"));
        VIBotX.log.info(element.getChildText("sender"));
        VIBotX.log.info(element.getChildText("message"));
        VIBotX.log.info(element.getChildText("date"));
        if (!tell.getReceiver().equals(element.getChildText("receiver")))
            continue;
        if (!tell.getSender().equals(element.getChildText("sender")))
            continue;
        if (!tell.getMessage().equals(element.getChildText("message")))
            continue;
        if (!tell.getDateString().equals(element.getChildText("date")))
            continue;

        toremove.add(element);/*from   w  w w  .j a va  2s.  c  o m*/
    }
    for (Element e : toremove) {
        e.detach();
    }
    try {
        write();
    } catch (IOException e) {
        VIBotX.log.error("Error deleting from TellPlugin Xml File", e);
    }
}

From source file:odml.core.Reader.java

License:Open Source License

/**
 * Converts the DOM representation of the metadata-file to the tree like odML structure.
 * // w  w w  .  j  a v  a2 s.  co  m
 * @param dom - {@link Document}: the document to parse
 */
public void createTree(Document dom) {
    root = new Section();
    if (dom == null) {
        return;
    }
    Element rootElement = dom.getRootElement();
    String odmlVersion = rootElement.getAttribute("version").getValue();
    if (Float.parseFloat(odmlVersion) != 1.0) {
        System.out.println("Can not handle odmlVersion: " + odmlVersion + " stopping further processing!");
        return;
    }

    String author = rootElement.getChildText("author");
    root.setDocumentAuthor(author);
    Date date;
    String temp = rootElement.getChildText("date");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try {
        date = sdf.parse(temp);
    } catch (Exception e) {
        date = null;
    }
    root.setDocumentDate(date);
    String version = rootElement.getChildText("version");
    root.setDocumentVersion(version);
    URL url = null;
    temp = rootElement.getChildText("repository");
    if (temp != null && !temp.isEmpty()) {
        try {
            url = new URL(temp);
        } catch (Exception e) {
            System.out.println("Reader.parseSection.repository: " + e);
        }
    }
    root.setRepository(url);
    root.setFileUrl(this.fileUrl);

    for (Element domSection : rootElement.getChildren("section")) {
        if (rootElement.isAncestor(domSection)) {
            root.add(parseSection(domSection));
        }
    }
    confirmLinks(root);
}

From source file:odml.core.Reader.java

License:Open Source License

/**
 * Parses an xml section of the metadata file and returns it. Subsections are parsed in a recursive
 * manner.//from   ww w .  j a va  2 s .c o m
 * 
 * @param domSection - {@link Element}: the section that is to parse
 * @return {@link Section}: the Section representation of the dom section
 */
private Section parseSection(Element domSection) {
    String type = domSection.getChildText("type");
    String name = domSection.getChildText("name");
    String reference = domSection.getChildText("reference");
    String definition = domSection.getChildText("definition");
    URL mapURL = null;
    String temp = domSection.getChildText("mapping");
    if (temp != null && !temp.isEmpty()) {
        try {
            mapURL = new URL(temp);
        } catch (Exception e) {
            System.out.println("odml.core.Reader.parseSection.mappingURL handling: " + e.getMessage());
        }
    }

    URL url = null;
    temp = domSection.getChildText("repository");
    if (temp != null && !temp.isEmpty()) {
        try {
            url = new URL(domSection.getChildText("repository"));
        } catch (Exception e) {
            url = null;
            System.out.println("Reader.parseSection.repository: " + e.getMessage());
        }
    }
    String link = domSection.getChildText("link");
    String include = domSection.getChildText("include");
    Section section;
    try {
        section = new Section(name, type, reference);
        section.setDefinition(definition);
        section.setRepository(url);
        section.setMapping(mapURL);
        section.setLink(link, true);
        if (link != null) {
            links.add(section);
        }
        section.setInclude(include);
        if (include != null) {
            includes.add(section);
        }
    } catch (Exception e) {
        System.out.println("Reader.parseSection: exception while creating section: " + e.getMessage());
        return null;
    }
    Property tempProp;
    for (Element element : domSection.getChildren("property")) {
        section.add(parseProperty(element));
    }
    for (Element element : domSection.getChildren("section")) {
        section.add(parseSection(element));
    }
    return section;
}

From source file:odml.core.Reader.java

License:Open Source License

/**
 * Parses property and creates the odMLProperty representation of it.
 * /*www.  jav  a2  s.co m*/
 * @param domProperty - {@link Element}: the Element to parse. (should be a property element)
 * @return {@link Property} the {@link Property} representation of this domElement
 */
private Property parseProperty(Element domProperty) {
    String name;
    name = domProperty.getChildTextTrim("name");
    String dependency;
    String dependencyValue;
    String definition;
    URL mapURL = null;

    String temp = domProperty.getChildText("mapping");

    if (temp != null && !temp.isEmpty() && !temp.endsWith("?")) {
        try {
            mapURL = new URL(temp);
        } catch (Exception e) {
            System.out.println("odml.core.Reader.parseProperty.mappingURL handling: \n"
                    + " \t> tried to form URL out of: '" + domProperty.getChildText("mapping")
                    + "'\n\t= mapURL of Property named: " + name + e.getMessage());
        }
    }
    definition = domProperty.getChildText("definition");
    dependency = domProperty.getChildText("dependency");
    dependencyValue = domProperty.getChildText("dependencyValue");
    Vector<Value> tmpValues = new Vector<Value>();
    for (Element element : domProperty.getChildren("value")) {
        tmpValues.add(parseValue(element));
    }

    Property property;
    try {
        property = new Property(name, tmpValues, definition, dependency, dependencyValue, mapURL);
    } catch (Exception e) {
        System.out.println("odml.core.Reader.parseProperty: create new prop failed. " + e.getMessage());
        property = null;
    }
    return property;
}

From source file:odml.core.Reader.java

License:Open Source License

/**
 * Parses value and creates the odMLValue representation of it.
 * /*from   www.  j  av a 2s  .c o m*/
 * @param domValue
 *            - {@link Element}: the Element to parse. (should be a value element)
 * @return {@link Value} the {@link Value} representation of this domElement
 */
private Value parseValue(Element domValue) {
    Value value;

    String content;
    String unit;
    Object uncertainty;
    String type;
    String filename;
    String definition;
    String reference;
    String encoder;
    String checksum;
    content = domValue.getTextTrim();
    if (content == null) {
        content = "";
    }
    unit = domValue.getChildText("unit");
    uncertainty = domValue.getChildText("uncertainty");
    type = domValue.getChildText("type");
    filename = domValue.getChildText("filename");
    definition = domValue.getChildText("definition");
    reference = domValue.getChildText("reference");
    checksum = domValue.getChildText("checksum");
    encoder = domValue.getChildText("encoder");
    try {
        value = new Value(content, unit, uncertainty, type, filename, definition, reference, encoder, checksum);
    } catch (Exception e) {
        System.out.println("odml.core.Reader.parseValue: create Value failed. " + e.getMessage());
        return null;
    }
    return value;
}