1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
package ihm.projet2.modele.importers;
import ihm.projet2.modele.Carnet;
import ihm.projet2.modele.Champ;
import ihm.projet2.modele.Contact;
import ihm.projet2.modele.Groupe;
import ihm.projet2.vue.dialogs.PopupConfirmation;
import ihm.projet2.vue.utils.ImageLoader;
import java.io.FileOutputStream;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
* Un exporteur de {@link Carnet} vers un fichier XML.
*
*/
public class CarnetXMLExporter implements CarnetExporter {
/** Nom du fichier vers lequel exporter. */
private String nomFichier;
/**
* Cree un exporteur de {@link Carnet} XML.
*
* @param nom Nom du fichier vers lequel exporter.
*/
public CarnetXMLExporter(String nom) {
this.nomFichier = nom;
}
/**
* Encrypte la chaine donnee avec le mot de passe.
*
* @param string Chaine a encrypter.
* @param mdp Mot de passe.
* @return La chaine encryptee.
*/
private String encrypter(String string, String mdp) {
if (mdp == "") {
return string;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < string.length(); i++) {
sb.append((char) (string.charAt(i) + mdp.charAt(i % mdp.length())));
}
return sb.toString();
}
@Override
public void exporter(Carnet c) {
try {
Element root = new Element("carnet");
root.setAttribute("mdp", this.encrypter(c.getMdp(), c.getMdp()));
Element contacts = new Element("contacts");
Element groupes = new Element("groupes");
root.addContent(contacts);
root.addContent(groupes);
for (Contact contact : c.getContacts()) {
Element cont = new Element("contact");
cont.setAttribute("nom", this.encrypter(contact.getNom()
.getDonnee(),
c.getMdp()));
if (contact.getImagePath() != null) {
cont.setAttribute("photo",
this.encrypter(contact.getImagePath(),
c.getMdp()));
}
contacts.addContent(cont);
for (Champ champ : contact.getChamps()) {
Element champElt = new Element("champ");
champElt.setAttribute("nom", this.encrypter(champ.getType()
.name(),
c.getMdp()));
champElt.setAttribute("donnee",
this.encrypter(champ.getDonnee(),
c.getMdp()));
cont.addContent(champElt);
}
}
for (Groupe g : c.getGroupes()) {
Element groupe = new Element("groupe");
groupe.setAttribute("nom", this.encrypter(g.getNom(),
c.getMdp()));
for (Contact co : g.getContacts()) {
Element groupElt = new Element("contact");
groupElt
.setText(this
.encrypter(
String
.valueOf(c
.getContacts()
.indexOf(co)),
c.getMdp()));
groupe.addContent(groupElt);
}
groupes.addContent(groupe);
}
Document doc = new Document(root);
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
sortie.output(doc, new FileOutputStream(this.nomFichier));
new PopupConfirmation(null, "Le carnet a bien ete sauvegarde",
ImageLoader.getImage("/smile_64.png"));
}
catch (Exception e) {
e.printStackTrace();
}
}
} |
Partager