Coverage Report - org.owasp.dependencycheck.data.lucene.LuceneUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
LuceneUtils
100%
16/16
90%
9/10
9
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.data.lucene;
 19  
 
 20  
 import org.apache.lucene.util.Version;
 21  
 
 22  
 /**
 23  
  * <p>
 24  
  * Lucene utils is a set of utilize written to make constructing Lucene queries simpler.</p>
 25  
  *
 26  
  * @author Jeremy Long <jeremy.long@owasp.org>
 27  
  */
 28  
 public final class LuceneUtils {
 29  
 
 30  
     /**
 31  
      * The current version of Lucene being used. Declaring this one place so an upgrade doesn't require hunting through
 32  
      * the code base.
 33  
      */
 34  1
     public static final Version CURRENT_VERSION = Version.LUCENE_45;
 35  
 
 36  
     /**
 37  
      * Private constructor as this is a utility class.
 38  
      */
 39  
     private LuceneUtils() {
 40  
     }
 41  
 
 42  
     /**
 43  
      * Appends the text to the supplied StringBuilder escaping Lucene control characters in the process.
 44  
      *
 45  
      * @param buf a StringBuilder to append the escaped text to
 46  
      * @param text the data to be escaped
 47  
      */
 48  
     @SuppressWarnings("fallthrough")
 49  
     @edu.umd.cs.findbugs.annotations.SuppressWarnings(
 50  
             value = "SF_SWITCH_NO_DEFAULT",
 51  
             justification = "The switch below does have a default.")
 52  
     public static void appendEscapedLuceneQuery(StringBuilder buf,
 53  
             final CharSequence text) {
 54  
 
 55  105
         if (text == null || buf == null) {
 56  1
             return;
 57  
         }
 58  
 
 59  1061
         for (int i = 0; i < text.length(); i++) {
 60  957
             final char c = text.charAt(i);
 61  957
             switch (c) {
 62  
                 case '+':
 63  
                 case '-':
 64  
                 case '&':
 65  
                 case '|':
 66  
                 case '!':
 67  
                 case '(':
 68  
                 case ')':
 69  
                 case '{':
 70  
                 case '}':
 71  
                 case '[':
 72  
                 case ']':
 73  
                 case '^':
 74  
                 case '"':
 75  
                 case '~':
 76  
                 case '*':
 77  
                 case '?':
 78  
                 case ':':
 79  
                 case '\\': //it is supposed to fall through here
 80  60
                     buf.append('\\');
 81  
                 default:
 82  957
                     buf.append(c);
 83  
                     break;
 84  
             }
 85  
         }
 86  104
     }
 87  
 
 88  
     /**
 89  
      * Escapes the text passed in so that it is treated as data instead of control characters.
 90  
      *
 91  
      * @param text data to be escaped
 92  
      * @return the escaped text.
 93  
      */
 94  
     public static String escapeLuceneQuery(final CharSequence text) {
 95  
 
 96  104
         if (text == null) {
 97  1
             return null;
 98  
         }
 99  
 
 100  103
         int size = text.length();
 101  103
         size = size >> 1;
 102  103
         final StringBuilder buf = new StringBuilder(size);
 103  
 
 104  103
         appendEscapedLuceneQuery(buf, text);
 105  
 
 106  103
         return buf.toString();
 107  
     }
 108  
 }