164819763SVladimir Kotal /* 264819763SVladimir Kotal * CDDL HEADER START 364819763SVladimir Kotal * 464819763SVladimir Kotal * The contents of this file are subject to the terms of the 564819763SVladimir Kotal * Common Development and Distribution License (the "License"). 664819763SVladimir Kotal * You may not use this file except in compliance with the License. 764819763SVladimir Kotal * 864819763SVladimir Kotal * See LICENSE.txt included in this distribution for the specific 964819763SVladimir Kotal * language governing permissions and limitations under the License. 1064819763SVladimir Kotal * 1164819763SVladimir Kotal * When distributing Covered Code, include this CDDL HEADER in each 1264819763SVladimir Kotal * file and include the License file at LICENSE.txt. 1364819763SVladimir Kotal * If applicable, add the following below this CDDL HEADER, with the 1464819763SVladimir Kotal * fields enclosed by brackets "[]" replaced with your own identifying 1564819763SVladimir Kotal * information: Portions Copyright [yyyy] [name of copyright owner] 1664819763SVladimir Kotal * 1764819763SVladimir Kotal * CDDL HEADER END 1864819763SVladimir Kotal */ 1964819763SVladimir Kotal 2064819763SVladimir Kotal /* 2164819763SVladimir Kotal * Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved. 2264819763SVladimir Kotal */ 2364819763SVladimir Kotal package org.opengrok.indexer.configuration; 2464819763SVladimir Kotal 2564819763SVladimir Kotal import java.util.regex.Pattern; 2664819763SVladimir Kotal import java.util.regex.PatternSyntaxException; 2764819763SVladimir Kotal 2864819763SVladimir Kotal public class PatternUtil { PatternUtil()2964819763SVladimir Kotal private PatternUtil() { 3064819763SVladimir Kotal // private to enforce static 3164819763SVladimir Kotal } 3264819763SVladimir Kotal 3364819763SVladimir Kotal /** 3464819763SVladimir Kotal * A check if a pattern contains at least one pair of parentheses meaning 3564819763SVladimir Kotal * that there is at least one capture group. This group must not be empty. 3664819763SVladimir Kotal */ 37*76b59292SVladimir Kotal private static final String PATTERN_SINGLE_GROUP = ".*\\([^\\)]+\\).*"; 3864819763SVladimir Kotal /** 3964819763SVladimir Kotal * Error string for invalid patterns without a single group. This is passed 4064819763SVladimir Kotal * as a first argument to the constructor of PatternSyntaxException and in 4164819763SVladimir Kotal * the output it is followed by the invalid pattern. 4264819763SVladimir Kotal * 4364819763SVladimir Kotal * @see PatternSyntaxException 4464819763SVladimir Kotal * @see #PATTERN_SINGLE_GROUP 4564819763SVladimir Kotal */ 46*76b59292SVladimir Kotal private static final String PATTERN_MUST_CONTAIN_GROUP = "The pattern must contain at least one non-empty group -"; 4764819763SVladimir Kotal 4864819763SVladimir Kotal /** 4964819763SVladimir Kotal * Check and compile the bug pattern. 5064819763SVladimir Kotal * 5164819763SVladimir Kotal * @param pattern the new pattern 5264819763SVladimir Kotal * @throws PatternSyntaxException when the pattern is not a valid regexp or 5364819763SVladimir Kotal * does not contain at least one capture group and the group does not 5464819763SVladimir Kotal * contain a single character 5564819763SVladimir Kotal */ compilePattern(String pattern)5664819763SVladimir Kotal public static String compilePattern(String pattern) throws PatternSyntaxException { 5764819763SVladimir Kotal if (!pattern.matches(PATTERN_SINGLE_GROUP)) { 5864819763SVladimir Kotal throw new PatternSyntaxException(PATTERN_MUST_CONTAIN_GROUP, pattern, 0); 5964819763SVladimir Kotal } 6064819763SVladimir Kotal return Pattern.compile(pattern).toString(); 6164819763SVladimir Kotal } 6264819763SVladimir Kotal } 63