예제 #1
0
        private static PackageItem IteratePackages(Package pkg)
        {
            PackageItem      pkgItem     = new PackageItem();
            Master           master      = null;
            PackagePartItem  pkgPartItem = null;
            List <MasterMap> masterMaps  = new List <MasterMap>();
            Page             page        = null;

            if (pkg == null)
            {
                return(pkgItem);
            }

            foreach (PackagePart pkgPart in pkg.GetParts())
            {
                pkgPartItem = new PackagePartItem();
                Console.WriteLine("=================================================================================");
                Console.WriteLine("Package part URI : {0}", pkgPart.Uri);
                Console.WriteLine("Content Type : {0}", pkgPart.ContentType.ToString());
                pkgPartItem.Uri         = pkgPart.Uri.ToString();
                pkgPartItem.ContentType = pkgPart.ContentType;
                pkgItem.PackagePartItems.Add(pkgPartItem);
                try
                {
                    if (pkgPart.ContentType.IndexOf("application/vnd.ms-visio.masters+xml") > -1) //this is page type content type
                    {
                        //masterMap = getMasterMap(pkgPart);
                        masterMaps = getMasterMaps(pkgPart, pkgItem);
                        if (masterMaps != null)
                        {
                            pkgItem.MasterMaps = masterMaps;
                        }
                    }
                    else if (pkgPart.ContentType.IndexOf("application/vnd.ms-visio.master+xml") > -1) //this is page type content type
                    {
                        master = getMaster(pkgPart, pkgItem);
                        if (master != null)
                        {
                            pkgItem.Masters.Add(master);
                        }
                    }
                    else if (pkgPart.ContentType.IndexOf("application/vnd.ms-visio.page+xml") > -1) //this is page type content type
                    {
                        page = getPages(pkgPart, pkgItem);
                        if (page != null)
                        {
                            pkgItem.Pages.Add(page);
                        }
                    }

                    Console.WriteLine("--------------------------- Relationships ---------------------------------------");

                    foreach (var pkgRelationship in pkgPart.GetRelationships())
                    {
                        Console.WriteLine("ID: {0} | Source : {1} -> Target : {2} | Relationship Type : {3}",
                                          pkgRelationship.Id, pkgRelationship.SourceUri, pkgRelationship.TargetUri, pkgRelationship.RelationshipType);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error : " + ex.Message);
                }
                finally
                {
                }
                Console.WriteLine("=================================================================================");
            }

            BuildNodeRelationship(pkgItem);

            Console.WriteLine("End of operation");

            return(pkgItem);
        }
예제 #2
0
        private static Page getPages(PackagePart pkgPart, PackageItem pkgItem)
        {
            Page page = new Page {
                Uri = pkgPart.Uri.ToString(), ContentType = pkgPart.ContentType
            };

            PackagePartItem packagePartItem    = null;
            XDocument       packagePartContent = null;

            Console.WriteLine("--------------------------- Elements ---------------------------------------");
            packagePartContent = getPckagePartContent(pkgPart);
            if (packagePartContent != null)
            {
                //packagePartItem = new PackagePartItem { Uri = pkgPart.Uri.ToString(), ContentType = pkgPart.ContentType };

                XmlReader           reader           = packagePartContent.CreateReader();
                XmlNamespaceManager namespaceManager = new XmlNamespaceManager(reader.NameTable);
                namespaceManager.AddNamespace("aw", "http://schemas.microsoft.com/office/visio/2012/main");
                namespaceManager.AddNamespace("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");

                foreach (var element in packagePartContent.Descendants().Where(el => el.Name.LocalName.Equals("Shape")))
                {
                    try
                    {
                        if (element.Attribute("Name") != null && element.Attribute("Master") != null)
                        {
                            packagePartItem = new PackagePartItem {
                                ContentType = pkgPart.ContentType
                            };
                            Console.WriteLine("\tElement Id: {0} | Type: {1} | Element [Name]: {2} | Master: {3}",
                                              element.Attribute("ID").Value, element.Attribute("Type").Value,
                                              element.Attribute("Name").Value, element.Attribute("Master").Value);

                            packagePartItem.ID     = element.Attribute("ID").Value;
                            packagePartItem.Type   = element.Attribute("Type").Value;
                            packagePartItem.Name   = element.Attribute("Name").Value;
                            packagePartItem.Master = element.Attribute("Master").Value;

                            MasterMap masterMap = pkgItem.MasterMaps.Where(mm => mm.Id == packagePartItem.Master).FirstOrDefault();
                            if (masterMap != null)
                            {
                                packagePartItem.MasterItem = pkgItem.Masters.FirstOrDefault(m => m.RefId == masterMap.RefId);
                            }

                            packagePartItem.Uri = string.Format("/visio/masters/master{0}.xml", packagePartItem.Master);

                            //packagePartItem.Properties.Add("ID", element.Attribute("ID").Value);
                            //packagePartItem.Properties.Add("Type", element.Attribute("Type").Value);
                            //packagePartItem.Properties.Add("Name", element.Attribute("Name").Value);
                            //packagePartItem.Properties.Add("Master", element.Attribute("Master").Value);

                            //http://schemas.microsoft.com/office/visio/2012/main
                            var selectedElements = element.XPathSelectElements("./aw:Section[@N='Property']/aw:Row/aw:Cell", namespaceManager);
                            //int indx = 0;
                            string parkedValue = null;
                            foreach (var selectedElement in selectedElements)
                            {
                                bool isKeyFound = true;
                                if (selectedElement.Attribute("N") != null &&
                                    (selectedElement.Attribute("N").Value.Equals("Value") ||
                                     selectedElement.Attribute("N").Value.Equals("Label")))
                                {
                                    string key = selectedElement.Parent.FirstAttribute.Value;
                                    if (packagePartItem.MasterItem != null && packagePartItem.MasterItem.Properties.ContainsKey(key))
                                    {
                                        key = packagePartItem.MasterItem.Properties[key];
                                    }
                                    //key += "_" + packagePartItem.MasterItem.Properties[key];
                                    else
                                    {
                                        isKeyFound = false;
                                        Console.WriteLine("Key Not found " + key);

                                        if (selectedElement.Attribute("N").Value.Equals("Label"))
                                        {
                                            key        = selectedElement.Attribute("V").Value;
                                            isKeyFound = true;
                                        }
                                    }

                                    if (selectedElement.Attribute("V") != null)
                                    {
                                        Console.WriteLine("\t\tCell Value : {0}", selectedElement.Attribute("V").Value);

                                        if (selectedElement.Attribute("N").Value.Equals("Value"))
                                        {
                                            parkedValue = selectedElement.Attribute("V").Value.Replace("\n", "");
                                        }

                                        if (isKeyFound && parkedValue != null)
                                        {
                                            if (!packagePartItem.Properties.ContainsKey(key))
                                            {
                                                packagePartItem.Properties.Add(key, parkedValue);
                                            }
                                            parkedValue = null;
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("\t\tCell Value : {0}", selectedElement.Value);
                                        if (selectedElement.Attribute("N").Value.Equals("Value"))
                                        {
                                            parkedValue = selectedElement.Value.Replace("\n", "");
                                        }

                                        if (isKeyFound && parkedValue != null)
                                        {
                                            if (!packagePartItem.Properties.ContainsKey(key))
                                            {
                                                packagePartItem.Properties.Add(key, parkedValue);
                                            }
                                            parkedValue = null;
                                        }
                                    }
                                }
                            }

                            page.PackagePartItems.Add(packagePartItem);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            Console.WriteLine("--------------------------- End Of Elements ---------------------------------------");

            return(page);
        }