Example #1
0
        /**
         * Adds an external relationship to a part (except relationships part).
         *
         * The targets of external relationships are not subject to the same
         * validity checks that internal ones are, as the contents is potentially
         * any file, URL or similar.
         *
         * @param target
         *            External target of the relationship
         * @param relationshipType
         *            Type of relationship.
         * @param id
         *            Relationship unique id.
         * @return The newly created and added relationship
         * @see org.apache.poi.OpenXml4Net.opc.RelationshipSource#addExternalRelationship(java.lang.String,
         *      java.lang.String)
         */
        public PackageRelationship AddExternalRelationship(String target,
                                                           String relationshipType, String id)
        {
            if (target == null)
            {
                throw new ArgumentException("target");
            }
            if (relationshipType == null)
            {
                throw new ArgumentException("relationshipType");
            }

            if (_relationships == null)
            {
                _relationships = new PackageRelationshipCollection();
            }

            Uri targetURI;

            try
            {
                targetURI = PackagingUriHelper.ParseUri(target, UriKind.RelativeOrAbsolute);
            }
            catch (UriFormatException e)
            {
                throw new ArgumentException("Invalid target - " + e);
            }

            return(_relationships.AddRelationship(targetURI, TargetMode.External,
                                                  relationshipType, id));
        }
Example #2
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 e)
                {
                    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);
        }
Example #3
0
        /**
         * Constructor. Makes a ValidPartName object from a String part name.
         *
         * @param partName
         *            Part name to valid and to create.
         * @param checkConformance
         *            Flag to specify if the contructor have to validate the OPC
         *            conformance. Must be always <code>true</code> except for
         *            special URI like '/' which is needed for internal use by
         *            OpenXml4Net but is not valid.
         * @throws InvalidFormatException
         *             Throw if the specified part name is not conform to Open
         *             Packaging Convention specifications.
         */

        internal PackagePartName(string partName, bool checkConformance)
        {
            Uri partURI;

            try
            {
                partURI = PackagingUriHelper.ParseUri(partName, UriKind.RelativeOrAbsolute);
            }
            catch (UriFormatException)
            {
                throw new ArgumentException(
                          "partName argmument is not a valid OPC part name !");
            }

            if (checkConformance)
            {
                ThrowExceptionIfInvalidPartUri(partURI);
            }
            else
            {
                if (!PackagingUriHelper.PACKAGE_ROOT_URI.Equals(partURI))
                {
                    throw new OpenXml4NetException(
                              "OCP conformance must be check for ALL part name except special cases : ['/']");
                }
            }
            this.partNameURI    = partURI;
            this.isRelationship = IsRelationshipPartURI(this.partNameURI);
        }