001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.builder.util;
022    
023    import java.util.regex.Pattern;
024    
025    /**
026     * @author Franck WOLFF
027     */
028    public class StringUtil {
029            
030            public static String unNull(String s) {
031                    return s == null ? "" : s;
032            }
033        
034        public static String defaultText(String s, String def) {
035            return (s == null || s.length() == 0 ? def : s);
036        }
037    
038            public static String tokenize(String s, int max, String delimiter) {
039                    
040                    if (max <= 0 || s == null || s.length() < max)
041                            return s;
042    
043                    StringBuilder sb = new StringBuilder();
044                    
045                    int length = 0;
046                    for (String token : s.split("\\s", -1)) {
047                            if (length > 0) {
048                                    if (length + token.length() >= max) {
049                                            sb.append(delimiter);
050                                            length = 0;
051                                    }
052                                    else {
053                                            sb.append(' ');
054                                            length++;
055                                    }
056                            }
057                            sb.append(token);
058                            length += token.length();
059                    }
060                    
061                    return sb.toString();
062            }
063            
064            public static String join(String[] items, char separator) {
065                    return join(items, String.valueOf(separator));
066            }
067            
068            public static String join(String[] items, String separator) {
069                    StringBuilder sb = new StringBuilder();
070                    for (String item : items) {
071                            if (sb.length() > 0)
072                                    sb.append(separator);
073                            sb.append(item);
074                    }
075                    return sb.toString();
076            }
077            
078            public static String[] split(String items, char separator) {
079                    return split(items, String.valueOf(separator));
080            }
081            
082            public static String[] split(String items, String separator) {
083                    return items.split(Pattern.quote(separator), -1);
084            }
085            
086            public static String regexifyPathPattern(String pattern) {
087                    StringBuilder sb = new StringBuilder();
088                    
089                    boolean quote = false;
090                    sb.append('^');
091                    for (int i = 0; i < pattern.length(); i++) {
092                            char c = pattern.charAt(i);
093                            switch (c) {
094                            case '*':
095                                    if (quote) {
096                                            sb.append("\\E");
097                                            quote = false;
098                                    }
099                                    // Double star (any character even the path separator '/').
100                                    if (i + 1 < pattern.length() && pattern.charAt(i + 1) == '*') {
101                                            sb.append(".*");
102                                            i++;
103                                    }
104                                    // Single star (any character except '/').
105                                    else
106                                            sb.append("[^/]*");
107                                    break;
108                            case '?':
109                                    if (quote) {
110                                            sb.append("\\E");
111                                            quote = false;
112                                    }
113                                    sb.append("[^/]");
114                                    break;
115                            default:
116                                    if (!quote) {
117                                            sb.append("\\Q");
118                                            quote = true;
119                                    }
120                                    sb.append(c);
121                                    break;
122                            }
123                    }
124                    if (quote)
125                            sb.append("\\E");
126                    sb.append('$');
127                    
128                    return sb.toString();
129            }
130    }