001    /**
002     * Copyright (c) 2000-present 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    import com.liferay.portal.kernel.templateparser.BaseTransformerListener;
020    import com.liferay.portal.kernel.xml.Document;
021    
022    import java.util.List;
023    import java.util.Map;
024    import java.util.regex.Matcher;
025    import java.util.regex.Pattern;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class RegexTransformerListener extends BaseTransformerListener {
031    
032            @Override
033            public String onOutput(
034                    String output, String languageId, Map<String, String> tokens) {
035    
036                    if (_log.isDebugEnabled()) {
037                            _log.debug("onOutput");
038                    }
039    
040                    return replace(output);
041            }
042    
043            @Override
044            public String onScript(
045                    String script, Document document, String languageId,
046                    Map<String, String> tokens) {
047    
048                    if (_log.isDebugEnabled()) {
049                            _log.debug("onScript");
050                    }
051    
052                    return replace(script);
053            }
054    
055            protected String replace(String s) {
056                    if (s == null) {
057                            return s;
058                    }
059    
060                    List<Pattern> patterns = RegexTransformerUtil.getPatterns();
061                    List<String> replacements = RegexTransformerUtil.getReplacements();
062    
063                    for (int i = 0; i < patterns.size(); i++) {
064                            Pattern pattern = patterns.get(i);
065                            String replacement = replacements.get(i);
066    
067                            Matcher matcher = pattern.matcher(s);
068    
069                            s = matcher.replaceAll(replacement);
070                    }
071    
072                    return s;
073            }
074    
075            private static Log _log = LogFactoryUtil.getLog(
076                    RegexTransformerListener.class);
077    
078    }