Exemple #1
0
        static void PrintTransformPlug(ITransformPlug plug)
        {
            IList <ITransformGroup> groups = plug.Facets;

            foreach (ITransformGroup group in groups)
            {
                Console.WriteLine("Group name = " + group.Name);

                Console.WriteLine("\nPrinting links...");
                foreach (ITransformLink link in group.Links)
                {
                    //Console.WriteLine(link.Name + " " + link.SourceReferenceName + " " + link.TargetReferenceName + " " + link.SourceReferenceType + " " + link.TargetReferenceType);
                    Console.WriteLine("\t{0} - {1} => {2}", link.Name, link.Source, link.Target);
                }

                Console.WriteLine("\nPrinting formulas...");
                foreach (IFormula formula in group.Formulas)
                {
                    Console.WriteLine(formula.Name + " " + formula.Description + " " + formula.FormulaType);
                    foreach (IParameter par in formula.Parameters)
                    {
                        Console.WriteLine("\tParameter: " + par.Reference.Name + " " + par.Reference.ReferenceType);
                    }
                }
            }
        }
        //Make this function resilience to nulls to allow Plug to be created
        // even when partial information is available
        public static byte[] GenerateExcelFromTransformPlug(ITransformPlug transformPlug)
        {
            ExcelPackage pck = new ExcelPackage();

            if (transformPlug.SourceDocument != null)
            {
                DocumentPlugtoExcel.GenerateWorksheetFromDocumentPlug(pck.Workbook, transformPlug.SourceDocument, "Source#{0}");
            }

            if (transformPlug.TargetDocument != null)
            {
                DocumentPlugtoExcel.GenerateWorksheetFromDocumentPlug(pck.Workbook, transformPlug.TargetDocument, "Target#{0}");
            }

            if (transformPlug.Facets != null)
            {
                string srcName    = transformPlug.SourceDocument != null ? transformPlug.SourceDocument.Name : "Source";
                string targetName = transformPlug.TargetDocument != null ? transformPlug.TargetDocument.Name : "Target";
                GenerateWorksheetFromFacets(pck.Workbook, transformPlug.Facets, srcName, targetName);
                //GenerateFormulaWorksheet(pck.Workbook, transformPlug.Facets);
            }


            byte[] buffer = pck.GetAsByteArray();

            return(buffer);
        }
        public static void WriteTransformPlugToHttp(ITransformPlug transformPlug, HttpResponseBase response)
        {
            byte[] b = GenerateExcelFromTransformPlug(transformPlug);
            response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            response.AddHeader("content-disposition", string.Format("attachment;  filename={0}.xlsx", transformPlug.SourceDocument));

            response.OutputStream.Write(b, 0, b.Length);
            response.OutputStream.Close();
        }
 public PlugDefinition(ITransformPlug plug, DateTime start, DateTime end)
 {
     this.transformPlug = plug;
     this.validFrom     = start;
     this.validTo       = end;
 }
        public static string TransformSourceToTargetXml(ITransformPlug transPlug, string inputText)
        {
            mappedElements = new Dictionary <string, string>();

            input = XElement.Parse(inputText);

            inputDoc = XDocument.Parse(inputText);

            plug = transPlug;

            targetXML = null;

            bool flag = false;

            //handleAutoField(flag);

            targetXML = null;

            foreach (var node in input.Descendants())
            {
                flag = false;

                if (!node.HasElements)
                {
                    if (node.PreviousNode == null && (node.Parent.Parent.Parent.Parent == null || node.Parent.Parent.Parent.Parent.Parent == null))
                    {
                        flag = true;
                    }
                    else
                    {
                        if (node.PreviousNode != null)
                        {
                            flag = false;
                        }
                        else
                        {
                            XElement tempNode = node;
                            while (tempNode != null)
                            {
                                if (tempNode.Parent.PreviousNode == null)
                                {
                                    if (tempNode.Parent.Parent.Parent.Parent == null || tempNode.Parent.Parent.Parent.Parent.Parent == null)
                                    {
                                        flag = true;
                                        break;
                                    }
                                    tempNode = tempNode.Parent;
                                }
                                else
                                {
                                    if (tempNode.Parent.Parent.Parent.Parent == null || tempNode.Parent.Parent.Parent.Parent.Parent == null)
                                    {
                                        flag = true;
                                    }
                                    else
                                    {
                                        flag = false;
                                    }
                                    break;
                                }
                            }
                        }
                    }

                    string current = string.Empty;
                    var    nodes   = node.AncestorsAndSelf();
                    foreach (var element in nodes)
                    {
                        current = "->" + element.Name + current;
                    }

                    string mappedElement = GetMapping(current);

                    if (!string.IsNullOrEmpty(mappedElement))
                    {
                        try
                        {
                            mappedElements.Add(current, mappedElement);
                        }
                        catch (ArgumentException e)
                        {
                        }
                        CreateXML(node, flag);

                        //CreateXML(node.Value, mappedElement, flag);
                    }
                }
            }
            //mapFormula();
            //targetXML.Add(parent);
            return(targetXML.ToString());
        }
