analyseWriter/src/baseEvaluations/ecritureBaseEvaluation.java

96 lines
2.9 KiB
Java

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 MEPTL.commandes;
import cXML.Run;
import cXML.node;
public class ecritureBaseEvaluation {
public ecritureBaseEvaluation(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);
}
if(file!=null) {
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("<\\bevaluation\\b.{1,500}\\bdate\\b=\\\""+ evaluation.getAttributs().get("date") +"\\\".*?<\\/\\bevaluation\\b>");
Matcher m = p.matcher(baseToString.trim());
String debut;
String fin;
String XMLBase="";
if(m.find()) {
System.out.println("match start ="+ m.start());
System.out.println("match end ="+ m.end());
//Supprime le node évluation pour le remplacer
debut = baseToString.substring(0,m.start());
fin = baseToString.substring(m.end());
String codeXMLEvaluation = Run.ecritureNode(evaluation, 0).toString().replace("\t","").replace("\r", "").replace("\n", "").replaceAll(">/{1,}<","><");
XMLBase = debut + codeXMLEvaluation + fin;
}
try {
// écriture du node
Path outputFilePath = Paths.get("C:/Users/pabr6/Downloads/test.xml");
BufferedWriter fichier = Files.newBufferedWriter(outputFilePath, StandardCharsets.UTF_8);
fichier.write(XMLBase);
fichier.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch (Exception e) {
}
}
// try {
// Run.ecritureNodeEnXML(commandes.evaluationsBase, commandes.NameBaseEvaluations, directoryName, Run.TypeFile.Evaluation);
// } catch (IOException e) {
// e.printStackTrace();
// }
}
}