001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }