001    /**
002     * Copyright (c) 2000-2010 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.util.Validator;
020    import com.liferay.portal.util.PropsUtil;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    import java.util.regex.Pattern;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class RegexTransformerUtil {
030    
031            public static List<Pattern> getPatterns() {
032                    return _instance._patterns;
033            }
034    
035            public static List<String> getReplacements() {
036                    return _instance._replacements;
037            }
038    
039            private RegexTransformerUtil() {
040                    _patterns = new ArrayList<Pattern>();
041                    _replacements = new ArrayList<String>();
042    
043                    for (int i = 0; i < 100; i++) {
044                            String regex = PropsUtil.get(
045                                    "journal.transformer.regex.pattern." + i);
046                            String replacement = PropsUtil.get(
047                                    "journal.transformer.regex.replacement." + i);
048    
049                            if (Validator.isNull(regex) || Validator.isNull(replacement)) {
050                                    break;
051                            }
052    
053                            if (_log.isInfoEnabled()) {
054                                    _log.info(
055                                            "Pattern " + regex + " will be replaced with " +
056                                                    replacement);
057                            }
058    
059                            _patterns.add(Pattern.compile(regex));
060                            _replacements.add(replacement);
061                    }
062            }
063    
064            private static Log _log = LogFactoryUtil.getLog(RegexTransformerUtil.class);
065    
066            private static RegexTransformerUtil _instance = new RegexTransformerUtil();
067    
068            private List<Pattern> _patterns;
069            private List<String> _replacements;
070    
071    }