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.List;
020import java.util.regex.Matcher;
021import java.util.regex.PatternSyntaxException;
022
023/**
024 * This class allows the application of multiplex {@link IRegexReplacement}s to
025 * a string.
026 * 
027 * 
028 * @author Florian Deissenboeck
029 */
030public class RegexReplacementProcessor {
031
032        /** The list of replacements. */
033        private final List<IRegexReplacement> expressions;
034
035        /** Create a new replacement processor. */
036        public RegexReplacementProcessor(List<IRegexReplacement> expressions) {
037                this.expressions = expressions;
038        }
039
040        /**
041         * Apply replacements to a string.
042         * 
043         * @return the input string after the application of all replacements or the
044         *         input string if the list of replacements is empty.
045         * @throws PatternSyntaxException
046         *             unfortunately method
047         *             {@link Matcher#replaceAll(java.lang.String)} throws an
048         *             {@link IndexOutOfBoundsException} if a non-existent capturing
049         *             group is referenced. This method converts this exception to a
050         *             {@link PatternSyntaxException}.
051         */
052        public String process(String text) throws PatternSyntaxException {
053                String result = text;
054                for (IRegexReplacement expr : expressions) {
055                        Matcher matcher = expr.getPattern().matcher(result);
056                        String replacement = expr.getReplacement();
057
058                        try {
059                                result = matcher.replaceAll(replacement);
060                        } catch (IndexOutOfBoundsException ex) {
061                                throw new PatternSyntaxException(ex.getMessage(), replacement, -1);
062                        }
063                }
064
065                return result;
066        }
067}