Example usage for org.jdom2 Element getChildren

List of usage examples for org.jdom2 Element getChildren

Introduction

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

Prototype

public List<Element> getChildren() 

Source Link

Document

This returns a List of all the child elements nested directly (one level deep) within this element, as Element objects.

Usage

From source file:ch.unifr.diuf.diva.did.commands.AbstractGradientManipulation.java

License:Open Source License

@Override
public float execute(Element task) throws IOException, JDOMException, InterruptedException {
    // Read the noise level
    float boost = 1;

    String imgName = script.preprocess(task.getAttributeValue("ref"));
    if (imgName == null) {
        throw new IllegalArgumentException("\n" + commandName + ": requires a ref");
    }/*from  w w w .  j  a v a 2  s .  c  o  m*/
    if (!script.getImages().containsKey(imgName)) {
        throw new IllegalArgumentException("\n" + commandName + ": cannot find image " + imgName);
    }
    Image image = script.getImages().get(imgName);

    // Read additional parameters
    int nbSteps = 500;
    float density = 1;
    final int SINGLE_CORE = 0;
    final int MULTI_CORES = 1;
    final int GPU = 2;
    int algoType = MULTI_CORES;
    int currentGPU = 0;
    for (Element child : task.getChildren()) {
        if (child.getName().equals("iterations")) {
            nbSteps = Integer.parseInt(script.preprocess(child.getText()));
            continue;
        }

        if (child.getName().equals("multi-core")) {
            algoType = MULTI_CORES;
            continue;
        }

        if (child.getName().equals("single-core")) {
            algoType = SINGLE_CORE;
            continue;
        }

        if (child.getName().equalsIgnoreCase("gpu")) {
            algoType = GPU;
            if (child.getText() != null && !child.getText().equals("")) {
                currentGPU = Integer.parseInt(child.getText());
            }
            continue;
        }
    }

    GradientMap[] grad = { new GradientMap(image, 0), new GradientMap(image, 1), new GradientMap(image, 2) };

    modifyGradient(task, grad, image);

    System.out.println("Starting reconstruction");
    for (int lvl = 0; lvl < 3; lvl++) {
        switch (algoType) {
        case SINGLE_CORE:
            grad[lvl].CPUReconstruct(nbSteps);
            break;
        case MULTI_CORES:
            grad[lvl].MultiCPUReconstruct(nbSteps);
            break;
        case GPU:
            grad[lvl].GPUReconstruct(currentGPU, nbSteps);
            break;
        }
    }
    for (int lvl = 0; lvl < 3; lvl++) {
        grad[lvl].pasteValues(image, lvl);
        grad[lvl] = null;
        System.gc();
        Thread.yield();
    }
    return 0;
}

From source file:ch.unifr.diuf.diva.did.commands.Calc.java

License:Open Source License

@Override
public float execute(Element task) throws IOException, JDOMException, InterruptedException {
    float val = getAttributeFloat(task, "start");

    for (Element child : task.getChildren()) {
        if (child.getName().equals("add")) {
            val += getFloat(child);
            continue;
        }//from  w  w  w . j av  a2s .  co  m

        if (child.getName().equals("sub")) {
            val -= getFloat(child);
            continue;
        }

        if (child.getName().equals("multiply-by")) {
            val *= getFloat(child);
            continue;
        }

        if (child.getName().equals("divide-by")) {
            val /= getFloat(child);
            continue;
        }

        if (child.getName().equals("sqrt")) {
            val = (float) Math.sqrt(val);
            continue;
        }

        if (child.getName().equals("log")) {
            float base = (float) Math.exp(1);
            if (child.getAttributeValue("base") != null) {
                base = Float.parseFloat(child.getAttributeValue("base"));
            }
            val = (float) (Math.log(val) / Math.log(base));
            continue;
        }

        if (child.getName().equals("exp")) {
            val = (float) Math.exp(val);
            continue;
        }

        if (child.getName().equals("pow")) {
            float pow = getFloat(child);
            val = (float) Math.pow(val, pow);
            continue;
        }
    }

    return val;
}

