Beispiel #1
0
        //
        // public interface
        //

        #region public void BindComponent(object sender, PDFDataBindEventArgs args)

        /// <summary>
        /// Main method that sets the value of the senders property to the result of this instances XPath expression.
        /// Using the current data context.
        ///  Signature can be attached to an IPDFBindableComponent DataBound or DataBinding events.
        /// </summary>
        /// <param name="sender">The instance that raised the event</param>
        /// <param name="args"></param>
        public void BindComponent(object sender, PDFDataBindEventArgs args)
        {
            if (null == sender)
            {
                throw new ArgumentNullException("sender");
            }
            if (null == args)
            {
                throw new ArgumentNullException("args");
            }
            if (null == args.Context)
            {
                throw new ArgumentNullException("args.Context");
            }

            PDFDataStack stack = args.Context.DataStack;

            IPDFDataSource src = stack.Source;

            //Pop the current one so we can get the next one up (for count and position operations)
            object data = stack.Pop();

            try
            {
                DoBindComponent(sender, data, args.Context);
            }
            catch (Exception ex)
            {
                if (args.Context.Conformance == ParserConformanceMode.Lax)
                {
                    args.Context.TraceLog.Add(TraceLevel.Error, "Item Binding", "Could not Set property '" + this.Property.Name + "': " + ex.Message, ex);
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                //Put the data we popped back on
                args.Context.DataStack.Push(data, src);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="container"></param>
        /// <param name="containerposition"></param>
        /// <param name="template"></param>
        /// <param name="context"></param>
        protected override void DoBindDataIntoContainer(IPDFContainerComponent container, int containerposition, PDFDataContext context)
        {
            int          prevcount = context.CurrentIndex;
            PDFDataStack stack     = context.DataStack;

            object         origData = stack.HasData ? stack.Current : null;
            IPDFDataSource source   = stack.HasData ? stack.Source : null;

            foreach (WithField fld in this.Fields)
            {
                this.BindingActions.Add(origData, source, fld);
            }

            if (this.AutoBindContent != DataAutoBindContent.None)
            {
                this.AddAutoBindFields(origData, source, context);
            }

            this.BindActionedComponents(this.BindingActions, context);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="container"></param>
        /// <param name="containerposition"></param>
        /// <param name="template"></param>
        /// <param name="context"></param>
        protected virtual void DoBindDataIntoContainer(IPDFContainerComponent container, int containerposition, PDFDataContext context)
        {
            int          prevcount = context.CurrentIndex;
            PDFDataStack stack     = context.DataStack;

            object         data   = stack.HasData ? context.DataStack.Current : null;
            IPDFDataSource source = stack.HasData ? context.DataStack.Source : null;

            int         count      = 0;
            int         added      = 0;
            IEnumerator enumerator = null;

            if (CanEnumerate(data))
            {
                if (context.ShouldLogDebug)
                {
                    context.TraceLog.Begin(TraceLevel.Verbose, "Binding Template", "Starting to bind enumerable data into container " + this.ID);
                }


                IEnumerable ienum = data as IEnumerable;
                enumerator = this.CreateEnumerator(ienum);
                while (enumerator.MoveNext())
                {
                    context.CurrentIndex = count;
                    context.DataStack.Push(enumerator.Current, source);
                    int number = 0;

                    IPDFTemplate template = this.GetTemplateForBinding(context, count, added + containerposition);
                    if (null != template)
                    {
                        number = InstantiateAndAddWithTemplate(template, count, added + containerposition, container, context);
                    }

                    context.DataStack.Pop();


                    if (context.ShouldLogDebug)
                    {
                        context.TraceLog.Add(TraceLevel.Debug, "Binding Template", "Bound data into template index : " + count + " and added " + number + " components");
                    }

                    count++;
                    added += number;
                }
                if (count == 0)
                {
                    context.TraceLog.Add(TraceLevel.Message, "Binding Template", "ZERO items were data bound with the Binding Template Component as MoveNext returned false from the start");
                }

                if (context.ShouldLogDebug)
                {
                    context.TraceLog.End(TraceLevel.Verbose, "Binding Template", "Completed binding enumerable data into Binding Template Component, " + added + " components added, with " + count + " enumerable data items");
                }
                else if (context.ShouldLogVerbose)
                {
                    context.TraceLog.Add(TraceLevel.Verbose, "Binding Template", "Completed binding enumerable data into Binding Template Component, " + added + " components added, with " + count + " enumerable data items");
                }
            }
            else if (data != null)
            {
                if (context.ShouldLogDebug)
                {
                    context.TraceLog.Begin(TraceLevel.Verbose, "Binding Template", "Starting to bind single data into Binding Template Component");
                }

                context.CurrentIndex = 1;
                context.DataStack.Push(data, source);
                IPDFTemplate template = this.GetTemplateForBinding(context, count, added + containerposition);
                if (null != template)
                {
                    added += InstantiateAndAddWithTemplate(template, count, added + containerposition, container, context);
                }
                context.DataStack.Pop();

                if (context.ShouldLogDebug)
                {
                    context.TraceLog.End(TraceLevel.Verbose, "Binding Template", "Completed binding single data into the Binding Template Component, " + added + " components added.");
                }
                else if (context.ShouldLogVerbose)
                {
                    context.TraceLog.Add(TraceLevel.Verbose, "Binding Template", "Completed binding single data into the Binding Template Component, " + added + " components added.");
                }
            }
            else
            {
                if (context.ShouldLogDebug)
                {
                    context.TraceLog.Begin(TraceLevel.Verbose, "Binding Template", "Starting to bind into Binding Template Component with NO context data");
                }

                context.CurrentIndex = 1;
                IPDFTemplate template = this.GetTemplateForBinding(context, count, added + containerposition);
                if (null != template)
                {
                    added += InstantiateAndAddWithTemplate(template, count, added + containerposition, container, context);
                }

                if (context.ShouldLogDebug)
                {
                    context.TraceLog.End(TraceLevel.Verbose, "Binding Template", "Completed binding the Binding Template Component with NO data, " + added + " components added.");
                }
                else if (context.ShouldLogVerbose)
                {
                    context.TraceLog.Add(TraceLevel.Verbose, "Binding Template", "Completed binding the Binding Template Component with NO data, " + added + " components added.");
                }
            }

            context.CurrentIndex = prevcount;
        }