OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 177 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
9
 * language governing permissions and limitations under the License.
Line 19... Line 19...
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;
22
import java.text.Normalizer;
23
import java.text.Normalizer.Form;
23
import java.text.Normalizer.Form;
-
 
24
import java.text.SimpleDateFormat;
24
import java.util.ArrayList;
25
import java.util.ArrayList;
25
import java.util.Collections;
26
import java.util.Collections;
26
import java.util.HashMap;
27
import java.util.HashMap;
27
import java.util.HashSet;
28
import java.util.HashSet;
28
import java.util.Iterator;
29
import java.util.Iterator;
Line 357... Line 358...
357
                res = index;
358
                res = index;
358
        }
359
        }
359
        return res;
360
        return res;
360
    }
361
    }
361
 
362
 
-
 
363
    static private final Pattern singleQuote = Pattern.compile("'", Pattern.LITERAL);
-
 
364
    static public final Pattern SINGLE_QUOTED_PATTERN = Pattern.compile("'(('')|[^'])*'");
-
 
365
    static private final Pattern twoSingleQuote = Pattern.compile("''", Pattern.LITERAL);
-
 
366
 
-
 
367
    /**
-
 
368
     * Quote a string with single quotes ( e.g. for {@link SimpleDateFormat}).
-
 
369
     * 
-
 
370
     * @param s an arbitrary string, eg "salut\ l'ami".
-
 
371
     * @return the quoted form, eg "'salut\ l''ami'".
-
 
372
     */
-
 
373
    static public final String singleQuote(String s) {
-
 
374
        if (s.isEmpty())
-
 
375
            return "''";
-
 
376
        else
-
 
377
            return "'" + singleQuote.matcher(s).replaceAll("''") + "'";
-
 
378
    }
-
 
379
 
-
 
380
    static public final String unSingleQuote(String s) {
-
 
381
        if (!SINGLE_QUOTED_PATTERN.matcher(s).matches())
-
 
382
            throw new IllegalArgumentException("Invalid quoted string " + s);
-
 
383
        return unSingleQuoteUnsafe(s);
-
 
384
    }
-
 
385
 
-
 
386
    private static String unSingleQuoteUnsafe(String s) {
-
 
387
        return twoSingleQuote.matcher(s.substring(1, s.length() - 1)).replaceAll("'");
-
 
388
    }
-
 
389
 
-
 
390
    static public final String unSingleQuote(Matcher m) {
-
 
391
        if (m.pattern() != SINGLE_QUOTED_PATTERN)
-
 
392
            throw new IllegalArgumentException("Matcher not from SINGLE_QUOTED_PATTERN : " + m);
-
 
393
        return unSingleQuoteUnsafe(m.group());
-
 
394
    }
-
 
395
 
362
    static private final Pattern quotePatrn = Pattern.compile("\"", Pattern.LITERAL);
396
    static private final Pattern quotePatrn = Pattern.compile("\"", Pattern.LITERAL);
363
    static private final Pattern slashPatrn = Pattern.compile("(\\\\+)");
397
    static private final Pattern slashPatrn = Pattern.compile("(\\\\+)");
364
 
398
 
365
    static public String doubleQuote(String s) {
399
    static public String doubleQuote(String s) {
366
        return doubleQuote(s, true);
400
        return doubleQuote(s, true);
Line 738... Line 772...
738
 
772
 
739
        return strings;
773
        return strings;
740
    }
774
    }
741
 
775
 
742
    /**
776
    /**
743
     * convert a byte array to its hexa representation
777
     * Convert a byte array to its hexa representation (uppercase)
-
 
778
     * 
-
 
779
     * @param bytes to converts
-
 
780
     * @param size of the byte array to use
-
 
781
     * @return the hexa representation
744
     */
782
     */
