47dacd4c7c9423be2044d4307b4fca979d353816dobrou/*
47dacd4c7c9423be2044d4307b4fca979d353816dobrou * CDDL HEADER START
47dacd4c7c9423be2044d4307b4fca979d353816dobrou *
47dacd4c7c9423be2044d4307b4fca979d353816dobrou * The contents of this file are subject to the terms of the
47dacd4c7c9423be2044d4307b4fca979d353816dobrou * Common Development and Distribution License (the "License").
47dacd4c7c9423be2044d4307b4fca979d353816dobrou * You may not use this file except in compliance with the License.
47dacd4c7c9423be2044d4307b4fca979d353816dobrou *
47dacd4c7c9423be2044d4307b4fca979d353816dobrou * See LICENSE.txt included in this distribution for the specific
47dacd4c7c9423be2044d4307b4fca979d353816dobrou * language governing permissions and limitations under the License.
47dacd4c7c9423be2044d4307b4fca979d353816dobrou *
47dacd4c7c9423be2044d4307b4fca979d353816dobrou * When distributing Covered Code, include this CDDL HEADER in each
47dacd4c7c9423be2044d4307b4fca979d353816dobrou * file and include the License file at LICENSE.txt.
47dacd4c7c9423be2044d4307b4fca979d353816dobrou * If applicable, add the following below this CDDL HEADER, with the
47dacd4c7c9423be2044d4307b4fca979d353816dobrou * fields enclosed by brackets "[]" replaced with your own identifying
47dacd4c7c9423be2044d4307b4fca979d353816dobrou * information: Portions Copyright [yyyy] [name of copyright owner]
47dacd4c7c9423be2044d4307b4fca979d353816dobrou *
47dacd4c7c9423be2044d4307b4fca979d353816dobrou * CDDL HEADER END
47dacd4c7c9423be2044d4307b4fca979d353816dobrou */
47dacd4c7c9423be2044d4307b4fca979d353816dobrou
47dacd4c7c9423be2044d4307b4fca979d353816dobrou/*
5d4e5c041325456d2db42e7a1a5a6cd64cc2c455dobrou * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
47dacd4c7c9423be2044d4307b4fca979d353816dobrou */
47dacd4c7c9423be2044d4307b4fca979d353816dobrou
697eb84b62379360d04234d89b6344648af012a5dobroupackage org.opensolaris.opengrok.search.context;
697eb84b62379360d04234d89b6344648af012a5dobrou
697eb84b62379360d04234d89b6344648af012a5dobrouimport java.util.logging.Level;
6336b638e9afd018de5f6c516eac4775d140fdaeJHKSTimport java.util.logging.Logger;
697eb84b62379360d04234d89b6344648af012a5dobrouimport java.util.regex.Pattern;
697eb84b62379360d04234d89b6344648af012a5dobrouimport java.util.regex.PatternSyntaxException;
6336b638e9afd018de5f6c516eac4775d140fdaeJHKSTimport org.opensolaris.opengrok.logger.LoggerFactory;
697eb84b62379360d04234d89b6344648af012a5dobrou
697eb84b62379360d04234d89b6344648af012a5dobrou/**
697eb84b62379360d04234d89b6344648af012a5dobrou *
697eb84b62379360d04234d89b6344648af012a5dobrou * @author dobrou@gmail.com
697eb84b62379360d04234d89b6344648af012a5dobrou */
697eb84b62379360d04234d89b6344648af012a5dobrouclass RegexpMatcher extends LineMatcher {
6336b638e9afd018de5f6c516eac4775d140fdaeJHKST
6336b638e9afd018de5f6c516eac4775d140fdaeJHKST private static final Logger LOGGER = LoggerFactory.getLogger(RegexpMatcher.class);
6336b638e9afd018de5f6c516eac4775d140fdaeJHKST
697eb84b62379360d04234d89b6344648af012a5dobrou private final Pattern termRegexp;
697eb84b62379360d04234d89b6344648af012a5dobrou
697eb84b62379360d04234d89b6344648af012a5dobrou public RegexpMatcher(String term, boolean caseInsensitive) {
697eb84b62379360d04234d89b6344648af012a5dobrou super(caseInsensitive);
697eb84b62379360d04234d89b6344648af012a5dobrou Pattern regexp;
697eb84b62379360d04234d89b6344648af012a5dobrou try {
697eb84b62379360d04234d89b6344648af012a5dobrou regexp = Pattern.compile(term, caseInsensitive ? Pattern.CASE_INSENSITIVE : 0 );
697eb84b62379360d04234d89b6344648af012a5dobrou } catch ( PatternSyntaxException e) {
697eb84b62379360d04234d89b6344648af012a5dobrou regexp = null;
6336b638e9afd018de5f6c516eac4775d140fdaeJHKST LOGGER.log(Level.WARNING, "RegexpMatcher: {0}", e.getMessage() );
697eb84b62379360d04234d89b6344648af012a5dobrou }
697eb84b62379360d04234d89b6344648af012a5dobrou this.termRegexp = regexp;
697eb84b62379360d04234d89b6344648af012a5dobrou }
697eb84b62379360d04234d89b6344648af012a5dobrou
697eb84b62379360d04234d89b6344648af012a5dobrou @Override
697eb84b62379360d04234d89b6344648af012a5dobrou public int match(String line) {
697eb84b62379360d04234d89b6344648af012a5dobrou return (termRegexp != null && termRegexp.matcher(line).matches() )
697eb84b62379360d04234d89b6344648af012a5dobrou ? MATCHED
697eb84b62379360d04234d89b6344648af012a5dobrou : NOT_MATCHED
697eb84b62379360d04234d89b6344648af012a5dobrou ;
697eb84b62379360d04234d89b6344648af012a5dobrou }
697eb84b62379360d04234d89b6344648af012a5dobrou
697eb84b62379360d04234d89b6344648af012a5dobrou}