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.io.IOException;
    22  import java.net.MalformedURLException;
    23  import java.util.LinkedList;
    24  import java.util.List;
    25  import java.util.logging.Level;
    26  import java.util.logging.Logger;
    27  import org.apache.lucene.analysis.TokenStream;
    28  import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
    29  import org.owasp.dependencycheck.utils.UrlStringUtils;
    30
    31  /**
    32   * <p>Takes a TokenStream and splits or adds tokens to correctly index version
    33   * numbers.</p>
    34   * <p><b>Example:</b> "3.0.0.RELEASE" -> "3 3.0 3.0.0 RELEASE
    35   * 3.0.0.RELEASE".</p>
    36   *
    37   * @author Jeremy Long (jeremy.long@owasp.org)
    38   */
    39  public final class UrlTokenizingFilter extends AbstractTokenizingFilter {
    40
    41      /**
    42       * Constructs a new VersionTokenizingFilter.
    43       *
    44       * @param stream the TokenStream that this filter will process
    45       */
    46      public UrlTokenizingFilter(TokenStream stream) {
    47          super(stream);
    48      }
    49
    50      /**
    51       * Increments the underlying TokenStream and sets CharTermAttributes to
    52       * construct an expanded set of tokens by concatenating tokens with the
    53       * previous token.
    54       *
    55       * @return whether or not we have hit the end of the TokenStream
    56       * @throws IOException is thrown when an IOException occurs
    57       */
    58      @Override
    59      public boolean incrementToken() throws IOException {
    60          final LinkedList<String> tokens = getTokens();
    61          final CharTermAttribute termAtt = getTermAtt();
    62          if (tokens.size() == 0 && input.incrementToken()) {
    63              final String text = new String(termAtt.buffer(), 0, termAtt.length());
    64              if (UrlStringUtils.containsUrl(text)) {
    65                  final String[] parts = text.split("\\s");
    66                  for (String part : parts) {
    67                      if (UrlStringUtils.isUrl(part)) {
    68                          try {
    69                              final List<String> data = UrlStringUtils.extractImportantUrlData(part);
    70                              tokens.addAll(data);
    71                          } catch (MalformedURLException ex) {
    72                              Logger.getLogger(UrlTokenizingFilter.class.getName()).log(Level.INFO, "error parsing " + part, ex);
    73                              tokens.add(part);
    74                          }
    75                      } else {
    76                          tokens.add(part);
    77                      }
    78                  }
    79              } else {
    80                  tokens.add(text);
    81              }
    82          }
    83          return addTerm();
    84      }
    85  }