Exemplo n.º 1
0
        /**
         * Retrieves the parts from this package. We assume that the package has not
         * been yet inspect to retrieve all the parts, this method will open the
         * archive and look for all parts contain inside it. If the package part
         * list is not empty, it will be emptied.
         *
         * @return All parts contain in this package.
         * @throws InvalidFormatException
         *             Throws if the package is not valid.
         */

        protected override PackagePart[] GetPartsImpl()
        {
            if (this.partList == null)
            {
                // The package has just been created, we create an empty part
                // list.
                this.partList = new PackagePartCollection();
            }

            if (this.zipArchive == null)
            {
                PackagePart[] pp = new PackagePart[this.partList.Values.Count];
                this.partList.Values.CopyTo(pp, 0);
                return(pp);
            }
            // First we need to parse the content type part
            IEnumerator entries = this.zipArchive.Entries;

            while (entries.MoveNext())
            {
                ZipEntry entry = (ZipEntry)entries.Current;
                if (entry.Name.Equals(
                        ContentTypeManager.CONTENT_TYPES_PART_NAME))
                {
                    try {
                        this.contentTypeManager = new ZipContentTypeManager(
                            ZipArchive.GetInputStream(entry), this);
                    } catch (IOException e) {
                        throw new InvalidFormatException(e.Message);
                    }
                    break;
                }
            }

            // At this point, we should have loaded the content type part
            if (this.contentTypeManager == null)
            {
                throw new InvalidFormatException(
                          "Package should contain a content type part [M1.13]");
            }

            // Now create all the relationships
            // (Need to create relationships before other
            //  parts, otherwise we might create a part before
            //  its relationship exists, and then it won't tie up)
            entries = this.zipArchive.Entries;
            while (entries.MoveNext())
            {
                ZipEntry        entry    = (ZipEntry)entries.Current;
                PackagePartName partName = BuildPartName(entry);
                if (partName == null)
                {
                    continue;
                }

                // Only proceed for Relationships at this stage
                String contentType = contentTypeManager.GetContentType(partName);
                if (contentType != null && contentType.Equals(ContentTypes.RELATIONSHIPS_PART))
                {
                    try {
                        partList[partName] = new ZipPackagePart(this, entry,
                                                                partName, contentType);
                    } catch (InvalidOperationException e) {
                        throw new InvalidFormatException(e.Message);
                    }
                }
            }

            // Then we can go through all the other parts
            entries = this.zipArchive.Entries;
            while (entries.MoveNext())
            {
                ZipEntry        entry    = entries.Current as ZipEntry;
                PackagePartName partName = BuildPartName(entry);
                if (partName == null)
                {
                    continue;
                }

                String contentType = contentTypeManager
                                     .GetContentType(partName);
                if (contentType != null && contentType.Equals(ContentTypes.RELATIONSHIPS_PART))
                {
                    // Already handled
                }
                else if (contentType != null)
                {
                    try {
                        partList[partName] = new ZipPackagePart(this, entry,
                                                                partName, contentType);
                    } catch (InvalidOperationException e) {
                        throw new InvalidFormatException(e.Message);
                    }
                }
                else
                {
                    throw new InvalidFormatException(
                              "The part "
                              + partName.URI.OriginalString
                              + " does not have any content type ! Rule: Package require content types when retrieving a part from a package. [M.1.14]");
                }
            }
            ZipPackagePart[] returnArray = new ZipPackagePart[partList.Count];
            partList.Values.CopyTo(returnArray, 0);
            return(returnArray);
        }
Exemplo n.º 2
0
        /**
         * Retrieves the parts from this package. We assume that the package has not
         * been yet inspect to retrieve all the parts, this method will open the
         * archive and look for all parts contain inside it. If the package part
         * list is not empty, it will be emptied.
         *
         * @return All parts contain in this package.
         * @throws InvalidFormatException
         *             Throws if the package is not valid.
         */

        protected override PackagePart[] GetPartsImpl(){
        if (this.partList == null) {
            // The package has just been created, we create an empty part
            // list.
            this.partList = new PackagePartCollection();
        }

        if (this.zipArchive == null)
        {
            PackagePart[] pp = new PackagePart[this.partList.Values.Count];
            this.partList.Values.CopyTo(pp, 0);
            return pp;
        }
        // First we need to parse the content type part
        IEnumerator entries = this.zipArchive.Entries;
        while (entries.MoveNext()) {
            ZipEntry entry = (ZipEntry)entries.Current;
            if (entry.Name.ToLower().Equals(
                    ContentTypeManager.CONTENT_TYPES_PART_NAME.ToLower()))
            {
                try {
                    this.contentTypeManager = new ZipContentTypeManager(
                            ZipArchive.GetInputStream(entry), this);
                } catch (IOException e) {
                    throw new InvalidFormatException(e.Message);
                }
                break;
            }
        }

        // At this point, we should have loaded the content type part
        if (this.contentTypeManager == null) {
            throw new InvalidFormatException(
                    "Package should contain a content type part [M1.13]");
        }

        // Now create all the relationships
        // (Need to create relationships before other
        //  parts, otherwise we might create a part before
        //  its relationship exists, and then it won't tie up)
        entries = this.zipArchive.Entries;
        while (entries.MoveNext()) {
            ZipEntry entry = (ZipEntry)entries.Current;
            PackagePartName partName = BuildPartName(entry);
            if(partName == null) continue;

            // Only proceed for Relationships at this stage
            String contentType = contentTypeManager.GetContentType(partName);
            if (contentType != null && contentType.Equals(ContentTypes.RELATIONSHIPS_PART)) {
                try {
                    partList[partName]= new ZipPackagePart(this, entry,
                        partName, contentType);
                } catch (InvalidOperationException e) {
                    throw new InvalidFormatException(e.Message);
                }
            }
        }

        // Then we can go through all the other parts
        entries = this.zipArchive.Entries;
        while (entries.MoveNext()) {
            ZipEntry entry = entries.Current as ZipEntry;
            PackagePartName partName = BuildPartName(entry);
            if(partName == null) continue;

            String contentType = contentTypeManager
                    .GetContentType(partName);
            if (contentType != null && contentType.Equals(ContentTypes.RELATIONSHIPS_PART)) {
                // Already handled
            }
            else if (contentType != null) 
            {
                try {
                    partList[partName]= new ZipPackagePart(this, entry,
                            partName, contentType);
                } catch (InvalidOperationException e) {
                    throw new InvalidFormatException(e.Message);
                }
            } else {
                throw new InvalidFormatException(
                        "The part "
                                + partName.URI.OriginalString
                                + " does not have any content type ! Rule: Package require content types when retrieving a part from a package. [M.1.14]");
            }
        }
            ZipPackagePart[] returnArray =new ZipPackagePart[partList.Count];
            partList.Values.CopyTo(returnArray,0);
        return returnArray;
    }
