Example usage for org.jdom2.output Format getPrettyFormat

List of usage examples for org.jdom2.output Format getPrettyFormat

Introduction

In this page you can find the example usage for org.jdom2.output Format getPrettyFormat.

Prototype

public static Format getPrettyFormat() 

Source Link

Document

Returns a new Format object that performs whitespace beautification with 2-space indents, uses the UTF-8 encoding, doesn't expand empty elements, includes the declaration and encoding, and uses the default entity escape strategy.

Usage

From source file:view.MobilePartnerView.java

public boolean createXML(String url, String user, String driver, String password) throws Exception {
    Element root = new Element("dbconf");
    Document doc = new Document();

    Element child1 = new Element("url");
    // child1.addContent("jdbc:mysql://localhost:3306/mobile_partner");
    child1.addContent(url);/*from   w ww  .j a v  a2s. c  o m*/
    child1.setAttribute("name", "javax.persistence.jdbc.url");

    Element child2 = new Element("user");
    // child2.addContent("root");
    child2.addContent(user);
    child2.setAttribute("name", "javax.persistence.jdbc.user");

    Element child3 = new Element("driver");
    //child3.addContent("com.mysql.jdbc.Driver");
    child3.addContent(driver);
    child3.setAttribute("name", "javax.persistence.jdbc.driver");

    Element child4 = new Element("password");
    //  child4.addContent("nbuser");
    child4.addContent(password);
    child4.setAttribute("name", "javax.persistence.jdbc.password");

    root.addContent(child1);
    root.addContent(child2);
    root.addContent(child3);
    root.addContent(child4);

    doc.setRootElement(root);

    XMLOutputter outter = new XMLOutputter();
    outter.setFormat(Format.getPrettyFormat());
    outter.output(doc, new FileWriter(new File(dir + "\\dbconf.xml")));
    File xmlFile = new File(dir + "\\dbconf.xml");
    if (xmlFile.exists()) {
        return true;
    } else {
        return false;
    }
}

From source file:vue.general.Application.java

