Kenmegne
2025-12-10 e34275be46c7c50daa5f5229a4c19c4e0ffa83ff
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.megatim.generatetxt.utilities;
 
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.dhatim.fastexcel.reader.Cell;
import org.dhatim.fastexcel.reader.CellType;
import static org.dhatim.fastexcel.reader.CellType.BOOLEAN;
import static org.dhatim.fastexcel.reader.CellType.EMPTY;
import static org.dhatim.fastexcel.reader.CellType.NUMBER;
import static org.dhatim.fastexcel.reader.CellType.STRING;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
/**
 *
 * @author ASUS
 */
public class ParserUtils {
    //Nombre be balise <entity> dans le validateur XML
 
    private static final int ENTITY_LIST_SIZE = 2;
 
    private ParserUtils() {
 
    }
 
    public static Map<Integer, Map> getValidateurElements(String validatorPath) throws Exception {
        byte[] bytes = Files.readAllBytes(new File(validatorPath).toPath());
        Document doc = intialize(bytes);
        NodeList entityList = doc.getElementsByTagName("entity"); //Liste des noeud <entity>
 
        //Map qui stocke le champ à une position, ainsi que les caractéristiques de champ(typeDonnee, taille, index de debut d'extraction)
        Map<Integer, Map> mapParams = new HashMap<>();
 
        int position = 0;
 
        if (entityList.getLength() == ENTITY_LIST_SIZE) {
 
            //Liste des noeud de type <validation> du deuximème Noeud <entity>
            NodeList entityFileLineChildren = entityList.item(1).getChildNodes();
 
            //parcours de la balise <entity> pour récupérer ses enfants
            for (int i = 0; i < entityFileLineChildren.getLength(); i++) {
 
                if (entityFileLineChildren.item(i) != null && entityFileLineChildren.item(i).getAttributes() != null) {
 
                    //balise <validation>
                    Node validation = entityFileLineChildren.item(i);
 
                    if (validation != null) {
 
                        position++;
 
                        //Liste des assertions de la balise <validation>
                        NodeList assertions = validation.getChildNodes();
 
                        for (int r = 0; r < assertions.getLength(); r++) {
 
                            Node assertion = assertions.item(r);
 
                            Map<String, String> mapAttributes = new HashMap<>();
 
                            if (assertion != null && assertion.getAttributes() != null) {
 
                                //Les attributs de la balise <assertion>
                                NamedNodeMap attributesAssertion = assertion.getAttributes();
 
                                for (int k = 0; k < attributesAssertion.getLength(); k++) {
 
                                    String nodeValue = attributesAssertion.item(k).getNodeValue();
                                    String nodeName = attributesAssertion.item(k).getNodeName();
 
                                    if (nodeName.equals("error-code")) {
                                        mapAttributes.put(nodeName, nodeValue);
                                        break;
                                    }
                                }
 
                                //Liste des balises <param-value> de la balise <assertion>
                                NodeList params = assertion.getChildNodes();
 
                                for (int j = 0; j < params.getLength(); j++) {
 
                                    if (params.item(j) != null && params.item(j).getAttributes() != null) {
 
                                        //Les attributs de la balise <param-value>
                                        NamedNodeMap attributesParamValue = params.item(j).getAttributes();
 
                                        for (int k = 0; k < attributesParamValue.getLength(); k++) {
 
                                            String nodeValue = attributesParamValue.item(k).getNodeValue();
                                            String nextNodeValue = attributesParamValue.item(++k).getNodeValue();
                                            mapAttributes.put(nodeValue, nextNodeValue);
 
                                        }
 
                                        mapParams.put(position, mapAttributes);
 
                                    }
 
                                }
 
                            }
 
                        }
 
                    }
 
                }
 
            }
 
        }
 
        return mapParams;
 
    }
 
