Example usage for org.jdom2 Element getAttribute

List of usage examples for org.jdom2 Element getAttribute

Introduction

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

Prototype

public Attribute getAttribute(final String attname) 

Source Link

Document

This returns the attribute for this element with the given name and within no namespace, or null if no such attribute exists.

Usage

From source file:com.xebialabs.overcast.support.libvirt.jdom.FilesystemXml.java

License:Apache License

/**
 * Get map of {@link Filesystem}s. The key in the map is the target inside the domain. This will only return
 * filesystems of type 'mount'.//from   w  w w . ja v  a2 s  .  co m
 */
public static Map<String, Filesystem> getFilesystems(Document domainXml) {
    Map<String, Filesystem> ret = Maps.newHashMap();
    XPathFactory xpf = XPathFactory.instance();
    XPathExpression<Element> fsExpr = xpf.compile(XPATH_FILESYSTEM, Filters.element());
    List<Element> filesystems = fsExpr.evaluate(domainXml);
    for (Element fs : filesystems) {
        Attribute accessMode = fs.getAttribute("accessmode");
        String source = fs.getChild("source").getAttribute("dir").getValue();
        String target = fs.getChild("target").getAttribute("dir").getValue();
        boolean readOnly = fs.getChild("readonly") != null;

        ret.put(target, new Filesystem(source, target,
                AccessMode.valueOf(accessMode.getValue().toUpperCase(Locale.US)), readOnly));
    }
    return ret;
}

From source file:com.xebialabs.overcast.support.libvirt.jdom.InterfaceXml.java

License:Apache License

/**
 * Get a map of mac addresses of interfaces defined on the domain. This is somewhat limited at the moment. It is
 * assumed that only one network interface with mac is connected to a bridge or network. For instance if you have a
 * bridged network device connected to 'br0' then you will find it's MAC address with the key 'br0'.
 *///from ww w.j a va  2  s  .c  o m
public static Map<String, String> getMacs(Document domainXml) {
    Map<String, String> macs = Maps.newHashMap();
    XPathFactory xpf = XPathFactory.instance();
    XPathExpression<Element> interfaces = xpf.compile("/domain/devices/interface", Filters.element());
    for (Element iface : interfaces.evaluate(domainXml)) {
        String interfaceType = iface.getAttribute("type").getValue();
        logger.debug("Detecting IP on network of type '{}'", interfaceType);
        if ("bridge".equals(interfaceType)) {
            Element macElement = iface.getChild("mac");
            String mac = macElement.getAttribute("address").getValue();
            Element sourceElement = iface.getChild("source");
            String bridge = sourceElement.getAttribute("bridge").getValue();
            logger.info("Detected MAC '{}' on bridge '{}'", mac, bridge);
            macs.put(bridge, mac);
        } else if ("network".equals(interfaceType)) {
            Element macElement = iface.getChild("mac");
            String mac = macElement.getAttribute("address").getValue();
            Element sourceElement = iface.getChild("source");
            String network = sourceElement.getAttribute("network").getValue();
            logger.info("Detected MAC '{}' on network '{}'", mac, network);
            macs.put(network, mac);
        } else {
            logger.warn("Ignoring network of type {}", interfaceType);
        }
    }
    return macs;
}

From source file:converter.ConverterBean.java

@Override
public Map<Monnaie, Double> euroToOtherCurrencies(double amount) {
    Map<Monnaie, Double> map = new HashMap<>();

    try {//from  w  w  w .  ja  v  a  2 s . c  o  m
        this.init();

        for (Element e : racine.getChildren()) {
            String currency = e.getAttribute("currency").getValue();
            Double rate = Double.parseDouble(e.getAttribute("rate").getValue());

            Monnaie m = new Monnaie();
            m.codeMonnaie = currency;
            m.tauxDeChange = rate;
            m.pays = getFullNameMonay(currency);
            m.nomMonnaie = currency;

            Double calculated = m.tauxDeChange * rate;

            map.put(m, calculated);
        }

    } catch (JDOMException | IOException e) {
        e.printStackTrace();
    }
    return map;

}

