public static bool TryGetBinder(string prefix, out IPDFBindingExpressionFactory binder)
        {
            GenerationConfigurationSection section = GeneratorSection;

            if (null == section)
            {
                binder = null;
                return(false);
            }

            BindingFactoryElementCollection col = section.ExpressionBinders;
            BindingFactoryElement           ele = null;

            if (col == null || col.Count == 0)
            {
                binder = null;
                return(false);
            }
            else if (col.TryGetBindingElement(prefix, out ele))
            {
                binder = ele.GetFactory();
                return(null != binder);
            }
            else
            {
                binder = null;
                return(false);
            }
        }
예제 #2
0
        /// <summary>
        /// Returns true if the value provided is an expression that matches the pattern required for a binding expression.
        /// If it does value will be modified to be just the content of that expression and the bindingFactory will be set to the
        /// factory that handles these expression types.
        /// </summary>
        /// <param name="value">The string to check if it is a binding expression. If it does match, then it will be modifed </param>
        /// <param name="bindingfactory"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static bool IsBindingExpression(ref string value, out IPDFBindingExpressionFactory bindingfactory, PDFGeneratorSettings settings = null)
        {
            bindingfactory = null;
            if (string.IsNullOrEmpty(value) || value.Length < 4)
            {
                return(false);
            }
            else if (!(value.StartsWith(BindingStartChar) && value.EndsWith(BindingEndChar)))
            {
                return(false);
            }
            else if (value.StartsWith(BindingStartEscape))
            {
                value = BindingStartChar + value.Substring(BindingStartEscape.Length);
                return(false);
            }
            else
            {
                int separatorIndex = value.IndexOf(BindingKeySeparator);
                if (separatorIndex < 0)
                {
                    return(false);
                }

                string factoryKey = value.Substring(BindingStartChar.Length, separatorIndex - BindingStartChar.Length);

                //ensure the factories are initialized (and will raise an appropriate exception that can be handled if not.
                //doesn't need to be thread safe as it will simply reset the collection.
                if (null == _configFactories)
                {
                    _configFactories = InitFactories();
                }

                if (_configFactories.TryGetValue(factoryKey, out bindingfactory))
                {
                    int offset = BindingStartChar.Length + factoryKey.Length + BindingKeySeparator.Length;
                    int len    = value.Length - (offset + 1);
                    value = value.Substring(offset, len);
                    return(true);
                }
                else if (null != settings)
                {
                    settings.TraceLog.Add(TraceLevel.Warning, "XML Parser", string.Format(Errors.BindingPrefixIsNotKnown, factoryKey, value));
                    return(false);
                }
                else
                {
                    return(false);
                }
            }
        }
예제 #3
0
 /// <summary>
 /// Returns an instance of the IPDFBindingExpressionFactory that can return delegate event handlers for binding expressions.
 /// </summary>
 /// <returns></returns>
 public virtual IPDFBindingExpressionFactory GetFactory()
 {
     if (null == this._factory)
     {
         try
         {
             Type   t        = Support.GetTypeFromName(this.FactoryType);
             object instance = System.Activator.CreateInstance(t);
             IPDFBindingExpressionFactory factory = (IPDFBindingExpressionFactory)instance;
             _factory = factory;
         }
         catch (Exception ex)
         {
             throw new System.Configuration.ConfigurationErrorsException(string.Format("Could not create an instace of the type '{0}' : {1}", this.FactoryType, ex.Message), ex);
         }
     }
     return(_factory);
 }
예제 #4
0
        /// <summary>
        /// Initializes and returns a dictionary of the standard and
        /// configured binding expression factories based on the prefix
        /// </summary>
        /// <returns></returns>
        private static Dictionary <string, IPDFBindingExpressionFactory> InitFactories()
        {
            Dictionary <string, IPDFBindingExpressionFactory> factories = new Dictionary <string, IPDFBindingExpressionFactory>();

            //let's be thread safe here just so we can be sure
            lock (_configlock)
            {
                //add the standard factories
                factories[ItemBindingKey] = new BindingItemExpressionFactory();
                //factories[QueryStringBindingKey] = new BindingQueryStringExpressionFactory();
                factories[XPathBindingKey] = new BindingXPathExpressionFactory();

                try
                {
                    var service = ServiceProvider.GetService <IScryberConfigurationService>();
                    var config  = service.ParsingOptions.Bindings;
                    if (null != config && config.Count > 0)
                    {
                        foreach (var ele in config)
                        {
                            IPDFBindingExpressionFactory factory = ele.GetFactory();
                            string key = ele.Prefix;

                            //By using the set accessor this will overwrite any of the standard factories previously added.
                            factories[key] = factory;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new System.Exception("Failed to initialize the configured binding expression factories :" + ex.Message, ex);
                }
            }

            return(factories);
        }
예제 #5
0
        /// <summary>
        /// Returns true if the value provided is an expression that matches the pattern required for a binding expression.
        /// If it does value will be modified to be just the content of that expression and the bindingFactory will be set to the
        /// factory that handles these expression types.
        /// </summary>
        /// <param name="value">The string to check if it is a binding expression. If it does match, then it will be modifed </param>
        /// <param name="bindingfactory"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static bool IsBindingExpression(ref string value, out IPDFBindingExpressionFactory bindingfactory, PDFGeneratorSettings settings = null)
        {
            bindingfactory = null;

            if (!value.Contains(BindingStartChar))
            {
                return(false);
            }
            else if (value.StartsWith(BindingStartChar) && value.EndsWith(BindingEndChar))
            {
                return(TryGetBindingExpression(ref value, out bindingfactory, settings));
            }
            else if (value.Contains(BindingDoubleSeparator))
            {
                var match = handlebarsMatcher.Match(value);
                if (match == null || match.Success == false)
                {
                    return(false);
                }

                if (null == _configFactories)
                {
                    _configFactories = InitFactories();
                }

                if (!_configFactories.TryGetValue(HandlebarsExpressionBindingKey, out bindingfactory))
                {
                    if (null != settings)
                    {
                        settings.TraceLog.Add(TraceLevel.Error, "Binding", "Handlebars expression factorys has not been initialized with the key " + HandlebarsExpressionBindingKey);
                    }
                    return(false);
                }

                var           pos = 0;
                StringBuilder sb  = new StringBuilder();

                var quoteChar = "\"";

                while (match != null && match.Success)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(", ");
                    }
                    else
                    {
                        sb.Append("concat(");
                    }

                    string sub;

                    if (match.Index > pos)
                    {
                        sub = value.Substring(pos, match.Index - pos);
                        sb.Append(quoteChar);
                        sb.Append(sub);
                        sb.Append(quoteChar);
                        sb.Append(", ");
                    }
                    sub = match.Value;
                    sb.Append(sub.Substring(HandlebarsExpressionInset, sub.Length - HandlebarsExpressionLength));

                    pos = match.Index + match.Value.Length;

                    match = match.NextMatch();
                }

                if (pos < value.Length)
                {
                    sb.Append(", ");
                    sb.Append(quoteChar);
                    sb.Append(value.Substring(pos));
                    sb.Append(quoteChar);
                }

                sb.Append(")");
                value = sb.ToString();
                return(true);
            }
            else
            {
                return(false);
            }
        }