Class MacroResolver
- Namespace
- CMS.MacroEngine
- Assembly
- CMS.MacroEngine.dll
Class to resolve the macros, provides data to the resolving process.
public class MacroResolver : IMacroResolver, INotCopyThreadItem
- Inheritance
-
objectMacroResolver
- Implements
- Extension Methods
Constructors
MacroResolver()
Constructor
[Obsolete("This constructor is meant for system purposes, it shouldn't be used directly. Use GetInstance instead.")]
public MacroResolver()
Fields
UNRESOLVED_RETURN_VALUE
Represents unresolved keyword.
public static readonly object UNRESOLVED_RETURN_VALUE
Field Value
- object
Properties
Culture
Culture for the resolving
public string Culture { get; set; }
Property Value
- string
ResolverName
Resolver name used for context specific resolving.
public string ResolverName { get; set; }
Property Value
- string
Examples
The macro with resolver parameter e.g. {% CurrentUser.UserName|(resolver)CustomResolverName %} will be resolved only with the same resolver name specified in this property.
var resolver = MacroResolver.CreateChild();
resolver.ResolverName = "CustomResolverName";
var result = resolver.ResolveMacros("{% CurrentUser.UserName|(resolver)CustomResolverName %}");
- See Also
Settings
Macro resolver settings.
public MacroSettings Settings { get; set; }
Property Value
SourceObject
Object with source data - To pass parameters to Custom macro function.
public object SourceObject { get; set; }
Property Value
- object
Methods
AddAnonymousSourceData(params object[])
Adds new anonymous data source of general type to the resolver.
public void AddAnonymousSourceData(params object[] data)
Parameters
dataobject[]Data to register to the resolver
AddSourceAlias(string, string)
Adds alternative source expression.
public virtual void AddSourceAlias(string source, string expression)
Parameters
sourcestringSource for which the alternative expression should be created
expressionstringExpression which will be evaluated if the source is not found in base data sources
CheckDataSources(string, EvaluationContext)
Checks all the data sources for the value. Returns true if given data member was found within supported data sources.
public virtual EvaluationResult CheckDataSources(string expression, EvaluationContext context)
Parameters
expressionstringData member to look for
contextEvaluationContextEvaluation context
Returns
CreateChild()
Creates new instance of MacroResolver as a child resolver that loads the data from the parent resolver by default.
public virtual MacroResolver CreateChild()
Returns
Remarks
Settings are cloned from parent resolver.
EncapsulateObject(object, EvaluationContext)
Encapsulates object if needed (DataSet, DataRow, DataTable to their Container equivalent).
public static object EncapsulateObject(object obj, EvaluationContext context = null)
Parameters
objobjectObject to be encapsulated
contextEvaluationContextEvaluation context
Returns
- object
FinalizeResult(object, bool, EvaluationContext)
Finalizes the result (makes the result string).
protected string FinalizeResult(object result, bool processChilden, EvaluationContext context)
Parameters
resultobjectResult object
processChildenboolIf true, the children get finalized as well
contextEvaluationContextEvaluation context
Returns
- string
GetAnonymousSources()
Returns list of all aggregated anonymous sources (from the whole resolver hierarchy).
public List<object> GetAnonymousSources()
Returns
- List<object>
GetInstance(bool)
Creates new instance of MacroResolver.
public static MacroResolver GetInstance(bool inheritFromGlobalResolver = true)
Parameters
inheritFromGlobalResolverboolIf true, the resolver will be a child of the GlobalResolver (recommended if you need full feature resolver).
Returns
GetNamedSourceData(string)
Gets the named source data by the given selector.
public object GetNamedSourceData(string selector)
Parameters
selectorstringSelector
Returns
- object
GetObjectValue(object, int, EvaluationContext)
Gets the object value at given index (this is called when indexer [(int)] is used in the expression).
public virtual EvaluationResult GetObjectValue(object objectToCheck, int index, EvaluationContext context)
Parameters
objectToCheckobjectSource object to get the index-th value from
indexintIndex of the item to get
contextEvaluationContextEvaluation context
Returns
GetObjectValue(object, string, EvaluationContext)
Gets the object value of specified name.
public virtual EvaluationResult GetObjectValue(object objectToCheck, string columnName, EvaluationContext context)
Parameters
objectToCheckobjectSource object to get the index-th value from
columnNamestringName of the value to get
contextEvaluationContextEvaluation context
Returns
GetSourceAlias(string)
Gets the named source data by the given selector.
public virtual string GetSourceAlias(string selector)
Parameters
selectorstringSelector
Returns
- string
IsDataItemAvailable(string)
Checks all the data sources for the value and returns true if the data item is registered.
public virtual bool IsDataItemAvailable(string itemName)
Parameters
itemNamestringName of the data item
Returns
- bool
PostProcessResult(object, bool)
If the object is InfoObject, returns its DisplayName column (if exists).
protected object PostProcessResult(object result, bool allowEnumeration = true)
Parameters
resultobjectResult to process
allowEnumerationboolIf true, the process allows enumeration of the result
Returns
- object
PrioritizeProperty(string)
Prioritizes a property (equivalent to using SetNamedSourceData with last parameter true).
public void PrioritizeProperty(string propertyName)
Parameters
propertyNamestringName of the property to prioritize
RegisterNamespace(IMacroNamespace, string, bool, bool)
Registers a namespace within this macro resolver
public void RegisterNamespace(IMacroNamespace ns, string name = null, bool allowAnonymous = false, bool hidden = false)
Parameters
nsIMacroNamespaceNamespace object
namestringNamespace name. If not set, the name is automatically taken from the type name of the namespace object with removed Namespace suffix.
allowAnonymousboolIf true, the namespace members are registered also as anonymous
hiddenboolIf true, the namespace is hidden and doesn't show up in the Intellisense
RegisterObjectValueByIndexHandler(params GetObjectValueByIndexHandler[])
Registers GetObjectValueByName handler(s) to extend possibilities of MacroEngine to dig values from particular object types. These handlers are internally used when a property is requested out of a given object. First the default object types are checked, then all the handlers (the order of the handler evaluation is not guaranteed).
public static void RegisterObjectValueByIndexHandler(params MacroResolver.GetObjectValueByIndexHandler[] handlers)
Parameters
handlersGetObjectValueByIndexHandler[]Handler(s) to attach
RegisterObjectValueByNameHandler(params GetObjectValueByNameHandler[])
Registers GetObjectValueByName handler(s) to extend possibilities of MacroEngine to dig values from particular object types. These handlers are internally used when a property is requested out of a given object. First the default object types are checked, then all the handlers (the order of the handler evaluation is not guaranteed).
public static void RegisterObjectValueByNameHandler(params MacroResolver.GetObjectValueByNameHandler[] handlers)
Parameters
handlersGetObjectValueByNameHandler[]Handler(s) to attach
Resolve(string, MacroSettings)
Resolves the context macros.
public static string Resolve(string inputText, MacroSettings settings = null)
Parameters
inputTextstringText to resolve
settingsMacroSettingsMacro context and settings
Returns
- string
ResolveCustomMacro(string, string)
Resolves the custom macro.
public EvaluationResult ResolveCustomMacro(string baseExpression, string fullExpression)
Parameters
baseExpressionstringBase expression (without the parameters)
fullExpressionstringFull expression (with the parameters)
Returns
ResolveMacroExpression(ResolveExpressionSettings)
Resolves the data macro expression (expects expression without {% %} brackets).
public EvaluationResult ResolveMacroExpression(ResolveExpressionSettings settings)
Parameters
settingsResolveExpressionSettingsSettings of the resolving process
Returns
Remarks
Returns null when macro evaluation throws exception
ResolveMacroExpression(string, bool, bool, string)
Resolves the data macro expression (expects expression without {% %} brackets). Use RemoveDataMacroBrackets(string) method to remove brackets if needed.
public EvaluationResult ResolveMacroExpression(string expression, bool keepObjectsAsResult = false, bool skipSecurityCheck = false, string type = "%")
Parameters
expressionstringMacro expression without {% %} brackets
keepObjectsAsResultboolIf true, when the result is InfoObject it is the result, if false, object is resolved as its displayname (for backward compatibility)
skipSecurityCheckboolIf true, security check is not performed
typestringType of the expression (? or $ or %)
Returns
Remarks
Returns null when macro evaluation throws exception
ResolveMacros(string, MacroSettings)
Resolves all supported macro types in the given text within specified resolving context.
public virtual string ResolveMacros(string text, MacroSettings settings = null)
Parameters
textstringInput text with macros to be resolved
settingsMacroSettingsMacro context to be used for resolving (if null, context of the resolver is used)
Returns
- string
SetAnonymousSourceData(params object[])
Sets (replaces all existing anonymous sources) specified data source (without name) of general type to the resolver.
public void SetAnonymousSourceData(params object[] data)
Parameters
dataobject[]Data to register to the resolver
Remarks
This method must not be used for GlobalResolver instance. Use AddAnonymousSourceData(params object[]) instead.
SetHiddenNamedSourceData(string, Func<EvaluationContext, object>)
Registers the named source data with late binding. Callback is called whenever the value is needed by MacroResolver.
public void SetHiddenNamedSourceData(string selector, Func<EvaluationContext, object> callback)
Parameters
selectorstringData selector
callbackFunc<EvaluationContext, object>Callback function which is called to get the data
SetHiddenNamedSourceData(string, object)
Registers the named source data with late binding. Callback is called whenever the value is needed by MacroResolver.
public void SetHiddenNamedSourceData(string selector, object data)
Parameters
selectorstringData selector
dataobjectData
SetNamedSourceData(MacroField, bool)
Registers given field as the named source data.
public void SetNamedSourceData(MacroField field, bool isPrioritized = true)
Parameters
fieldMacroFieldField to register
isPrioritizedboolIf true, this item will appear at the top of AutoCompletion and ObjectTree
SetNamedSourceData(IDictionary<string, object>, bool)
Sets given name-value pairs to the resolver.
public void SetNamedSourceData(IDictionary<string, object> data, bool isPrioritized = true)
Parameters
dataIDictionary<string, object>Data to register to the resolver
isPrioritizedboolIf true, this item will appear at the top of AutoCompletion and ObjectTree
SetNamedSourceData(params Hashtable[])
Sets hashtable data source to the resolver. Keys of the table are considered names of the data.
public void SetNamedSourceData(params Hashtable[] data)
Parameters
dataHashtable[]Data to register to the resolver
SetNamedSourceData(string, object, bool)
Registers the named source data. The data can be accessed with macro {%Selector.ColumnName%}. Selector has to be in identifier format.
public void SetNamedSourceData(string selector, object data, bool isPrioritized = true)
Parameters
selectorstringData selector
dataobjectData
isPrioritizedboolIf true, this item will appear at the top of AutoCompletion and ObjectTree
SetNamedSourceDataCallback(string, Func<EvaluationContext, object>, bool)
Registers the named source data with late binding. Callback is called whenever the value is needed by MacroResolver.
public void SetNamedSourceDataCallback(string selector, Func<EvaluationContext, object> callback, bool isPrioritized = true)
Parameters
selectorstringData selector
callbackFunc<EvaluationContext, object>Callback function which is called to get the data
isPrioritizedboolIf true, this item will appear at the top of AutoCompletion and ObjectTree
Events
OnGetValue
Gets value event.
public event MacroResolver.OnGetValueEventHandler OnGetValue
Event Type
OnResolveCustomMacro
Custom macro event.
public static event EventHandler<MacroEventArgs> OnResolveCustomMacro
Event Type
- EventHandler<MacroEventArgs>