better xml check
This commit is contained in:
@@ -14,9 +14,9 @@
|
||||
color: #ddd;
|
||||
}
|
||||
|
||||
h3 {
|
||||
border-top: 1px solid grey;
|
||||
}
|
||||
h3 {
|
||||
border-top: 1px solid grey;
|
||||
}
|
||||
|
||||
.container {
|
||||
border: 1px solid #777;
|
||||
@@ -177,6 +177,7 @@ h3 {
|
||||
</div>
|
||||
<div class="container">
|
||||
<h2>Tester le fichier lxxplot</h2>
|
||||
<div>Le fichier est il un xml ? : <span id="isXml" class="test-result">...</span></div>
|
||||
<div>Le fichier est il un lxxplot ? : <span id="isLxxplot" class="test-result">...</span></div>
|
||||
<div>Le fichier possede t il une shape sur le premier layer ? :<span id="hasShape"
|
||||
class="test-result">...</span></div>
|
||||
@@ -217,13 +218,16 @@ h3 {
|
||||
<script>
|
||||
// declaration des variables
|
||||
var xmlInputContent = "";
|
||||
var xmlInputDoc = undefined;
|
||||
var xmlOutputContent = "";
|
||||
var shape = "";
|
||||
var isXml = false;
|
||||
var isLxxplot = false;
|
||||
var hasShape = false;
|
||||
var hasGroup = false;
|
||||
var filledOptionalFields = undefined;
|
||||
var filledFields = [];
|
||||
var isXmlDiv = document.getElementById("isXml");
|
||||
var isLxxplotDiv = document.getElementById("isLxxplot");
|
||||
var hasShapeDiv = document.getElementById("hasShape");
|
||||
var hasGroupDiv = document.getElementById("hasGroup");
|
||||
@@ -297,18 +301,13 @@ h3 {
|
||||
// reinitialise les resultats du test de fichier
|
||||
function resetChecks() {
|
||||
let content = '<span class="no-answer">Sans avis</span>';
|
||||
isXmlDiv.innerHTML = content;
|
||||
isLxxplotDiv.innerHTML = content;
|
||||
hasShapeDiv.innerHTML = content;
|
||||
hasGroupDiv.innerHTML = content;
|
||||
dataFields.hidden = true;
|
||||
}
|
||||
|
||||
// action quand le document est bien chargé
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
resetChecks();
|
||||
fillSelectOptions();
|
||||
});
|
||||
|
||||
// Remplit la liste déroulante de type d'appareil
|
||||
function fillSelectOptions() {
|
||||
dataTypeSelect.innerHTML = '<option value="">Sélectionner un type d\'appareil</option>';
|
||||
@@ -320,12 +319,48 @@ h3 {
|
||||
});
|
||||
}
|
||||
|
||||
// action quand le document est bien chargé
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
resetChecks();
|
||||
fillSelectOptions();
|
||||
});
|
||||
|
||||
// ecoute si le type d'appareil est changé
|
||||
dataTypeSelect.addEventListener("change", () => {
|
||||
const selectedOptionId = dataTypeSelect.value;
|
||||
displayoptionalFields(selectedOptionId);
|
||||
});
|
||||
|
||||
// charge le fichier lxxplot
|
||||
function loadFile() {
|
||||
const fileInput = document.getElementById('fileInput');
|
||||
const file = fileInput.files[0];
|
||||
if (!file) {
|
||||
alert("Veuillez sélectionner un fichier lxxplot.");
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = function (e) {
|
||||
xmlInputContent = e.target.result;
|
||||
dataFields.hidden = !runChecks();
|
||||
};
|
||||
reader.readAsText(file);
|
||||
}
|
||||
|
||||
// verifie le contenu du fichier lxxplot
|
||||
function runChecks() {
|
||||
isXml = isValidXML();
|
||||
if (!isXml) return false;
|
||||
isLxxplot = checkIfValidPath("/lxplot", 'isLxxplot');
|
||||
hasShape = checkIfValidPath("/lxplot/layers/layer/shape", 'hasShape');
|
||||
if (hasShape) {
|
||||
getShape();
|
||||
}
|
||||
hasGroup = checkIfValidPath("/lxplot/layers/layer/shape/class[text() = 'LXGroup']", 'hasGroup');
|
||||
return (isXml && isLxxplot && hasShape && hasGroup);
|
||||
}
|
||||
|
||||
// Affiche les champs optionnels en fonction du type d'appareil sélectionné
|
||||
function displayoptionalFields(selectedOptionId) {
|
||||
optionalFields.innerHTML = "";// Efface les champs précédents
|
||||
@@ -354,53 +389,67 @@ h3 {
|
||||
});
|
||||
}
|
||||
|
||||
// charge le fichier lxxplot
|
||||
function loadFile() {
|
||||
const fileInput = document.getElementById('fileInput');
|
||||
const file = fileInput.files[0];
|
||||
if (!file) {
|
||||
alert("Veuillez sélectionner un fichier lxxplot.");
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Vérifie si une chaîne de caractères est un XML valide.
|
||||
* @param {string} xmlString - La chaîne XML à tester
|
||||
* @returns {boolean|Error} - Retourne true si valide, sinon une Error avec le message d'erreur
|
||||
* mistral ai. prompt : "quel test effectuer sur un document afin de verifier que ce soit bien un arbre xml ? propose moi un test en javascript natif
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = function (e) {
|
||||
xmlInputContent = e.target.result;
|
||||
dataFields.hidden = !runChecks();
|
||||
};
|
||||
reader.readAsText(file);
|
||||
}
|
||||
"
|
||||
*/
|
||||
function isValidXML() {
|
||||
try {
|
||||
// Crée un nouveau DOMParser
|
||||
const parser = new DOMParser();
|
||||
// Parse la chaîne XML
|
||||
xmlInputDoc = parser.parseFromString(xmlInputContent, "application/xml");
|
||||
|
||||
// verifie le contenu du fichier lxxplot
|
||||
function runChecks() {
|
||||
isLxxplot = checkIfValidPath("/lxplot", 'isLxxplot');
|
||||
hasShape = checkIfValidPath("/lxplot/layers/layer/shape", 'hasShape');
|
||||
if (hasShape) {
|
||||
getShape();
|
||||
// Récupère les erreurs de parsing
|
||||
const parserErrors = xmlInputDoc.getElementsByTagName("parsererror");
|
||||
if (parserErrors.length > 0) {
|
||||
const errorMessage = parserErrors[0].textContent;
|
||||
//throw new Error(`XML invalide : ${errorMessage}`);
|
||||
isXmlDiv.innerHTML = '<span class="invalid">non</span>';
|
||||
return false;
|
||||
}
|
||||
|
||||
// Vérifie qu'il y a un élément racine et qu'il n'y a qu'un seul enfant à la racine du document
|
||||
if (xmlInputDoc.documentElement.nodeName === "parsererror") {
|
||||
//throw new Error("Le document XML n'est pas bien formé.");
|
||||
isXmlDiv.innerHTML = '<span class="invalid">non</span>';
|
||||
return false;
|
||||
}
|
||||
|
||||
// Vérifie qu'il n'y a qu'un seul élément racine
|
||||
if (xmlInputDoc.childNodes.length !== 1 || xmlInputDoc.documentElement.nodeType !== Node.ELEMENT_NODE) {
|
||||
// throw new Error("Le document XML doit avoir un seul élément racine.");
|
||||
isXmlDiv.innerHTML = '<span class="invalid">non</span>';
|
||||
return false;
|
||||
}
|
||||
|
||||
isXmlDiv.innerHTML = '<span class="valid">oui</span>';
|
||||
return true;
|
||||
} catch (e) {
|
||||
isXmlDiv.innerHTML = '<span class="invalid">non</span>';
|
||||
return e;
|
||||
}
|
||||
hasGroup = checkIfValidPath("/lxplot/layers/layer/shape/class[text() = 'LXGroup']", 'hasGroup');
|
||||
return (isLxxplot && hasShape && hasGroup);
|
||||
}
|
||||
|
||||
// petit utilitaire pour tester le contenu du fichier lxxplot
|
||||
function checkIfValidPath(xpathQuery, resultDiv) {
|
||||
resultDiv = document.getElementById(resultDiv);
|
||||
let status = parseXml(xpathQuery).status;
|
||||
let status = runXpathQuery(xpathQuery).status;
|
||||
resultDiv.innerHTML = status ? '<span class="valid">oui</span>' : '<span class="invalid">non</span>';
|
||||
return status;
|
||||
}
|
||||
|
||||
// petit utilitaire permettant de tester le xml du fichier lxxplot
|
||||
function parseXml(xpathQuery) {
|
||||
function runXpathQuery(xpathQuery) {
|
||||
try {
|
||||
// Parser le XML
|
||||
const parser = new DOMParser();
|
||||
const xmlDoc = parser.parseFromString(xmlInputContent, "text/xml");
|
||||
|
||||
// Exécuter la requête XPath
|
||||
const xpathResult = xmlDoc.evaluate(
|
||||
const xpathResult = xmlInputDoc.evaluate(
|
||||
xpathQuery,
|
||||
xmlDoc,
|
||||
xmlInputDoc,
|
||||
null,
|
||||
XPathResult.ANY_TYPE,
|
||||
null
|
||||
@@ -428,20 +477,21 @@ h3 {
|
||||
// reuperer la shape depuis le fichier lxxplot
|
||||
function getShape() {
|
||||
try {
|
||||
// Parser le XML
|
||||
const parser = new DOMParser();
|
||||
const xmlDoc = parser.parseFromString(xmlInputContent, "text/xml");
|
||||
|
||||
// Exécuter la requête XPath
|
||||
const xpathResult = xmlDoc.evaluate(
|
||||
const xpathResult = xmlInputDoc.evaluate(
|
||||
"/lxplot/layers/layer/shape",
|
||||
xmlDoc,
|
||||
xmlInputDoc,
|
||||
null,
|
||||
XPathResult.FIRST_ORDERED_NODE_TYPE,
|
||||
null
|
||||
);
|
||||
|
||||
shape = xpathResult.singleNodeValue;
|
||||
if (xpathResult.length > 0) {
|
||||
return { "status": true, "data": shape };
|
||||
} else {
|
||||
return { "status": false, "data": "Not found" };
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("error parsing xml", e.message);
|
||||
return { "status": false, "data": "Error : " + e.message };
|
||||
|
||||
Reference in New Issue
Block a user