Example usage for org.jdom2 Element getAttributeValue

List of usage examples for org.jdom2 Element getAttributeValue

Introduction

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

Prototype

public String getAttributeValue(final String attname) 

Source Link

Document

This returns the attribute value for the attribute with the given name and within no namespace, null if there is no such attribute, and the empty string if the attribute value is empty.

Usage

From source file:codigoFonte.Sistema.java

public User pesquisarUser(String matricula) {
    File file = new File("Sistema.xml");
    Document newDocument = null;// w ww .  j a  va2s . c o m
    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 removeUser(User u) {
    File file = new File("Sistema.xml");
    Document newDocument = null;//from   w w w . j av a  2s.com
    Element root = null;
    Element user = 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();
    } else {
        root = new Element("sistema");

        newDocument = new Document(root);
    }

    List<Element> listusers = root.getChildren("user");
    for (Element e : listusers) {
        if (e.getAttributeValue("matrcula").equals(u.getMatricula()) && e.getChildren("livro").size() == 0) {
            root.removeContent(e);

            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);
            }

            success = true;
            return success;
        }
    }
    return success;
}

From source file:codigoFonte.Sistema.java

public boolean autentica(User u) {
    File file = new File("Sistema.xml");
    Document newDocument = null;//from   w  ww .  j av a  2 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:codigoFonte.Sistema.java

public boolean autenticaAdmin(String username, String password) {
    File file = new File("Sistema.xml");
    Document newDocument = null;//from   www .j  ava2 s .  com
    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);
    }
    if (root.getAttributeValue("username").equals(username)
            && root.getAttributeValue("password").equals(password)) {
        autenticado = true;
        return autenticado;
    }
    return autenticado;
}

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

License:Open Source License

private ResultSpec parseResultClass(final Element resultElement) throws DecisionTreeParserException {
    if (resultElement == null)
        throw new DecisionTreeParserException("Missing result-type element");

    final String className = resultElement.getAttributeValue("class");
    final Class clazz;
    try {//from  w  w w .j ava 2s  . com
        clazz = Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new DecisionTreeParserException("Result class not found: " + className);
    }

    final ResultSpec.Builder builder = new ResultSpec.Builder(clazz);
    parseResultAttributes(builder, resultElement, clazz);
    return builder.build();
}

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 w w . j av  a  2 s. c o m*/
}

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

License:Open Source License

private ResultAttribute parseResultAttribute(final Element element, final Method method)
        throws DecisionTreeParserException {
    final String elementType = element.getName();
    final String attributeName = element.getAttributeValue("name");
    if ("string-attribute".equals(elementType)) {
        return parseResultStringAttribute(element, attributeName, method);
    } else if ("boolean-attribute".equals(elementType)) {
        return parseResultBooleanAttribute(element, attributeName, method);
    } else if ("text-attribute".equals(elementType)) {
        return parseResultTextAttribute(element, attributeName, method);
    } else if ("integer-attribute".equals(elementType)) {
        return parseResultIntegerAttribute(element, attributeName, method);
    } else {/*from   w  w  w.j av a 2 s .c  o m*/
        throw new DecisionTreeParserException("Unknown result attribute type: " + elementType);
    }
}

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

License:Open Source License

private ResultAttribute parseResultTextAttribute(final Element element, final String name,
        final Method method) {
    final TextResultAttribute.Builder builder = new TextResultAttribute.Builder(name, method);
    builder.setDefaultValue(element.getAttributeValue("default"));
    return builder.build();
}

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

License:Open Source License

private StringResultAttribute parseResultStringAttribute(final Element element, final String name,
        final Method method) throws DecisionTreeParserException {
    final StringResultAttribute.Builder builder = new StringResultAttribute.Builder(name, method);
    for (Element value : element.getChildren("value")) {
        final String text = value.getTextNormalize();
        builder.addEnumValue(text);//  w  w w.java2  s.  c om
        if ("true".equals(value.getAttributeValue("default"))) {
            builder.setDefaultValue(text);
        }
    }

    return builder.build();
}

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

License:Open Source License

private Node parseInputs(final String parentPath, final List<Element> inputs,
        final Map<String, InputType> types, final ResultSpec resultSpec) throws DecisionTreeParserException {
    final String inputName = getInputName(parentPath, inputs);
    final String nodePath = parentPath + inputName;
    final InputType inputType = getInputType(inputName, types);
    final NodeBuilder builder = inputType.createNodeBuilder();

    for (Element input : inputs) {
        final String value = input.getAttributeValue("value");
        final String refId = input.getAttributeValue("refid");
        final String childPath = nodePath + "=" + value;
        final List<Element> children = input.getChildren();

        if (refId != null) {
            if (!children.isEmpty()) {
                throw new DecisionTreeParserException(
                        "Node at path " + childPath + " may not have both a refid and child elements.");
            } else {
                builder.addReferenceMapping(value, refId);
            }// w w w  .j  a v  a 2 s.  c  om
        } else if (children.isEmpty()) {
            throw new DecisionTreeParserException(
                    "Node at path " + childPath + " must have a result, child inputs, or a refid");
        } else if (children.size() == 1 && "result".equals(children.get(0).getName())) {
            builder.addResultMapping(value, parseResult(children.get(0), resultSpec));
        } else {
            builder.addNodeMapping(value, parseInputs(childPath + "/", children, types, resultSpec));
        }
    }

    return builder.build();
}