    /**
     * Méthode permettant de convertir la données contenu dans une cellule excel
     * en chaîne de caractères
     *
     * @param cell
     * @return
     */
    public static String getCellValueAsString(Cell cell) {
 
        String val = "";
 
        if (cell != null) {
 
            CellType cellType = cell.getType();
 
            switch (cellType) {
 
                case EMPTY:
                    break;
 
                case STRING:
 
                    if (!cell.getText().equals("null")) {
 
                        val = cell.getText().replaceAll("\\p{Cntrl}", "");
 
                    }
 
                    break;
 
                case NUMBER:
 
                    val = cell.asNumber().toPlainString();
 
                    if (val.equals("null")) {
 
                    }
 
                    break;
 
                case BOOLEAN:
 
                    val = String.valueOf(cell.asBoolean());
 
                    if (val.equals("null")) {
 
                    }
 
                    break;
 
                default:
                    break;
 
            }
 
        }
 
        return val;
 
    }
 
    /**
     * Méthode qui retire le caractère retour charriot dans une chaîne
     *
     * @param str
     * @return
     */
    public static String removeReturnCharriot(String str) {
 
        String[] tab = str.split("\n");
 
        String result = "";
 
        for (String s : tab) {
 
            result += s;
        }
 
        return result;
 
    }
 
    /**
     * Méthode utilitaire pour extraire les données dans un fichier xml
     *
     * @param array
     * @return
     * @throws Exception
     */
    private static Document intialize(byte[] array) throws Exception {
 
        /**
         * Défini un factory qui aide à obtenir un parseur qui produit un arbre
         * d'objets DOM à partir d'un docuent XML.
         */
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 
        /**
         * création d'un objet du builder pour parser le fichier XML.
         */
        DocumentBuilder db = dbf.newDocumentBuilder();
 
        Document doc = db.parse(new ByteArrayInputStream(array));
 
        doc.getDocumentElement().normalize();
 
        return doc;
    }
 
    /**
     * Méthode pour obtenir l'objet de typr char correspondant à chaque
     * délimiteur
     *
     * @param delimiteur
     * @return
     */
    public static char characterOfDelimiteur(String delimiteur) {
        char character;
 
        switch (delimiteur) {
            case "\\t":
                character = '\t';
                break;
            case "\\n":
                character = '\n';
                break;
            case "":
                character = '\0';
                break;
            default:
                character = delimiteur.charAt(0);
        }
 
        return character;
    }
 
    /**
     * Méthode permettant d'encoder une chaîne de caractères
     *
     * @param datas : tableau de String à encoder
     * @param charset : encodage à utiliser
     * @return
     */
    public static String[] encodeStrings(String[] datas, Charset charset) {
        String[] encodedStrings = new String[datas.length];
 
        for (int i = 0; i < encodedStrings.length; i++) {
            byte[] bytesOfString = datas[i].getBytes();
            encodedStrings[i] = new String(bytesOfString, charset);
 
        }
        return encodedStrings;
    }
 
    /**
     * Méthode qui extrait une sous-chaîne dans une chaîne de caractères en
     * utilisant un délimiteur
     *
     * @param line : chaîne mère à séparer
     * @param character : délimiteur de chaîne
     * @return : retourne un tableau contenant les châines extraites
     */
    public static String[] splitIntoColumns(String line, char character) {
 
        int count = 0;
 
        for (int i = 0; i < line.length(); i++) {
 
            if (line.charAt(i) == character) {
                count++;
            }
 
        }
 
        String[] columns = new String[count + 1];
 
        StringBuilder builder = new StringBuilder(0);
 
        count = 0;
 
        for (int i = 0; i < line.length(); i++) {
 
            if (line.charAt(i) == character) {
 
                columns[count] = builder.toString();
 
                builder = new StringBuilder(0);
 
                count++;
 
            } else {
 
                builder.append(line.charAt(i));
            }
 
        }
 
        columns[count] = builder.toString();
 
        return columns;
    }
 
    /**
     * Méthode qui écrit dans un fichier
     *
     * @param finalColumnsTableLine : liste des châines à écrire dans le fichier
     * @param bufferWriter : ressource permettant d'écrire dans le fichier
     */
    public static void writeToFile(String[] finalColumnsTableLine, final BufferedWriter bufferWriter) throws IOException {
 
        //Ecriture dans le fichier output
        StringBuilder safeLineBuilder = new StringBuilder(0);
 
        for (String s : finalColumnsTableLine) {
            safeLineBuilder.append(s);
        }
 
        String safeLine = safeLineBuilder.toString();
 
        if (safeLine.length() > 0) {
 
            bufferWriter.write(safeLine + System.lineSeparator());
 
        }
    }
}