1 /*
2 * Copyright (c) 2002-2023, City of Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
33 */
34
35 package fr.paris.lutece.plugins.utilitairesfo.utils;
36
37 import java.text.Normalizer;
38 import java.util.Arrays;
39 import java.util.StringJoiner;
40
41 public class StringUtils extends org.apache.commons.lang3.StringUtils
42 {
43 private static final String STRING_UNICODE_THORN_UPPER = "þ";
44 private static final String STRING_UNICODE_TIRET = "-";
45 private static final char CHAR_BACK_SLASH = '\\';
46 private static final char CHAR_TIRET = '-';
47 private static final char CHAR_QUOTE = '\'';
48 private static final char CHAR_DOUBLE_QUOTE = '"';
49 private static final char CHAR_QUOTE_1 = '‘';
50 private static final char CHAR_QUOTE_2 = '’';
51 private static final char CHAR_CHAR_SPACE = ' ';
52 private static final String[] CHAR_TO_REPLACE_FOR_NAME = { "μ", "µ", "Μ", "Ÿ", // mu, micro, mu majuscule
53 "Þ", STRING_UNICODE_THORN_UPPER, STRING_UNICODE_TIRET, "\\", "'", "\"", "‘", "’" // Þ,þ,ß
54 // symbols
55 };
56
57 private static final String[] CHAR_REPLACEMENT_FOR_NAME = { "M", "M", "M", "Y", // M, M, M
58 "ß", "ß", STRING_UNICODE_TIRET, STRING_UNICODE_TIRET, "'", "'", "'", "'" // ß,ß
59 // symbols
60 };
61
62 private StringUtils()
63 {
64
65 }
66
67 /**
68 * Concatène les chaines non nulles avec le séparateur fourni (sans séparateur si null)
69 * @param separateur séparateur
70 * @param chaines liste dse chaines à concaténer
71 * @return chaine concaténée
72 */
73 public static String concateneStringNonVideAvecSeparateur( String separateur, String... chaines )
74 {
75 if ( chaines == null )
76 {
77 return StringUtils.EMPTY;
78 }
79
80 StringJoiner joiner = new StringJoiner( separateur!=null?separateur: StringUtils.EMPTY );
81 Arrays.stream( chaines ).filter( org.apache.commons.lang3.StringUtils::isNotBlank ).forEach( joiner::add );
82
83 return joiner.toString( );
84 }
85
86 /**
87 * Concatène les chaines non nulles et non "null" avec le séparateur fourni (sans séparateur si null)
88 * @param separateur séparateur
89 * @param chaines liste dse chaines à concaténer
90 * @return chaine concaténée
91 */
92 public static String concateneStringNonVideNonStrNullAvecSeparateur( String separateur, String... chaines )
93 {
94 if ( chaines == null )
95 {
96 return StringUtils.EMPTY;
97 }
98
99 StringJoiner joiner = new StringJoiner( separateur!=null?separateur: StringUtils.EMPTY );
100 Arrays.stream( chaines ).filter( org.apache.commons.lang3.StringUtils::isNotBlank ).filter( str -> !org.apache.commons.lang3.StringUtils.equalsIgnoreCase( "null", str.trim( ) ) ).forEach( joiner::add );
101
102 return joiner.toString( );
103 }
104
105
106 /**
107 * Remplace les caractères autres que chiffres et lettres non accentuées
108 * @param text texte à traiter
109 * @param specialReplacement caractère de remplacement des caractères spéciaux, par chaine vide si null
110 * @return
111 */
112 public static String remplaceCaracteresSpeciaux( String text, String specialReplacement )
113 {
114 return text == null ? null : text.replaceAll( "[^a-zA-Z0-9]+", specialReplacement == null ? StringUtils.EMPTY : specialReplacement );
115 }
116
117 /**
118 * Remplace les accents par le caractère non accentué correspondant.
119 * @param text texte à traiter
120 * @return
121 */
122 public static String remplaceAccents( String text )
123 {
124 return text == null ? null : Normalizer.normalize( text, Normalizer.Form.NFD ).replaceAll( "\\p{InCombiningDiacriticalMarks}+", StringUtils.EMPTY );
125 }
126
127 /**
128 * Met le nom en majuscule
129 * @param nom
130 * @return
131 */
132 public static String formatteNom( String nom )
133 {
134 if ( nom == null )
135 {
136 return null;
137 }
138
139 // To upper case
140 String sNormalized = nom.toUpperCase( );
141
142 // Replacement char
143 sNormalized = StringUtils.replaceEach( sNormalized, CHAR_TO_REPLACE_FOR_NAME, CHAR_REPLACEMENT_FOR_NAME );
144
145 return trim( sNormalized );
146
147 }
148
149 public static String formatteChar( String character )
150 {
151 if ( character == null )
152 {
153 return null;
154 }
155
156 // To upper case
157 String sNormalized = character.toUpperCase( );
158
159 // Replacement char
160 sNormalized = StringUtils.replaceEach( sNormalized, CHAR_TO_REPLACE_FOR_NAME, CHAR_REPLACEMENT_FOR_NAME );
161
162 return sNormalized;
163
164 }
165
166 /**
167 *
168 * @param prenom
169 * @return
170 */
171 public static String formattePrenom( String prenom )
172 {
173 if ( prenom == null )
174 {
175 return null;
176 }
177
178 StringBuilder sb = new StringBuilder( );
179 // La première passe en majuscule
180 boolean bCapitalizeNext = true;
181
182 for ( char c : prenom.toCharArray( ) )
183 {
184 if ( c == CHAR_BACK_SLASH || c == CHAR_TIRET || c == CHAR_QUOTE || c == CHAR_DOUBLE_QUOTE || c == CHAR_QUOTE_1 || c == CHAR_QUOTE_2 || c == CHAR_CHAR_SPACE )
185 {
186 bCapitalizeNext = true;
187 String s = String.valueOf( c );
188 s = formatteChar( s );
189 sb.append( s );
190 }
191 else
192 {
193 if ( bCapitalizeNext )
194 {
195 String s = String.valueOf( c );
196 s = formatteChar( s );
197 sb.append( s );
198 bCapitalizeNext = false;
199 }
200 else
201 {
202 String s = String.valueOf( c );
203 s = s.toLowerCase( );
204 sb.append( s );
205 }
206 }
207 }
208
209 return trim( sb.toString( ) );
210 }
211 }