Exemple #6
0
        public static IFatpipeDocument Transform(IFatpipeDocument sourceDocument, ITransformPlug transformPlug)
        {
            if (sourceDocument == null)
            {
                throw new ArgumentNullException("sourceDocument");
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            FatpipeDocument targetDocument = new FatpipeDocument();

            targetDocument.RootFragment = transformPlug.TargetDocument.RootPluglet.ConstructDocumentFragment(null, null);;

            //TODO: Handle loops
            foreach (ITransformGroup transformGroup in transformPlug.Facets)
            {
                Logger.Debug("DocumentTransformer.Transform", "Processing {0} tranformGroup", transformGroup.Name);

                foreach (ITransformLink transformLink in transformGroup.Links)
                {
                    Logger.Debug("DocumentTransformer.Transform", "Processing Link#-{0}: {1} [{2}] => {3} [{4}]"
                                 , transformLink.Name
                                 , transformLink.Source.ReferenceType, transformLink.Source.Name
                                 , transformLink.Target.ReferenceType, transformLink.Target.Name);

                    //TODO: Handle all kind of transformation
                    if (transformLink.Source.ReferenceType == ReferenceType.Document &&
                        transformLink.Target.ReferenceType == ReferenceType.Document)
                    {
                        // Traverse source path
                        IDocumentFragment sourceFragment = sourceDocument.RootFragment.MoveTo(transformLink.Source.Name);
                        if (sourceFragment != null)
                        {
                            IPluglet targetPluglet = targetDocument.RootFragment.Pluglet.MoveTo(transformLink.Target.Name);

                            if (targetPluglet != null)
                            {
                                Logger.Debug("DocumentTransformer.Transform", "Source = {0}, Target = {1}", sourceFragment.Name, targetPluglet.Tag);

                                Dictionary <string, string> attributes = new Dictionary <string, string>();
                                foreach (IPluglet attr in targetPluglet.Attributes)
                                {
                                    string attributeName = attr.Name;
                                    attributeName.Remove(0, 1);
                                    attributeName.Remove(attributeName.Length, 1);
                                    attributes.Add(attributeName, attributeName);
                                }

                                bool   isAttribute = false;
                                string name        = transformLink.Target.Name.Substring(
                                    transformLink.Target.Name.LastIndexOf(targetPluglet.PathSeperator) + targetPluglet.PathSeperator.Length);
                                // Check if this transformation point to attribute or leaf node
                                if (targetPluglet.Attributes != null && attributes.ContainsKey(name))
                                {
                                    isAttribute = true;
                                }

                                if (isAttribute == false)
                                {
                                    IDocumentFragment newFragment = targetPluglet.ConstructDocumentFragment(null, sourceFragment.Value);
                                    ((DocumentFragment)targetDocument.RootFragment).AddDocumentFragment(newFragment);
                                }
                                else
                                {
                                    ((DocumentFragment)targetDocument.RootFragment).AddDocumentFragment(transformLink.Target.Name, sourceFragment.Value);
                                }
                            }
                            else
                            {
                                string error = string.Format("Link#-{0}: {1} path not found in target tree", transformLink.Name, transformLink.Target.Name);
                                if (targetDocument.Errors == null)
                                {
                                    targetDocument.Errors = new List <string>();
                                }
                                targetDocument.Errors.Add(error);

                                Logger.Error("DocumentTransformer.Transform", EventId.DocTransformerNoMapping, error);
                            }
                        }
                    }
                    else
                    {
                        Logger.Debug("DocumentTransformer.Transform", "Ignoring Link#-{0}", transformLink.Name);
                    }
                }

                //TODO: Handle transformGroup.Formulas
            }

            sw.Stop();
            Logger.Debug("DocumentTransformer.Transform", "Stop. Elapsed time {0} ms", sw.ElapsedMilliseconds);

            return(targetDocument);
        }