From source file:ch.unifr.diuf.diva.did.commands.ImageCreator.java

License:Open Source License

@Override
public float execute(Element task) throws IOException, JDOMException {
    String id = getAttribute(task, "id");
    if (id == null) {
        throw new IllegalArgumentException("\n" + commandName + ": requires an id");
    }/*from  w  ww.j av  a 2  s.c  o  m*/

    boolean alreadyInitialized = false;
    Image img = null;
    for (Element param : task.getChildren()) {
        if (param.getName().equals("load")) {
            if (alreadyInitialized) {
                throw new IllegalArgumentException("\n" + commandName + ": cannot be created twice");
            }
            String fname = param.getAttributeValue("file");
            img = loadImage(fname);
            alreadyInitialized = true;
            continue;
        }

        if (param.getName().equals("copy")) {
            if (alreadyInitialized) {
                throw new IllegalArgumentException("\n" + commandName + ", <image> cannot be created twice");
            }
            String otherName = param.getAttributeValue("ref");
            img = copyImage(otherName);
            alreadyInitialized = true;
            continue;
        }

        if (param.getName().equals("diff")) {
            if (alreadyInitialized) {
                throw new IllegalArgumentException("\n" + commandName + ", <image> cannot be created twice");
            }
            String a = getChildString(param, "a");
            String b = getChildString(param, "b");
            Image ia = script.getImages().get(a);
            Image ib = script.getImages().get(b);
            img = new Image(ia.getWidth(), ia.getHeight());
            for (int x = 0; x < ia.getWidth(); x++) {
                for (int y = 0; y < ia.getHeight(); y++) {
                    img.set(0, x, y, ia.get(0, x, y) - ib.get(0, x, y));
                    img.set(1, x, y, ia.get(1, x, y) - ib.get(1, x, y));
                    img.set(2, x, y, ia.get(2, x, y) - ib.get(2, x, y));
                }
            }
            alreadyInitialized = true;
            continue;
        }
    }
    script.getImages().put(id, img);
    return 0;
}

From source file:codigoFonte.Sistema.java

