// 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); } }
// 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); } }
//Returns the content type for the part, if present, else returns null. internal ContentType GetContentType(PackUriHelper.ValidatedPartUri partUri) { //Step 1: Check if there is an override entry present corresponding to the //partUri provided. Override takes precedence over the default entries if (_overrideDictionary != null) { if (_overrideDictionary.ContainsKey(partUri)) { return(_overrideDictionary[partUri]); } } //Step 2: Check if there is a default entry corresponding to the //extension of the partUri provided. string extension = partUri.PartUriExtension; if (_defaultDictionary.ContainsKey(extension)) { return(_defaultDictionary[extension]); } //Step 3: If we did not find an entry in the override and the default //dictionaries, this is an error condition return(null); }
//Adds the Default entry if it is the first time we come across //the extension for the partUri, does nothing if the content type //corresponding to the default entry for the extension matches or //adds a override corresponding to this part and content type. //This call is made when a new part is being added to the package. // This method assumes the partUri is valid. internal void AddContentType(PackUriHelper.ValidatedPartUri partUri, ContentType contentType, CompressionLevel compressionLevel) { //save the compressionOption and deflateOption that should be used //to create the content type item later if (!_contentTypeStreamExists) { _cachedCompressionLevel = compressionLevel; } // Figure out whether the mapping matches a default entry, can be made into a new // default entry, or has to be entered as an override entry. bool foundMatchingDefault = false; string extension = partUri.PartUriExtension; // Need to create an override entry? if (extension.Length == 0 || (_defaultDictionary.ContainsKey(extension) && !(foundMatchingDefault = _defaultDictionary[extension].AreTypeAndSubTypeEqual(contentType)))) { AddOverrideElement(partUri, contentType); } // Else, either there is already a mapping from extension to contentType, // or one needs to be created. else if (!foundMatchingDefault) { AddDefaultElement(extension, contentType); } }
private void WriteOverrideElement(XmlWriter xmlWriter, PackUriHelper.ValidatedPartUri partUri, ContentType contentType) { xmlWriter.WriteStartElement(s_overrideTagName); xmlWriter.WriteAttributeString(s_partNameAttributeName, partUri.PartUriString); xmlWriter.WriteAttributeString(s_contentTypeAttributeName, contentType.ToString()); xmlWriter.WriteEndElement(); }
/// <summary> /// Constructs a ZipPackagePart. This is called from ZipPackage.CreatePartCore in streaming /// production. /// No piece is created until the first write operation on the associated stream. Therefore /// this constructor does not take a ZipFileInfo. /// </summary> /// <param name="container"></param> /// <param name="zipArchive"></param> /// <param name="partUri"></param> /// <param name="compressionOption"></param> /// <param name="contentType"></param> internal ZipPackagePart(ZipPackage container, ZipArchive zipArchive, PackUriHelper.ValidatedPartUri partUri, string contentType, CompressionOption compressionOption) : base(container, partUri, contentType, compressionOption) { _zipArchive = zipArchive; }
//Deletes the override entry corresponding to the partUri, if it exists internal void DeleteContentType(PackUriHelper.ValidatedPartUri partUri) { if (_overrideDictionary != null) { if (_overrideDictionary.Remove(partUri)) { _dirty = true; } } }
//------------------------------------------------------ // // Public Events // //------------------------------------------------------ // None //------------------------------------------------------ // // Internal Constructors // //------------------------------------------------------ #region Internal Constructors /// <summary> /// Constructs a ZipPackagePart for an atomic (i.e. non-interleaved) part. /// This is called from the ZipPackage class as a result of GetPartCore, /// GetPartsCore or CreatePartCore methods /// </summary> /// <param name="zipPackage"></param> /// <param name="zipArchive"></param> /// <param name="zipArchiveEntry"></param> /// <param name="zipStreamManager"></param> /// <param name="partUri"></param> /// <param name="compressionOption"></param> /// <param name="contentType"></param> internal ZipPackagePart(ZipPackage zipPackage, ZipArchive zipArchive, ZipArchiveEntry zipArchiveEntry, ZipStreamManager zipStreamManager, PackUriHelper.ValidatedPartUri partUri, string contentType, CompressionOption compressionOption) : base(zipPackage, partUri, contentType, compressionOption) { _zipPackage = zipPackage; _zipArchive = zipArchive; _zipStreamManager = zipStreamManager; _zipArchiveEntry = zipArchiveEntry; }
private void AddOverrideElement(PackUriHelper.ValidatedPartUri partUri, ContentType contentType) { //Delete any entry corresponding in the Override dictionary //corresponding to the PartUri for which the contentType is being added. //This is to compensate for dead override entries in the content types file. DeleteContentType(partUri); //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, contentType); _dirty = true; }