001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.i18n;
029
030import org.opencms.i18n.CmsMultiMessages.I_KeyFallbackHandler;
031import org.opencms.main.CmsLog;
032import org.opencms.util.CmsStringUtil;
033import org.opencms.util.I_CmsRegexSubstitution;
034
035import java.util.regex.Matcher;
036import java.util.regex.Pattern;
037
038import org.apache.commons.logging.Log;
039
040import com.google.common.base.Optional;
041
042/**
043 * Message key fallback handler used to enable reuse of localization keys for editor labels.<p>
044 *
045 * This handler, when given a key of the form label.foo.bar (starting with 'label' and containing at least three
046 * dot-separated components), removes the second component (in the example, this results in the key label.bar).<p>
047 *
048 * This can be useful if you want to reuse the same key for fields of multiple content types.
049 */
050public class CmsRemoveInnerNameFallback implements I_KeyFallbackHandler, I_CmsRegexSubstitution {
051
052    /** The logger instance for this class. */
053    private static final Log LOG = CmsLog.getLog(CmsRemoveInnerNameFallback.class);
054
055    /** Regex string. */
056    public static final String REGEX_STR = "^label\\.[^.]+\\.(.+)$";
057
058    /** Regex pattern instance. */
059    public static final Pattern PATTERN = Pattern.compile(REGEX_STR);
060
061    /**
062     * Creates a new instance.<p>
063     *
064     * @param config the configuration (ignored)
065     */
066    public CmsRemoveInnerNameFallback(String config) {
067        // do nothing, we just need this constructor for reflection purposes
068    }
069
070    /**
071     * @see org.opencms.i18n.CmsMultiMessages.I_KeyFallbackHandler#getFallbackKey(java.lang.String)
072     */
073    public Optional<String> getFallbackKey(String key) {
074
075        String result = CmsStringUtil.substitute(PATTERN, key, this);
076        if (result.equals(key)) {
077            return Optional.absent();
078        } else {
079            LOG.debug("Replacing message key " + key + " with " + result);
080            return Optional.fromNullable(result);
081        }
082    }
083
084    /**
085     * @see org.opencms.util.I_CmsRegexSubstitution#substituteMatch(java.lang.String, java.util.regex.Matcher)
086     */
087    public String substituteMatch(String string, Matcher matcher) {
088
089        return "label." + matcher.group(1);
090    }
091
092}