745
    public static String bytesToHexString(byte[] bytes) {
783
    public static String bytesToHexString(byte[] bytes, int length) {
746
        final int length = bytes.length;
-
 
747
        char[] hexChars = new char[length * 2];
784
        char[] hexChars = new char[length * 2];
748
        for (int j = 0; j < length; j++) {
785
        for (int j = 0; j < length; j++) {
749
            int v = bytes[j] & 0xFF;
786
            int v = bytes[j] & 0xFF;
750
            hexChars[j * 2] = hexArray[v >>> 4];
787
            hexChars[j * 2] = hexArray[v >>> 4];
751
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
788
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
752
        }
789
        }
753
        return new String(hexChars);
790
        return new String(hexChars);
754
    }
791
    }
755
 
792
 
-
 
793
    public static String charToHex(char c) {
-
 
794
        int v = c & 0xFF;
-
 
795
        char[] hexChars = new char[2];
-
 
796
        hexChars[0] = hexArray[v >>> 4];
-
 
797
        hexChars[1] = hexArray[v & 0x0F];
-
 
798
        return new String(hexChars);
-
 
799
    }
-
 
800
 
-
 
801
    /**
-
 
802
     * Convert a byte array to its hexa representation (uppercase)
-
 
803
     * 
-
 
804
     * @param bytes to converts
-
 
805
     * @return the hexa representation
-
 
806
     */
-
 
807
    public static String bytesToHexString(byte[] bytes) {
-
 
808
        return bytesToHexString(bytes, bytes.length);
-
 
809
    }
-
 
810
 
756
    /**
811
    /**
757
     * Whether the parameter is empty.
812
     * Whether the parameter is empty.
758
     * 
813
     * 
759
     * @param s the string to test.
814
     * @param s the string to test.
760
     * @return <code>true</code> if <code>null</code> or {@link String#isEmpty() empty}.
815
     * @return <code>true</code> if <code>null</code> or {@link String#isEmpty() empty}.
Line 766... Line 821...
766
    public static boolean isEmpty(final String s, final boolean trim) {
821
    public static boolean isEmpty(final String s, final boolean trim) {
767
        return s == null || (trim ? s.trim() : s).isEmpty();
822
        return s == null || (trim ? s.trim() : s).isEmpty();
768
    }
823
    }
769
 
824
 
770
    /**
825
    /**
771
     * Return the first parameter that is non-empty.
-
 
772
     * 
-
 
773
     * @param s1 the string to test.
-
 
774
     * @param s2 the alternate value.
-
 
775
     * @return <code>s1</code> if not <code>null</code> and not {@link String#isEmpty() empty},
-
 
776
     *         <code>s2</code> otherwise.
-
 
777
     */
-
 
778
    public static String coalesce(String s1, String s2) {
-
 
779
        return isEmpty(s1) ? s2 : s1;
-
 
780
    }
-
 
781
 
-
 
782
    /**
-
 
783
     * Return the first value that is non-empty.
826
     * Return the first value that is non-empty.
784
     * 
827
     * 
785
     * @param values values to test for emptiness.
828
     * @param values values to test for emptiness.
786
     * @return the first value that is neither <code>null</code> nor {@link String#isEmpty() empty}.
829
     * @return the first value that is neither <code>null</code> nor {@link String#isEmpty() empty},
-
 
830
     *         <code>null</code> if none.
787
     */
831
     */
788
    public static String coalesce(String... values) {
832
    public static String coalesce(String... values) {
789
        return coalesce(false, values);
833
        return coalesce(false, values);
790
    }
834
    }
791
 
835
 
792
    public static String coalesce(final boolean trim, String... values) {
836
    public static String coalesce(final boolean trim, String... values) {
793
        for (final String s : values)
837
        for (final String s : values) {
794
            if (!isEmpty(s, trim))
838
            if (!isEmpty(s, trim))
795
                return s;
839
                return s;
-
 
840
        }
796
        return null;
841
        return null;
797
    }
842
    }
798
 
843
 
799
    public static String toAsciiString(String str) {
844
    public static String toAsciiString(String str) {
800
        if (str == null) {
845
        if (str == null) {