Esempio n. 1
0
        // 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.Assert(false, "Unknown value type for properties");
                        }
                    }
                }
        }
        /// <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(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(RelationshipTagName, reader.LocalName) == 0) &&
                                    (string.CompareOrdinal(PackagingUtilities.RelationshipNamespaceUri, reader.NamespaceURI) == 0))
                                {
                                    ThrowIfXmlBaseAttributeIsPresent(reader);

                                    int expectedAttributesCount = 3;

                                    string?targetModeAttributeValue = reader.GetAttribute(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(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);
                        }
                    }
                }
            }
        }
Esempio n. 3
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, TypesNamespaceUri) == 0) &&
                            (string.CompareOrdinal(reader.Name, 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, TypesNamespaceUri) == 0) &&
                                    (string.CompareOrdinal(reader.Name, DefaultTagName) == 0))
                                {
                                    ProcessDefaultTagAttributes(reader);
                                }
                                else if (reader.NodeType == XmlNodeType.Element &&
                                         reader.Depth == 1 &&
                                         (string.CompareOrdinal(reader.NamespaceURI, TypesNamespaceUri) == 0) &&
                                         (string.CompareOrdinal(reader.Name, OverrideTagName) == 0))
                                {
                                    ProcessOverrideTagAttributes(reader);
                                }
                                else if (reader.NodeType == XmlNodeType.EndElement && reader.Depth == 0 && string.CompareOrdinal(reader.Name, 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);
                        }
                    }
            }