| 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 | * Dependency-check-core is free software: you can redistribute it and/or modify it | |
| 5 | * under the terms of the GNU General Public License as published by the Free | |
| 6 | * Software Foundation, either version 3 of the License, or (at your option) any | |
| 7 | * later version. | |
| 8 | * | |
| 9 | * Dependency-check-core is distributed in the hope that it will be useful, but | |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | |
| 12 | * details. | |
| 13 | * | |
| 14 | * You should have received a copy of the GNU General Public License along with | |
| 15 | * dependency-check-core. If not, see http://www.gnu.org/licenses/. | |
| 16 | * | |
| 17 | * Copyright (c) 2012 Jeremy Long. All Rights Reserved. | |
| 18 | */ | |
| 19 | package org.owasp.dependencycheck.data.lucene; | |
| 20 | ||
| 21 | /** | |
| 22 | * <p>Lucene utils is a set of utilize written to make constructing Lucene | |
| 23 | * queries simpler.</p> | |
| 24 | * | |
| 25 | * @author Jeremy Long (jeremy.long@owasp.org) | |
| 26 | */ | |
| 27 | public final class LuceneUtils { | |
| 28 | ||
| 29 | /** | |
| 30 | * Private constructor as this is a utility class. | |
| 31 | */ | |
| 32 | 0 | private LuceneUtils() { |
| 33 | 0 | } |
| 34 | ||
| 35 | /** | |
| 36 | * Appends the text to the supplied StringBuilder escaping Lucene control | |
| 37 | * characters in the process. | |
| 38 | * | |
| 39 | * @param buf a StringBuilder to append the escaped text to | |
| 40 | * @param text the data to be escaped | |
| 41 | */ | |
| 42 | @SuppressWarnings("fallthrough") | |
| 43 | @edu.umd.cs.findbugs.annotations.SuppressWarnings( | |
| 44 | value = "SF_SWITCH_NO_DEFAULT", | |
| 45 | justification = "The switch below does have a default.") | |
| 46 | public static void appendEscapedLuceneQuery(StringBuilder buf, | |
| 47 | final CharSequence text) { | |
| 48 | ||
| 49 | 2487 | if (text == null || buf == null) { |
| 50 | 3 | return; |
| 51 | } | |
| 52 | ||
| 53 | 25440 | for (int i = 0; i < text.length(); i++) { |
| 54 | 22956 | final char c = text.charAt(i); |
| 55 | 22956 | switch (c) { |
| 56 | case '+': | |
| 57 | case '-': | |
| 58 | case '&': | |
| 59 | case '|': | |
| 60 | case '!': | |
| 61 | case '(': | |
| 62 | case ')': | |
| 63 | case '{': | |
| 64 | case '}': | |
| 65 | case '[': | |
| 66 | case ']': | |
| 67 | case '^': | |
| 68 | case '"': | |
| 69 | case '~': | |
| 70 | case '*': | |
| 71 | case '?': | |
| 72 | case ':': | |
| 73 | case '\\': //it is supposed to fall through here | |
| 74 | 504 | buf.append('\\'); |
| 75 | default: | |
| 76 | 22956 | buf.append(c); |
| 77 | break; | |
| 78 | } | |
| 79 | } | |
| 80 | 2484 | } |
| 81 | ||
| 82 | /** | |
| 83 | * Escapes the text passed in so that it is treated as data instead of | |
| 84 | * control characters. | |
| 85 | * | |
| 86 | * @param text data to be escaped | |
| 87 | * @return the escaped text. | |
| 88 | */ | |
| 89 | public static String escapeLuceneQuery(final CharSequence text) { | |
| 90 | ||
| 91 | 2448 | if (text == null) { |
| 92 | 3 | return null; |
| 93 | } | |
| 94 | ||
| 95 | 2445 | int size = text.length(); |
| 96 | 2445 | size = size >> 1; |
| 97 | 2445 | final StringBuilder buf = new StringBuilder(size); |
| 98 | ||
| 99 | 2445 | appendEscapedLuceneQuery(buf, text); |
| 100 | ||
| 101 | 2445 | return buf.toString(); |
| 102 | } | |
| 103 | } |