xmlproject.TraitementEtudiant.java Source code

Java tutorial

Introduction

Here is the source code for xmlproject.TraitementEtudiant.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlproject;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
 *
 * @author Aissa El Mehdi Mecheoud <wehdi-kun@hotmail.fr>
 */
public class TraitementEtudiant implements ajoutElement {

    private Element racine;
    private Document document;

    private Element etudiant = null;
    private Element nom = null;
    private Element prenom = null;
    private Element code = null;
    private Element departement = null;
    private Element moyenne = null;

    public TraitementEtudiant() {
        etudiant = new Element("etudiant");
    }

    /**
     *
     * @param file
     * @throws JDOMException
     * @throws IOException
     */
    @Override
    public void readFile(String file) throws JDOMException, IOException {

        SAXBuilder sxb = new SAXBuilder();
        document = sxb.build(file);
        this.racine = document.getRootElement();

    }

    @Override
    public void saveFile(String file) throws FileNotFoundException, IOException {

        XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
        sortie.output(document, new FileOutputStream(file));
    }

    public void addEtudiant(String nom, String prenom, String code, String departement, String moyenne)
            throws IOException {

        Attribute id = new Attribute("id", code);

        this.etudiant.setAttribute(id);
        racine.addContent(etudiant);
        this.nom = new Element("nom");
        this.nom.setText(nom);

        this.prenom = new Element("prenom");
        this.prenom.setText(prenom);
        this.code = new Element("code");
        this.code.setText(code);
        this.departement = new Element("departement");
        this.departement.setText(departement);
        this.moyenne = new Element("moyenne");
        this.moyenne.setText(moyenne);

        this.etudiant.addContent(this.nom);
        this.etudiant.addContent(this.prenom);
        this.etudiant.addContent(this.code);
        this.etudiant.addContent(this.departement);
        this.etudiant.addContent(this.moyenne);

    }

    public Element getElement() {
        return this.etudiant;
    }

    /**
     *
     */
    @Override
    public void relunch() {
        this.etudiant.detach();
        this.racine.detach();
        this.document.detachRootElement();

        this.prenom.detach();
        this.nom.detach();
        this.departement.detach();
        this.code.detach();
        this.moyenne.detach();
    }

    public java.util.List<String> getNomEtudiant() {

        String nom = null;

        java.util.List<Element> listEtudiants = racine.getChildren("etudiant");
        java.util.List<String> nomPrenom = new ArrayList();
        ;

        Iterator i = listEtudiants.iterator();
        int j = 0;

        while (i.hasNext()) {

            Element courant = (Element) i.next();
            nom = courant.getChild("nom").getText();

            nomPrenom.add(nom);
        }
        // System.out.println(" taaab 2" + n );
        return nomPrenom;
    }
    //

    public java.util.List<String> getPreomEtudiant() {

        String prenom = null;

        java.util.List<Element> listEtudiants = racine.getChildren("etudiant");
        java.util.List<String> nomPrenom = new ArrayList();
        ;

        Iterator i = listEtudiants.iterator();
        int j = 0;

        while (i.hasNext()) {

            Element courant = (Element) i.next();
            prenom = courant.getChild("prenom").getText();
            nomPrenom.add(prenom);
        }

        return nomPrenom;
    }

    //retourne une list de code
    public java.util.List<String> getCodeEtudiant() {

        String code = null;

        java.util.List<Element> listEtudiants = racine.getChildren("etudiant");
        java.util.List<String> tabCode = new ArrayList();

        Iterator i = listEtudiants.iterator();

        while (i.hasNext()) {

            Element courant = (Element) i.next();
            code = courant.getChild("code").getText();

            tabCode.add(code);
        }

        return tabCode;
    }

    //retourne une lise de donnees grace a un code
    public java.util.List<String> getInformation(String code) throws InterruptedException {
        String code2 = null;
        java.util.List<Element> listEtudiants = racine.getChildren();
        java.util.List<String> tabDonnees = new ArrayList();
        String nom2 = null;
        String prenom2 = null;
        String departement2 = null;

        for (Element courant : listEtudiants) {
            code2 = courant.getChild("code").getText();
            if (code2 == null ? code == null : code2.equals(code)) {
                nom2 = courant.getChild("nom").getText();
                prenom2 = courant.getChild("prenom").getText();
                departement2 = courant.getChild("departement").getText();
                tabDonnees.add(nom2);
                tabDonnees.add(prenom2);
                tabDonnees.add(departement2);
                tabDonnees.add(code2);

                System.out.println("Traiteme,tn : " + nom2 + prenom2 + departement2);

                break;
            } else {

                System.out.println("Erreur dans le code !");
            }
        }
        return tabDonnees;
    }

    public List<String> getMoyenneEtudiant() {
        String moy = null;

        java.util.List<Element> listEtudiants = racine.getChildren("etudiant");
        java.util.List<String> moyenne = new ArrayList();

        Iterator i = listEtudiants.iterator();

        while (i.hasNext()) {

            Element courant = (Element) i.next();
            moy = courant.getChild("moyenne").getText();

            moyenne.add(moy);
        }
        return moyenne;
    }
}