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) 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.TokenStream;
    24  import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
    25
    26  /**
    27   * <p>Takes a TokenStream and splits or adds tokens to correctly index version
    28   * numbers.</p>
    29   * <p><b>Example:</b> "3.0.0.RELEASE" -> "3 3.0 3.0.0 RELEASE
    30   * 3.0.0.RELEASE".</p>
    31   *
    32   * @author Jeremy Long (jeremy.long@owasp.org)
    33   * @deprecated version information is no longer stored in lucene
    34   */
    35  @Deprecated
    36  public final class VersionTokenizingFilter extends AbstractTokenizingFilter {
    37
    38      /**
    39       * Constructs a new VersionTokenizingFilter.
    40       *
    41       * @param stream the TokenStream that this filter will process
    42       */
    43      public VersionTokenizingFilter(TokenStream stream) {
    44          super(stream);
    45      }
    46
    47      /**
    48       * Increments the underlying TokenStream and sets CharTermAttributes to
    49       * construct an expanded set of tokens by concatenating tokens with the
    50       * previous token.
    51       *
    52       * @return whether or not we have hit the end of the TokenStream
    53       * @throws IOException is thrown when an IOException occurs
    54       */
    55      @Override
    56      public boolean incrementToken() throws IOException {
    57          final LinkedList<String> tokens = getTokens();
    58          final CharTermAttribute termAtt = getTermAtt();
    59          if (tokens.size() == 0 && input.incrementToken()) {
    60              final String version = new String(termAtt.buffer(), 0, termAtt.length());
    61              final String[] toAnalyze = version.split("[_-]");
    62              //ensure we analyze the whole string as one too
    63              analyzeVersion(version);
    64              for (String str : toAnalyze) {
    65                  analyzeVersion(str);
    66              }
    67          }
    68          return addTerm();
    69      }
    70
    71      /**
    72       * <p>Analyzes the version and adds several copies of the version as
    73       * different tokens. For example, the version 1.2.7 would create the tokens
    74       * 1 1.2 1.2.7. This is useful in discovering the correct version -
    75       * sometimes a maintenance or build number will throw off the version
    76       * identification.</p>
    77       *
    78       * <p>expected&nbsp;format:&nbps;major.minor[.maintenance[.build]]</p>
    79       *
    80       * @param version the version to analyze
    81       */
    82      private void analyzeVersion(String version) {
    83          //todo should we also be splitting on dash or underscore? we would need
    84          //  to incorporate the dash or underscore back in...
    85          final LinkedList<String> tokens = getTokens();
    86          final String[] versionParts = version.split("\\.");
    87          String dottedVersion = null;
    88          for (String current : versionParts) {
    89              if (!current.matches("^/d+$")) {
    90                  tokens.add(current);
    91              }
    92              if (dottedVersion == null) {
    93                  dottedVersion = current;
    94              } else {
    95                  dottedVersion = dottedVersion + "." + current;
    96              }
    97              tokens.add(dottedVersion);
    98          }
    99      }
    100 }