OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 174 Rev 177
Line 17... Line 17...
17
package org.openconcerto.utils;
17
package org.openconcerto.utils;
18
 
18
 
19
import java.awt.FontMetrics;
19
import java.awt.FontMetrics;
20
import java.math.BigDecimal;
20
import java.math.BigDecimal;
21
import java.nio.charset.Charset;
21
import java.nio.charset.Charset;
-
 
22
import java.text.Normalizer;
-
 
23
import java.text.Normalizer.Form;
22
import java.util.ArrayList;
24
import java.util.ArrayList;
23
import java.util.Collections;
25
import java.util.Collections;
24
import java.util.HashMap;
26
import java.util.HashMap;
25
import java.util.HashSet;
27
import java.util.HashSet;
26
import java.util.Iterator;
28
import java.util.Iterator;
27
import java.util.LinkedHashMap;
29
import java.util.LinkedHashMap;
28
import java.util.List;
30
import java.util.List;
-
 
31
import java.util.Locale;
29
import java.util.Map;
32
import java.util.Map;
30
import java.util.Set;
33
import java.util.Set;
31
import java.util.regex.Matcher;
34
import java.util.regex.Matcher;
32
import java.util.regex.Pattern;
35
import java.util.regex.Pattern;
33
 
36
 
-
 
37
import com.ibm.icu.lang.UCharacter;
-
 
38
 
34
/**
39
/**
35
 * @author Sylvain CUAZ
40
 * @author Sylvain CUAZ
36
 */
41
 */
37
public class StringUtils {
42
public class StringUtils {
38
 
43
 
Line 828... Line 833...
828
            if (matcher.find())
833
            if (matcher.find())
829
                return matcher;
834
                return matcher;
830
        }
835
        }
831
        return null;
836
        return null;
832
    }
837
    }
-
 
838
 
-
 
839
    static public interface Search {
-
 
840
        public boolean equals(String str, String anotherString);
-
 
841
 
-
 
842
        public boolean startsWith(String str, String prefix);
-
 
843
 
-
 
844
        public boolean endsWith(String str, String suffix);
-
 
845
 
-
 
846
        public boolean contains(String str, String anotherString);
-
 
847
    }
-
 
848
 
-
 
849
    static public final Search EXACT_SEARCH = new Search() {
-
 
850
        @Override
-
 
851
        public boolean startsWith(String str, String prefix) {
-
 
852
            return str.startsWith(prefix);
-
 
853
        }
-
 
854
 
-
 
855
        @Override
-
 
856
        public boolean equals(String str, String anotherString) {
-
 
857
            return str.equals(anotherString);
-
 
858
        }
-
 
859
 
-
 
860
        @Override
-
 
861
        public boolean endsWith(String str, String suffix) {
-
 
862
            return str.endsWith(suffix);
-
 
863
        }
-
 
864
 
-
 
865
        @Override
-
 
866
        public boolean contains(String str, String searchStr) {
-
 
867
            return str.contains(searchStr);
-
 
868
        }
-
 
869
    };
-
 
870
 
-
 
871
    // Simple (Single-Character) Case Mapping
-
 
872
    static public final Search SIMPLE_CASE_MAPPING_SEARCH = new Search() {
-
 
873
        @Override
-
 
874
        public boolean startsWith(String str, String prefix) {
-
 
875
            return str.regionMatches(true, 0, prefix, 0, prefix.length());
-
 
876
        }
-
 
877
 
-
 
878
        @Override
-
 
879
        public boolean equals(String str, String anotherString) {
-
 
880
            return str.equalsIgnoreCase(anotherString);
-
 
881
        }
-
 
882
 
-
 
883
        @Override
-
 
884
        public boolean endsWith(String str, String suffix) {
-
 
885
            final int suffixLength = suffix.length();
-
 
886
            return str.regionMatches(true, str.length() - suffixLength, suffix, 0, suffixLength);
-
 
887
        }
-
 
888
 
-
 
889
        @Override
-
 
890
        public boolean contains(String str, String searchStr) {
-
 
891
            final int length = searchStr.length();
-
 
892
            if (length == 0)
-
 
893
                return true;
-
 
894
 
-
 
895
            for (int i = str.length() - length; i >= 0; i--) {
-
 
896
                if (str.regionMatches(true, i, searchStr, 0, length))
-
 
897
                    return true;
-
 
898
            }
-
 
899
            return false;
-
 
900
        }
-
 
901
    };
