2024-04-10 12:29:17 +02:00
package baseEvaluations ;
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.node ;
public class removeEvaluation {
public removeEvaluation ( node evaluation ) {
String directoryName = " " ;
File file = null ;
if ( commandes . PathBaseEvaluationDefaut ) {
directoryName = Paths . get ( " " ) . toAbsolutePath ( ) . toString ( ) ;
} else {
directoryName = commandes . PathBaseEvaluations ;
}
if ( commandes . os . contains ( " Win " ) ) {
file = new File ( directoryName + " \\ " + commandes . NameBaseEvaluations ) ;
} else {
file = new File ( directoryName + " / " + commandes . NameBaseEvaluations ) ;
}
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,}< " , " >< " ) ;
2024-04-20 14:42:29 +02:00
Pattern p = Pattern . compile ( " < \\ bevaluation \\ b.{1,800} \\ bdate \\ b= \\ \" " + evaluation . getAttributs ( ) . get ( " date " ) + " \\ \" .*?< \\ / \\ bevaluation \\ b> " ) ;
2024-04-10 12:29:17 +02:00
Matcher m = p . matcher ( baseToString ) ;
if ( m . find ( ) ) {
//Supprime le node évluation pour le remplacer par le nouveau.
debut = baseToString . substring ( 0 , m . start ( ) ) ;
fin = baseToString . substring ( m . end ( ) ) ;
XMLBase = debut + 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>L'évaluation <b> " + evaluation . getAttributs ( ) . get ( " name " ) + " </b> a été enregistrée dans la base de données.</p> "
+ " <p> " + file . getAbsolutePath ( ) + " </p></html> " ) ;
JOptionPane . showMessageDialog ( frame , texte ) ;
} catch ( IOException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
} catch ( Exception e ) {
}
}
}
}