Пример #1
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 IllegalStateException
                        // 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);
            }
        }