コード例 #1
0
ファイル: EPCReader.cs プロジェクト: wdstest/devkit-c
        // read file property in core.xml, find out the creator, datatime...
        // read [content_types].xml, find out the total file contains in the zip.
        // read _rels folder, find out the files relationship, find out the child of objects.
        // .rels contains define the ocreproperties.
        public EPCDataView readEPCFile(String epcPathFile)
        {
            // must has [content_Types].xml
            // /_rels/.rels must contain at least the metadata/core-properties and target. which resqml example doesnt' following.
            // core properties should contains creator, created, version description, id, title
            Dictionary <string, EPCObject> TopObjectList = new Dictionary <string, EPCObject>();
            List <EPCObject> listObjects = new List <EPCObject>();
            EPCCoreProperty  epcCore     = new EPCCoreProperty();

            List <KeyValuePair <String, EPCObject> > parentChild = new List <KeyValuePair <String, EPCObject> >();

            try
            {
                using (ZipPackage package = (ZipPackage)Package.Open(epcPathFile, FileMode.Open, FileAccess.Read))
                {
                    PackageRelationshipCollection packageRels = package.GetRelationships();
                    //package Rels must contains id "CoreProperties" property. getTargetUri directory.
                    String sourceURI = "";
                    String coreURI   = getCorePropertyURI(packageRels, ref sourceURI);
                    PackagePartCollection packageParts = package.GetParts();
                    foreach (PackagePart packagepart in packageParts)
                    {
                        // match the coreURI.
                        // find the coreProperty
                        if ((packagepart.Uri != null) && (packagepart.Uri.Equals(sourceURI + coreURI)))
                        {
                            epcCore.Creator     = packagepart.Package.PackageProperties.Creator;
                            epcCore.Created     = packagepart.Package.PackageProperties.Created;
                            epcCore.Version     = packagepart.Package.PackageProperties.Version;
                            epcCore.Description = packagepart.Package.PackageProperties.Description;
                            epcCore.Identifier  = packagepart.Package.PackageProperties.Identifier;
                            epcCore.Keywords    = packagepart.Package.PackageProperties.Keywords;
                            epcCore.Title       = packagepart.Package.PackageProperties.Title;
                        }
                        else
                        {
                            // create each epcobject for package part
                            if (isPart(packagepart))
                            {
                                EPCObject epcObject      = new EPCObject();
                                String    epcContentType = "";
                                String    version        = "";
                                String    epcObjectType  = "";
                                EPCPartValidator.parseContentType(packagepart.ContentType, ref epcContentType, ref version, ref epcObjectType);
                                epcObject.EpcContentType = epcContentType;
                                epcObject.EPCObjectType  = epcObjectType;
                                epcObject.SchemaVersion  = version;
                                epcObject.PackageFile    = epcPathFile;
                                epcObject.EpcFileName    = this.getfilename(packagepart.Uri.ToString());
                                epcObject.Uuid           = this.getuuid(packagepart.Uri.ToString());
                                epcObject.Uri            = packagepart.Uri;
                                listObjects.Add(epcObject);
                                //get rest relationship items
                                PackageRelationshipCollection partRels = null;
                                try
                                {
                                    if (packagepart != null)
                                    {
                                        partRels = packagepart.GetRelationships();
                                    }
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine("warning : " + e.Message);
                                }
                                if (partRels != null)
                                {
                                    // create pair key for relationships parent and child
                                    foreach (PackageRelationship partsRel in partRels)
                                    {
                                        EPCRelationshipType epcrelType = EPCPartValidator.getRelationshipType(partsRel.RelationshipType);
                                        // conver the target and source into the parent and child table.
                                        if ((epcrelType == EPCRelationshipType.destinationObject) ||
                                            (epcrelType == EPCRelationshipType.mlToExternalPartProxy) ||
                                            (epcrelType == EPCRelationshipType.destinationMedia) ||
                                            (epcrelType == EPCRelationshipType.externalResource) ||
                                            (epcrelType == EPCRelationshipType.chunkedPart)
                                            )
                                        {
                                            string name = getfilename(packagepart.Uri.ToString());
                                            //string childname =  getfilename(partsRel.TargetUri.ToString());
                                            EPCObject obj = new EPCObject();
                                            obj.TargetMode  = partsRel.TargetMode;;
                                            obj.EpcFileName = partsRel.TargetUri.ToString();
                                            if ((obj.TargetMode == TargetMode.External) && (obj.EpcFileName.EndsWith(".h5")))
                                            {
                                                obj.HDF5 = true;
                                                epcObject.EpcFileName = obj.EpcFileName;
                                                epcObject.HDF5        = true;
                                            }
                                            KeyValuePair <string, EPCObject> pair = new KeyValuePair <string, EPCObject>(name, obj);
                                            if (!isDuplicated(parentChild, pair))
                                            {
                                                parentChild.Add(pair);
                                            }
                                            //else
                                            //Console.WriteLine("source already add in. skip ...");
                                        }
                                        if ((epcrelType == EPCRelationshipType.sourceObject) ||
                                            (epcrelType == EPCRelationshipType.extenalPartProxyToMI) ||
                                            (epcrelType == EPCRelationshipType.sourceMedia))
                                        {
                                            string    name      = getfilename(partsRel.TargetUri.ToString());
                                            string    childname = getfilename(packagepart.Uri.ToString());
                                            EPCObject obj       = new EPCObject();
                                            obj.TargetMode  = partsRel.TargetMode;;
                                            obj.EpcFileName = childname;
                                            KeyValuePair <string, EPCObject> pair = new KeyValuePair <string, EPCObject>(name, obj);
                                            if (!isDuplicated(parentChild, pair))
                                            {
                                                parentChild.Add(pair);
                                            }
                                            //else
                                            //Console.WriteLine("source already add in. skip ...");
                                        }
                                    }
                                }
                            }
                        }
                    }

                    //sort the hierachy from the pair
                    listObjects.Sort(new EPCObjectComparer());
                    TopObjectList = sortHierarchy(parentChild, listObjects);
                    // assign to dataviewer.
                    EPCDataView dataviewer = new EPCDataView();
                    dataviewer.TopObjectList   = TopObjectList;
                    dataviewer.ListObjects     = listObjects;
                    dataviewer.EpcCoreProperty = epcCore;
                    return(dataviewer);
                }
            }catch (Exception e)
            {
                throw new Exception(String.Format("the file is not validate EPC file : {0} ", e.Message));
            }
        }