Example usage for org.jdom2 Element Element

List of usage examples for org.jdom2 Element Element

Introduction

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

Prototype

public Element(final String name) 

Source Link

Document

Create a new element with the supplied (local) name and no namespace.

Usage

From source file:codigoFonte.Sistema.java

public User pesquisarUser(String matricula) {
    File file = new File("Sistema.xml");
    Document newDocument = null;//w  w w . j  a  v a2  s. co 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 ww  w  .  ja v a2  s.  c om
    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 w w .j  ava  2s  .c  o m
    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;//w w w  .  j  a v a 2s  .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);
    }
    if (root.getAttributeValue("username").equals(username)
            && root.getAttributeValue("password").equals(password)) {
        autenticado = true;
        return autenticado;
    }
    return autenticado;
}

From source file:com.android.helpme.demo.utils.position.Position.java

License:Apache License

@Override
public Element getElementAs(String string) {
    Element object = new Element(string);

    object.setAttribute(LONGITUDE, this.longitude.toString());
    object.setAttribute(LATITUDE, this.latitude.toString());
    object.setAttribute(SPEED, this.speed.toString());
    object.setAttribute(DIRECTION, this.direction.toString());
    object.setAttribute(PRECISION, this.precision.toString());
    object.setAttribute(DATE, this.date.toString());
    return object;
}

From source file:com.archimatetool.canvas.templates.wizard.SaveCanvasAsTemplateWizard.java

License:Open Source License

private String createManifest() throws IOException {
    Document doc = new Document();
    Element root = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_MANIFEST);
    doc.setRootElement(root);//from   w w  w  .  jav  a 2  s. co m

    // Type
    root.setAttribute(ITemplateXMLTags.XML_TEMPLATE_ATTRIBUTE_TYPE,
            CanvasModelTemplate.XML_CANVAS_TEMPLATE_ATTRIBUTE_TYPE_MODEL);

    // Timestamp
    root.setAttribute(ITemplateXMLTags.XML_TEMPLATE_ATTRIBUTE_TIMESTAMP,
            Long.toString(System.currentTimeMillis()));

    // Name
    Element elementName = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_NAME);
    elementName.setText(fTemplateName);
    root.addContent(elementName);

    // Description
    Element elementDescription = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_DESCRIPTION);
    elementDescription.setText(fTemplateDescription);
    root.addContent(elementDescription);

    // Thumbnail
    if (fIncludeThumbnail) {
        String keyThumb = TemplateManager.ZIP_ENTRY_THUMBNAILS + "1.png"; //$NON-NLS-1$
        Element elementKeyThumb = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_KEY_THUMBNAIL);
        elementKeyThumb.setText(keyThumb);
        root.addContent(elementKeyThumb);
    }

    return JDOMUtils.write2XMLString(doc);
}

From source file:com.archimatetool.editor.model.impl.EditorModelManager.java

License:Open Source License

public void saveState() throws IOException {
    Document doc = new Document();
    Element rootElement = new Element("models"); //$NON-NLS-1$
    doc.setRootElement(rootElement);/*w  w  w . jav a 2 s  . c om*/
    for (IArchimateModel model : getModels()) {
        File file = model.getFile(); // has been saved
        if (file != null) {
            Element modelElement = new Element("model"); //$NON-NLS-1$
            modelElement.setAttribute("file", file.getAbsolutePath()); //$NON-NLS-1$
            rootElement.addContent(modelElement);
        }
    }
    JDOMUtils.write2XMLFile(doc, backingFile);
}

From source file:com.archimatetool.templates.impl.wizard.SaveArchimateModelAsTemplateWizard.java

License:Open Source License

private String createManifest() throws IOException {
    Document doc = new Document();
    Element root = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_MANIFEST);
    doc.setRootElement(root);/*from  ww  w .  j a va2 s . c o  m*/

    // Type
    root.setAttribute(ITemplateXMLTags.XML_TEMPLATE_ATTRIBUTE_TYPE,
            ArchimateModelTemplate.XML_TEMPLATE_ATTRIBUTE_TYPE_MODEL);

    // Timestamp
    root.setAttribute(ITemplateXMLTags.XML_TEMPLATE_ATTRIBUTE_TIMESTAMP,
            Long.toString(System.currentTimeMillis()));

    // Name
    Element elementName = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_NAME);
    elementName.setText(fTemplateName);
    root.addContent(elementName);

    // Description
    Element elementDescription = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_DESCRIPTION);
    elementDescription.setText(fTemplateDescription);
    root.addContent(elementDescription);

    // Thumbnails
    if (fIncludeThumbnails) {
        if (fSelectedDiagramModel != null) {
            int i = 1;
            for (IDiagramModel dm : fModel.getDiagramModels()) {
                if (dm == fSelectedDiagramModel) {
                    String keyThumb = TemplateManager.ZIP_ENTRY_THUMBNAILS + i + ".png"; //$NON-NLS-1$
                    Element elementKeyThumb = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_KEY_THUMBNAIL);
                    elementKeyThumb.setText(keyThumb);
                    root.addContent(elementKeyThumb);
                    break;
                }
                i++;
            }
        }
    }

    return JDOMUtils.write2XMLString(doc);
}