Exemplo n.º 3
0
        /**
         * Retrieves the parts from this package. We assume that the package has not
         * been yet inspect to retrieve all the parts, this method will open the
         * archive and look for all parts contain inside it. If the package part
         * list is not empty, it will be emptied.
         *
         * @return All parts contain in this package.
         * @throws InvalidFormatException
         *             Throws if the package is not valid.
         */

        protected override PackagePart[] GetPartsImpl()
        {
            if (this.partList == null)
            {
                // The package has just been created, we create an empty part
                // list.
                this.partList = new PackagePartCollection();
            }

            if (this.zipArchive == null)
            {
                PackagePart[] pp = new PackagePart[this.partList.Values.Count];
                this.partList.Values.CopyTo(pp, 0);
                return(pp);
            }
            // First we need to parse the content type part
            IEnumerator entries = this.zipArchive.Entries;

            while (entries.MoveNext())
            {
                ZipEntry entry = (ZipEntry)entries.Current;
                if (entry.Name.ToLower().Equals(
                        ContentTypeManager.CONTENT_TYPES_PART_NAME.ToLower()))
                {
                    try
                    {
                        this.contentTypeManager = new ZipContentTypeManager(
                            ZipArchive.GetInputStream(entry), this);
                    }
                    catch (IOException e)
                    {
                        throw new InvalidFormatException(e.Message, e);
                    }
                    break;
                }
            }

            // At this point, we should have loaded the content type part
            if (this.contentTypeManager == null)
            {
                int numEntries = 0;
                // Is it a different Zip-based format?
                bool hasMimetype    = false;
                bool hasSettingsXML = false;
                entries = this.zipArchive.Entries;
                while (entries.MoveNext())
                {
                    ZipEntry entry = entries.Current as ZipEntry;
                    if (entry.Name.Equals(MIMETYPE))
                    {
                        hasMimetype = true;
                    }
                    if (entry.Name.Equals(SETTINGS_XML))
                    {
                        hasSettingsXML = true;
                    }
                    numEntries++;
                }
                if (hasMimetype && hasSettingsXML)
                {
                    throw new ODFNotOfficeXmlFileException(
                              "The supplied data appears to be in ODF (Open Document) Format. " +
                              "Formats like these (eg ODS, ODP) are not supported, try Apache ODFToolkit");
                }
                if (numEntries == 0)
                {
                    throw new NotOfficeXmlFileException(
                              "No valid entries or contents found, this is not a valid OOXML " +
                              "(Office Open XML) file");
                }
                // Fallback exception
                throw new InvalidFormatException(
                          "Package should contain a content type part [M1.13]");
            }

            // Now create all the relationships
            // (Need to create relationships before other
            //  parts, otherwise we might create a part before
            //  its relationship exists, and then it won't tie up)
            entries = this.zipArchive.Entries;
            while (entries.MoveNext())
            {
                ZipEntry        entry    = (ZipEntry)entries.Current;
                PackagePartName partName = BuildPartName(entry);
                if (partName == null)
                {
                    continue;
                }

                // Only proceed for Relationships at this stage
                String contentType = contentTypeManager.GetContentType(partName);
                if (contentType != null && contentType.Equals(ContentTypes.RELATIONSHIPS_PART))
                {
                    try
                    {
                        PackagePart part = new ZipPackagePart(this, entry, partName, contentType);
                        partList[partName] = part;
                    }
                    catch (InvalidOperationException e)
                    {
                        throw new InvalidFormatException(e.Message, e);
                    }
                }
            }

            // Then we can go through all the other parts
            entries = this.zipArchive.Entries;
            while (entries.MoveNext())
            {
                ZipEntry        entry    = entries.Current as ZipEntry;
                PackagePartName partName = BuildPartName(entry);
                if (partName == null)
                {
                    continue;
                }

                String contentType = contentTypeManager.GetContentType(partName);
                if (contentType != null && contentType.Equals(ContentTypes.RELATIONSHIPS_PART))
                {
                    // Already handled
                }
                else if (contentType != null)
                {
                    try
                    {
                        PackagePart part = new ZipPackagePart(this, entry, partName, contentType);
                        partList[partName] = part;
                    }
                    catch (InvalidOperationException e)
                    {
                        throw new InvalidFormatException(e.Message, e);
                    }
                }
                else
                {
                    throw new InvalidFormatException(
                              "The part "
                              + partName.URI.OriginalString
                              + " does not have any content type ! Rule: Package require content types when retrieving a part from a package. [M.1.14]");
                }
            }
            ZipPackagePart[] returnArray = new ZipPackagePart[partList.Count];
            partList.Values.CopyTo(returnArray, 0);
            return(returnArray);
        }