001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 The ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package org.conqat.lib.commons.string; 018 019import java.util.regex.Pattern; 020import java.util.regex.PatternSyntaxException; 021 022/** 023 * Default implementation of {@link IRegexReplacement}. 024 */ 025public class RegexReplacement implements IRegexReplacement { 026 027 /** The pattern. */ 028 private final Pattern pattern; 029 030 /** The replacement. */ 031 private final String replacement; 032 033 /** 034 * Create a new regex replacement. Syntax for patterns, flags and 035 * replacements is specified in the API documentation of 036 * {@link java.util.regex.Pattern} and {@link java.util.regex.Matcher}. 037 * 038 * @throws PatternSyntaxException 039 * if the pattern has a syntax error 040 */ 041 public RegexReplacement(String regex, String replacement, int flags) throws PatternSyntaxException { 042 pattern = Pattern.compile(regex, flags); 043 this.replacement = replacement; 044 } 045 046 /** 047 * Create a new regex replacement. Syntax for patterns and replacements is 048 * specified in the API documentation of {@link java.util.regex.Pattern} and 049 * {@link java.util.regex.Matcher}. 050 * 051 * @throws PatternSyntaxException 052 * if the pattern has a syntax error 053 */ 054 public RegexReplacement(String regex, String replacement) throws PatternSyntaxException { 055 this(regex, replacement, 0); 056 } 057 058 /** 059 * Create a new regex replacement that does not replace the pattern matches 060 * by another string but deletes them. Syntax for patterns and flags is 061 * specified in the API documentation of {@link java.util.regex.Pattern}. 062 * 063 * @throws PatternSyntaxException 064 * if the pattern has a syntax error 065 */ 066 public RegexReplacement(String regex, int flags) throws PatternSyntaxException { 067 this(regex, StringUtils.EMPTY_STRING, flags); 068 } 069 070 /** 071 * Create a new regex replacement that does not replace the pattern matches 072 * by another string but deletes them. Syntax for patterns is specified in 073 * the API documentation of {@link java.util.regex.Pattern}. 074 * 075 * @throws PatternSyntaxException 076 * if the pattern has a syntax error 077 */ 078 public RegexReplacement(String regex) throws PatternSyntaxException { 079 this(regex, 0); 080 } 081 082 /** {@inheritDoc} */ 083 @Override 084 public Pattern getPattern() { 085 return pattern; 086 } 087 088 /** {@inheritDoc} */ 089 @Override 090 public String getReplacement() { 091 return replacement; 092 } 093 094}