| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| LuceneUtils |
|
| 9.0;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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | |
| 21 | import org.apache.lucene.util.Version; | |
| 22 | ||
| 23 | /** | |
| 24 | * <p> | |
| 25 | * Lucene utils is a set of utilize written to make constructing Lucene queries simpler.</p> | |
| 26 | * | |
| 27 | * @author Jeremy Long | |
| 28 | */ | |
| 29 | public final class LuceneUtils { | |
| 30 | ||
| 31 | /** | |
| 32 | * The current version of Lucene being used. Declaring this one place so an upgrade doesn't require hunting through the code | |
| 33 | * base. | |
| 34 | */ | |
| 35 | 1 | public static final Version CURRENT_VERSION = Version.LUCENE_47; |
| 36 | ||
| 37 | /** | |
| 38 | * Private constructor as this is a utility class. | |
| 39 | */ | |
| 40 | 0 | private LuceneUtils() { |
| 41 | 0 | } |
| 42 | ||
| 43 | /** | |
| 44 | * Appends the text to the supplied StringBuilder escaping Lucene control characters in the process. | |
| 45 | * | |
| 46 | * @param buf a StringBuilder to append the escaped text to | |
| 47 | * @param text the data to be escaped | |
| 48 | */ | |
| 49 | @SuppressWarnings("fallthrough") | |
| 50 | @SuppressFBWarnings( | |
| 51 | value = "SF_SWITCH_NO_DEFAULT", | |
| 52 | justification = "The switch below does have a default.") | |
| 53 | public static void appendEscapedLuceneQuery(StringBuilder buf, | |
| 54 | final CharSequence text) { | |
| 55 | ||
| 56 | 150 | if (text == null || buf == null) { |
| 57 | 1 | return; |
| 58 | } | |
| 59 | ||
| 60 | 1291 | for (int i = 0; i < text.length(); i++) { |
| 61 | 1142 | final char c = text.charAt(i); |
| 62 | 1142 | switch (c) { |
| 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 ':': | |
| 80 | case '\\': //it is supposed to fall through here | |
| 81 | 58 | buf.append('\\'); |
| 82 | default: | |
| 83 | 1142 | buf.append(c); |
| 84 | break; | |
| 85 | } | |
| 86 | } | |
| 87 | 149 | } |
| 88 | ||
| 89 | /** | |
| 90 | * Escapes the text passed in so that it is treated as data instead of control characters. | |
| 91 | * | |
| 92 | * @param text data to be escaped | |
| 93 | * @return the escaped text. | |
| 94 | */ | |
| 95 | public static String escapeLuceneQuery(final CharSequence text) { | |
| 96 | ||
| 97 | 149 | if (text == null) { |
| 98 | 1 | return null; |
| 99 | } | |
| 100 | ||
| 101 | 148 | int size = text.length(); |
| 102 | 148 | size = size >> 1; |
| 103 | 148 | final StringBuilder buf = new StringBuilder(size); |
| 104 | ||
| 105 | 148 | appendEscapedLuceneQuery(buf, text); |
| 106 | ||
| 107 | 148 | return buf.toString(); |
| 108 | } | |
| 109 | } |