public boolean addUser(User u) throws IOException {

    boolean success = false, matriculaExists = false;
    File file = new File("Sistema.xml");
    Document newDocument = null;//w w  w .j  av  a 2s. c  o m
    Element root = null;
    Attribute matricula = null, nome = null, tipo = null, senha = null;
    Element user = null;

    if (u.getTipo().isEmpty() || u.getTipo().isEmpty() || u.getPassword().isEmpty()) {
        return success;
    }

    if (file.exists()) {
        SAXBuilder builder = new SAXBuilder();
        try {
            newDocument = builder.build(file);
        } catch (JDOMException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        root = newDocument.getRootElement();
    } else {
        root = new Element("sistema");

        newDocument = new Document(root);
    }

    if (root.getChildren().size() > 0) {
        List<Element> listUsers = root.getChildren();
        for (Element a : listUsers) {
            if (a.getAttributeValue("matrcula").equals(u.getMatricula())) {
                matriculaExists = true;
            }
        }
    }

    if (!matriculaExists) {
        user = new Element("user");

        matricula = new Attribute("matrcula", this.newId());
        tipo = new Attribute("tipo", u.getTipo());
        nome = new Attribute("nome", u.getNome());
        senha = new Attribute("senha", u.getPassword());

        user.setAttribute(matricula);
        user.setAttribute(nome);
        user.setAttribute(tipo);
        user.setAttribute(senha);
        //user.setAttribute(divida);

        root.addContent(user);

        success = true;
    }
    //        

    XMLOutputter out = new XMLOutputter();

    try {
        FileWriter arquivo = new FileWriter(file);
        out.output(newDocument, arquivo);
    } catch (IOException ex) {
        Logger.getLogger(Sistema.class.getName()).log(Level.SEVERE, null, ex);
    }

    return success;
}

From source file:codigoFonte.Sistema.java

public boolean editarUser(User u) {
    File file = new File("Sistema.xml");
    Document newDocument = null;/*from   w  w  w  .j av a  2  s  . com*/
    Element root = null;
    boolean success = false;

    if (file.exists()) {
        SAXBuilder builder = new SAXBuilder();
        try {
            newDocument = builder.build(file);
        } catch (JDOMException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        root = newDocument.getRootElement();
    }

    List<Element> listusers = root.getChildren();
    for (Element e : listusers) {
        if (e.getAttributeValue("matrcula").equals(u.getMatricula())) {
            e.getAttribute("nome").setValue(u.getNome());
            e.getAttribute("tipo").setValue(u.getTipo());
            e.getAttribute("senha").setValue(u.getPassword());

            success = true;

            XMLOutputter out = new XMLOutputter();

            try {
                FileWriter arquivo = new FileWriter(file);
                out.output(newDocument, arquivo);
            } catch (IOException ex) {
                Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
            }
            return success;
        }
    }
    return success;
}

From source file:codigoFonte.Sistema.java

public ArrayList<User> listarUser() {
    File file = new File("Sistema.xml");
    Document newDocument = null;/* w w  w .  ja  va  2s  .com*/
    Element root = null;
    ArrayList<User> users = new ArrayList<User>();
    ;
    ArrayList<Livro> livros = new ArrayList<Livro>();

    if (file.exists()) {
        SAXBuilder builder = new SAXBuilder();
        try {
            newDocument = builder.build(file);
        } catch (JDOMException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        root = newDocument.getRootElement();
    } else {
        root = new Element("sistema");

        newDocument = new Document(root);
    }

    List<Element> listusers = root.getChildren();
    for (Element e : listusers) {
        User user = new User(null, null, null, null);
        List<Element> listlivro = e.getChildren("livro");
        List<Element> historico = e.getChildren("histrico");

        if (!listlivro.isEmpty())

            for (Element l : listlivro) {
                Livro livro = new Livro(null, null, null, 0);
                livro.setAutor(l.getAttributeValue("autor"));
                livro.setEditora(l.getAttributeValue("editora"));
                livro.setTitulo(l.getAttributeValue("ttulo"));
                livros.add(livro);
            }

        //List<Element> historico = e.getChildren("histrico");
        ArrayList<Livro> historicoObjeto = new ArrayList<Livro>();
        if (!historico.isEmpty()) {
            for (Element h : historico) {
                Livro livroHistorico = new Livro(null, null, null, 0);
                livroHistorico.setAutor(h.getAttributeValue("autor"));
                livroHistorico.setEditora(h.getAttributeValue("editora"));
                livroHistorico.setTitulo(h.getAttributeValue("ttulo"));
                historicoObjeto.add(livroHistorico);
            }
        }

        user.setMatricula(e.getAttributeValue("matrcula"));
        user.setNome(e.getAttributeValue("nome"));
        user.setTipo(e.getAttributeValue("tipo"));
        user.setLivros(livros);

        users.add(user);
    }
    return users;
}

From source file:codigoFonte.Sistema.java

public User pesquisarUser(String matricula) {
    File file = new File("Sistema.xml");
    Document newDocument = null;/*from w  w w  .j a va2s .  com*/
    Element root = null;
    User user = null;

    if (file.exists()) {
        SAXBuilder builder = new SAXBuilder();
        try {
            newDocument = builder.build(file);
        } catch (JDOMException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        root = newDocument.getRootElement();
    } else {
        root = new Element("sistema");

        newDocument = new Document(root);
    }

    List<Element> listusers = root.getChildren();
    for (Element e : listusers) {
        if (e.getAttributeValue("matrcula").equals(matricula)) {
            List<Element> listlivros = e.getChildren("livro");
            user = new User(null, null, null, null);
            if (!listlivros.isEmpty()) {
                for (Element l : listlivros) {
                    Livro livro = new Livro(null, null, null, 0);
                    livro.setAutor(l.getAttributeValue("autor"));
                    livro.setEditora(l.getAttributeValue("editora"));
                    livro.setTitulo(l.getAttributeValue("ttulo"));
                    livro.setEntrega(l.getAttributeValue("dataEntrega"));
                    livro.setAluguel(l.getAttributeValue("dataAluguel"));
                    livro.setId(l.getAttributeValue("id"));
                    user.getLivros().add(livro);
                }
            }
            List<Element> historico = e.getChildren("historico");
            if (!historico.isEmpty()) {
                for (Element h : historico) {
                    Livro livroHistorico = new Livro(null, null, null, 0);
                    livroHistorico.setAutor(h.getAttributeValue("autor"));
                    livroHistorico.setEditora(h.getAttributeValue("editora"));
                    livroHistorico.setTitulo(h.getAttributeValue("ttulo"));
                    livroHistorico.setEntrega(h.getAttributeValue("dataEntrega"));
                    livroHistorico.setAluguel(h.getAttributeValue("dataAluguel"));
                    livroHistorico.setId(h.getAttributeValue("id"));
                    user.getHistorico().add(livroHistorico);
                }
            }
            user.setMatricula(matricula);
            user.setTipo(e.getAttributeValue("tipo"));
            user.setNome(e.getAttributeValue("nome"));
            user.setPassword(e.getAttributeValue("senha"));

            return user;
        }
    }
    return user;
}

From source file:codigoFonte.Sistema.java

public boolean autentica(User u) {
    File file = new File("Sistema.xml");
    Document newDocument = null;/*from  w ww.  j a va2  s  .  c  om*/
    Element root = null;
    boolean autenticado = false;

    if (file.exists()) {
        SAXBuilder builder = new SAXBuilder();
        try {
            newDocument = builder.build(file);
        } catch (JDOMException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        root = newDocument.getRootElement();
    } else {
        root = new Element("sistema");

        newDocument = new Document(root);
    }

    User user = null;
    List<Element> listusers = root.getChildren();
    for (Element e : listusers) {
        if (e.getAttributeValue("matrcula").equals(u.getMatricula())
                && e.getAttributeValue("senha").equals(u.getPassword())) {
            autenticado = true;
            return autenticado;
        }
    }
    return autenticado;
}

From source file:com.abyala.decisiontree.SimpleDecisionTreeParser.java

License:Open Source License

private void parseResultAttributes(final ResultSpec.Builder builder, final Element element,
        final Class resultClass) throws DecisionTreeParserException {
    final Map<String, Method> annotatedResultMethods = parseAnnotatedMethods(resultClass);

    for (Element child : element.getChildren()) {
        final String attributeName = child.getAttributeValue("name");
        final Method method = annotatedResultMethods.get(attributeName);
        if (method == null)
            throw new DecisionTreeParserException("Result attribute \"" + attributeName
                    + "\" not defined on result class " + resultClass.getName());

        final ResultAttribute attribute = parseResultAttribute(child, method);
        builder.addAttribute(attribute);
    }//from w  ww  . ja  va 2 s.  com
}

From source file:com.abyala.decisiontree.SimpleDecisionTreeParser.java

License:Open Source License

private Node parseTable(final Element tableElement, final Map<String, InputType> types,
        final ResultSpec resultSpec) throws DecisionTreeParserException {
    if (tableElement == null) {
        throw new DecisionTreeParserException("Invalid XML: No element named \"tree\" found");
    }// www.j  a v a  2s. c  o m

    final Node rootNode = parseInputs("/", tableElement.getChildren(), types, resultSpec);
    rootNode.validate();
    return rootNode;
}