-
 
902
 
-
 
903
    static public abstract class NormalizeSearch implements Search {
-
 
904
 
-
 
905
        protected abstract String normalize(String s);
-
 
906
 
-
 
907
        @Override
-
 
908
        public boolean startsWith(String str, String prefix) {
-
 
909
            return normalize(str).startsWith(normalize(prefix));
-
 
910
        }
-
 
911
 
-
 
912
        @Override
-
 
913
        public boolean equals(String str, String anotherString) {
-
 
914
            return normalize(str).equals(normalize(anotherString));
-
 
915
        }
-
 
916
 
-
 
917
        @Override
-
 
918
        public boolean endsWith(String str, String suffix) {
-
 
919
            return normalize(str).endsWith(normalize(suffix));
-
 
920
        }
-
 
921
 
-
 
922
        @Override
-
 
923
        public boolean contains(String str, String searchStr) {
-
 
924
            return normalize(str).contains(normalize(searchStr));
-
 
925
        }
-
 
926
    };
-
 
927
 
-
 
928
    // Fixed Locale
-
 
929
    static public final class LowerCaseMappingSearch extends NormalizeSearch {
-
 
930
 
-
 
931
        private final Locale locale;
-
 
932
 
-
 
933
        public LowerCaseMappingSearch(Locale locale) {
-
 
934
            super();
-
 
935
            this.locale = locale;
-
 
936
        }
-
 
937
 
-
 
938
        public final Locale getLocale() {
-
 
939
            return this.locale;
-
 
940
        }
-
 
941
 
-
 
942
        @Override
-
 
943
        protected String normalize(String s) {
-
 
944
            return s.toLowerCase(this.getLocale());
-
 
945
        }
-
 
946
    };
-
 
947
 
-
 
948
    // Locale.getDefault() at the time of the call
-
 
949
    static public final Search DEFAULT_LOWERCASE_MAPPING_SEARCH = new NormalizeSearch() {
-
 
950
        @Override
-
 
951
        protected String normalize(String s) {
-
 
952
            return s.toLowerCase();
-
 
953
        }
-
 
954
    };
-
 
955
 
-
 
956
    static public final Search CASE_FOLDING_SEARCH = new NormalizeSearch() {
-
 
957
        @Override
-
 
958
        protected String normalize(String s) {
-
 
959
            return normalizeCase(s);
-
 
960
        }
-
 
961
    };
-
 
962
 
-
 
963
    static private final Pattern BLANK_PATTERN = Pattern.compile("\\p{Blank}+");
-
 
964
 
-
 
965
    // replace tabs and multiple spaces by one space
-
 
966
    static public final String normalizeBlanks(String s) {
-
 
967
        return BLANK_PATTERN.matcher(s.trim()).replaceAll(" ");
-
 
968
    }
-
 
969
 
-
 
970
    static private final Pattern DIACRITICAL_PATTERN = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
-
 
971
 
-
 
972
    static public String removeDiacritical(final String s) {
-
 
973
        return DIACRITICAL_PATTERN.matcher(Normalizer.normalize(s, Form.NFD)).replaceAll("");
-
 
974
    }
-
 
975
 
-
 
976
    static public final String normalizeCase(final String s) {
-
 
977
        // the JRE only does Single-Character Case Mapping so it can't handle Tschüß/TSCHÜSS
-
 
978
        // http://userguide.icu-project.org/transforms/casemappings
-
 
979
        // https://unicode.org/faq/casemap_charprop.html#casemap
-
 
980
        return UCharacter.foldCase(s, true);
-
 
981
    }
-
 
982
 
-
 
983
    static public final String normalizeForSearch(final String s) {
-
 
984
        return normalizeCase(removeDiacritical(normalizeBlanks(s)));
-
 
985
    }
833
}
986
}