001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: StringUtils.java 28482 2008-01-04 15:33:39Z sfermigier $
020 */
021
022package org.nuxeo.common.utils;
023
024import java.text.Normalizer;
025import java.util.ArrayList;
026import java.util.List;
027import java.util.Map;
028
029/**
030 * Utils for String manipulations.
031 *
032 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035public final class StringUtils {
036
037    private static final String PLAIN_ASCII =
038    // grave
039    "AaEeIiOoUu"
040    // acute
041            + "AaEeIiOoUuYy"
042            // circumflex
043            + "AaEeIiOoUuYy"
044            // tilde
045            + "AaEeIiOoUuYy"
046            // umlaut
047            + "AaEeIiOoUuYy"
048            // ring
049            + "Aa"
050            // cedilla
051            + "Cc";
052
053    private static final String UNICODE = "\u00C0\u00E0\u00C8\u00E8\u00CC\u00EC\u00D2\u00F2\u00D9\u00F9"
054            + "\u00C1\u00E1\u00C9\u00E9\u00CD\u00ED\u00D3\u00F3\u00DA\u00FA\u00DD\u00FD"
055            + "\u00C2\u00E2\u00CA\u00EA\u00CE\u00EE\u00D4\u00F4\u00DB\u00FB\u0176\u0177"
056            + "\u00C2\u00E2\u00CA\u00EA\u00CE\u00EE\u00D4\u00F4\u00DB\u00FB\u0176\u0177"
057            + "\u00C4\u00E4\u00CB\u00EB\u00CF\u00EF\u00D6\u00F6\u00DC\u00FC\u0178\u00FF" + "\u00C5\u00E5"
058            + "\u00C7\u00E7";
059
060    // This is an utility class.
061    private StringUtils() {
062    }
063
064    /**
065     * Replaces accented characters from a non-null String by their ascii equivalent.
066     *
067     * @param normalize if true, normalize the string using NFC
068     * @since 7.1
069     */
070    public static String toAscii(String s, boolean normalize) {
071        if (normalize) {
072            s = Normalizer.normalize(s, Normalizer.Form.NFC);
073        }
074        StringBuilder sb = new StringBuilder();
075        int n = s.length();
076        for (int i = 0; i < n; i++) {
077            char c = s.charAt(i);
078            int pos = UNICODE.indexOf(c);
079            if (pos > -1) {
080                sb.append(PLAIN_ASCII.charAt(pos));
081            } else {
082                sb.append(c);
083            }
084        }
085        return sb.toString();
086    }
087
088    /**
089     * Replaces accented characters from a non-null String by their ascii equivalent.
090     */
091    public static String toAscii(String s) {
092        return toAscii(s, false);
093    }
094
095    /**
096     * Joins the elements of the provided array into a single String containing the provided list of elements.
097     *
098     * @deprecated since 5.7, use {@link org.apache.commons.lang.StringUtils#join(Object[])} instead
099     */
100    @Deprecated
101    public static String join(Object[] array) {
102        return org.apache.commons.lang.StringUtils.join(array);
103    }
104
105    /**
106     * Joins the elements of the provided array with an optional separator into a single String containing the provided
107     * list of elements.
108     *
109     * @deprecated since 5.7, use {@link org.apache.commons.lang.StringUtils#join(Object[], String)} instead
110     */
111    @Deprecated
112    public static String join(Object[] array, String separator) {
113        return org.apache.commons.lang.StringUtils.join(array, separator);
114    }
115
116    /**
117     * Joins the elements of the provided array with an optional separator into a single String containing the provided
118     * list of elements.
119     *
120     * @deprecated since 5.7, use {@link org.apache.commons.lang.StringUtils#join(Object[], char)} instead
121     */
122    @Deprecated
123    public static String join(Object[] array, char separator) {
124        return org.apache.commons.lang.StringUtils.join(array, separator);
125    }
126
127    /**
128     * Joins the elements of the provided {@link List} with an optional separator into a single String containing the
129     * provided elements.
130     *
131     * @deprecated since 5.7, use {@link org.apache.commons.lang.StringUtils#join(java.util.Collection, String)} instead
132     */
133    @Deprecated
134    public static String join(List<String> list, String separator) {
135        return org.apache.commons.lang.StringUtils.join(list, separator);
136    }
137
138    /**
139     * Joins the elements of the provided {@link List} into a single String containing the provided elements.
140     *
141     * @deprecated since 5.7, use {@link org.apache.commons.lang.StringUtils#join(java.util.Collection, null)} instead
142     */
143    @Deprecated
144    public static String join(List<String> list) {
145        return org.apache.commons.lang.StringUtils.join(list, null);
146    }
147
148    /**
149     * Joins the elements of the provided {@link List} with an optional separator into a single String containing the
150     * provided elements.
151     *
152     * @deprecated since 5.7, use {@link org.apache.commons.lang.StringUtils#join(java.util.Collection, char)} instead
153     */
154    @Deprecated
155    public static String join(List<String> list, char separator) {
156        return org.apache.commons.lang.StringUtils.join(list, separator);
157    }
158
159    public static String[] split(String str, char delimiter, boolean trim) {
160        int s = 0;
161        int e = str.indexOf(delimiter, s);
162        if (e == -1) {
163            if (trim) {
164                str = str.trim();
165            }
166            return new String[] { str };
167        }
168        List<String> ar = new ArrayList<String>();
169        do {
170            String segment = str.substring(s, e);
171            if (trim) {
172                segment = segment.trim();
173            }
174            ar.add(segment);
175            s = e + 1;
176            e = str.indexOf(delimiter, s);
177        } while (e != -1);
178
179        int len = str.length();
180        if (s < len) {
181            String segment = str.substring(s);
182            if (trim) {
183                segment = segment.trim();
184            }
185            ar.add(segment);
186        } else {
187            ar.add("");
188        }
189
190        return ar.toArray(new String[ar.size()]);
191    }
192
193    /**
194     * Converts a string to a sequence of hexadecimal characters, using a non-obvious encoding (unicode code points with
195     * all leading 0 stripped for each character).
196     *
197     * @deprecated since 5.7, use {@link org.apache.commons.codec.binary.Hex#encodeHexString(byte[])} instead
198     */
199    @Deprecated
200    public static String toHex(String string) {
201        char[] chars = string.toCharArray();
202        StringBuilder buf = new StringBuilder();
203        for (char c : chars) {
204            buf.append(Integer.toHexString(c).toUpperCase());
205        }
206        return buf.toString();
207    }
208
209    /**
210     * Expands any variable found in the given expression with the values in the given map.
211     * <p>
212     * The variable format is ${property_key}.
213     *
214     * @param expression the expression to expand
215     * @param properties a map containing variables
216     */
217    public static String expandVars(String expression, Map<?, ?> properties) {
218        return Vars.expand(expression, properties);
219    }
220
221}