001 /* 002 * Copyright (C) 2012 eXo Platform SAS. 003 * 004 * This is free software; you can redistribute it and/or modify it 005 * under the terms of the GNU Lesser General Public License as 006 * published by the Free Software Foundation; either version 2.1 of 007 * the License, or (at your option) any later version. 008 * 009 * This software is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * You should have received a copy of the GNU Lesser General Public 015 * License along with this software; if not, write to the Free 016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 018 */ 019 020 package org.crsh.util; 021 022 import java.util.ArrayList; 023 import java.util.Arrays; 024 import java.util.HashMap; 025 import java.util.HashSet; 026 import java.util.Iterator; 027 import java.util.LinkedList; 028 import java.util.List; 029 import java.util.Map; 030 import java.util.regex.Matcher; 031 import java.util.regex.Pattern; 032 033 public class Utils { 034 035 public static <E> ArrayList<E> newArrayList() { 036 return new ArrayList<E>(); 037 } 038 039 public static <E> LinkedList<E> newLinkedList() { 040 return new LinkedList<E>(); 041 } 042 043 public static <E> HashSet<E> newHashSet() { 044 return new HashSet<E>(); 045 } 046 047 public static <K, V> HashMap<K, V> newHashMap() { 048 return new HashMap<K, V>(); 049 } 050 051 public static <K, V, M extends Map<K, V>> M map(M map, K key, V value) { 052 map.put(key, value); 053 return map; 054 } 055 056 public static <K, V> HashMap<K, V> map(K key, V value) { 057 HashMap<K, V> map = new HashMap<K, V>(); 058 map.put(key, value); 059 return map; 060 } 061 062 public static <E> List<E> list(E... elements) { 063 return Arrays.asList(elements); 064 } 065 066 public static <E> List<E> list(Iterable<E> iterable) { 067 return list(iterable.iterator()); 068 } 069 070 public static <E> List<E> list(Iterator<E> iterator) { 071 ArrayList<E> list = new ArrayList<E>(); 072 while (iterator.hasNext()) { 073 list.add(iterator.next()); 074 } 075 return list; 076 } 077 078 public static int indexOf(CharSequence s, int off, char c) { 079 for (int len = s.length();off < len;off++) { 080 if (s.charAt(off) == c) { 081 return off; 082 } 083 } 084 return -1; 085 } 086 087 public static String trimLeft(String s) { 088 if (s == null) { 089 throw new NullPointerException("No null string accepted"); 090 } 091 int index = 0; 092 int len = s.length(); 093 while (index < len) { 094 if (s.charAt(index) == ' ') { 095 index++; 096 } else { 097 break; 098 } 099 } 100 if (index > 0) { 101 return s.substring(index); 102 } else { 103 return s; 104 } 105 } 106 107 public static <E> E notNull(E e1, E e2) { 108 if (e1 != null) { 109 return e1; 110 } else { 111 return e2; 112 } 113 } 114 115 private static final Pattern FOO = Pattern.compile("" + 116 "(\\*)" + // Wildcard * 117 "|" + 118 "(\\?)" + // Wildcard ? 119 "|" + 120 "(?:\\[([^)]+)\\])" + // Range 121 "|" + 122 "(\\\\.)" // Escape 123 ); 124 125 /** 126 * Create a pattern that transforms a glob expression into a regular expression, the following task are supported 127 * <ul> 128 * <li>* : Match any number of unknown characters</li> 129 * <li>? : Match one unknown character</li> 130 * <li>[characters] : Match a character as part of a group of characters</li> 131 * <li>\ : Escape character</li> 132 * </ul> 133 * 134 * @param globex the glob expression 135 * @return the regular expression 136 * @throws NullPointerException when the globex argument is null 137 */ 138 public static String globexToRegex(String globex) throws NullPointerException { 139 if (globex == null) { 140 throw new NullPointerException("No null globex accepted"); 141 } 142 StringBuilder regex = new StringBuilder(); 143 int prev = 0; 144 Matcher matcher = FOO.matcher(globex); 145 while (matcher.find()) { 146 int next = matcher.start(); 147 if (next > prev) { 148 regex.append(Pattern.quote(globex.substring(prev, next))); 149 } 150 if (matcher.group(1) != null) { 151 regex.append(".*"); 152 } else if (matcher.group(2) != null) { 153 regex.append("."); 154 } else if (matcher.group(3) != null) { 155 regex.append("["); 156 regex.append(Pattern.quote(matcher.group(3))); 157 regex.append("]"); 158 } else if (matcher.group(4) != null) { 159 regex.append(Pattern.quote(Character.toString(matcher.group(4).charAt(1)))); 160 } else { 161 throw new UnsupportedOperationException("Not handled yet"); 162 } 163 prev = matcher.end(); 164 } 165 if (prev < globex.length()) { 166 regex.append(Pattern.quote(globex.substring(prev))); 167 } 168 return regex.toString(); 169 } 170 171 }