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

Classes in this File | Line Coverage | Branch Coverage | Complexity
TokenPairConcatenatingFilter |
100% | 26/26
100% | 8/8
| 2.2;2.2

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 java.io.IOException;
22 |  |
     import java.util.LinkedList;
23 |  |
     import org.apache.lucene.analysis.TokenFilter;
24 |  |
     import org.apache.lucene.analysis.TokenStream;
25 |  |
     import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
26 |  |
27 |  |
     /**
28 |  |
      * <p>Takes a TokenStream and adds additional tokens by concatenating pairs of
29 |  |
      * words.</p>
30 |  |
      * <p><b>Example:</b> "Spring Framework Core" -> "Spring SpringFramework
31 |  |
      * Framework FrameworkCore Core".</p>
32 |  |
      *
33 |  |
      * @author Jeremy Long (jeremy.long@owasp.org)
34 |  |
      */
35 |  |
     public final class TokenPairConcatenatingFilter extends TokenFilter {
36 |  |
37 |  |
         /**
38 |  |
          * The char term attribute.
39 |  |
          */
40 | 26 |
         private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
41 |  |
         /**
42 |  |
          * The previous word parsed.
43 |  |
          */
44 |  |
         private String previousWord;
45 |  |
         /**
46 |  |
          * A list of words parsed.
47 |  |
          */
48 |  |
         private final LinkedList<String> words;
49 |  |
50 |  |
         /**
51 |  |
          * Returns the previous word. This is needed in the test cases.
52 |  |
          *
53 |  |
          * @return te previous word
54 |  |
          */
55 |  |
         protected String getPreviousWord() {
56 | 2 |
             return previousWord;
57 |  |
         }
58 |  |
59 |  |
         /**
60 |  |
          * Returns the words list. This is needed in the test cases.
61 |  |
          *
62 |  |
          * @return the words list
63 |  |
          */
64 |  |
         protected LinkedList<String> getWords() {
65 | 1 |
             return words;
66 |  |
         }
67 |  |
68 |  |
         /**
69 |  |
          * Constructs a new TokenPairConcatenatingFilter.
70 |  |
          *
71 |  |
          * @param stream the TokenStream that this filter will process
72 |  |
          */
73 |  |
         public TokenPairConcatenatingFilter(TokenStream stream) {
74 | 26 |
             super(stream);
75 | 26 |
             words = new LinkedList<String>();
76 | 26 |
         }
77 |  |
78 |  |
         /**
79 |  |
          * Increments the underlying TokenStream and sets CharTermAttributes to
80 |  |
          * construct an expanded set of tokens by concatenating tokens with the
81 |  |
          * previous token.
82 |  |
          *
83 |  |
          * @return whether or not we have hit the end of the TokenStream
84 |  |
          * @throws IOException is thrown when an IOException occurs
85 |  |
          */
86 |  |
         @Override
87 |  |
         public boolean incrementToken() throws IOException {
88 |  |
89 |  |
             //collect all the terms into the words collection
90 | 7578 |
             while (input.incrementToken()) {
91 | 2130 |
                 final String word = new String(termAtt.buffer(), 0, termAtt.length());
92 | 2130 |
                 words.add(word);
93 | 2130 |
             }
94 |  |
95 |  |
             //if we have a previousTerm - write it out as its own token concatenated
96 |  |
             // with the current word (if one is available).
97 | 5448 |
             if (previousWord != null && words.size() > 0) {
98 | 2102 |
                 final String word = words.getFirst();
99 | 2102 |
                 clearAttributes();
100 | 2102 |
                 termAtt.append(previousWord).append(word);
101 | 2102 |
                 previousWord = null;
102 | 2102 |
                 return true;
103 |  |
             }
104 |  |
             //if we have words, write it out as a single token
105 | 3346 |
             if (words.size() > 0) {
106 | 2130 |
                 final String word = words.removeFirst();
107 | 2130 |
                 clearAttributes();
108 | 2130 |
                 termAtt.append(word);
109 | 2130 |
                 previousWord = word;
110 | 2130 |
                 return true;
111 |  |
             }
112 | 1216 |
             return false;
113 |  |
         }
114 |  |
115 |  |
         /**
116 |  |
          * <p>Resets the Filter and clears any internal state data that may have
117 |  |
          * been left-over from previous uses of the Filter.</p>
118 |  |
          * <p><b>If this Filter is re-used this method must be called between
119 |  |
          * uses.</b></p>
120 |  |
          */
121 |  |
         public void clear() {
122 | 3 |
             previousWord = null;
123 | 3 |
             words.clear();
124 | 3 |
         }
125 |  |
     }