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