analyseWriter/src/baseListesEtudiants/ecritureBaseListesEtudiants...

105 lines
3.3 KiB
Java

package baseListesEtudiants;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import MEPTL.commandes;
import cXML.Run;
import cXML.node;
public class ecritureBaseListesEtudiants {
public ecritureBaseListesEtudiants(node csv) {
if(csv==null) return;
if(csv.isVide()) return ;
File file = null;
String directoryName = Paths.get("").toAbsolutePath().toString();
if(commandes.os.contains("Win")) {
file = new File(directoryName + "\\" + "base_listes_etudiants.xml");
}else {
file = new File(directoryName + "/" + "base_listes_etudiants.xml");
}
String debut="";
String fin="";
String XMLBase="";
if(file!=null) {
//Lecture de la base de données
BufferedReader br;
try {
br = new BufferedReader(
new InputStreamReader(
new FileInputStream(file.getAbsoluteFile()), "UTF-8"));
String line;
StringBuilder targetString = new StringBuilder();
while ((line = br.readLine()) != null) {
targetString.append(line);
}
String baseToString = targetString.toString().replace("\t","").replace("\r", "").replace("\n", "").replaceAll(">/{1,}<","><");
Pattern p = Pattern.compile("<\\bfileCSV\\b.{1,100}\\bnameListStudent\\b=\\\""+ csv.getAttributs().get("nameListStudent") +"\\\".*?<\\/\\bfileCSV\\b>");
Matcher m = p.matcher(baseToString);
if(m.find()) {
//Supprime le node fileCSV pour le remplacer par le nouveau.
debut = baseToString.substring(0,m.start());
fin = baseToString.substring(m.end());
String codeXMLEvaluation = Run.ecritureNode(csv, 0).toString().replace("\t","").replace("\r", "").replace("\n", "").replaceAll(">/{1,}<","><");
XMLBase = debut + codeXMLEvaluation + fin;
}else {
//Ajoute une nouvelle liste (fileCSV) au début de la base
p = Pattern.compile("<ListesEtudiants.*?>");
m = p.matcher(baseToString);
if(m.find()) {
debut = baseToString.substring(0,m.end());
fin = baseToString.substring(m.end());
String codeXMLEvaluation = Run.ecritureNode(csv, 0).toString().replace("\t","").replace("\r", "").replace("\n", "").replaceAll(">/{1,}<","><");
XMLBase = debut + codeXMLEvaluation + fin;
}
}
try {
// écriture du node
Path outputFilePath = file.toPath();
BufferedWriter fichier = Files.newBufferedWriter(outputFilePath, StandardCharsets.UTF_8);
fichier.write(XMLBase);
fichier.close();
JFrame frame = new JFrame();
JLabel texte = new JLabel("<html><h1>Enregistrement réussi</h1><p>La liste <b>"+csv.getAttributs().get("nameListStudent")+ "</b> a été enregistrée dans la base de données.</p>"
+ "<p>"+ file.getAbsolutePath() +"</p></html>");
JOptionPane.showMessageDialog(frame, texte);
} catch (IOException e) {
e.printStackTrace();
}
}catch (Exception e) {
}
}
}
}