org.opencms.util
public class CmsMacroResolver extends java.lang.Object implements I_CmsMacroResolver
%(key)
or ${key}
in an input String.
Starting with OpenCms 7.0, the preferred form of a macro is %(key)
. This is to
avoid conflicts / confusion with the JSP EL, which also uses the ${key}
syntax.
The macro names that can be resolved depend of the context objects provided to the resolver
using the set...
methods.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
KEY_ATTRIBUTE
The prefix indicating that the key represents an OpenCms runtime attribute.
|
static java.lang.String |
KEY_CONTEXT_PATH
Key used to specify the context path as macro value.
|
static java.lang.String |
KEY_CURRENT_ORGUNIT_DESCRIPTION
Key used to specify the description of the current organizational unit as macro value.
|
static java.lang.String |
KEY_CURRENT_ORGUNIT_FQN
Key used to specify the full qualified name of the current organizational unit as macro value.
|
static java.lang.String |
KEY_CURRENT_TIME
Key used to specify the current time as macro value.
|
static java.lang.String |
KEY_CURRENT_USER_CITY
Key used to specify the city of the current user as macro value.
|
static java.lang.String |
KEY_CURRENT_USER_COUNTRY
Key used to specify the country of the current user as macro value.
|
static java.lang.String |
KEY_CURRENT_USER_DISPLAYNAME
Key used to specify the display name of the current user as macro value.
|
static java.lang.String |
KEY_CURRENT_USER_EMAIL
Key used to specify the email address of the current user as macro value.
|
static java.lang.String |
KEY_CURRENT_USER_FIRSTNAME
Key used to specify the first name of the current user as macro value.
|
static java.lang.String |
KEY_CURRENT_USER_FULLNAME
Key used to specify the full name of the current user as macro value.
|
static java.lang.String |
KEY_CURRENT_USER_INSTITUTION
Key used to specify the institution of the current user as macro value.
|
static java.lang.String |
KEY_CURRENT_USER_LASTLOGIN
Key used to specify the last login date of the current user as macro value.
|
static java.lang.String |
KEY_CURRENT_USER_LASTNAME
Key used to specify the last name of the current user as macro value.
|
static java.lang.String |
KEY_CURRENT_USER_NAME
Key used to specify the user name of the current user as macro value.
|
static java.lang.String |
KEY_CURRENT_USER_STREET
Key used to specify the street of the current user as macro value.
|
static java.lang.String |
KEY_CURRENT_USER_ZIP
Key used to specify the zip code of the current user as macro value.
|
static java.lang.String |
KEY_LOCALIZED_PREFIX
Key prefix used to specify the value of a localized key as macro value.
|
static java.lang.String |
KEY_OPENCMS
Identifier for "magic" parameter names.
|
static java.lang.String |
KEY_PAGE_CONTEXT
The prefix indicating that the key represents a page context object.
|
static java.lang.String |
KEY_PROJECT_ID
Key used to specify the project id as macro value.
|
static java.lang.String |
KEY_PROPERTY
The prefix indicating that the key represents a property to be read on the current request URI.
|
static java.lang.String |
KEY_PROPERTY_ELEMENT
The prefix indicating that the key represents a property to be read on the current element.
|
static java.lang.String |
KEY_REQUEST_ENCODING
Key used to specify the request encoding as macro value.
|
static java.lang.String |
KEY_REQUEST_FOLDER
Key used to specify the folder of the request URI as macro value.
|
static java.lang.String |
KEY_REQUEST_LOCALE
Key user to specify the request locale as macro value.
|
static java.lang.String |
KEY_REQUEST_PARAM
The prefix indicating that the key represents a HTTP request parameter.
|
static java.lang.String |
KEY_REQUEST_SITEROOT
Key used to specify the request site root as macro value.
|
static java.lang.String |
KEY_REQUEST_URI
Key used to specify the request uri as macro value.
|
static java.lang.String |
KEY_VALIDATION_PATH
Key used to specify the validation path as macro value.
|
static java.lang.String |
KEY_VALIDATION_REGEX
Key used to specify the validation regex as macro value.
|
static java.lang.String |
KEY_VALIDATION_VALUE
Key used to specify the validation value as macro value.
|
protected java.util.Map<java.lang.String,java.lang.String> |
m_additionalMacros
A map of additional values provided by the calling class.
|
protected CmsObject |
m_cms
The OpenCms user context to use for resolving macros.
|
protected javax.servlet.jsp.PageContext |
m_jspPageContext
The JSP's page context to use for resolving macros.
|
protected boolean |
m_keepEmptyMacros
Indicates if unresolved macros should be kept "as is" or replaced by an empty String.
|
protected CmsMessages |
m_messages
The messages resource bundle to resolve localized keys with.
|
protected java.lang.String |
m_resourceName
The resource name to use for resolving macros.
|
static java.util.List<java.lang.String> |
VALUE_NAMES
The "magic" commands wrapped in a List.
|
MACRO_DELIMITER, MACRO_DELIMITER_OLD, MACRO_END, MACRO_END_OLD, MACRO_START, MACRO_START_OLD
Constructor and Description |
---|
CmsMacroResolver() |
Modifier and Type | Method and Description |
---|---|
void |
addDynamicMacro(java.lang.String name,
org.apache.commons.collections.Factory factory)
Adds a macro whose value will be dynamically generated at macro resolution time.
|
void |
addMacro(java.lang.String key,
java.lang.String value)
Adds a customized macro to this macro resolver.
|
static java.lang.String |
formatMacro(java.lang.String input)
Adds macro delimiters to the given input,
for example
key becomes %(key) . |
java.lang.String |
getMacroValue(java.lang.String macro)
Resolves a single macro to the macro value, returns
null if the macro could not be resolved. |
boolean |
isKeepEmptyMacros()
Returns
true if macros that could not be resolved are kept "as is" in the
input String, false if they are replaced by an empty String. |
static boolean |
isMacro(java.lang.String input)
Returns
true if the given input String if formatted like a macro,
that is it starts with and ends with
. |
static boolean |
isMacro(java.lang.String input,
java.lang.String macroName)
Returns
true if the given input String is a macro equal to the given macro name. |
static java.lang.String |
localizedKeyMacro(java.lang.String keyName,
java.lang.Object[] params)
Returns a macro for the given localization key with the given parameters.
|
static CmsMacroResolver |
newInstance()
Factory method to create a new
CmsMacroResolver instance. |
java.lang.String |
resolveMacros(java.lang.String input)
Resolves the macros in the given input.
|
static java.lang.String |
resolveMacros(java.lang.String input,
CmsObject cms,
CmsMessages messages)
Resolves the macros in the given input using the provided parameters.
|
static java.lang.String |
resolveMacros(java.lang.String input,
I_CmsMacroResolver resolver)
Resolves macros in the provided input String using the given macro resolver.
|
CmsMacroResolver |
setAdditionalMacros(java.util.Map<java.lang.String,java.lang.String> additionalMacros)
Provides a set of additional macros to this macro resolver.
|
CmsMacroResolver |
setCmsObject(CmsObject cms)
Provides an OpenCms user context to this macro resolver, required to resolve certain macros.
|
CmsMacroResolver |
setJspPageContext(javax.servlet.jsp.PageContext jspPageContext)
Provides a JSP page context to this macro resolver, required to resolve certain macros.
|
CmsMacroResolver |
setKeepEmptyMacros(boolean keepEmptyMacros)
Controls of macros that can't be resolved are left unchanged in the input,
or are replaced with an empty String.
|
CmsMacroResolver |
setMessages(CmsMessages messages)
Provides a set of
to this macro resolver,
required to resolve localized macros. |
CmsMacroResolver |
setResourceName(java.lang.String resourceName)
Provides a resource name to this macro resolver, required to resolve certain macros.
|
static java.lang.String |
stripMacro(java.lang.String input)
Strips the macro delimiters from the given input,
for example
%(key) or ${key} becomes key . |
public static final java.lang.String KEY_ATTRIBUTE
public static final java.lang.String KEY_CONTEXT_PATH
public static final java.lang.String KEY_CURRENT_ORGUNIT_DESCRIPTION
public static final java.lang.String KEY_CURRENT_ORGUNIT_FQN
public static final java.lang.String KEY_CURRENT_TIME
public static final java.lang.String KEY_CURRENT_USER_CITY
public static final java.lang.String KEY_CURRENT_USER_COUNTRY
public static final java.lang.String KEY_CURRENT_USER_DISPLAYNAME
public static final java.lang.String KEY_CURRENT_USER_EMAIL
public static final java.lang.String KEY_CURRENT_USER_FIRSTNAME
public static final java.lang.String KEY_CURRENT_USER_FULLNAME
public static final java.lang.String KEY_CURRENT_USER_INSTITUTION
public static final java.lang.String KEY_CURRENT_USER_LASTLOGIN
public static final java.lang.String KEY_CURRENT_USER_LASTNAME
public static final java.lang.String KEY_CURRENT_USER_NAME
public static final java.lang.String KEY_CURRENT_USER_STREET
public static final java.lang.String KEY_CURRENT_USER_ZIP
public static final java.lang.String KEY_LOCALIZED_PREFIX
public static final java.lang.String KEY_OPENCMS
public static final java.lang.String KEY_PAGE_CONTEXT
public static final java.lang.String KEY_PROJECT_ID
public static final java.lang.String KEY_PROPERTY
public static final java.lang.String KEY_PROPERTY_ELEMENT
public static final java.lang.String KEY_REQUEST_ENCODING
public static final java.lang.String KEY_REQUEST_FOLDER
public static final java.lang.String KEY_REQUEST_LOCALE
public static final java.lang.String KEY_REQUEST_PARAM
public static final java.lang.String KEY_REQUEST_SITEROOT
public static final java.lang.String KEY_REQUEST_URI
public static final java.lang.String KEY_VALIDATION_PATH
public static final java.lang.String KEY_VALIDATION_REGEX
public static final java.lang.String KEY_VALIDATION_VALUE
protected java.util.Map<java.lang.String,java.lang.String> m_additionalMacros
protected javax.servlet.jsp.PageContext m_jspPageContext
protected boolean m_keepEmptyMacros
protected CmsMessages m_messages
protected java.lang.String m_resourceName
public static final java.util.List<java.lang.String> VALUE_NAMES
public CmsMacroResolver()
public void addDynamicMacro(java.lang.String name, org.apache.commons.collections.Factory factory)
The value will be generated for each occurence of the macro in a string.
name
- the name of the macrofactory
- the macro value generatorpublic void addMacro(java.lang.String key, java.lang.String value)
key
- the macro to addvalue
- the value to return if the macro is encounteredpublic static java.lang.String formatMacro(java.lang.String input)
key
becomes %(key)
.input
- the input to format as a macropublic java.lang.String getMacroValue(java.lang.String macro)
I_CmsMacroResolver
null
if the macro could not be resolved.getMacroValue
in interface I_CmsMacroResolver
macro
- the macro to resolvenull
if the macro could not be resolvedI_CmsMacroResolver.getMacroValue(java.lang.String)
public boolean isKeepEmptyMacros()
I_CmsMacroResolver
true
if macros that could not be resolved are kept "as is" in the
input String, false
if they are replaced by an empty String.isKeepEmptyMacros
in interface I_CmsMacroResolver
true
if macros that could not be resolved are kept "as is" in the
input String, false
if they are replaced by an empty StringI_CmsMacroResolver.isKeepEmptyMacros()
public static boolean isMacro(java.lang.String input)
true
if the given input String if formatted like a macro,
that is it starts with I_CmsMacroResolver.MACRO_DELIMITER_OLD
+
I_CmsMacroResolver.MACRO_START_OLD
and ends with
I_CmsMacroResolver.MACRO_END_OLD
.input
- the input to check for a macrotrue
if the given input String if formatted like a macropublic static boolean isMacro(java.lang.String input, java.lang.String macroName)
true
if the given input String is a macro equal to the given macro name.input
- the input to check for a macromacroName
- the macro name to check fortrue
if the given input String is a macro equal to the given macro namepublic static java.lang.String localizedKeyMacro(java.lang.String keyName, java.lang.Object[] params)
keyName
- the name of the localized keyparams
- the optional parameter arraypublic static CmsMacroResolver newInstance()
CmsMacroResolver
instance.CmsMacroResolver
public java.lang.String resolveMacros(java.lang.String input)
Calls
until no more macros can
be resolved in the input. This way "nested" macros in the input are resolved as well.resolveMacros(String)
resolveMacros
in interface I_CmsMacroResolver
input
- the input to resolve the macros inI_CmsMacroResolver.resolveMacros(java.lang.String)
public static java.lang.String resolveMacros(java.lang.String input, CmsObject cms, CmsMessages messages)
A macro in the form %(key)
or ${key}
in the content is replaced with it's assigned value
returned by the
method of the given
I_CmsMacroResolver.getMacroValue(String)
instance.I_CmsMacroResolver
If a macro is found that can not be mapped to a value by the given macro resolver, it is left untouched in the input.
input
- the input in which to resolve the macroscms
- the OpenCms user context to use when resolving macrosmessages
- the message resource bundle to use when resolving macrospublic static java.lang.String resolveMacros(java.lang.String input, I_CmsMacroResolver resolver)
A macro in the form %(key)
or ${key}
in the content is replaced with it's assigned value
returned by the
method of the given
I_CmsMacroResolver.getMacroValue(String)
instance.I_CmsMacroResolver
If a macro is found that can not be mapped to a value by the given macro resolver,
controls if the macro is replaced by
an empty String, or is left untouched in the input.I_CmsMacroResolver.isKeepEmptyMacros()
input
- the input in which to resolve the macrosresolver
- the macro resolver to usepublic CmsMacroResolver setAdditionalMacros(java.util.Map<java.lang.String,java.lang.String> additionalMacros)
Macros added with addMacro(String, String)
are added to the same set
additionalMacros
- the additional macros to addpublic CmsMacroResolver setCmsObject(CmsObject cms)
cms
- the OpenCms user contextpublic CmsMacroResolver setJspPageContext(javax.servlet.jsp.PageContext jspPageContext)
jspPageContext
- the JSP page context to usepublic CmsMacroResolver setKeepEmptyMacros(boolean keepEmptyMacros)
keepEmptyMacros
- the replacement flag to useisKeepEmptyMacros()
public CmsMacroResolver setMessages(CmsMessages messages)
CmsMessages
to this macro resolver,
required to resolve localized macros.messages
- the message resource bundle to usepublic CmsMacroResolver setResourceName(java.lang.String resourceName)
resourceName
- the resource name to usepublic static java.lang.String stripMacro(java.lang.String input)
%(key)
or ${key}
becomes key
.
In case the input is not a macro, null
is returned.
input
- the input to stripnull