View Javadoc
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      public static final Version CURRENT_VERSION = Version.LUCENE_47;
36  
37      /**
38       * Private constructor as this is a utility class.
39       */
40      private LuceneUtils() {
41      }
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          if (text == null || buf == null) {
57              return;
58          }
59  
60          for (int i = 0; i < text.length(); i++) {
61              final char c = text.charAt(i);
62              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 '/':
81                  case '\\': //it is supposed to fall through here
82                      buf.append('\\');
83                  default:
84                      buf.append(c);
85                      break;
86              }
87          }
88      }
89  
90      /**
91       * Escapes the text passed in so that it is treated as data instead of control characters.
92       *
93       * @param text data to be escaped
94       * @return the escaped text.
95       */
96      public static String escapeLuceneQuery(final CharSequence text) {
97          if (text == null) {
98              return null;
99          }
100         final int size = text.length() << 1;
101         final StringBuilder buf = new StringBuilder(size);
102         appendEscapedLuceneQuery(buf, text);
103         return buf.toString();
104     }
105 }