1 17 package org.alfresco.util; 18 19 import org.alfresco.repo.search.impl.lucene.QueryParser; 20 21 22 26 public class SearchLanguageConversion 27 { 28 37 public static LanguageDefinition DEF_XPATH_LIKE = new SimpleLanguageDef('\\', "%", "_", "\\%_"); 38 47 public static LanguageDefinition DEF_REGEX = new SimpleLanguageDef('\\', ".*", ".", "\\*.+?^$(){}|"); 48 51 public static LanguageDefinition DEF_LUCENE = new LuceneLanguageDef(); 52 61 public static LanguageDefinition DEF_CIFS = new SimpleLanguageDef('\\', "*", "?", "\"*\\<>?/:|£%&+;"); 62 63 69 public static String escapeForXPathLike(String str) 70 { 71 return escape(DEF_XPATH_LIKE, str); 72 } 73 74 80 public static String escapeForRegex(String str) 81 { 82 return escape(DEF_REGEX, str); 83 } 84 85 91 public static String escapeForLucene(String str) 92 { 93 return escape(DEF_LUCENE, str); 94 } 95 96 99 private static String escape(LanguageDefinition def, String str) 100 { 101 StringBuilder sb = new StringBuilder (str.length() * 2); 102 103 char[] chars = str.toCharArray(); 104 for (int i = 0; i < chars.length; i++) 105 { 106 if (def.isReserved(chars[i])) 108 { 109 sb.append(def.escapeChar); 111 } 112 sb.append(chars[i]); 113 } 114 return sb.toString(); 115 } 116 117 124 public static String convertXPathLikeToRegex(String xpathLikeClause) 125 { 126 return "(?s)" + convert(DEF_XPATH_LIKE, DEF_REGEX, xpathLikeClause); 127 } 128 129 136 public static String convertXPathLikeToLucene(String xpathLikeClause) 137 { 138 return convert(DEF_XPATH_LIKE, DEF_LUCENE, xpathLikeClause); 139 } 140 141 148 public static String convertCifsToLucene(String cifsNamePath) 149 { 150 return convert(DEF_CIFS, DEF_LUCENE, cifsNamePath); 151 } 152 153 public static String convert(LanguageDefinition from, LanguageDefinition to, String query) 154 { 155 char[] chars = query.toCharArray(); 156 157 StringBuilder sb = new StringBuilder (chars.length * 2); 158 159 boolean escaping = false; 160 161 for (int i = 0; i < chars.length; i++) 162 { 163 if (escaping) { 165 sb.append(to.escapeChar); sb.append(chars[i]); escaping = false; 168 } 169 else if (chars[i] == from.escapeChar) { 171 escaping = true; 172 } 173 else if (query.startsWith(from.multiCharWildcard, i)) { 175 sb.append(to.multiCharWildcard); 177 } 178 else if (query.startsWith(from.singleCharWildcard, i)) { 180 sb.append(to.singleCharWildcard); 182 } 183 else if (to.isReserved(chars[i])) { 185 sb.append(to.escapeChar).append(chars[i]); 186 } 187 else { 189 sb.append(chars[i]); 190 } 191 } 192 return sb.toString(); 193 } 194 195 198 public static abstract class LanguageDefinition 199 { 200 public final char escapeChar; 201 public final String multiCharWildcard; 202 public final String singleCharWildcard; 203 204 public LanguageDefinition(char escapeChar, String multiCharWildcard, String singleCharWildcard) 205 { 206 this.escapeChar = escapeChar; 207 this.multiCharWildcard = multiCharWildcard; 208 this.singleCharWildcard = singleCharWildcard; 209 } 210 public abstract boolean isReserved(char ch); 211 } 212 private static class SimpleLanguageDef extends LanguageDefinition 213 { 214 private String reserved; 215 public SimpleLanguageDef(char escapeChar, String multiCharWildcard, String singleCharWildcard, String reserved) 216 { 217 super(escapeChar, multiCharWildcard, singleCharWildcard); 218 this.reserved = reserved; 219 } 220 @Override 221 public boolean isReserved(char ch) 222 { 223 return (reserved.indexOf(ch) > -1); 224 } 225 } 226 private static class LuceneLanguageDef extends LanguageDefinition 227 { 228 private String reserved; 229 public LuceneLanguageDef() 230 { 231 super('\\', "*", "?"); 232 init(); 233 } 234 237 private void init() 238 { 239 StringBuilder sb = new StringBuilder (20); 240 for (char ch = 0; ch < 256; ch++) 241 { 242 char[] chars = new char[] {ch}; 243 String unescaped = new String (chars); 244 String escaped = QueryParser.escape(unescaped); 246 if (!escaped.equals(unescaped)) 247 { 248 sb.append(ch); 250 } 251 } 252 reserved = sb.toString(); 253 } 254 @Override 255 public boolean isReserved(char ch) 256 { 257 return (reserved.indexOf(ch) > -1); 258 } 259 } 260 } 261 | Popular Tags |