Exemplo n.º 1
0
 /**
  * Retrieves or Creates if none exists, core package property part.
  * 
  * @return The PackageProperties part of this package.
  */
 public PackageProperties GetPackageProperties()
 {
     this.ThrowExceptionIfWriteOnly();
     // If no properties part has been found then we Create one
     if (this.packageProperties == null)
     {
         this.packageProperties = new PackagePropertiesPart(this,
                 PackagingUriHelper.CORE_PROPERTIES_PART_NAME);
     }
     return this.packageProperties;
 }
Exemplo n.º 2
0
        /**
         * Load the parts of the archive if it has not been done yet. The
         * relationships of each part are not loaded.
         * Note - Rule M4.1 states that there may only ever be one Core
         *  Properties Part, but Office produced files will sometimes
         *  have multiple! As Office ignores all but the first, we relax
         *  Compliance with Rule M4.1, and ignore all others silently too. 
         * @return All this package's parts.
         */
        public List<PackagePart> GetParts()
        {
            ThrowExceptionIfWriteOnly();

            // If the part list is null, we parse the package to retrieve all parts.
            if (partList == null)
            {
                /* Variables use to validate OPC Compliance */

                // Check rule M4.1 -> A format consumer shall consider more than
                // one core properties relationship for a package to be an error
                // (We just log it and move on, as real files break this!)
                bool hasCorePropertiesPart = false;
                bool needCorePropertiesPart = true;

                PackagePart[] parts = this.GetPartsImpl();
                this.partList = new PackagePartCollection();
                foreach (PackagePart part in parts)
                {
                    bool pnFound = false;
                    foreach (PackagePartName pn in partList.Keys)
                    {
                        if (part.PartName.Name.StartsWith(pn.Name))
                        {
                            pnFound = true;
                            break;
                        }
                    }

                    if (pnFound)
                        throw new InvalidFormatException(
                                "A part with the name '"
                                        + part.PartName +
                                        "' already exist : Packages shall not contain equivalent " +
                                    "part names and package implementers shall neither create " +
                                    "nor recognize packages with equivalent part names. [M1.12]");

                    // Check OPC compliance rule M4.1
                    if (part.ContentType.Equals(
                            ContentTypes.CORE_PROPERTIES_PART))
                    {
                        if (!hasCorePropertiesPart)
                            hasCorePropertiesPart = true;
                        else
                            Console.WriteLine(
                                    "OPC Compliance error [M4.1]: there is more than one core properties relationship in the package ! " +
                                    "POI will use only the first, but other software may reject this file.");
                    }



                    if (partUnmarshallers.ContainsKey(part.contentType))
                    {
                        PartUnmarshaller partUnmarshaller = partUnmarshallers[part.contentType];
                        UnmarshallContext context = new UnmarshallContext(this,
                                part.PartName);
                        try
                        {
                            PackagePart unmarshallPart = partUnmarshaller
                                    .Unmarshall(context, part.GetInputStream());
                            partList[unmarshallPart.PartName] = unmarshallPart;

                            // Core properties case-- use first CoreProperties part we come across
                            // and ignore any subsequent ones
                            if (unmarshallPart is PackagePropertiesPart &&
                                    hasCorePropertiesPart &&
                                    needCorePropertiesPart)
                            {
                                this.packageProperties = (PackagePropertiesPart)unmarshallPart;
                                needCorePropertiesPart = false;
                            }
                        }
                        catch (IOException)
                        {
                            logger.Log(POILogger.WARN, "Unmarshall operation : IOException for "
                                    + part.PartName);
                            continue;
                        }
                        catch (InvalidOperationException invoe)
                        {
                            throw new InvalidFormatException(invoe.Message);
                        }
                    }
                    else
                    {
                        try
                        {
                            partList[part.PartName] = part;
                        }
                        catch (InvalidOperationException e)
                        {
                            throw new InvalidFormatException(e.Message);
                        }
                    }
                }
            }
            return new List<PackagePart>(partList.Values);
        }
Exemplo n.º 3
0
 internal CoreProperties(PackagePropertiesPart part)
 {
     this.part = part;
 }