Back to home page

OSCL-LXR

 
 

    


0001 /**
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements.  See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership.  The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License.  You may obtain a copy of the License at
0009  *
0010  *     http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing, software
0013  * distributed under the License is distributed on an "AS IS" BASIS,
0014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015  * See the License for the specific language governing permissions and
0016  * limitations under the License.
0017  */
0018 
0019 package org.apache.hive.service.cli;
0020 
0021 import org.apache.log4j.Layout;
0022 import org.apache.log4j.PatternLayout;
0023 
0024 /**
0025  * CLIServiceUtils.
0026  *
0027  */
0028 public class CLIServiceUtils {
0029 
0030 
0031   private static final char SEARCH_STRING_ESCAPE = '\\';
0032   public static final Layout verboseLayout = new PatternLayout(
0033     "%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n");
0034   public static final Layout nonVerboseLayout = new PatternLayout(
0035     "%-5p : %m%n");
0036 
0037   /**
0038    * Convert a SQL search pattern into an equivalent Java Regex.
0039    *
0040    * @param pattern input which may contain '%' or '_' wildcard characters, or
0041    * these characters escaped using {@code getSearchStringEscape()}.
0042    * @return replace %/_ with regex search characters, also handle escaped
0043    * characters.
0044    */
0045   public static String patternToRegex(String pattern) {
0046     if (pattern == null) {
0047       return ".*";
0048     } else {
0049       StringBuilder result = new StringBuilder(pattern.length());
0050 
0051       boolean escaped = false;
0052       for (int i = 0, len = pattern.length(); i < len; i++) {
0053         char c = pattern.charAt(i);
0054         if (escaped) {
0055           if (c != SEARCH_STRING_ESCAPE) {
0056             escaped = false;
0057           }
0058           result.append(c);
0059         } else {
0060           if (c == SEARCH_STRING_ESCAPE) {
0061             escaped = true;
0062             continue;
0063           } else if (c == '%') {
0064             result.append(".*");
0065           } else if (c == '_') {
0066             result.append('.');
0067           } else {
0068             result.append(Character.toLowerCase(c));
0069           }
0070         }
0071       }
0072       return result.toString();
0073     }
0074   }
0075 
0076 }