private void jBconnectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBconnectionActionPerformed

    /**//from w  w  w.  ja  v  a 2  s .c  o m
     * Aprs le clic sur le bouton se connecter
     */
    IDao dao = FactoryDao.getDAO("Utilisateur"); //Appel de la factory pour reccuperer le DaoUtilisateur
    listeUtilisateur = dao.selectAll(); //Reccupration d'un type liste via la mthode selectAll (ensemble des users)
    userText = this.jTUser.getText();//Reccupration des champs saisies
    mdpText = this.jTpassword.getText();

    /**
     * Authentification en bouclant sur la liste d'utilisateur
     */
    for (Object o : listeUtilisateur) {
        u = (Utilisateur) o;

        if (u.getLogin().equals(userText) && u.getPassword().equals(mdpText)) {
            u = new Utilisateur(u.getId(), u.getLogin(), u.getPassword(), u.getId_role(), u.getNom(),
                    u.getPrenom(), u.getEmail(), u.getTelPort(), u.getTelFixe());
            jLmessage.setText("Authentification russie");

            this.jInternalFrame1.setVisible(false);
            this.menuBar.setVisible(true);

            //Menu selon le role de l'utilisateur
            switch (u.getId_role()) {
            case 1://Cas d'un admin(DIRECTEUR)
                a = new Admin(u.getId(), u.getLogin(), u.getPassword(), u.getId_role(), u.getNom(),
                        u.getPrenom(), u.getEmail(), u.getTelPort(), u.getTelFixe());
                this.MedecinMenu.setVisible(false);
                this.SecretaireMenu.setVisible(false);
                this.RadiologieMenu.setVisible(false);
                this.GeneralisteMenu.setVisible(false);
                this.ChirurgienMenu.setVisible(false);
                this.DentisteMenu.setVisible(false);
                this.AdminMenu.setVisible(true);
                this.jButton5.setVisible(false);
                this.jButton1.setVisible(false);
                this.jButton2.setVisible(false);
                this.ItemGererDossierMedical2.setVisible(false);
                this.jBCreationUser.setVisible(true);
                this.jBStat.setVisible(true);
                break;
            case 2://Cas d'une secretaire
                s = new Secretaire(u.getId(), u.getLogin(), u.getPassword(), u.getId_role(), u.getNom(),
                        u.getPrenom(), u.getEmail(), u.getTelPort(), u.getTelFixe());
                this.MedecinMenu.setVisible(false);
                this.SecretaireMenu.setVisible(true);
                this.RadiologieMenu.setVisible(false);
                this.GeneralisteMenu.setVisible(false);
                this.ChirurgienMenu.setVisible(false);
                this.DentisteMenu.setVisible(false);
                this.AdminMenu.setVisible(false);
                this.jButton5.setVisible(false);
                this.jButton1.setVisible(false);
                this.jButton2.setVisible(false);
                this.ItemGererDossierMedical2.setVisible(false);
                this.internalSecretaire = new SecretaireJFrame();
                this.desktopPane.add(this.internalSecretaire);
                internalSecretaire.setTitle("Accueil");
                break;
            case 3://Cas d'un generaliste
                m = new Medecin(u.getId(), u.getLogin(), u.getPassword(), u.getId_role(), u.getNom(),
                        u.getPrenom(), u.getEmail(), u.getTelPort(), u.getTelFixe());
                g = new Generaliste(u.getId(), u.getLogin(), u.getPassword(), u.getId_role(), u.getNom(),
                        u.getPrenom(), u.getEmail(), u.getTelPort(), u.getTelFixe());
                this.MedecinMenu.setVisible(true);
                this.GeneralisteMenu.setVisible(true);
                this.DentisteMenu.setVisible(false);
                this.ChirurgienMenu.setVisible(false);
                this.SecretaireMenu.setVisible(false);
                this.AdminMenu.setVisible(false);
                this.RadiologieMenu.setVisible(false);
                this.jButton5.setVisible(true);
                this.jButton1.setVisible(true);
                this.jButton2.setVisible(true);
                this.ItemGererDossierMedical2.setVisible(true);
                break;
            case 4://Cas d'un Dentiste
                m = new Medecin(u.getId(), u.getLogin(), u.getPassword(), u.getId_role(), u.getNom(),
                        u.getPrenom(), u.getEmail(), u.getTelPort(), u.getTelFixe());
                d = new Dentiste(u.getId(), u.getLogin(), u.getPassword(), u.getId_role(), u.getNom(),
                        u.getPrenom(), u.getEmail(), u.getTelPort(), u.getTelFixe());
                this.MedecinMenu.setVisible(true);
                this.DentisteMenu.setVisible(true);
                this.RadiologieMenu.setVisible(false);
                this.GeneralisteMenu.setVisible(false);
                this.ChirurgienMenu.setVisible(false);
                this.SecretaireMenu.setVisible(false);
                this.AdminMenu.setVisible(false);
                this.jButton5.setVisible(true);
                this.jButton1.setVisible(true);
                this.jButton2.setVisible(true);
                this.ItemGererDossierMedical2.setVisible(true);
                break;
            case 5://Cas d'un Radiologue
                m = new Medecin(u.getId(), u.getLogin(), u.getPassword(), u.getId_role(), u.getNom(),
                        u.getPrenom(), u.getEmail(), u.getTelPort(), u.getTelFixe());
                r = new Radiologue(u.getId(), u.getLogin(), u.getPassword(), u.getId_role(), u.getNom(),
                        u.getPrenom(), u.getEmail(), u.getTelPort(), u.getTelFixe());
                this.MedecinMenu.setVisible(true);
                this.RadiologieMenu.setVisible(true);
                this.GeneralisteMenu.setVisible(false);
                this.ChirurgienMenu.setVisible(false);
                this.DentisteMenu.setVisible(false);
                this.SecretaireMenu.setVisible(false);
                this.AdminMenu.setVisible(false);
                this.jButton5.setVisible(true);
                this.jButton1.setVisible(true);
                this.jButton2.setVisible(true);
                this.ItemGererDossierMedical2.setVisible(true);
                break;
            default:
                break;
            }
            break;
        } else {
            jLmessage.setText("Authentification rate");
        }
    }

    try {
        // --- Partie 1 : Creation du DOM en memoire
        org.jdom2.Document arbreDom = new org.jdom2.Document(new Element("racine"));

        // --- Partie 2 : fermeture ou Output (dom2fichier)
        // --- Ecriture sur le DD dans le document XML
        // --- du contenu de l'arbre DOM qui est en RAM        

        // --- Les variables
        String lsFichier = "infos.xml";
        SAXBuilder sxb = new SAXBuilder();
        Element racine = new Element("Contact");

        XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
        sortie.output(arbreDom, new FileOutputStream(lsFichier));

        arbreDom = sxb.build(new File(lsFichier));

        // --- Recuperation de l'element racine
        racine = arbreDom.getRootElement();

        dao = FactoryDao.getDAO("Utilisateur");
        Utilisateur user = (Utilisateur) dao.selectById(u.getId());

        Element userBalise = new Element("utilisateur");
        userBalise.setAttribute("id", String.valueOf(user.getId()));

        Element nom = new Element("nom");
        nom.setText(user.getNom());
        Element prenom = new Element("prenom");
        prenom.setText(user.getPrenom());
        Element tel_port = new Element("tel_port");
        tel_port.setText(user.getTelPort());
        Element tel_fixe = new Element("tel_fixe");
        tel_fixe.setText(user.getTelFixe());
        Element email = new Element("email");
        email.setText(user.getEmail());

        userBalise.addContent(nom);
        userBalise.addContent(prenom);
        userBalise.addContent(tel_port);
        userBalise.addContent(tel_fixe);
        userBalise.addContent(email);
        racine.addContent(userBalise);

        // --- Partie 3 : fermeture ou Output (dom2fichier)
        // --- Ecriture sur le DD dans le document XML
        // --- du contenu de l'arbre DOM qui est en RAM
        XMLOutputter sortie2 = new XMLOutputter(Format.getPrettyFormat());
        sortie.output(arbreDom, new FileOutputStream(lsFichier));

        System.out.println("Article ajout");

    } // FIN TRY

    catch (Exception e) {
        System.err.println(e.getMessage());
    }

}

