xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/search/context/RegexpMatcher.java (revision 0e4c55544f8ea0a68e8bae37b0e502097e008ec1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
22  */
23 package org.opengrok.indexer.search.context;
24 
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import java.util.regex.Pattern;
28 import java.util.regex.PatternSyntaxException;
29 import org.opengrok.indexer.logger.LoggerFactory;
30 
31 /**
32  *
33  * @author dobrou@gmail.com
34  */
35 class RegexpMatcher extends LineMatcher {
36 
37     private static final Logger LOGGER = LoggerFactory.getLogger(RegexpMatcher.class);
38 
39     private final Pattern termRegexp;
40 
RegexpMatcher(String term, boolean caseInsensitive)41     RegexpMatcher(String term, boolean caseInsensitive) {
42         super(caseInsensitive);
43         Pattern regexp;
44         try {
45             regexp = Pattern.compile(term, caseInsensitive ? Pattern.CASE_INSENSITIVE : 0 );
46         } catch ( PatternSyntaxException  e) {
47             regexp = null;
48             LOGGER.log(Level.WARNING, "RegexpMatcher: {0}", e.getMessage() );
49         }
50         this.termRegexp = regexp;
51     }
52 
53     @Override
match(String line)54     public int match(String line) {
55         return (termRegexp != null && termRegexp.matcher(line).matches()) ? MATCHED : NOT_MATCHED;
56     }
57 
58 }
59