⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsmacroresolver.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        return result.toString();
    }

    /**
     * Adds a customized macro to this macro resolver.<p>
     * 
     * @param key the macro to add
     * @param value the value to return if the macro is encountered
     */
    public void addMacro(String key, String value) {

        if (m_additionalMacros == null) {
            // use lazy initializing
            m_additionalMacros = new HashMap();
        }
        m_additionalMacros.put(key, value);
    }

    /**
     * @see org.opencms.util.I_CmsMacroResolver#getMacroValue(java.lang.String)
     */
    public String getMacroValue(String macro) {

        if (m_messages != null) {
            if (macro.startsWith(CmsMacroResolver.KEY_LOCALIZED_PREFIX)) {
                String keyName = macro.substring(CmsMacroResolver.KEY_LOCALIZED_PREFIX.length());
                return m_messages.keyWithParams(keyName);
            }
        }

        if (m_jspPageContext != null) {

            if (macro.startsWith(CmsMacroResolver.KEY_REQUEST_PARAM)) {
                // the key is a request parameter  
                macro = macro.substring(CmsMacroResolver.KEY_REQUEST_PARAM.length());
                return m_jspPageContext.getRequest().getParameter(macro);
            }

            if (macro.startsWith(CmsMacroResolver.KEY_PAGE_CONTEXT)) {
                // the key is a page context object
                macro = macro.substring(CmsMacroResolver.KEY_PAGE_CONTEXT.length());
                int scope = m_jspPageContext.getAttributesScope(macro);
                return m_jspPageContext.getAttribute(macro, scope).toString();
            }

            if ((m_cms != null) && macro.startsWith(CmsMacroResolver.KEY_PROPERTY_ELEMENT)) {

                // the key is a cms property to be read on the current element

                macro = macro.substring(CmsMacroResolver.KEY_PROPERTY_ELEMENT.length());
                CmsFlexController controller = CmsFlexController.getController(m_jspPageContext.getRequest());
                try {
                    CmsProperty property = m_cms.readPropertyObject(
                        controller.getCurrentRequest().getElementUri(),
                        macro,
                        false);
                    if (property != CmsProperty.getNullProperty()) {
                        return property.getValue();
                    }
                } catch (CmsException e) {
                    if (LOG.isWarnEnabled()) {
                        LOG.warn(Messages.get().getBundle().key(
                            Messages.LOG_PROPERTY_READING_FAILED_2,
                            macro,
                            controller.getCurrentRequest().getElementUri()), e);
                    }
                }
            }
        }

        if (m_cms != null) {

            if (macro.startsWith(CmsMacroResolver.KEY_PROPERTY)) {
                // the key is a cms property to be read on the current request URI
                macro = macro.substring(CmsMacroResolver.KEY_PROPERTY.length());
                try {
                    CmsProperty property = m_cms.readPropertyObject(m_cms.getRequestContext().getUri(), macro, true);
                    if (property != CmsProperty.getNullProperty()) {
                        return property.getValue();
                    }
                } catch (CmsException e) {
                    if (LOG.isWarnEnabled()) {
                        CmsMessageContainer message = Messages.get().container(
                            Messages.LOG_PROPERTY_READING_FAILED_2,
                            macro,
                            m_cms.getRequestContext().getUri());
                        LOG.warn(message.key(), e);
                    }
                }

            }

            if (macro.startsWith(CmsMacroResolver.KEY_OPENCMS)) {

                // the key is a shortcut for a cms runtime value

                String originalKey = macro;
                macro = macro.substring(CmsMacroResolver.KEY_OPENCMS.length());
                int index = VALUE_NAMES.indexOf(macro);
                String value = null;

                switch (index) {
                    case 0:
                        // "uri"
                        value = m_cms.getRequestContext().getUri();
                        break;
                    case 1:
                        // "filename"
                        value = m_resourceName;
                        break;
                    case 2:
                        // folder
                        value = m_cms.getRequestContext().getFolderUri();
                        break;
                    case 3:
                        // default.encoding
                        value = OpenCms.getSystemInfo().getDefaultEncoding();
                        break;
                    default:
                        // return the key "as is"
                        value = originalKey;
                        break;
                }

                return value;
            }

            if (CmsMacroResolver.KEY_CURRENT_USER_NAME.equals(macro)) {
                // the key is the current users login name
                return m_cms.getRequestContext().currentUser().getName();
            }

            if (CmsMacroResolver.KEY_CURRENT_USER_FIRSTNAME.equals(macro)) {
                // the key is the current users first name
                return m_cms.getRequestContext().currentUser().getFirstname();
            }

            if (CmsMacroResolver.KEY_CURRENT_USER_LASTNAME.equals(macro)) {
                // the key is the current users last name
                return m_cms.getRequestContext().currentUser().getLastname();
            }

            if (CmsMacroResolver.KEY_CURRENT_USER_FULLNAME.equals(macro)) {
                // the key is the current users full name
                return m_cms.getRequestContext().currentUser().getFullName();
            }

            if (CmsMacroResolver.KEY_CURRENT_USER_EMAIL.equals(macro)) {
                // the key is the current users email address
                return m_cms.getRequestContext().currentUser().getEmail();
            }

            if (CmsMacroResolver.KEY_CURRENT_USER_STREET.equals(macro)) {
                // the key is the current users address
                return m_cms.getRequestContext().currentUser().getAddress();
            }

            if (CmsMacroResolver.KEY_CURRENT_USER_ZIP.equals(macro)) {
                // the key is the current users zip code
                return m_cms.getRequestContext().currentUser().getZipcode();
            }

            if (CmsMacroResolver.KEY_CURRENT_USER_COUNTRY.equals(macro)) {
                // the key is the current users country
                return m_cms.getRequestContext().currentUser().getCountry();
            }

            if (CmsMacroResolver.KEY_CURRENT_USER_CITY.equals(macro)) {
                // the key is the current users city
                return m_cms.getRequestContext().currentUser().getCity();
            }

            if (CmsMacroResolver.KEY_REQUEST_URI.equals(macro)) {
                // the key is the currently requested uri
                return m_cms.getRequestContext().getUri();
            }

            if (CmsMacroResolver.KEY_REQUEST_FOLDER.equals(macro)) {
                // the key is the currently requested folder
                return CmsResource.getParentFolder(m_cms.getRequestContext().getUri());
            }

            if (CmsMacroResolver.KEY_REQUEST_ENCODING.equals(macro)) {
                // the key is the current encoding of the request
                return m_cms.getRequestContext().getEncoding();
            }

            if (CmsMacroResolver.KEY_REQUEST_LOCALE.equals(macro)) {
                // the key is the current locale of the request
                return m_cms.getRequestContext().getLocale().toString();
            }

        }

        if (CmsMacroResolver.KEY_CURRENT_TIME.equals(macro)) {
            // the key is the current system time
            return String.valueOf(System.currentTimeMillis());
        }

        if (m_additionalMacros != null) {
            return (String)m_additionalMacros.get(macro);
        }

        return null;
    }

    /**
     * @see org.opencms.util.I_CmsMacroResolver#isKeepEmptyMacros()
     */
    public boolean isKeepEmptyMacros() {

        return m_keepEmptyMacros;
    }

    /**
     * Resolves the macros in the given input.<p>
     * 
     * Calls <code>{@link #resolveMacros(String)}</code> until no more macros can 
     * be resolved in the input. This way "nested" macros in the input are resolved as well.<p> 
     * 
     * @see org.opencms.util.I_CmsMacroResolver#resolveMacros(java.lang.String)
     */
    public String resolveMacros(String input) {

        String result = input;

        if (input != null) {
            String lastResult;
            do {
                // save result for next comparison
                lastResult = result;
                // resolve the macros
                result = CmsMacroResolver.resolveMacros(result, this);
                // if nothing changes then the final result is found
            } while (!result.equals(lastResult));
        }

        // return the result
        return result;
    }

    /**
     * Provides a set of additional macros to this macro resolver.<p>
     * 
     * Macros added with {@link #addMacro(String, String)} are added to the same set
     * 
     * @param additionalMacros the additional macros to add
     * 
     * @return this instance of the macro resolver
     */
    public CmsMacroResolver setAdditionalMacros(Map additionalMacros) {

        m_additionalMacros = additionalMacros;
        return this;
    }

    /**
     * Provides an OpenCms user context to this macro resolver, required to resolve certain macros.<p>
     * 
     * @param cms the OpenCms user context
     * 
     * @return this instance of the macro resolver
     */
    public CmsMacroResolver setCmsObject(CmsObject cms) {

        m_cms = cms;
        return this;
    }

    /**
     * Provides a JSP page context to this macro resolver, required to resolve certain macros.<p>
     * 
     * @param jspPageContext the JSP page context to use
     * 
     * @return this instance of the macro resolver
     */
    public CmsMacroResolver setJspPageContext(PageContext jspPageContext) {

        m_jspPageContext = jspPageContext;
        return this;
    }

    /**
     * Controls of macros that can't be resolved are left unchanged in the input,
     * or are replaced with an empty String.<p>
     * 
     * @param keepEmptyMacros the replacemanet flag to use
     * 
     * @return this instance of the macro resolver
     * 
     * @see #isKeepEmptyMacros()
     */
    public CmsMacroResolver setKeepEmptyMacros(boolean keepEmptyMacros) {

        m_keepEmptyMacros = keepEmptyMacros;
        return this;
    }

    /**
     * Provides a set of <code>{@link CmsMessages}</code> to this macro resolver, 
     * required to resolve localized macros.<p>
     * 
     * @param messages the message resource bundle to use
     * 
     * @return this instance of the macro resolver
     */
    public CmsMacroResolver setMessages(CmsMessages messages) {

        m_messages = messages;
        return this;
    }

    /**
     * Provides a resource name to this macro resolver, required to resolve certain macros.<p>
     * 
     * @param resourceName the resource name to use
     * 
     * @return this instance of the macro resolver
     */
    public CmsMacroResolver setResourceName(String resourceName) {

        m_resourceName = resourceName;
        return this;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -