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.jsp.util;
029
030import org.opencms.util.CmsMacroResolver;
031import org.opencms.util.CmsStringUtil;
032import org.opencms.util.CmsUUID;
033
034import java.util.HashMap;
035import java.util.Map;
036import java.util.regex.Matcher;
037import java.util.regex.Pattern;
038
039/**
040 * Macro resolver used to temporarily replace localization message macros with random UUIDs and then replace the UUIDs
041 * with the original key after all other macro processing has happened.
042 *
043 * <p>We need this because we want to preserve the message key, but evaluate macros they may be nested in.
044 */
045public class CmsKeyDummyMacroResolver extends CmsMacroResolver {
046
047    /** Pattern to match message key macros. */
048    public static final Pattern PATTERN_MESSAGE = Pattern.compile("^%\\(key\\.([^\\)]++)\\)$");
049
050    /** Pattern to match UUIDs. */
051    private static final Pattern UUID_PATTERN = Pattern.compile(CmsUUID.UUID_REGEX);
052
053    /** The macro resolver to delegate to. */
054    private CmsMacroResolver m_delegate;
055
056    /** The map containing the original string for each ID it was replaced with. */
057    private Map<CmsUUID, String> m_keys = new HashMap<>();
058
059    /**
060     * Creates a new instance
061     *
062     * @param delegate the macro resolver to delegate to
063     */
064    public CmsKeyDummyMacroResolver(CmsMacroResolver delegate) {
065
066        m_delegate = delegate;
067    }
068
069    /**
070     * Extracts the message from a string of the form %(key.{message}), or returns null if the input string
071     * does not have this form.
072     *
073     * @param s the input string
074     * @return the key the extracted message key
075     */
076    public static String getKey(String s) {
077
078        if (s == null) {
079            return null;
080        }
081        Matcher matcher = PATTERN_MESSAGE.matcher(s);
082        if (matcher.matches()) {
083            return matcher.group(1);
084        } else {
085            return null;
086        }
087    }
088
089    /**
090     * Gets the localization key name of a (potentially nested) key macro in a base string.<p>
091     *
092     *
093     * @param s the base string
094     * @param delegate the macro resolver used to resolve macros which the key macro may be nested in
095     * @return the localization key
096     */
097    public static String getKey(String s, CmsMacroResolver delegate) {
098
099        if (s == null) {
100            return null;
101        }
102        CmsKeyDummyMacroResolver resolver = new CmsKeyDummyMacroResolver(delegate);
103        String resolved = resolver.resolveMacros(s);
104        return getKey(resolved);
105    }
106
107    /**
108     * @see org.opencms.util.CmsMacroResolver#getMacroValue(java.lang.String)
109     */
110    @Override
111    public String getMacroValue(String macro) {
112
113        if (macro.startsWith(CmsMacroResolver.KEY_LOCALIZED_PREFIX)) {
114            String key = macro.substring(CmsMacroResolver.KEY_LOCALIZED_PREFIX.length());
115            CmsUUID id = new CmsUUID();
116            m_keys.put(id, key);
117            return id.toString();
118        } else {
119            String result = m_delegate.getMacroValue(macro);
120            return result;
121        }
122    }
123
124    /**
125     * @see org.opencms.util.CmsMacroResolver#resolveMacros(java.lang.String)
126     */
127    @Override
128    public String resolveMacros(String input) {
129
130        String processedInput = super.resolveMacros(input);
131        String result = CmsStringUtil.substitute(UUID_PATTERN, processedInput, (s, matcher) -> {
132            CmsUUID id = new CmsUUID(matcher.group());
133            if (m_keys.containsKey(id)) {
134                return "%(key." + m_keys.get(id) + ")";
135            } else {
136                return matcher.group();
137            }
138        });
139        return result;
140    }
141
142}