protected IPDFDataSource GetDataSourceComponent(string datasourceComponentID, PDFDataContext context) { IPDFDataSource datasourceComponent = null; if (string.IsNullOrEmpty(datasourceComponentID)) { throw new ArgumentNullException("datasourceComponentID"); } Component found = base.FindDocumentComponentById(datasourceComponentID); if (found == null) { throw RecordAndRaise.ArgumentNull("DataSourceID", Errors.CouldNotFindControlWithID, datasourceComponentID); } else if (!(found is IPDFDataSource)) { throw RecordAndRaise.Argument("DataSourceID", Errors.AssignedDataSourceIsNotIPDFDataSource, datasourceComponentID); } else { datasourceComponent = ((IPDFDataSource)found); } return(datasourceComponent); }
/// <summary> /// Pushes a new object onto the data stack /// </summary> /// <param name="data"></param> public void Push(object data, IPDFDataSource source) { //if (null == source) // throw new ArgumentNullException("source"); this.stack.Push(data); this.sources.Push(source); }
/// <summary> /// If this component has a specified datasourceId then the component will be loaded and set to the out parameter. The method will then return true. /// If this component does not have a datasourceId then method will return false. /// </summary> /// <param name="context"></param> /// <param name="datasourceComponent"></param> /// <returns></returns> protected bool HasAssignedDataSourceComponent(PDFDataContext context, out IPDFDataSource datasourceComponent) { if (string.IsNullOrEmpty(this.DataSourceID) == false) { datasourceComponent = GetDataSourceComponent(this.DataSourceID, context); return(null != datasourceComponent); } else { datasourceComponent = null; return(false); } }
/// <summary> /// Returns the required binding data based upon the specified parameters or null if there is no required source. /// </summary> /// <param name="datasourceComponent">The Component in the document that implements the IPDFDataSource interface. /// Set it to null to ignore this parameter. /// If it is set, and so is the select path, then this path will be used /// by the IPDFDataSource to extract the required source. /// </param> /// <param name="datasourcevalue">The value of the data source. /// If not null then the value will be returned unless a select path is set. /// If the select path is set then this value must be IXPathNavigable and the returned value will be the result of a select on the navigator</param> /// <param name="stack">If neither the Component or value are set, then the current data from the stack will be /// used (if and only if theres is a select path). If there is no select path then null will be returned. /// If there is a select path then the current data must implement IXPathNavigable</param> /// <param name="selectpath">The path to use to extract values</param> /// <returns>The required data or null.</returns> public static object GetBindingData(IPDFDataSource datasourceComponent, object datasourcevalue, string selectpath, PDFDataContext context) { throw new NotSupportedException("No Longer using the XPathDataHelper"); //object data = null; //try //{ // if (null != datasourceComponent) // { // data = datasourceComponent.Select(selectpath, context); // } // else if (datasourcevalue != null) // { // if (!string.IsNullOrEmpty(selectpath)) // { // if (datasourcevalue is System.Xml.XPath.IXPathNavigable) // { // System.Xml.XPath.XPathNavigator nav = ((System.Xml.XPath.IXPathNavigable)data).CreateNavigator(); // System.Xml.XPath.XPathNodeIterator itter = nav.Select(selectpath, context.NamespaceResolver); // data = itter; // } // else // new ArgumentException(Errors.DatabindingSourceNotXPath, "selectpath"); // } // else // data = datasourcevalue; // } // else if (!string.IsNullOrEmpty(selectpath)) // { // data = context.DataStack.Current; // if (null == data || !(data is System.Xml.XPath.IXPathNavigable)) // new ArgumentException(Errors.DatabindingSourceNotXPath, "stack.Current"); // data = ((System.Xml.XPath.IXPathNavigable)data).CreateNavigator().Select(selectpath, context.NamespaceResolver); // } //} //catch (Exception ex) //{ // throw new PDFException("Could not get the binding data for expression '" + selectpath + "'", ex); //} //return data; }
// // 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); }
protected virtual void AddAutoBindColumns(PDFDataContext context) { if (string.IsNullOrEmpty(this.DataSourceID)) { throw new InvalidOperationException("Can only auto bind the schema when the DataGrid has an explicit DataSourceID and the referencing source supports Schema derriving"); } IPDFDataSource found = base.FindDocumentComponentById(this.DataSourceID) as IPDFDataSource; if (null == found || found.SupportsDataSchema == false) { throw new InvalidOperationException("Can only auto bind the schema when the DataGrid has an explicit DataSourceID and the referencing source supports Schema derriving"); } PDFDataSchema schema = found.GetDataSchema(this.SelectPath, context); if (null == schema || schema.Items == null || schema.Items.Count == 0) { context.TraceLog.Add(TraceLevel.Warning, "PDFDataGrid", string.Format("Cannot autobind the columns as no schema items were returned for the path '{0}'", this.SelectPath)); } else { foreach (PDFDataItem item in schema.Items) { if (ShouldIncludeAutoBoundItem(item)) { DataGridColumn col = GetDataColumnForType(item.DataType); if (null != col) { col.HeaderText = string.IsNullOrEmpty(item.Title) ? item.Name : item.Title; col.SetDataSourceBindingItem(item, context); if (context.TraceLog.ShouldLog(TraceLevel.Debug)) { context.TraceLog.Add(TraceLevel.Debug, "PDFDataGrid", string.Format("The data column was automatically created for the schdema item '{0}' in data grid '{1}'", item, this)); } this.Columns.Add(col); } } } } }
protected virtual bool EvaluateTestExpression(string expr, IPDFDataSource source, object data, PDFDataContext context) { return(source.EvaluateTestExpression(expr, data, context)); }
protected virtual System.Xml.Xsl.XslCompiledTransform DoGetTransformer(int cacheduration, IPDFDataSource source, PDFDataContext context) { if (null == _transformer) { string path = this.XSLTPath; if (string.IsNullOrEmpty(path)) { throw new NullReferenceException(string.Format(Errors.XSLTPathOrTransformerNotSetOnInstance, source.ID)); } path = source.MapPath(path); IPDFCacheProvider cache = ((Scryber.Components.Document)source.Document).CacheProvider; System.Xml.Xsl.XslCompiledTransform transformer; object found; if (cacheduration > 0 && cache.TryRetrieveFromCache(XSLTCacheType, path, out found)) { transformer = (System.Xml.Xsl.XslCompiledTransform)found; } else { transformer = new System.Xml.Xsl.XslCompiledTransform(XSLTUseDebug); try { transformer.Load(path); } catch (Exception ex) { throw new PDFDataException(string.Format(Errors.XSLTCouldNotBeLoadedFromPath, path), ex); } if (cacheduration > 0) { cache.AddToCache(XSLTCacheType, path, transformer, new TimeSpan(0, cacheduration, 0)); } } _transformer = transformer; } return(_transformer); }
// // .ctor // public System.Xml.XPath.XPathNavigator TransformData(System.Xml.XPath.XPathNavigator nav, int cacheduration, IPDFDataSource source, PDFDataContext context) { //Check we have something to use for a transformation. if (string.IsNullOrEmpty(this.XSLTPath) && null == this.Transformer) { return(nav); } System.Xml.XmlWriter xmlWriter = null; System.IO.MemoryStream memoryStream = null; System.IO.StreamWriter streamWriter = null; System.Xml.XmlDocument result = null; System.Xml.XmlDocument output = new System.Xml.XmlDocument(); try { System.Xml.Xsl.XslCompiledTransform trans = this.DoGetTransformer(cacheduration, source, context); System.Xml.Xsl.XsltArgumentList args = this.DoGetArguments(context); memoryStream = new System.IO.MemoryStream(); streamWriter = new System.IO.StreamWriter(memoryStream, Encoding.UTF8); System.Xml.XmlWriterSettings writerSettings = CreateWriterSettings(); xmlWriter = System.Xml.XmlWriter.Create(streamWriter, writerSettings); trans.Transform(nav, args, xmlWriter); xmlWriter.Flush(); streamWriter.Flush(); result = new System.Xml.XmlDocument(); memoryStream.Position = 0; result.Load(memoryStream); } catch (Exception ex) { throw new PDFDataException(Errors.CouldNotTransformInputData, ex); } finally { if (null != xmlWriter) { xmlWriter.Close(); } if (null != streamWriter) { streamWriter.Dispose(); } if (null != memoryStream) { memoryStream.Dispose(); } } return(result.CreateNavigator()); }
protected override void DoBindDataIntoContainer(IPDFContainerComponent container, int containerposition, PDFDataContext context) { IPDFDataSource origSource = context.DataStack.Source; object origData = context.DataStack.Pop(); object parentData = null; if (context.DataStack.HasData) { parentData = context.DataStack.Current; } Style applied = this.GetAppliedStyle(); applied = applied.Flatten(); IEnumerator enumerator = this.GetDataEnumerator(origData); if (this.AutoBindContent != DataAutoBindContent.None) { this.AddAutoBindColumns(context); } TableGrid grid = new TableGrid(); container.Content.Add(grid); if (this.HasStyle && this.Style.HasValues) { this.Style.MergeInto(grid.Style, Style.DirectStylePriority); } if (!string.IsNullOrEmpty(this.StyleClass)) { grid.StyleClass = this.StyleClass; } if (this.HasOutline) { grid.Outline = this.Outline; } this.ApplyCellStyles(); int colindex = 0; int rowindex = -1; this._bindingActions = new List <BindingActionList>(); if (this.ShouldBuildHeader()) { TableHeaderRow header = new TableHeaderRow(); grid.Rows.Add(header); BindingActionList headBind = new BindingActionList(); this.BindingActions.Add(headBind); //Add the columns foreach (DataGridColumn column in this.Columns) { if (column.Visible == false) { continue; } Component comp = column.DoBuildHeaderCell(grid, header, rowindex, colindex, context); if (null != comp && null != parentData) { headBind.Add(new BindingAction(parentData, origSource, comp)); } colindex++; } rowindex++; } while (enumerator.MoveNext()) { colindex = 0; TableRow row = new TableRow(); grid.Rows.Add(row); BindingActionList rowBind = new BindingActionList(); this.BindingActions.Add(rowBind); foreach (DataGridColumn column in this.Columns) { if (column.Visible == false) { continue; } Component comp = column.DoBuildItemCell(grid, row, rowindex, colindex, context); if (null != comp) { rowBind.Add(new BindingAction(enumerator.Current, origSource, comp)); } colindex++; } rowindex++; } colindex = 0; if (this.ShouldBuildFooter()) { TableFooterRow footer = new TableFooterRow(); grid.Rows.Add(footer); BindingActionList footBind = new BindingActionList(); this.BindingActions.Add(footBind); foreach (DataGridColumn column in this.Columns) { if (column.Visible == false) { continue; } Component comp = column.DoBuildFooterCell(grid, footer, rowindex, colindex, context); if (null != comp && null != parentData) { footBind.Add(new BindingAction(parentData, origSource, comp)); } colindex++; } } context.DataStack.Push(origData, origSource); this.InitAndLoadRoot(grid, context); this.BindActionedComponents(this.BindingActions, context); bool allhidden = RemoveHiddenColumns(context); if (allhidden || grid.Rows.Count == 0) { grid.Visible = false; this.ShowEmptyTemplate(container, containerposition, context); } this.RemoveHeadersAndFooters(grid, context); _built = grid; }
public void Add(object data, IPDFDataSource source, IPDFBindableComponent comp) { BindingAction action = new BindingAction(data, source, comp); this.Add(action); }
public BindingAction(object data, IPDFDataSource source, IPDFBindableComponent comp) { this.Data = data; this.Component = comp; this.Source = source; }
/// <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; }
protected virtual void AddAutoBindFields(object data, IPDFDataSource source, PDFDataContext context) { if (string.IsNullOrEmpty(this.DataSourceID)) { throw new InvalidOperationException("Can only auto bind the schema when the With has an explicit DataSourceID and the referencing source supports Schema derriving"); } IPDFDataSource found = base.FindDocumentComponentById(this.DataSourceID) as IPDFDataSource; if (null == found || found.SupportsDataSchema == false) { throw new InvalidOperationException("Can only auto bind the schema when the With has an explicit DataSourceID and the referencing source supports Schema derriving"); } PDFDataSchema schema = found.GetDataSchema(this.SelectPath, context); if (null == schema || schema.Items == null || schema.Items.Count == 0) { context.TraceLog.Add(TraceLevel.Warning, "PDFWithFieldSet", string.Format("Cannot autobind the columns as no schema items were returned for the path '{0}'", this.SelectPath)); } else { if (context.TraceLog.ShouldLog(TraceLevel.Debug)) { context.TraceLog.Add(TraceLevel.Debug, "DataGrid", "Initializing and loading root component before binding"); } PDFTraceLog log = context.TraceLog; PDFInitContext init = new PDFInitContext(context.Items, log, context.PerformanceMonitor, this.Document); PDFLoadContext load = new PDFLoadContext(context.Items, log, context.PerformanceMonitor, this.Document); foreach (PDFDataItem item in schema.Items) { if (ShouldIncludeAutoBoundItem(item)) { WithBoundField field = GetFieldForType(item.DataType); if (null != field) { field.StyleClass = this.FieldClass; field.FieldLabel = string.IsNullOrEmpty(item.Title) ? item.Name : item.Title; field.LabelClass = this.LabelClass; field.ValueClass = this.ValueClass; field.LabelPostFix = this.LabelPostFix; field.LayoutType = this.AutoLayoutType; field.DataType = item.DataType; field.HideIfEmpty = this.HideEmptyFields; field.SetDataSourceBindingItem(item, context); if (context.TraceLog.ShouldLog(TraceLevel.Debug)) { context.TraceLog.Add(TraceLevel.Debug, "PDFWithFieldSet", string.Format("The data field was automatically created for the schdema item '{0}' in set '{1}'", item, this)); } this.Fields.Add(field); field.Init(init); field.Load(load); this.BindingActions.Add(new BindingAction(data, source, field)); } } } //added all the items } }