001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.journal.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.util.List;
021    import java.util.regex.Matcher;
022    import java.util.regex.Pattern;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class RegexTransformerListener extends TransformerListener {
028    
029            public String onXml(String s) {
030                    if (_log.isDebugEnabled()) {
031                            _log.debug("onXml");
032                    }
033    
034                    return s;
035            }
036    
037            public String onScript(String s) {
038                    if (_log.isDebugEnabled()) {
039                            _log.debug("onScript");
040                    }
041    
042                    s = replace(s);
043    
044                    return s;
045            }
046    
047            public String onOutput(String s) {
048                    if (_log.isDebugEnabled()) {
049                            _log.debug("onOutput");
050                    }
051    
052                    s = replace(s);
053    
054                    return s;
055            }
056    
057            protected String replace(String s) {
058                    if (s == null) {
059                            return s;
060                    }
061    
062                    List<Pattern> patterns = RegexTransformerUtil.getPatterns();
063                    List<String> replacements = RegexTransformerUtil.getReplacements();
064    
065                    for (int i = 0; i < patterns.size(); i++) {
066                            Pattern pattern = patterns.get(i);
067                            String replacement = replacements.get(i);
068    
069                            Matcher matcher = pattern.matcher(s);
070    
071                            s = matcher.replaceAll(replacement);
072                    }
073    
074                    return s;
075            }
076    
077            private static Log _log = LogFactoryUtil.getLog(
078                    RegexTransformerListener.class);
079    
080    }