1818 * @author Sandro642
1919 * @version 1.0
2020 */
21-
2221public class YamlUtils {
2322
2423 private ConnectLib connectLib = new ConnectLib ();
2524
26- /**
27- * Gets the base URL from the YAML configuration file.
28- * * This method reads the `infos.yml` file located in the directory specified by the
29- * @return the base URL as a String, or null if an error occurs.
30- */
31- public String getURL () {
32-
33- String yamlFilePath = connectLib .StoreAndRetrieve ().store .get (connectLib .StoreAndRetrieve ().FILE_LOCATION_KEY )
34- + "/infos.yml" ;
35-
36- try (InputStream inputStream = Files .newInputStream (Paths .get (yamlFilePath ))) {
37- Yaml yaml = new Yaml ();
38- Map <String , Object > yamlData = yaml .load (inputStream );
39- return (String ) yamlData .get ("urlPath" );
40- } catch (Exception ex ) {
25+ /**
26+ * Gets the base URL from the YAML configuration file.
27+ * This method reads the `infos.yml` file located in the directory specified by the
28+ * @return the base URL as a String, or null if an error occurs.
29+ */
30+ public String getURL () {
31+ String yamlFilePath = connectLib .StoreAndRetrieve ().store .get (connectLib .StoreAndRetrieve ().FILE_LOCATION_KEY )
32+ + "/infos.yml" ;
33+
34+ try (InputStream inputStream = Files .newInputStream (Paths .get (yamlFilePath ))) {
35+ Yaml yaml = new Yaml ();
36+ Map <String , Object > yamlData = yaml .load (inputStream );
37+ return (String ) yamlData .get ("urlPath" );
38+ } catch (Exception ex ) {
4139 connectLib .Logger ().ERROR (connectLib .LangManager ().getMessage (CategoriesType .YAMLUTILS_CLASS , "geturl.error" , Map .of ("file" , "infos.yml" , "exception" , ex .getMessage ())));
42- return null ;
43- }
44- }
45-
46- /**
47- * Checks if logging is enabled in the YAML configuration file.
48- * This method reads the `infos.yml` file and retrieves the `enableLogs` setting.
49- *
50- * @return true if logging is enabled, false if disabled, or null if an error occurs.
51- */
52- public Boolean isLogEnabled () {
53- String yamlFilePath = connectLib .StoreAndRetrieve ().store .get (connectLib .StoreAndRetrieve ().FILE_LOCATION_KEY )
54- + "/infos.yml" ;
55-
56- try (InputStream inputStream = Files .newInputStream (Paths .get (yamlFilePath ))) {
57- Yaml yaml = new Yaml ();
58- Map <String , Object > yamlData = yaml .load (inputStream );
59- return (Boolean ) yamlData .get ("enableLogs" );
60- } catch (Exception ex ) {
61- }
40+ return null ;
41+ }
42+ }
43+
44+ /**
45+ * Checks if logging is enabled in the YAML configuration file.
46+ * This method reads the `infos.yml` file and retrieves the `enableLogs` setting.
47+ *
48+ * @return true if logging is enabled, false if disabled, or null if an error occurs.
49+ */
50+ public Boolean isLogEnabled () {
51+ String yamlFilePath = connectLib .StoreAndRetrieve ().store .get (connectLib .StoreAndRetrieve ().FILE_LOCATION_KEY )
52+ + "/infos.yml" ;
53+
54+ try (InputStream inputStream = Files .newInputStream (Paths .get (yamlFilePath ))) {
55+ Yaml yaml = new Yaml ();
56+ Map <String , Object > yamlData = yaml .load (inputStream );
57+ return (Boolean ) yamlData .get ("enableLogs" );
58+ } catch (Exception ex ) {
59+ }
6260 return null ;
63- }
64-
65- /**
66- * Retrieves the routes defined in the YAML configuration file.
67- * This method reads the `infos.yml` file and returns a map of routes.
68- *
69- * @return a map of route names to their corresponding paths, or null if an error occurs.
70- */
71- public Map <String , String > getRoutes () {
72-
73- String yamlFilePath = connectLib .StoreAndRetrieve ().store .get (connectLib .StoreAndRetrieve ().FILE_LOCATION_KEY )
74- + "/infos.yml" ;
75-
76- try (InputStream inputStream = Files .newInputStream (Paths .get (yamlFilePath ))) {
77- Yaml yaml = new Yaml ();
78- Map <String , Object > yamlData = yaml .load (inputStream );
79-
80- return (Map <String , String >) yamlData .get ("routes" );
81- } catch (Exception ex ) {
82- return null ;
83- }
84- }
85-
86- /**
87- * Generates a template `infos.yml` file if it does not already exist.
88- * If the file exists, it updates the routes section with the provided routes.
89- *
90- * @param routes a map of route names to their corresponding paths
91- */
92- public void generateTemplateIfNotExists (Map <Enum <?>, String > routes ) {
93- String basePath = connectLib .HookManager ().BASE_PATH ();
94-
95- File directory = new File (basePath );
96-
97- if (!directory .exists ()) {
98- directory .mkdirs ();
99- }
100-
101- File file = new File (basePath , "infos.yml" );
102-
103- if (file .exists ()) {
104- try {
105- List <String > lines = new ArrayList <>();
106- try (BufferedReader reader = new BufferedReader (new FileReader (file ))) {
107- String line ;
108- while ((line = reader .readLine ()) != null ) {
109- lines .add (line );
110- }
111- }
112-
113- int routesStartIndex = -1 ;
114- int routesEndIndex = -1 ;
115-
116- for (int i = 0 ; i < lines .size (); i ++) {
117- String line = lines .get (i ).trim ();
118- if (line .equals ("routes:" )) {
119- routesStartIndex = i ;
120- } else if (routesStartIndex != -1 && line .matches ("^[a-zA-Z_][a-zA-Z0-9_]*:.*" )) {
121-
122- if (!line .startsWith ("#" ) && !line .matches ("^\\ s*[a-zA-Z_][a-zA-Z0-9_]*:\\ s*\" /.*" )) {
123- routesEndIndex = i ;
124- break ;
125- }
126- }
127- }
128-
129- // If routesEndIndex is not found, it means we reached the end of the file
130- if (routesStartIndex != -1 && routesEndIndex == -1 ) {
131- routesEndIndex = lines .size ();
132- }
133-
134- if (routesStartIndex != -1 ) {
135-
136- List <String > newLines = new ArrayList <>();
137- newLines .addAll (lines .subList (0 , routesStartIndex + 1 ));
138-
139- newLines .add (" #info: \" /info/version\" " );
140- newLines .add (" #ping: \" /ping\" " );
141- newLines .add (" #status: \" /status\" " );
142- newLines .add ("" );
143-
144-
145- for (Map .Entry <Enum <?>, String > entry : routes .entrySet ()) {
146- newLines .add (" " + entry .getKey ().name ().toLowerCase () + ": \" " + entry .getValue () + "\" " );
147- }
148-
149- if (routesEndIndex < lines .size ()) {
150- newLines .add ("" );
151- newLines .addAll (lines .subList (routesEndIndex , lines .size ()));
152- }
153-
154- try (FileWriter writer = new FileWriter (file )) {
155- for (String line : newLines ) {
156- writer .write (line + "\n " );
157- }
158- }
159- }
160-
161- } catch (IOException e ) {
61+ }
62+
63+ /**
64+ * Retrieves the routes defined in the YAML configuration file.
65+ * This method reads the `infos.yml` file and returns a map of routes.
66+ *
67+ * @return a map of route names to their corresponding paths, or null if an error occurs.
68+ */
69+ public Map <String , String > getRoutes () {
70+ String yamlFilePath = connectLib .StoreAndRetrieve ().store .get (connectLib .StoreAndRetrieve ().FILE_LOCATION_KEY )
71+ + "/infos.yml" ;
72+
73+ try (InputStream inputStream = Files .newInputStream (Paths .get (yamlFilePath ))) {
74+ Yaml yaml = new Yaml ();
75+ Map <String , Object > yamlData = yaml .load (inputStream );
76+
77+ return (Map <String , String >) yamlData .get ("routes" );
78+ } catch (Exception ex ) {
79+ return null ;
80+ }
81+ }
82+
83+ /**
84+ * Generates a template `infos.yml` file if it does not already exist.
85+ * If the file exists, it updates the routes section with the provided routes.
86+ *
87+ * @param routes a map of route names to their corresponding paths
88+ */
89+ public void generateTemplateIfNotExists (Map <Enum <?>, String > routes ) {
90+ String basePath = connectLib .HookManager ().BASE_PATH ();
91+
92+ File directory = new File (basePath );
93+
94+ if (!directory .exists ()) {
95+ directory .mkdirs ();
96+ }
97+
98+ File file = new File (basePath , "infos.yml" );
99+
100+ if (file .exists ()) {
101+ try {
102+ List <String > lines = new ArrayList <>();
103+ try (BufferedReader reader = new BufferedReader (new FileReader (file ))) {
104+ String line ;
105+ while ((line = reader .readLine ()) != null ) {
106+ lines .add (line );
107+ }
108+ }
109+
110+ int routesStartIndex = -1 ;
111+
112+ // Trouver l'index de la ligne "routes:"
113+ for (int i = 0 ; i < lines .size (); i ++) {
114+ String line = lines .get (i ).trim ();
115+ if (line .equals ("routes:" )) {
116+ routesStartIndex = i ;
117+ break ;
118+ }
119+ }
120+
121+ if (routesStartIndex != -1 ) {
122+ List <String > newLines = new ArrayList <>();
123+
124+ // Garder tout ce qui vient avant "routes:"
125+ newLines .addAll (lines .subList (0 , routesStartIndex + 1 ));
126+
127+ // Ajouter les commentaires d'exemple
128+ newLines .add (" #info: \" /info/version\" " );
129+ newLines .add (" #ping: \" /ping\" " );
130+ newLines .add (" #status: \" /status\" " );
131+ newLines .add ("" );
132+
133+ // Ajouter les nouvelles routes
134+ for (Map .Entry <Enum <?>, String > entry : routes .entrySet ()) {
135+ newLines .add (" " + entry .getKey ().name ().toLowerCase () + ": \" " + entry .getValue () + "\" " );
136+ }
137+
138+ // Trouver où se termine la section routes
139+ int afterRoutesIndex = lines .size ();
140+ for (int i = routesStartIndex + 1 ; i < lines .size (); i ++) {
141+ String line = lines .get (i ).trim ();
142+
143+ // Si on trouve une ligne qui n'est pas vide, pas un commentaire,
144+ // et qui ne commence pas par une indentation (donc pas une route)
145+ if (!line .isEmpty () && !line .startsWith ("#" )) {
146+ // Vérifier si c'est une ligne de route (avec indentation)
147+ String originalLine = lines .get (i );
148+ if (!originalLine .startsWith (" " ) && !originalLine .startsWith ("\t " )) {
149+ // C'est le début d'une nouvelle section
150+ afterRoutesIndex = i ;
151+ break ;
152+ }
153+ }
154+ }
155+
156+ // Ajouter ce qui vient après la section routes (comme # Logs, enableLogs, etc.)
157+ if (afterRoutesIndex < lines .size ()) {
158+ newLines .add ("" );
159+ newLines .addAll (lines .subList (afterRoutesIndex , lines .size ()));
160+ }
161+
162+ // Écrire le fichier mis à jour
163+ try (FileWriter writer = new FileWriter (file )) {
164+ for (String line : newLines ) {
165+ writer .write (line + "\n " );
166+ }
167+ }
168+ }
169+
170+ } catch (IOException e ) {
162171 connectLib .Logger ().ERROR (connectLib .LangManager ().getMessage (CategoriesType .YAMLUTILS_CLASS , "gentempifnotexist.error" , Map .of ("file" , "infos.yml" , "exception" , e .getMessage ())));
163- }
164- } else {
165-
166- StringBuilder template = new StringBuilder (
167- "# properties ConnectLib By Sandro642\n \n " +
168- "urlPath: \" http://localhost:8080/api\" \n \n " +
169- "routes:\n " +
170- " #info: \" /info/version\" \n " +
171- " #ping: \" /ping\" \n " +
172- " #status: \" /status\" \n \n " );
173-
174- for (Map .Entry <Enum <?>, String > entry : routes .entrySet ()) {
175- template .append (" " )
176- .append (entry .getKey ().name ().toLowerCase ())
177- .append (": \" " )
178- .append (entry .getValue ())
179- .append ("\" \n " );
180- }
181-
182- template .append ("\n \n # Logs\n " )
183- .append ("enableLogs: true\n " );
184-
185- try (FileWriter writer = new FileWriter (file )) {
186- writer .write (template .toString ());
172+ }
173+ } else {
174+ // Créer un nouveau fichier avec le template
175+ StringBuilder template = new StringBuilder (
176+ "# properties ConnectLib By Sandro642\n \n " +
177+ "urlPath: \" http://localhost:8080/api\" \n \n " +
178+ "routes:\n " +
179+ " #info: \" /info/version\" \n " +
180+ " #ping: \" /ping\" \n " +
181+ " #status: \" /status\" \n \n " );
182+
183+ for (Map .Entry <Enum <?>, String > entry : routes .entrySet ()) {
184+ template .append (" " )
185+ .append (entry .getKey ().name ().toLowerCase ())
186+ .append (": \" " )
187+ .append (entry .getValue ())
188+ .append ("\" \n " );
189+ }
190+
191+ template .append ("\n # Logs\n " )
192+ .append ("enableLogs: true\n " );
193+
194+ try (FileWriter writer = new FileWriter (file )) {
195+ writer .write (template .toString ());
187196
188197 connectLib .Logger ().ERROR (connectLib .LangManager ().getMessage (CategoriesType .YAMLUTILS_CLASS , "gentempifnotexist.needed" , Map .of ("file" , "infos.yml" )));
189- } catch (IOException e ) {
198+ } catch (IOException e ) {
190199 connectLib .Logger ().ERROR (connectLib .LangManager ().getMessage (CategoriesType .YAMLUTILS_CLASS , "gentempifnotexist.errorcreated" , Map .of ("file" , "infos.yml" , "exception" , e .getMessage ())));
191- }
192- }
193- }
194- }
200+ }
201+ }
202+ }
203+ }
0 commit comments