View Javadoc

    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) 2013 Jeremy Long. All Rights Reserved.
    18   */
    19  package org.owasp.dependencycheck.data.lucene;
    20
    21  import java.util.LinkedList;
    22  import org.apache.lucene.analysis.TokenFilter;
    23  import org.apache.lucene.analysis.TokenStream;
    24  import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
    25
    26  /**
    27   * An abstract tokenizing filter that can be used as the base for a tokenizing
    28   * filter.
    29   *
    30   * @author Jeremy Long (jeremy.long@owasp.org)
    31   */
    32  public abstract class AbstractTokenizingFilter extends TokenFilter {
    33
    34      /**
    35       * The char term attribute.
    36       */
    37      private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
    38
    39      /**
    40       * Gets the CharTermAttribute.
    41       *
    42       * @return the CharTermAttribute
    43       */
    44      protected CharTermAttribute getTermAtt() {
    45          return termAtt;
    46      }
    47      /**
    48       * A collection of tokens to add to the stream.
    49       */
    50      private final LinkedList<String> tokens;
    51
    52      /**
    53       * Gets the list of tokens.
    54       *
    55       * @return the list of tokens
    56       */
    57      protected LinkedList<String> getTokens() {
    58          return tokens;
    59      }
    60
    61      /**
    62       * Constructs a new AbstractTokenizingFilter.
    63       *
    64       * @param stream the TokenStream that this filter will process
    65       */
    66      public AbstractTokenizingFilter(TokenStream stream) {
    67          super(stream);
    68          tokens = new LinkedList<String>();
    69      }
    70
    71      /**
    72       * Adds a term, if one exists, from the tokens collection.
    73       *
    74       * @return whether or not a new term was added
    75       */
    76      protected boolean addTerm() {
    77          final boolean termAdded = tokens.size() > 0;
    78          if (termAdded) {
    79              final String term = tokens.pop();
    80              clearAttributes();
    81              termAtt.append(term);
    82          }
    83          return termAdded;
    84      }
    85  }