001    /*--------------------------------------------------------------------------+
002    $Id: RegexReplacementProcessor.java 26283 2010-02-18 11:18:57Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.string;
019    
020    import java.util.List;
021    import java.util.regex.Matcher;
022    import java.util.regex.PatternSyntaxException;
023    
024    /**
025     * This class allows the application of multiplex {@link IRegexReplacement}s to
026     * a string.
027     * 
028     * 
029     * @author Florian Deissenboeck
030     * @author $Author: juergens $
031     * @version $Rev: 26283 $
032     * @levd.rating GREEN Hash: 34CDDB3007890E0011169AE24AE44CA9
033     */
034    public class RegexReplacementProcessor {
035        /** The list of replacements. */
036        private final List<IRegexReplacement> expressions;
037    
038        /** Create a new replacement processor. */
039        public RegexReplacementProcessor(List<IRegexReplacement> expressions) {
040            this.expressions = expressions;
041        }
042    
043        /**
044         * Apply replacements to a string.
045         * 
046         * @return the input string after the application of all replacements or the
047         *         a copy of the input string if the list of replacements is empty.
048         * @throws PatternSyntaxException
049         *             unfortunately method
050         *             {@link Matcher#replaceAll(java.lang.String)} throws an
051         *             {@link IndexOutOfBoundsException} if a non-existent capturing
052         *             group is referenced. This method converts this exception to a
053         *             {@link PatternSyntaxException}.
054         */
055        public String process(String text) throws PatternSyntaxException {
056            // if expression list ist empty this returns the original string
057            String result = text;
058            for (IRegexReplacement expr : expressions) {
059                Matcher matcher = expr.getPattern().matcher(result);
060                String replacement = expr.getReplacement();
061    
062                try {
063                    result = matcher.replaceAll(replacement);
064                } catch (IndexOutOfBoundsException ex) {
065                    throw new PatternSyntaxException(ex.getMessage(), replacement,
066                            -1);
067                }
068            }
069    
070            return result;
071        }
072    }