Example #1
0
            // Process the attributes for the Default tag
            private void ProcessDefaultTagAttributes(XmlReader reader)
            {
                //There could be a namespace Attribute present at this level.
                //Also any other attribute on the <Default> tag is an error including xml: and xsi: attributes
                if (PackagingUtilities.GetNonXmlnsAttributeCount(reader) != 2)
                {
                    throw new XmlException(SR.DefaultTagDoesNotMatchSchema, null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
                }

                // get the required Extension and ContentType attributes

                string extensionAttributeValue = reader.GetAttribute(s_extensionAttributeName);

                ValidateXmlAttribute(s_extensionAttributeName, extensionAttributeValue, s_defaultTagName, reader);

                string contentTypeAttributeValue = reader.GetAttribute(s_contentTypeAttributeName);

                ThrowIfXmlAttributeMissing(s_contentTypeAttributeName, contentTypeAttributeValue, s_defaultTagName, reader);

                // The extensions are stored in the Default Dictionary in their original form , but they are compared
                // in a normalized manner using the ExtensionComparer.
                PackUriHelper.ValidatedPartUri temporaryUri = PackUriHelper.ValidatePartUri(
                    new Uri(s_temporaryPartNameWithoutExtension + extensionAttributeValue, UriKind.Relative));
                _defaultDictionary.Add(temporaryUri.PartUriExtension, new ContentType(contentTypeAttributeValue));

                //Skip the EndElement for Default Tag
                if (!reader.IsEmptyElement)
                {
                    ProcessEndElement(reader, s_defaultTagName);
                }
            }
Example #2
0
            // Process the attributes for the Default tag
            private void ProcessOverrideTagAttributes(XmlReader reader)
            {
                //There could be a namespace Attribute present at this level.
                //Also any other attribute on the <Override> tag is an error including xml: and xsi: attributes
                if (PackagingUtilities.GetNonXmlnsAttributeCount(reader) != 2)
                {
                    throw new XmlException(SR.OverrideTagDoesNotMatchSchema, null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
                }

                // get the required Extension and ContentType attributes

                string partNameAttributeValue = reader.GetAttribute(s_partNameAttributeName);

                ValidateXmlAttribute(s_partNameAttributeName, partNameAttributeValue, s_overrideTagName, reader);

                string contentTypeAttributeValue = reader.GetAttribute(s_contentTypeAttributeName);

                ThrowIfXmlAttributeMissing(s_contentTypeAttributeName, contentTypeAttributeValue, s_overrideTagName, reader);

                PackUriHelper.ValidatedPartUri partUri = PackUriHelper.ValidatePartUri(new Uri(partNameAttributeValue, UriKind.Relative));

                //Lazy initializing - ensure that the override dictionary has been initialized
                EnsureOverrideDictionary();

                // The part Uris are stored in the Override Dictionary in their original form , but they are compared
                // in a normalized manner using PartUriComparer.
                _overrideDictionary.Add(partUri, new ContentType(contentTypeAttributeValue));

                //Skip the EndElement for Override Tag
                if (!reader.IsEmptyElement)
                {
                    ProcessEndElement(reader, s_overrideTagName);
                }
            }
        /// <summary>
        /// Parse PackageRelationship Stream
        /// </summary>
        /// <param name="part">relationship part</param>
        /// <exception cref="XmlException">Thrown if XML is malformed</exception>
        private void ParseRelationshipPart(PackagePart part)
        {
            //We can safely open the stream as FileAccess.Read, as this code
            //should only be invoked if the Package has been opened in Read or ReadWrite mode.
            Debug.Assert(_package.FileOpenAccess == FileAccess.Read || _package.FileOpenAccess == FileAccess.ReadWrite,
                         "This method should only be called when FileAccess is Read or ReadWrite");

            using (Stream s = part.GetStream(FileMode.Open, FileAccess.Read))
            {
                // load from the relationship part associated with the given part
                using (XmlReader baseReader = XmlReader.Create(s))
                {
                    using (XmlCompatibilityReader reader = new XmlCompatibilityReader(baseReader, s_relationshipKnownNamespaces))
                    {
                        //This method expects the reader to be in ReadState.Initial.
                        //It will make the first read call.
                        PackagingUtilities.PerformInitialReadAndVerifyEncoding(baseReader);

                        //Note: After the previous method call the reader should be at the first tag in the markup.
                        //MoveToContent - Skips over the following - ProcessingInstruction, DocumentType, Comment, Whitespace, or SignificantWhitespace
                        //If the reader is currently at a content node then this function call is a no-op
                        reader.MoveToContent();

                        // look for our tag and namespace pair - throw if other elements are encountered
                        // Make sure that the current node read is an Element
                        if (reader.NodeType == XmlNodeType.Element &&
                            (reader.Depth == 0) &&
                            (String.CompareOrdinal(s_relationshipsTagName, reader.LocalName) == 0) &&
                            (String.CompareOrdinal(PackagingUtilities.RelationshipNamespaceUri, reader.NamespaceURI) == 0))
                        {
                            ThrowIfXmlBaseAttributeIsPresent(reader);

                            //There should be a namespace Attribute present at this level.
                            //Also any other attribute on the <Relationships> tag is an error including xml: and xsi: attributes
                            if (PackagingUtilities.GetNonXmlnsAttributeCount(reader) > 0)
                            {
                                throw new XmlException(SR.RelationshipsTagHasExtraAttributes, null, reader.LineNumber, reader.LinePosition);
                            }

                            // start tag encountered for Relationships
                            // now parse individual Relationship tags
                            while (reader.Read())
                            {
                                //Skips over the following - ProcessingInstruction, DocumentType, Comment, Whitespace, or SignificantWhitespace
                                //If the reader is currently at a content node then this function call is a no-op
                                reader.MoveToContent();

                                //If MoveToContent() takes us to the end of the content
                                if (reader.NodeType == XmlNodeType.None)
                                {
                                    continue;
                                }

                                if (reader.NodeType == XmlNodeType.Element &&
                                    (reader.Depth == 1) &&
                                    (String.CompareOrdinal(s_relationshipTagName, reader.LocalName) == 0) &&
                                    (String.CompareOrdinal(PackagingUtilities.RelationshipNamespaceUri, reader.NamespaceURI) == 0))
                                {
                                    ThrowIfXmlBaseAttributeIsPresent(reader);

                                    int expectedAttributesCount = 3;

                                    string targetModeAttributeValue = reader.GetAttribute(s_targetModeAttributeName);
                                    if (targetModeAttributeValue != null)
                                    {
                                        expectedAttributesCount++;
                                    }

                                    //check if there are expected number of attributes.
                                    //Also any other attribute on the <Relationship> tag is an error including xml: and xsi: attributes
                                    if (PackagingUtilities.GetNonXmlnsAttributeCount(reader) == expectedAttributesCount)
                                    {
                                        ProcessRelationshipAttributes(reader);

                                        //Skip the EndElement for Relationship
                                        if (!reader.IsEmptyElement)
                                        {
                                            ProcessEndElementForRelationshipTag(reader);
                                        }
                                    }
                                    else
                                    {
                                        throw new XmlException(SR.RelationshipTagDoesntMatchSchema, null, reader.LineNumber, reader.LinePosition);
                                    }
                                }
                                else
                                if (!(String.CompareOrdinal(s_relationshipsTagName, reader.LocalName) == 0 && (reader.NodeType == XmlNodeType.EndElement)))
                                {
                                    throw new XmlException(SR.UnknownTagEncountered, null, reader.LineNumber, reader.LinePosition);
                                }
                            }
                        }
                        else
                        {
                            throw new XmlException(SR.ExpectedRelationshipsElementTag, null, reader.LineNumber, reader.LinePosition);
                        }
                    }
                }
            }
        }
        // Deserialize properties part.
        private void ParseCorePropertyPart(PackagePart part)
        {
            XmlReaderSettings xrs = new XmlReaderSettings();

            xrs.NameTable = _nameTable;
            using (Stream stream = part.GetStream(FileMode.Open, FileAccess.Read))

                // Create a reader that uses _nameTable so as to use the set of tag literals
                // in effect as a set of atomic identifiers.
                using (XmlReader reader = XmlReader.Create(stream, xrs))
                {
                    //This method expects the reader to be in ReadState.Initial.
                    //It will make the first read call.
                    PackagingUtilities.PerformInitialReadAndVerifyEncoding(reader);

                    //Note: After the previous method call the reader should be at the first tag in the markup.
                    //MoveToContent - Skips over the following - ProcessingInstruction, DocumentType, Comment, Whitespace, or SignificantWhitespace
                    //If the reader is currently at a content node then this function call is a no-op
                    if (reader.MoveToContent() != XmlNodeType.Element ||
                        (object)reader.NamespaceURI != PackageXmlStringTable.GetXmlStringAsObject(PackageXmlEnum.PackageCorePropertiesNamespace) ||
                        (object)reader.LocalName != PackageXmlStringTable.GetXmlStringAsObject(PackageXmlEnum.CoreProperties))
                    {
                        throw new XmlException(SR.CorePropertiesElementExpected,
                                               null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
                    }

                    // The schema is closed and defines no attributes on the root element.
                    if (PackagingUtilities.GetNonXmlnsAttributeCount(reader) != 0)
                    {
                        throw new XmlException(SR.Format(SR.PropertyWrongNumbOfAttribsDefinedOn, reader.Name),
                                               null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
                    }

                    // Iterate through property elements until EOF. Note the proper closing of all
                    // open tags is checked by the reader itself.
                    // This loop deals only with depth-1 start tags. Handling of element content
                    // is delegated to dedicated functions.
                    int attributesCount;

                    while (reader.Read() && reader.MoveToContent() != XmlNodeType.None)
                    {
                        // Ignore end-tags. We check element errors on opening tags.
                        if (reader.NodeType == XmlNodeType.EndElement)
                        {
                            continue;
                        }

                        // Any content markup that is not an element here is unexpected.
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            throw new XmlException(SR.PropertyStartTagExpected,
                                                   null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
                        }

                        // Any element below the root should open at level 1 exclusively.
                        if (reader.Depth != 1)
                        {
                            throw new XmlException(SR.NoStructuredContentInsideProperties,
                                                   null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
                        }

                        attributesCount = PackagingUtilities.GetNonXmlnsAttributeCount(reader);

                        // Property elements can occur in any order (xsd:all).
                        object         localName      = reader.LocalName;
                        PackageXmlEnum xmlStringIndex = PackageXmlStringTable.GetEnumOf(localName);
                        string         valueType      = PackageXmlStringTable.GetValueType(xmlStringIndex);

                        if (Array.IndexOf(s_validProperties, xmlStringIndex) == -1) // An unexpected element is an error.
                        {
                            throw new XmlException(
                                      SR.Format(SR.InvalidPropertyNameInCorePropertiesPart, reader.LocalName),
                                      null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
                        }

                        // Any element not in the valid core properties namespace is unexpected.
                        // The following is an object comparison, not a string comparison.
                        if ((object)reader.NamespaceURI != PackageXmlStringTable.GetXmlStringAsObject(PackageXmlStringTable.GetXmlNamespace(xmlStringIndex)))
                        {
                            throw new XmlException(SR.UnknownNamespaceInCorePropertiesPart,
                                                   null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
                        }

                        if (string.CompareOrdinal(valueType, "String") == 0)
                        {
                            // The schema is closed and defines no attributes on this type of element.
                            if (attributesCount != 0)
                            {
                                throw new XmlException(SR.Format(SR.PropertyWrongNumbOfAttribsDefinedOn, reader.Name),
                                                       null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
                            }

                            RecordNewBinding(xmlStringIndex, GetStringData(reader), true /*initializing*/, reader);
                        }
                        else if (string.CompareOrdinal(valueType, "DateTime") == 0)
                        {
                            int allowedAttributeCount = (object)reader.NamespaceURI ==
                                                        PackageXmlStringTable.GetXmlStringAsObject(PackageXmlEnum.DublinCoreTermsNamespace)
                                                        ? 1 : 0;

                            // The schema is closed and defines no attributes on this type of element.
                            if (attributesCount != allowedAttributeCount)
                            {
                                throw new XmlException(SR.Format(SR.PropertyWrongNumbOfAttribsDefinedOn, reader.Name),
                                                       null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
                            }

                            if (allowedAttributeCount != 0)
                            {
                                ValidateXsiType(reader,
                                                PackageXmlStringTable.GetXmlStringAsObject(PackageXmlEnum.DublinCoreTermsNamespace),
                                                W3cdtf);
                            }

                            RecordNewBinding(xmlStringIndex, GetDateData(reader), true /*initializing*/, reader);
                        }
                        else // An unexpected element is an error.
                        {
                            Debug.Fail("Unknown value type for properties");
                        }
                    }
                }
        }
Example #5
0
            private void ParseContentTypesFile(System.Collections.ObjectModel.ReadOnlyCollection <ZipArchiveEntry> zipFiles)
            {
                // Find the content type stream, allowing for interleaving. Naming collisions
                // (as between an atomic and an interleaved part) will result in an exception being thrown.
                Stream s = OpenContentTypeStream(zipFiles);

                // Allow non-existent content type stream.
                if (s == null)
                {
                    return;
                }

                XmlReaderSettings xrs = new XmlReaderSettings();

                xrs.IgnoreWhitespace = true;

                using (s)
                    using (XmlReader reader = XmlReader.Create(s, xrs))
                    {
                        //This method expects the reader to be in ReadState.Initial.
                        //It will make the first read call.
                        PackagingUtilities.PerformInitialReadAndVerifyEncoding(reader);

                        //Note: After the previous method call the reader should be at the first tag in the markup.
                        //MoveToContent - Skips over the following - ProcessingInstruction, DocumentType, Comment, Whitespace, or SignificantWhitespace
                        //If the reader is currently at a content node then this function call is a no-op
                        reader.MoveToContent();

                        // look for our root tag and namespace pair - ignore others in case of version changes
                        // Make sure that the current node read is an Element
                        if ((reader.NodeType == XmlNodeType.Element) &&
                            (reader.Depth == 0) &&
                            (String.CompareOrdinal(reader.NamespaceURI, s_typesNamespaceUri) == 0) &&
                            (String.CompareOrdinal(reader.Name, s_typesTagName) == 0))
                        {
                            //There should be a namespace Attribute present at this level.
                            //Also any other attribute on the <Types> tag is an error including xml: and xsi: attributes
                            if (PackagingUtilities.GetNonXmlnsAttributeCount(reader) > 0)
                            {
                                throw new XmlException(SR.TypesTagHasExtraAttributes, null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
                            }

                            // start tag encountered
                            // now parse individual Default and Override tags
                            while (reader.Read())
                            {
                                //Skips over the following - ProcessingInstruction, DocumentType, Comment, Whitespace, or SignificantWhitespace
                                //If the reader is currently at a content node then this function call is a no-op
                                reader.MoveToContent();

                                //If MoveToContent() takes us to the end of the content
                                if (reader.NodeType == XmlNodeType.None)
                                {
                                    continue;
                                }

                                // Make sure that the current node read is an element
                                // Currently we expect the Default and Override Tag at Depth 1
                                if (reader.NodeType == XmlNodeType.Element &&
                                    reader.Depth == 1 &&
                                    (String.CompareOrdinal(reader.NamespaceURI, s_typesNamespaceUri) == 0) &&
                                    (String.CompareOrdinal(reader.Name, s_defaultTagName) == 0))
                                {
                                    ProcessDefaultTagAttributes(reader);
                                }
                                else
                                if (reader.NodeType == XmlNodeType.Element &&
                                    reader.Depth == 1 &&
                                    (String.CompareOrdinal(reader.NamespaceURI, s_typesNamespaceUri) == 0) &&
                                    (String.CompareOrdinal(reader.Name, s_overrideTagName) == 0))
                                {
                                    ProcessOverrideTagAttributes(reader);
                                }
                                else
                                if (reader.NodeType == XmlNodeType.EndElement && reader.Depth == 0 && String.CompareOrdinal(reader.Name, s_typesTagName) == 0)
                                {
                                    continue;
                                }
                                else
                                {
                                    throw new XmlException(SR.TypesXmlDoesNotMatchSchema, null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
                                }
                            }
                        }
                        else
                        {
                            throw new XmlException(SR.TypesElementExpected, null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
                        }
                    }
            }