From source file:wasr.actions.SaveDocAction.java

License:Apache License

@Override
public void actionPerformed(ActionEvent actionEvent) {

    File file = new File(WASRFrame.getInstance().getCurrentReport().getReportFolder(), "report.xml");

    Document wasrDoc = WASRFrame.getInstance().getCurrentReport().getReportDOM();

    try {/*from   ww  w  .  ja v a 2 s  .  co  m*/
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        XMLOutputter outputter = new XMLOutputter();
        outputter.setFormat(Format.getPrettyFormat());
        outputter.output(wasrDoc, bos);
        bos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace(); // TODO: push save errors up to UI.
    }
}

From source file:XML.JXML.java

public void CreateXML() {

    try {/*from  ww  w.  j a v  a2  s .  c  o  m*/
        Element root = new Element("DriveLessons");
        Document doc = new Document();

        Element stdnt = new Element("Student");
        stdnt.addContent(new Element("StudentName").addContent(studentName));
        stdnt.addContent(new Element("PermitNumber").addContent(studentPermitNumber));
        stdnt.addContent(new Element("DateOfBirth").addContent(studentDOB));

        Element instr = new Element("Instructor");
        instr.addContent(new Element("InstructorName").addContent(instructorName));
        instr.addContent(new Element("InstructorID").addContent(instructorID));

        Element eval = new Element("Evaluation");
        eval.addContent(new Element("EvaluationDate").addContent(evaluationDate));
        eval.addContent(new Element("Pass").addContent(getFinalGrade()));

        root.addContent(stdnt);
        root.addContent(instr);
        root.addContent(eval);

        doc.setRootElement(root);

        XMLOutputter outter = new XMLOutputter();
        outter.setFormat(Format.getPrettyFormat());
        outter.output(doc, new FileWriter(new File("C:\\temp\\myxml.xml")));
    } catch (IOException ex) {
        Logger.getLogger(JXML.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:xmlproject.TraitementEtudiant.java

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

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

From source file:xmlproject.TraitementNote.java

License:Open Source License

/**
 *
 * @param file/* w ww.ja  v  a 2s.c  om*/
 * @throws FileNotFoundException
 * @throws IOException
 */
@Override
public void saveFile(String file) throws FileNotFoundException, IOException {
    XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
    sortie.output(document, new FileOutputStream(file));

    this.etudiant.detach();
}

From source file:XMLWriter.WriteCodesXML.java

License:Open Source License

public static void writeCode(Code code, String xmlSource) {
    try {//from   w w  w  .  ja  v a 2s.co m
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        Element codigo = new Element("codigo", ns);
        codigo.setAttribute("id", code.getID());

        for (Element e : rootNode.getChildren("codigo", ns))
            if (e.getAttributeValue("id").equals(code.getID())) {
                rootNode.removeContent(e);
                break;
            }

        Element idProf = new Element("idProfesor", ns);
        idProf.setText(code.getIDProfesor());
        Element nombre = new Element("nombre", ns);
        nombre.setText(code.getNombre());
        Element lenguaje = new Element("lenguaje", ns);
        lenguaje.setText(code.getLenguaje());
        Element resaltar = new Element("resaltar", ns);
        resaltar.setText(code.getResaltar());
        List<Element> lineas = new ArrayList<Element>();
        for (String l : code.getLineas()) {
            Element linea = new Element("linea", ns);
            linea.setText(l);
            lineas.add(linea);
        }
        List<Element> comentarios = new ArrayList<Element>();
        if (code.getIDComentarios().size() > 0) {
            for (String c : code.getIDComentarios()) {
                Element comentario = new Element("idComentario", ns);
                comentario.setText(c);
                comentarios.add(comentario);
            }
        }

        codigo.addContent(idProf);
        codigo.addContent(nombre);
        codigo.addContent(lenguaje);
        codigo.addContent(resaltar);
        codigo.addContent(lineas);
        if (comentarios.size() > 0)
            codigo.addContent(comentarios);

        rootNode.addContent(codigo);

        Format form = Format.getPrettyFormat().clone();
        form.setTextMode(Format.TextMode.TRIM_FULL_WHITE);
        XMLOutputter xmlOut = new XMLOutputter(form);
        FileOutputStream file = new FileOutputStream(xmlSource);
        xmlOut.output(doc, file);
        file.close();
    } catch (IOException io) {
        System.out.println(io.getMessage());
    } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
    }
}

From source file:XMLWriter.WriteCodesXML.java

License:Open Source License

public static boolean deleteCode(Code code, String xmlSource) {
    try {//from   www  .j av  a2s  . c o  m
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        boolean find = false;

        for (Element e : rootNode.getChildren("codigo", ns))
            if (e.getAttributeValue("id").equals(code.getID())) {
                find = rootNode.removeContent(e);
                break;
            }
        if (!find)
            return false;

        Format form = Format.getPrettyFormat().clone();
        form.setTextMode(Format.TextMode.TRIM_FULL_WHITE);
        XMLOutputter xmlOut = new XMLOutputter(form);
        FileOutputStream file = new FileOutputStream(xmlSource);
        xmlOut.output(doc, file);
        file.close();
    } catch (IOException io) {
        System.out.println(io.getMessage());
    } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
    }
    return true;
}

From source file:XMLWriter.WriteCodesXML.java

License:Open Source License

public static boolean addComment(String idCode, String idComment, String xmlSource) {
    try {//from  w  ww .jav a 2 s.  c  o  m
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        boolean find = false;

        for (Element e : rootNode.getChildren("codigo", ns))
            if (e.getAttributeValue("id").equals(idCode)) {
                find = true;
                Element idComentario = new Element("idComentario", ns);
                idComentario.setText(idComment);
                e.addContent(idComentario);
                break;
            }
        if (!find)
            return false;

        Format form = Format.getPrettyFormat().clone();
        form.setTextMode(Format.TextMode.TRIM_FULL_WHITE);
        XMLOutputter xmlOut = new XMLOutputter(form);
        FileOutputStream file = new FileOutputStream(xmlSource);
        xmlOut.output(doc, file);
        file.close();
    } catch (IOException io) {
        System.out.println(io.getMessage());
    } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
    }
    return true;
}

