Beispiel #1
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);
                }
                catch (UriFormatException)
                {
                    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);
        }
Beispiel #2
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()));
        }
Beispiel #3
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);
     }
 }