@@ -1172,7 +1172,7 @@ public JsonObject regexYamlObject(String champ, String texte) {
11721172 String str = regexYamlString (champ , texte );
11731173 if (str != null ) {
11741174 Map <String , Object > map = yaml .load (str );
1175- return new JsonObject (map );
1175+ return map == null ? new JsonObject () : new JsonObject (map );
11761176 }
11771177 return null ;
11781178 }
@@ -1182,32 +1182,33 @@ public JsonArray regexYamlArray(String champ, String texte) {
11821182 String str = regexYamlString (champ , texte );
11831183 if (str != null ) {
11841184 List <Object > list = yaml .load (str );
1185- return new JsonArray (list );
1185+ return list == null ? new JsonArray () : new JsonArray (list );
11861186 }
11871187 return null ;
11881188 }
11891189
11901190 public String regexYamlString (String champ , String texte ) {
11911191 String o = null ;
11921192 if (champ != null && texte != null ) {
1193- String motif = "^" + champ + ": ?([>|-]{0,2}(\\ d*)\\ n)?([\\ s\\ S]*?)(?=\\ n^\\ w|\\ Z)" ;
1193+ String motif = "^( " + champ + ") : ?([>|-]{0,2}(\\ d*)\\ n)?([\\ s\\ S]*?)(?=\\ n^\\ w|\\ Z)" ;
11941194 Matcher m = Pattern .compile (motif , Pattern .MULTILINE ).matcher (texte );
11951195 boolean trouve = m .find ();
11961196 if (trouve ) {
11971197 String groupe1 = m .group (1 );
11981198 String groupe2 = m .group (2 );
11991199 String groupe3 = m .group (3 );
1200+ String groupe4 = m .group (4 );
12001201 Integer spaces = 2 ;
1201- if (groupe2 != null && groupe2 .length () > 0 )
1202- spaces = Integer .parseInt (groupe2 );
1203- o = groupe3 .replaceAll ("^" + String .join ("" , Collections .nCopies (spaces , " " )), "" ).replaceAll ("\n " + String .join ("" , Collections .nCopies (spaces , " " )), "\n " );
1202+ if (groupe3 != null && groupe3 .length () > 0 )
1203+ spaces = Integer .parseInt (groupe3 );
1204+ o = groupe4 .replaceAll ("^" + String .join ("" , Collections .nCopies (spaces , " " )), "" ).replaceAll ("\n " + String .join ("" , Collections .nCopies (spaces , " " )), "\n " );
12041205
1205- if (groupe1 != null && groupe1 .contains (">" ))
1206+ if (groupe2 != null && groupe2 .contains (">" ))
12061207 o = o .replaceAll ("\\ n([^\\ n])" , " $1" );
12071208
1208- if (groupe1 != null && groupe1 .contains ("-" ))
1209+ if (groupe2 != null && groupe2 .contains ("-" ))
12091210 o = o .replaceAll ("\\ n+\\ Z" , "" );
1210- else if (groupe1 == null || !groupe1 .contains ("+" ))
1211+ else if (groupe2 == null || !groupe2 .contains ("+" ))
12111212 o = o .replaceAll ("\\ n\\ Z" , "" );
12121213 } else {
12131214 motif = "^" + champ + ": (.*)" ;
@@ -1222,6 +1223,44 @@ else if(groupe1 == null || !groupe1.contains("+"))
12221223 return o ;
12231224 }
12241225
1226+ public JsonObject regexYamlJsonObject (String champ , String texte ) {
1227+ String str = null ;
1228+ if (champ != null && texte != null ) {
1229+ String champActuel = null ;
1230+ String motif = "^(" + champ + "): ?([>|-]{0,2}(\\ d*)\\ n)?([\\ s\\ S]*?)(?=\\ n^\\ w|\\ Z)" ;
1231+ Matcher m = Pattern .compile (motif , Pattern .MULTILINE ).matcher (texte );
1232+ boolean trouve = m .find ();
1233+ if (trouve ) {
1234+ String groupe1 = m .group (1 );
1235+ String groupe2 = m .group (2 );
1236+ String groupe3 = m .group (3 );
1237+ String groupe4 = m .group (4 );
1238+ Integer spaces = 2 ;
1239+ champActuel = groupe1 ;
1240+ if (groupe3 != null && groupe3 .length () > 0 )
1241+ spaces = Integer .parseInt (groupe3 );
1242+ str = groupe4 .replaceAll ("^" + String .join ("" , Collections .nCopies (spaces , " " )), "" ).replaceAll ("\n " + String .join ("" , Collections .nCopies (spaces , " " )), "\n " );
1243+
1244+ if (groupe2 != null && groupe2 .contains (">" ))
1245+ str = str .replaceAll ("\\ n([^\\ n])" , " $1" );
1246+
1247+ if (groupe2 != null && groupe2 .contains ("-" ))
1248+ str = str .replaceAll ("\\ n+\\ Z" , "" );
1249+ else if (groupe2 == null || !groupe2 .contains ("+" ))
1250+ str = str .replaceAll ("\\ n\\ Z" , "" );
1251+ }
1252+ if (champActuel != null ) {
1253+ Yaml yaml = new Yaml ();
1254+ Map <String , Object > map = yaml .load (str );
1255+ JsonObject element = map == null ? new JsonObject () : new JsonObject (map );
1256+ return new JsonObject ().put (champActuel , element );
1257+ } else {
1258+ return null ;
1259+ }
1260+ }
1261+ return null ;
1262+ }
1263+
12251264 public String regexLangueYamlString (String langueNom , String champ , String texte ) {
12261265 return regexLangueYamlString (langueNom , champ , texte , null );
12271266 }
0 commit comments