From source file:core.LectorXml.java

@Override
public void readNodeFile() {
    try {/*from  w  w  w.ja  v  a2  s. c  o m*/
        this.setDocument((Document) this.getBuilder().build(this.getFile()));
        this.setRootNode(this.getDocument().getRootElement());
        List list = (List) this.getRootNode().getChildren();

        for (Object object : list) {
            Element ob = (Element) object;
            System.out.println(ob.getName());
            if (ob.getName().equalsIgnoreCase("autor")) {
                try {
                    this.getXml().getAutor().setNombre(ob.getChild("nombre").getText());
                } catch (NullPointerException r) {
                    this.getXml().getAutor().setNombre("");
                }
                try {
                    this.getXml().getAutor().setDescripcion(ob.getChild("descripcion").getText());
                } catch (NullPointerException r) {
                    this.getXml().getAutor().setDescripcion("");
                }
                try {
                    this.getXml().getAutor().setVersion(ob.getChild("version").getText());
                } catch (NullPointerException r) {
                    this.getXml().getAutor().setVersion("");
                }
            } else if (ob.getName().equalsIgnoreCase("cuerpo")) {
                try {
                    Element tipo = ob.getChild("tipo");
                    this.getXml().getCuerpo()
                            .setColumnas(Integer.parseInt(tipo.getAttribute("columnas").getValue()));
                    this.getXml().getCuerpo()
                            .setTipo_datos(tipo.getAttribute("tipodatocolumna").getValue().split(","));
                } catch (NullPointerException r) {
                    this.getXml().getCuerpo().setColumnas(0);
                    this.getXml().getCuerpo().setTipo_datos(new String[] {});
                } catch (NumberFormatException n) {
                    this.getXml().getCuerpo().setColumnas(0);
                    this.getXml().getCuerpo().setTipo_datos(new String[] {});
                }
                try {
                    Element tipo = ob.getChild("claseprincipal");
                    this.getXml().getCuerpo().setMain(ob.getChild("claseprincipal").getValue());
                } catch (NullPointerException r) {
                    this.getXml().getCuerpo().setMain("");
                }
                ArrayList<String> parametros = new ArrayList<>();
                try {
                    System.out.println("***************  " + ob.getChild("parametro"));
                    Element param = ob.getChild("parametro");
                    for (Element e : param.getChildren()) {
                        System.out.println(e.getName());
                        try {
                            System.out.println(e.getName());
                            parametros.add(e.getName());
                        } catch (NullPointerException r) {
                            System.err.println("Se exploto");
                        }
                    }
                } catch (NullPointerException r) {

                } finally {
                    this.getXml().getCuerpo().setParametros(parametros);
                }
            }

        }

    } catch (JDOMException ex) {
        Logger.getLogger(LectorXml.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(LectorXml.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:core.ListComponenXml.java

@Override
public void readNodeFile() {
    try {/*from  www . j ava2 s.  com*/
        this.setDocument((Document) this.getBuilder().build(this.getFile()));
        this.setRootNode(this.getDocument().getRootElement());
        List list = (List) this.getRootNode().getChildren();
        for (Object object : list) {
            Element tem = (Element) object;
            this.setXml(new Xml());
            for (Element ob : tem.getChildren()) {
                System.out.println(ob.getName());
                if (ob.getName().equalsIgnoreCase("autor")) {
                    try {
                        this.getXml().getAutor().setNombre(ob.getChild("nombre").getText());
                    } catch (NullPointerException r) {
                        this.getXml().getAutor().setNombre("");
                    }
                    try {
                        this.getXml().getAutor().setDescripcion(ob.getChild("descripcion").getText());
                    } catch (NullPointerException r) {
                        this.getXml().getAutor().setDescripcion("");
                    }
                    try {
                        this.getXml().getAutor().setVersion(ob.getChild("version").getText());
                    } catch (NullPointerException r) {
                        this.getXml().getAutor().setVersion("");
                    }
                } else if (ob.getName().equalsIgnoreCase("status")) {
                    try {
                        this.getXml().getStatus()
                                .setActive(Boolean.valueOf(ob.getAttribute("active").getValue()));
                    } catch (Exception e) {
                        this.getXml().getStatus().setActive(true);
                    }
                } else if (ob.getName().equalsIgnoreCase("cuerpo")) {
                    try {
                        Element tipo = ob.getChild("tipo");
                        this.getXml().getCuerpo()
                                .setColumnas(Integer.parseInt(tipo.getAttribute("columnas").getValue()));
                        this.getXml().getCuerpo()
                                .setTipo_datos(tipo.getAttribute("tipodatocolumna").getValue().split(","));
                    } catch (NullPointerException r) {
                        this.getXml().getCuerpo().setColumnas(0);
                        this.getXml().getCuerpo().setTipo_datos(new String[] {});
                    } catch (NumberFormatException n) {
                        this.getXml().getCuerpo().setColumnas(0);
                        this.getXml().getCuerpo().setTipo_datos(new String[] {});
                    }
                    try {
                        Element tipo = ob.getChild("claseprincipal");
                        this.getXml().getCuerpo().setMain(ob.getChild("claseprincipal").getValue());
                    } catch (NullPointerException r) {
                        this.getXml().getCuerpo().setMain("");
                    }
                    ArrayList<String> parametros = new ArrayList<>();
                    try {
                        System.out.println("***************  " + ob.getChild("parametro"));
                        Element param = ob.getChild("parametro");
                        for (Element e : param.getChildren()) {
                            System.out.println(e.getName());
                            try {
                                System.out.println(e.getName());
                                parametros.add(e.getName());
                            } catch (NullPointerException r) {
                                System.err.println("Se exploto");
                            }
                        }
                    } catch (NullPointerException r) {

                    } finally {
                        this.getXml().getCuerpo().setParametros(parametros);
                    }
                }
            }
            this.getXmls().add(this.getXml());
        }

    } catch (JDOMException ex) {
        Logger.getLogger(LectorXml.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(LectorXml.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:core.ListComponenXml.java

@Override
public void readFile(Xml xml) {
    try {/*from   w w  w .j av  a2  s .c  o  m*/
        this.setDocument((Document) this.getBuilder().build(this.getFile()));
        this.setRootNode(this.getDocument().getRootElement());
        List list = (List) this.getRootNode().getChildren();
        for (Object object : list) {
            Element tem = (Element) object;
            this.setXml(new Xml());
            for (Element ob : tem.getChildren()) {
                System.out.println(ob.getName());
                if (ob.getName().equalsIgnoreCase("autor")) {
                    try {
                        this.getXml().getAutor().setNombre(ob.getChild("nombre").getText());
                    } catch (NullPointerException r) {
                        this.getXml().getAutor().setNombre("");
                    }
                    try {
                        this.getXml().getAutor().setDescripcion(ob.getChild("descripcion").getText());
                    } catch (NullPointerException r) {
                        this.getXml().getAutor().setDescripcion("");
                    }
                    try {
                        this.getXml().getAutor().setVersion(ob.getChild("version").getText());
                    } catch (NullPointerException r) {
                        this.getXml().getAutor().setVersion("");
                    }
                } else if (ob.getName().equalsIgnoreCase("status")) {
                    try {
                        this.getXml().getStatus()
                                .setActive(Boolean.valueOf(ob.getAttribute("active").getValue()));
                    } catch (Exception e) {
                        this.getXml().getStatus().setActive(true);
                    }
                } else if (ob.getName().equalsIgnoreCase("cuerpo")) {
                    try {
                        Element tipo = ob.getChild("tipo");
                        this.getXml().getCuerpo()
                                .setColumnas(Integer.parseInt(tipo.getAttribute("columnas").getValue()));
                        this.getXml().getCuerpo()
                                .setTipo_datos(tipo.getAttribute("tipodatocolumna").getValue().split(","));
                    } catch (NullPointerException r) {
                        this.getXml().getCuerpo().setColumnas(0);
                        this.getXml().getCuerpo().setTipo_datos(new String[] {});
                    } catch (NumberFormatException n) {
                        this.getXml().getCuerpo().setColumnas(0);
                        this.getXml().getCuerpo().setTipo_datos(new String[] {});
                    }
                    try {
                        Element tipo = ob.getChild("claseprincipal");
                        this.getXml().getCuerpo().setMain(ob.getChild("claseprincipal").getValue());
                    } catch (NullPointerException r) {
                        this.getXml().getCuerpo().setMain("");
                    }
                    ArrayList<String> parametros = new ArrayList<>();
                    try {
                        System.out.println("***************  " + ob.getChild("parametro"));
                        Element param = ob.getChild("parametro");
                        for (Element e : param.getChildren()) {
                            System.out.println(e.getName());
                            try {
                                System.out.println(e.getName());
                                parametros.add(e.getName());
                            } catch (NullPointerException r) {
                                System.err.println("Se exploto");
                            }
                        }
                    } catch (NullPointerException r) {

                    } finally {
                        this.getXml().getCuerpo().setParametros(parametros);
                    }
                }
            }
            this.xmls.add(this.getXml());
        }

    } catch (JDOMException ex) {
        Logger.getLogger(LectorXml.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(LectorXml.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:core.ValidXml.java

@Override
public boolean validEstructura(File xml_file) {
    try {//from  ww  w .  j a  v a 2  s  .  co  m
        SAXBuilder builder = new SAXBuilder();
        Document document = (Document) builder.build(xml_file);
        Element rootNode = document.getRootElement();
        List list = (List) rootNode.getChildren();
        Xml xml = new Xml();
        for (Object object : list) {
            Element ob = (Element) object;
            System.out.println(ob.getName());
            if (ob.getName().equalsIgnoreCase("autor")) {
                try {
                    xml.getAutor().setNombre(ob.getChild("nombre").getText());
                } catch (NullPointerException r) {
                    xml.getAutor().setNombre("");
                }
                try {
                    xml.getAutor().setDescripcion(ob.getChild("descripcion").getText());
                } catch (NullPointerException r) {
                    xml.getAutor().setDescripcion("");
                }
                try {
                    xml.getAutor().setVersion(ob.getChild("version").getText());
                } catch (NullPointerException r) {
                    xml.getAutor().setVersion("");
                }
            } else if (ob.getName().equalsIgnoreCase("cuerpo")) {
                try {
                    Element tipo = ob.getChild("tipo");
                    xml.getCuerpo().setColumnas(Integer.parseInt(tipo.getAttribute("columnas").getValue()));
                    xml.getCuerpo().setTipo_datos(tipo.getAttribute("tipodatocolumna").getValue().split(","));
                } catch (NullPointerException r) {
                    xml.getCuerpo().setColumnas(0);
                    xml.getCuerpo().setTipo_datos(new String[] {});
                } catch (NumberFormatException n) {
                    xml.getCuerpo().setColumnas(0);
                    xml.getCuerpo().setTipo_datos(new String[] {});
                }
                try {
                    Element tipo = ob.getChild("claseprincipal");
                    xml.getCuerpo().setMain(ob.getChild("claseprincipal").getValue());
                } catch (NullPointerException r) {
                    xml.getCuerpo().setMain("");
                }
                ArrayList<String> parametros = new ArrayList<>();
                try {
                    System.out.println("***************  " + ob.getChild("parametro"));
                    Element param = ob.getChild("parametro");
                    for (Element e : param.getChildren()) {
                        System.out.println(e.getName());
                        try {
                            System.out.println(e.getName());
                            parametros.add(e.getName());
                        } catch (NullPointerException r) {
                            System.err.println("Se exploto");
                        }
                    }
                } catch (NullPointerException r) {

                } finally {
                    xml.getCuerpo().setParametros(parametros);
                }
            }

        }
        boolean respuesta = true;
        if (!xml.getAutor().getNombre().equals("")) {
            return false;
        } else if (xml.getCuerpo().getTipo_datos().length == 0) {
            return false;
        } else if (xml.getCuerpo().getParametros().size() == 0) {
            return false;
        }
    } catch (JDOMException ex) {
        Logger.getLogger(ValidXml.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ValidXml.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
    }
    return true;
}

From source file:count_dep.Count_dep.java

public static LinkedList<Event> ReadEvents(File f) throws JDOMException, IOException {
    LinkedList<Event> res = new LinkedList<>();
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(f);
    Element foo = doc.getRootElement();
    List<Element> one_document = foo.getChildren();
    for (Element one_document1 : one_document) {
        List<Element> ERE = one_document1.getChildren();
        for (Element e : ERE) {
            if ("event".equals(e.getName())) {
                List<Element> mentions = e.getChildren("event_mention");
                for (Element m : mentions) {
                    Event eve = new Event();
                    Element charseq;
                    Element ldcscpope = m.getChild("ldc_scope");
                    charseq = ldcscpope.getChild("charseq");
                    eve.span = charseq.getText().replace("\n", " ");
                    Element anchor = m.getChild("anchor");
                    charseq = anchor.getChild("charseq");
                    eve.trigger = charseq.getText();
                    if (eve.trigger.equalsIgnoreCase("saturday")) {
                        int a = 0;
                        a = a + 1;//from w  w  w.  ja v  a 2 s.c o m
                    }
                    eve.eventtype = e.getAttribute("SUBTYPE").getValue();
                    eve.eventlargetype = e.getAttribute("TYPE").getValue();
                    List<Element> arguments = m.getChildren("event_mention_argument");
                    for (Element argu : arguments) {
                        String argumentstr = argu.getChild("extent").getChild("charseq").getText();
                        if ("U.S".equals(argumentstr) || "U.N".equals(argumentstr)
                                || "Feb".equals(argumentstr)) {
                            argumentstr += ".";
                        }
                        if (argumentstr.equalsIgnoreCase("Basra")) {
                            int a = 0;
                            a = a + 1;
                        }
                        eve.arguments.add(new EventArgument(argumentstr, argu.getAttributeValue("ROLE")));
                    }
                    eve.filename = f.getName();
                    res.add(eve);
                }

            }
        }
    }
    return res;
}

From source file:count_dep.Count_dep.java

public static LinkedList<Event> ReadGrishmanEvents(File f) throws JDOMException, IOException {
    LinkedList<Event> res = new LinkedList<>();
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(f);
    Element foo = doc.getRootElement();
    List<Element> one_document = foo.getChildren();
    for (Element one_document1 : one_document) {
        List<Element> ERE = one_document1.getChildren();
        for (Element e : ERE) {
            if ("event".equals(e.getName())) {
                List<Element> mentions = e.getChildren("event_mention");
                for (Element m : mentions) {
                    Event eve = new Event();
                    eve.filename = f.getName();
                    Element charseq;
                    Element anchor = m.getChild("anchor");
                    charseq = anchor.getChild("charseq");
                    eve.span = m.getChild("extent").getChild("charseq").getText();
                    eve.trigger = charseq.getText();
                    eve.eventtype = e.getAttribute("SUBTYPE").getValue();
                    List<Element> arguments = m.getChildren("event_mention_argument");
                    for (Element argu : arguments) {
                        eve.arguments/*from   w w w  .  ja  va2 s.  co  m*/
                                .add(new EventArgument(argu.getChild("extent").getChild("charseq").getText(),
                                        argu.getAttributeValue("ROLE")));
                    }
                    //   eve.filename = f.getName();
                    res.add(eve);
                }

            }

        }
    }
    return res;
}

From source file:cz.muni.fi.pb138.scxml2voicexmlj.XmlHelper.java

/**
 * Get attribute value from {@code element}
 * @throws IllegalArgumentException if no such attribute is present
 *//*from   w  w w  .  j a v  a  2  s. c  o m*/
public String extractAttribute(Element element, String name) {
    Attribute attribute = element.getAttribute(name);
    if (attribute == null) {
        throw new IllegalArgumentException("Element " + element + " doesnt have attribute " + name);
    }
    return attribute.getValue();
}