Ejemplo n.º 1
0
        /**
         * Configure the package.
         *
         * @param pkg
         */
        private static void ConfigurePackage(Package pkg)
        {
            // Content type manager
            pkg.contentTypeManager = new ZipContentTypeManager(null, pkg);
            // Add default content types for .xml and .rels
            pkg.contentTypeManager
            .AddContentType(
                PackagingUriHelper
                .CreatePartName(PackagingUriHelper.PACKAGE_RELATIONSHIPS_ROOT_URI),
                ContentTypes.RELATIONSHIPS_PART);
            pkg.contentTypeManager
            .AddContentType(PackagingUriHelper
                            .CreatePartName("/default.xml"),
                            ContentTypes.PLAIN_OLD_XML);

            // Init some Package properties
            pkg.packageProperties = new PackagePropertiesPart(pkg,
                                                              PackagingUriHelper.CORE_PROPERTIES_PART_NAME);
            pkg.packageProperties.SetCreatorProperty("Generated by OpenXml4Net");
            pkg.packageProperties.SetCreatedProperty(new Nullable <DateTime>(
                                                         new DateTime()));
        }
Ejemplo n.º 2
0
 /**
  * Builds a PackagePartName for the given ZipEntry,
  *  or null if it's the content types / invalid part
  */
 private PackagePartName BuildPartName(ZipEntry entry)
 {
     try
     {
         // We get an error when we parse [Content_Types].xml
         // because it's not a valid URI.
         if (entry.Name.Equals(
                 ContentTypeManager.CONTENT_TYPES_PART_NAME))
         {
             return(null);
         }
         return(PackagingUriHelper.CreatePartName(ZipHelper
                                                  .GetOPCNameFromZipItemName(entry.Name)));
     }
     catch (Exception)
     {
         // We assume we can continue, even in degraded mode ...
         //logger.log(POILogger.WARN,"Entry "
         //                + entry.getName()
         //                + " is not valid, so this part won't be add to the package.");
         return(null);
     }
 }
Ejemplo n.º 3
0
        /**
         * Get the PackagePart that is the target of a relationship.
         *
         * @param rel A relationship from this part to another one
         * @return The target part of the relationship
         */
        public PackagePart GetRelatedPart(PackageRelationship rel)
        {
            // Ensure this is one of ours
            if (!IsRelationshipExists(rel))
            {
                throw new ArgumentException("Relationship " + rel + " doesn't start with this part " + _partName);
            }

            // Get the target URI, excluding any relative fragments
            Uri target = rel.TargetUri;

            if (target.OriginalString.IndexOf('#') >= 0)
            {
                String t = target.ToString();
                try
                {
                    target = PackagingUriHelper.ParseUri(t.Substring(0, t.IndexOf('#')), UriKind.Absolute);
                }
                // ElectricSquare: The value e is declared but never used
                // catch (InvalidFormatException e) { }
                catch (InvalidFormatException) { }
                // ElectricSquare
                {
                    throw new InvalidFormatException("Invalid target URI: " + t);
                }
            }

            // Turn that into a name, and fetch
            PackagePartName relName = PackagingUriHelper.CreatePartName(target);
            PackagePart     part    = _container.GetPart(relName);

            if (part == null)
            {
                throw new ArgumentException("No part found for relationship " + rel);
            }
            return(part);
        }
Ejemplo n.º 4
0
        /**
         * Parse the relationship part and add all relationship in this collection.
         *
         * @param relPart
         *            The package part to parse.
         * @throws InvalidFormatException
         *             Throws if the relationship part is invalid.
         */
        private void ParseRelationshipsPart(PackagePart relPart)
        {
            try
            {
                logger.Log(POILogger.DEBUG, "Parsing relationship: " + relPart.PartName);
                XPathDocument xmlRelationshipsDoc = DocumentHelper.ReadDocument(relPart.GetInputStream());

                // Check OPC compliance M4.1 rule
                bool fCorePropertiesRelationship = false;
                ///xmlRelationshipsDoc.ChildNodes.GetEnumerator();
                XPathNavigator      xpathnav = xmlRelationshipsDoc.CreateNavigator();
                XmlNamespaceManager nsMgr    = new XmlNamespaceManager(xpathnav.NameTable);
                nsMgr.AddNamespace("x", PackageNamespaces.RELATIONSHIPS);

                XPathNodeIterator iterator = xpathnav.Select("//x:" + PackageRelationship.RELATIONSHIP_TAG_NAME, nsMgr);

                while (iterator.MoveNext())
                {
                    // Relationship ID
                    String id = iterator.Current.GetAttribute(PackageRelationship.ID_ATTRIBUTE_NAME, xpathnav.NamespaceURI);
                    // Relationship type
                    String type = iterator.Current.GetAttribute(
                        PackageRelationship.TYPE_ATTRIBUTE_NAME, xpathnav.NamespaceURI);

                    /* Check OPC Compliance */
                    // Check Rule M4.1
                    if (type.Equals(PackageRelationshipTypes.CORE_PROPERTIES))
                    {
                        if (!fCorePropertiesRelationship)
                        {
                            fCorePropertiesRelationship = true;
                        }
                        else
                        {
                            throw new InvalidFormatException(
                                      "OPC Compliance error [M4.1]: there is more than one core properties relationship in the package !");
                        }
                    }

                    /* End OPC Compliance */

                    // TargetMode (default value "Internal")
                    string     targetModeAttr = iterator.Current.GetAttribute(PackageRelationship.TARGET_MODE_ATTRIBUTE_NAME, xpathnav.NamespaceURI);
                    TargetMode targetMode     = TargetMode.Internal;
                    if (targetModeAttr != string.Empty)
                    {
                        targetMode = targetModeAttr.ToLower()
                                     .Equals("internal") ? TargetMode.Internal
                                : TargetMode.External;
                    }

                    // Target converted in URI
                    Uri    target = PackagingUriHelper.ToUri("http://invalid.uri"); // dummy url
                    String value  = iterator.Current.GetAttribute(
                        PackageRelationship.TARGET_ATTRIBUTE_NAME, xpathnav.NamespaceURI);;
                    try
                    {
                        // when parsing of the given uri fails, we can either
                        // ignore this relationship, which leads to InvalidOperationException
                        // later on, or use a dummy value and thus enable processing of the
                        // package
                        target = PackagingUriHelper.ToUri(value);
                    }
                    catch (UriFormatException e)
                    {
                        logger.Log(POILogger.ERROR, "Cannot convert " + value
                                   + " in a valid relationship URI-> dummy-URI used", e);
                    }
                    AddRelationship(target, targetMode, type, id);
                }
            }
            catch (Exception e)
            {
                logger.Log(POILogger.ERROR, e);
                throw new InvalidFormatException(e.Message);
            }
        }