From source file:com.archimatetool.templates.model.AbstractTemplate.java

License:Open Source License

@Override
public void save() throws IOException {
    if (fFile == null || !fFile.exists()) {
        return;/*  www .ja va 2s  . co m*/
    }

    // Manifest
    Document doc = new Document();
    Element root = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_MANIFEST);
    doc.setRootElement(root);

    Element elementName = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_NAME);
    elementName.setText(getName());
    root.addContent(elementName);

    Element elementDescription = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_DESCRIPTION);
    elementDescription.setText(getDescription());
    root.addContent(elementDescription);

    if (fKeyThumbnailPath != null) {
        Element elementKeyThumb = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_KEY_THUMBNAIL);
        elementKeyThumb.setText(fKeyThumbnailPath);
        root.addContent(elementKeyThumb);
    }

    String manifest = JDOMUtils.write2XMLString(doc);

    // Model
    String model = ZipUtils.extractZipEntry(fFile, TemplateManager.ZIP_ENTRY_MODEL);

    // Start a zip stream
    File tmpFile = File.createTempFile("architemplate", null); //$NON-NLS-1$
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tmpFile));
    ZipOutputStream zOut = new ZipOutputStream(out);

    ZipUtils.addStringToZip(manifest, TemplateManager.ZIP_ENTRY_MANIFEST, zOut);
    ZipUtils.addStringToZip(model, TemplateManager.ZIP_ENTRY_MODEL, zOut);

    // Thumbnails
    Image[] images = getThumbnails();
    int i = 1;
    for (Image image : images) {
        ZipUtils.addImageToZip(image, TemplateManager.ZIP_ENTRY_THUMBNAILS + i++ + ".png", zOut, SWT.IMAGE_PNG, //$NON-NLS-1$
                null);
    }

    zOut.flush();
    zOut.close();

    // Delete and copy
    fFile.delete();
    FileUtils.copyFile(tmpFile, fFile, false);
    tmpFile.delete();
}

From source file:com.archimatetool.templates.model.TemplateManager.java

License:Open Source License

public void saveUserTemplatesManifest() throws IOException {
    if (fUserTemplates == null || fUserTemplateGroups == null) {
        return;//from w w  w.  j a  v a2  s.c om
    }

    Document doc = new Document();
    Element rootElement = new Element(XML_TEMPLATE_ELEMENT_MANIFEST);
    doc.setRootElement(rootElement);

    for (ITemplate template : fUserTemplates) {
        Element templateElement = new Element(XML_TEMPLATE_ELEMENT_TEMPLATE);
        rootElement.addContent(templateElement);
        templateElement.setAttribute(XML_TEMPLATE_ATTRIBUTE_TYPE, template.getType());
        templateElement.setAttribute(XML_TEMPLATE_ATTRIBUTE_ID, template.getID());
        templateElement.setAttribute(XML_TEMPLATE_ATTRIBUTE_FILE, template.getFile().getAbsolutePath());
    }

    for (ITemplateGroup group : fUserTemplateGroups) {
        Element groupElement = new Element(XML_TEMPLATE_ELEMENT_GROUP);
        rootElement.addContent(groupElement);
        groupElement.setAttribute(XML_TEMPLATE_ATTRIBUTE_NAME, group.getName());
        for (ITemplate template : group.getTemplates()) {
            Element templateRefElement = new Element(XML_TEMPLATE_ELEMENT_TEMPLATE_REF);
            groupElement.addContent(templateRefElement);
            templateRefElement.setAttribute(XML_TEMPLATE_ATTRIBUTE_REF, template.getID());
        }
    }

    JDOMUtils.write2XMLFile(doc, getUserTemplatesManifestFile());
}