public Result <string, string> GetName( [NotNull] IDictionary <string, object> settings, [NotNull] ConfigurationGeneratorPreferences configGenerationPreferences) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } if (configGenerationPreferences == null) { throw new ArgumentNullException(nameof(configGenerationPreferences)); } var configurationNameToken = configGenerationPreferences.ConfigurationNameSetting; object value; if (settings.TryGetValue(configurationNameToken, out value) && value != null) { string configurationName = value.ToString(); _tokenUsageTracker.OnTokenUsed(configurationName, configurationNameToken); return(Result <string, string> .CreateSuccessResult(configurationName)); } return(Result <string, string> .CreateFailureResult($"Failed to get configuration name, as specified setting was not found: {configurationNameToken}")); }
public ProcessNodeResults ProcessNode( [NotNull] XElement element, [NotNull] IConfiguration configuration) { if (element == null) { throw new ArgumentNullException(nameof(element)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } var attributeName = XName.Get("applyWhen", XmlTemplate.ConfigGenXmlNamespace); var attribute = element.Attribute(attributeName); if (attribute == null) { throw new ArgumentException("Supplied element should contain an attribute with name 'applyWhen' in the configgen namespace.", nameof(element)); } var expression = attribute.Value; if (expression.IsNullOrEmpty()) { attribute.Remove(); return(ProcessNodeResults.CreateErrorResult(XmlTemplateErrorCodes.ConditionProcessingError, "Condition error: and empty condition was encountered")); } ExpressionEvaluationResults evaluationResults = _evaluator.Evaluate(configuration.ConfigurationName, expression, element.Name); foreach (var usedToken in evaluationResults.UsedTokens) { if (configuration.Contains(usedToken)) { _tokenUsageTracker.OnTokenUsed(configuration.ConfigurationName, usedToken); } else { _tokenUsageTracker.OnTokenNotRecognised(configuration.ConfigurationName, usedToken); } } if (evaluationResults.Result) { attribute.Remove(); } else { element.Remove(); } if (evaluationResults.ErrorCode != null) { return(ProcessNodeResults.CreateErrorResult(evaluationResults.ErrorCode, evaluationResults.ErrorMessage)); } return(ProcessNodeResults.CreateSuccessResult()); }
public string Target(Match match) { var tokenName = match.Groups[_tokenMatchGroupName].Value; object value; if (_configuration.TryGetValue(tokenName, out value) && value != null) { _tokenUsageTracker.OnTokenUsed(_configuration.ConfigurationName, tokenName); return(value.ToString()); } _tokenUsageTracker.OnTokenNotRecognised(_configuration.ConfigurationName, tokenName); return(null); }
protected ExpressionEvaluationResults EvaluateCondition( [NotNull] XElement element, [NotNull] IConfiguration configuration, [NotNull] IConfigurationExpressionEvaluator configurationExpressionEvaluator) { if (element == null) { throw new ArgumentNullException(nameof(element)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (configurationExpressionEvaluator == null) { throw new ArgumentNullException(nameof(configurationExpressionEvaluator)); } string expression = null; var conditionAttribute = element.Attribute(XName.Get(ConditionAttributeName, string.Empty)); if (conditionAttribute != null) { expression = conditionAttribute.Value; } ExpressionEvaluationResults evaluationResults = configurationExpressionEvaluator.Evaluate(configuration.ConfigurationName, expression, element.Name); foreach (var usedToken in evaluationResults.UsedTokens) { if (configuration.Contains(usedToken)) { _tokenUsageTracker.OnTokenUsed(configuration.ConfigurationName, usedToken); } else { _tokenUsageTracker.OnTokenNotRecognised(configuration.ConfigurationName, usedToken); } } return(evaluationResults); }
public SingleTemplateRenderResults Render([NotNull] IConfiguration configuration) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (_loadedTemplate == null) { throw new InvalidOperationException("Cannot render a template that has not been loaded."); } try { var settings = configuration.ToDictionary(); RazorTemplateRenderResult renderResult = _razorEngineRenderer.Render(settings); if (!renderResult.Success) { return(new SingleTemplateRenderResults( configuration: configuration, status: TemplateRenderResultStatus.Failure, renderedResult: null, encoding: _encoding, errors: new[] { new RazorTemplateError(RazorTemplateErrorCodes.GeneralRazorTemplateError, renderResult.Error) })); } var preferenceFailures = _preferencesManager.ApplyDefaultPreferences(renderResult.AppliedPreferences); if (preferenceFailures.Any()) { return(new SingleTemplateRenderResults( configuration: configuration, status: TemplateRenderResultStatus.Failure, renderedResult: null, encoding: _encoding, errors: preferenceFailures)); } foreach (var settingName in settings) { if (renderResult.UsedTokens.Contains(settingName.Key)) { _tokenUsageTracker.OnTokenUsed(configuration.ConfigurationName, settingName.Key); } } foreach (var settingName in renderResult.UnrecognisedTokens) { _tokenUsageTracker.OnTokenNotRecognised(configuration.ConfigurationName, settingName); } return(new SingleTemplateRenderResults( configuration: configuration, status: TemplateRenderResultStatus.Success, renderedResult: renderResult.RenderedResult, encoding: _encoding, errors: null)); } catch (Exception ex) { return(new SingleTemplateRenderResults( configuration: configuration, status: TemplateRenderResultStatus.Failure, renderedResult: null, encoding: _encoding, errors: new[] { new RazorTemplateError(RazorTemplateErrorCodes.GeneralRazorTemplateError, $"{ex.GetType().Name}: {ex.Message}") })); } }