From source file:XMLWriter.WriteCommentsXML.java

License:Open Source License

public static boolean addComment(Comment com, String xmlSource) {
    try {//from  w w w  . j  a v  a2  s. c  om
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        Element comentario = new Element("comentario", ns);
        comentario.setAttribute("id", com.getID());

        Element idUsuario = new Element("idUsuario", ns);
        idUsuario.setText(com.getIDUsuario());
        Element texto = new Element("texto", ns);
        texto.setText("\n            " + com.getTexto().replace("\n", "\n            ") + "\n        ");
        Element calificacion = new Element("calificacion", ns);
        calificacion.setText(com.getCalificacion().toString());
        Element fechaMod = new Element("fechaMod", ns);
        fechaMod.setText(com.getFechaMod());

        comentario.addContent(idUsuario);
        comentario.addContent(texto);
        comentario.addContent(calificacion);
        comentario.addContent(fechaMod);

        rootNode.addContent(comentario);

        Format form = Format.getPrettyFormat().clone();
        form.setTextMode(Format.TextMode.TRIM_FULL_WHITE);
        XMLOutputter xmlOut = new XMLOutputter(form);
        FileOutputStream file = new FileOutputStream(xmlSource);
        xmlOut.output(doc, file);
        file.close();
    } catch (IOException io) {
        System.out.println(io.getMessage());
    } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
    }

    return true;
}