001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005// http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.commons.util;
014
015import java.util.regex.Pattern;
016
017/**
018 * Some utility methods used in different Tapestry subprojects.
019 */
020public class CommonsUtils
021{
022
023    private static final String SLASH = "/";
024
025    private static final Pattern SLASH_PATTERN = Pattern.compile(SLASH);
026
027    private static final Pattern COMMA_PATTERN = Pattern.compile("\\s*,\\s*");
028    
029    /**
030     * Splits a path at each slash.
031     */
032    public static String[] splitPath(String path)
033    {
034        return SLASH_PATTERN.split(path);
035    }
036
037    /**
038     * Splits a value around commas. Whitespace around the commas is removed, as is leading and trailing whitespace.
039     *
040     * @since 5.1.0.0
041     */
042    public static String[] splitAtCommas(String value)
043    {
044        if (isBlank(value))
045            return EMPTY_STRING_ARRAY;
046
047        return COMMA_PATTERN.split(value.trim());
048    }
049
050    /**
051     * Returns true if the input is null, or is a zero length string (excluding leading/trailing whitespace).
052     */
053    
054    public static boolean isBlank(String input)
055    {
056        return input == null || input.length() == 0 || input.trim().length() == 0;
057    }
058
059    public static final String[] EMPTY_STRING_ARRAY = new String[0];
060
061}