##### Coverage Report - org.owasp.dependencycheck.data.lucene.LuceneUtils

Classes in this File | Line Coverage | Branch Coverage | Complexity
LuceneUtils |
88% | 15/17
33% | 9/27
| 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 | 1158 |
             if (text == null || buf == null) {
50 | 1 |
                 return;
51 |  |
             }
52 |  |
53 | 11488 |
             for (int i = 0; i < text.length(); i++) {
54 | 10331 |
                 final char c = text.charAt(i);
55 | 10331 |
                 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 | 226 |
                         buf.append('\\');
75 |  |
                     default:
76 | 10331 |
                         buf.append(c);
77 |  |
                         break;
78 |  |
                 }
79 |  |
             }
80 | 1157 |
         }
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 | 1117 |
             if (text == null) {
92 | 1 |
                 return null;
93 |  |
             }
94 |  |
95 | 1116 |
             int size = text.length();
96 | 1116 |
             size = size >> 1;
97 | 1116 |
             final StringBuilder buf = new StringBuilder(size);
98 |  |
99 | 1116 |
             appendEscapedLuceneQuery(buf, text);
100 |  |
101 | 1116 |
             return buf.toString();
102 |  |
         }
103 |  |
     }