private void Load(Stream xmlStream) { this.Clear(); if (xmlStream.Length == 0L) { return; } using (Stream stream = new BoundedStream(xmlStream, false, 0L, 524288L)) { using (XmlReader xmlReader = XmlReader.Create(stream)) { MasterCategoryListSerializer masterCategoryListSerializer = new MasterCategoryListSerializer(xmlReader); try { masterCategoryListSerializer.Deserialize(this); this.loadedWithProblems = masterCategoryListSerializer.HasFaults; } catch (CorruptDataException) { this.loadedWithProblems = true; if (xmlStream.Length > 524288L) { ExTraceGlobals.StorageTracer.TraceWarning <long, long>((long)this.GetHashCode(), "The size of MCL XML ({0}) has exceeded the limit of {1}. The loaded data is truncated.", xmlStream.Length, 524288L); } } } } }
private void CopyOrOverrideAttributes(XmlWriter xmlWriter, IEnumerable <PropValue> propValueOverrides) { Dictionary <string, object> dictionary = new Dictionary <string, object>(); Util.AddRange <KeyValuePair <string, object>, KeyValuePair <string, object> >(dictionary, MasterCategoryListSerializer.PropValuesToAttributeNameValues(propValueOverrides)); while (this.xmlReader.MoveToNextAttribute()) { if (this.IsFromCategoriesNamespace()) { object value; if (dictionary.TryGetValue(this.xmlReader.LocalName, out value)) { dictionary.Remove(this.xmlReader.LocalName); MasterCategoryListSerializer.WriteAttributeForProperty(xmlWriter, this.xmlReader.LocalName, value); } else { xmlWriter.WriteAttributeString(this.xmlReader.LocalName, this.xmlReader.Value); } } else { xmlWriter.WriteAttributeString(this.xmlReader.Prefix, this.xmlReader.LocalName, this.xmlReader.NamespaceURI, this.xmlReader.Value); } } MasterCategoryListSerializer.WriteAttributesForProperties(xmlWriter, dictionary); this.xmlReader.MoveToElement(); }
private static void WriteAttributesForProperties(XmlWriter xmlWriter, IEnumerable <KeyValuePair <string, object> > attrNameToValues) { foreach (KeyValuePair <string, object> keyValuePair in attrNameToValues) { MasterCategoryListSerializer.WriteAttributeForProperty(xmlWriter, keyValuePair.Key, keyValuePair.Value); } }
private static void WriteAttributeForProperty(XmlWriter xmlWriter, string localName, object value) { if (!PropertyError.IsPropertyNotFound(value)) { xmlWriter.WriteAttributeString(localName, MasterCategoryListSerializer.ConvertToXmlString(value)); } }
internal static void Serialize(MasterCategoryList mcl, XmlWriter xmlWriter) { MasterCategoryListSerializer.CatchSerializationException(delegate { xmlWriter.WriteStartElement("categories", "CategoryList.xsd"); IEnumerable <PropValue> properties = MasterCategoryListSerializer.GetProperties(new MasterCategoryListSerializer.TryGetPropertyDelegate(mcl.TryGetProperty), MasterCategoryListSchema.Instance.AllProperties); MasterCategoryListSerializer.WriteAttributesForProperties(xmlWriter, MasterCategoryListSerializer.PropValuesToAttributeNameValues(properties)); MasterCategoryListSerializer.WriteCategoryElements(xmlWriter, mcl); xmlWriter.WriteEndElement(); }); }
private static void WriteCategoryElements(XmlWriter xmlWriter, IEnumerable <Category> categoriesToWrite) { string prefix = xmlWriter.LookupPrefix("CategoryList.xsd"); foreach (Category @object in categoriesToWrite) { xmlWriter.WriteStartElement(prefix, "category", "CategoryList.xsd"); IEnumerable <PropValue> properties = MasterCategoryListSerializer.GetProperties(new MasterCategoryListSerializer.TryGetPropertyDelegate(@object.TryGetProperty), CategorySchema.Instance.AllProperties); MasterCategoryListSerializer.WriteAttributesForProperties(xmlWriter, MasterCategoryListSerializer.PropValuesToAttributeNameValues(properties)); xmlWriter.WriteEndElement(); } }
private void SaveToStream(SaveMode saveMode, Stream destination, Stream serverCopy) { XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.CloseOutput = false; if (saveMode != SaveMode.NoConflictResolution && serverCopy.Length != 0L) { try { using (Stream stream = new BoundedStream(serverCopy, false, 0L, 524288L)) { using (StreamReader streamReader = new StreamReader(stream, true)) { using (XmlReader xmlReader = XmlReader.Create(streamReader)) { xmlWriterSettings.Encoding = (streamReader.CurrentEncoding ?? Encoding.UTF8); using (XmlWriter xmlTextWriter = Util.GetXmlTextWriter(destination, xmlWriterSettings)) { new MasterCategoryListSerializer(xmlReader).SerializeUsingSource(this, xmlTextWriter); return; } } } } } catch (CorruptDataException inner) { if (saveMode == SaveMode.FailOnAnyConflict) { throw new SaveConflictException(ServerStrings.ExMclCannotBeResolved, inner); } } } destination.Position = 0L; destination.SetLength(0L); using (XmlWriter xmlWriter = XmlWriter.Create(destination, xmlWriterSettings)) { MasterCategoryListSerializer.Serialize(this, xmlWriter); } }
internal void Deserialize(MasterCategoryList mcl) { MasterCategoryListSerializer.CatchSerializationException(delegate { bool flag = false; this.xmlReader.Read(); while (!this.xmlReader.EOF) { if (this.xmlReader.NodeType == XmlNodeType.Element && this.IsFromCategoriesNamespace()) { this.EnsureLocationIsExpectedForKnownElements(flag); string localName; if ((localName = this.xmlReader.LocalName) != null) { if (localName == "categories") { mcl.SetProperties(this.ReadAttributes(MasterCategoryListSchema.Instance)); flag = true; this.xmlReader.Read(); continue; } if (localName == "category") { this.LoadCategory(mcl); continue; } } this.xmlReader.Skip(); } else { this.xmlReader.Skip(); } } if (!flag) { this.ReportFault(); } }); }
internal void SerializeUsingSource(MasterCategoryList mcl, XmlWriter xmlWriter) { MasterCategoryListSerializer.CatchSerializationException(delegate { bool flag = false; HashSet <Category> hashSet = new HashSet <Category>(mcl.Count); Util.AddRange <Category, Category>(hashSet, mcl); this.xmlReader.Read(); while (!this.xmlReader.EOF) { if (this.xmlReader.NodeType == XmlNodeType.Element && this.IsFromCategoriesNamespace()) { this.EnsureLocationIsExpectedForKnownElements(flag); string localName; if ((localName = this.xmlReader.LocalName) != null) { if (localName == "categories") { xmlWriter.WriteStartElement(this.xmlReader.Prefix, this.xmlReader.LocalName, this.xmlReader.NamespaceURI); this.CopyOrOverrideAttributes(xmlWriter, MasterCategoryListSerializer.GetProperties(new MasterCategoryListSerializer.TryGetPropertyDelegate(mcl.TryGetProperty), MasterCategoryListSchema.Instance.AllProperties)); flag = true; if (this.xmlReader.IsEmptyElement) { MasterCategoryListSerializer.WriteCategoryElements(xmlWriter, hashSet); xmlWriter.WriteEndElement(); } this.xmlReader.Read(); continue; } if (localName == "category") { Category category = this.FindMatchingCategory(mcl); if (category != null && hashSet.Contains(category)) { xmlWriter.WriteStartElement(this.xmlReader.Prefix, this.xmlReader.LocalName, this.xmlReader.NamespaceURI); this.CopyOrOverrideAttributes(xmlWriter, MasterCategoryListSerializer.GetProperties(new MasterCategoryListSerializer.TryGetPropertyDelegate(category.TryGetProperty), CategorySchema.Instance.AllProperties)); if (this.xmlReader.IsEmptyElement) { xmlWriter.WriteEndElement(); this.xmlReader.Skip(); } else { this.xmlReader.Read(); int depth = this.xmlReader.Depth; while (this.xmlReader.Depth >= depth) { xmlWriter.WriteNode(this.xmlReader, false); } } hashSet.Remove(category); continue; } this.xmlReader.Skip(); continue; } } xmlWriter.WriteNode(this.xmlReader, false); } else if (this.xmlReader.NodeType == XmlNodeType.EndElement && this.xmlReader.LocalName == "categories") { MasterCategoryListSerializer.WriteCategoryElements(xmlWriter, hashSet); xmlWriter.WriteNode(this.xmlReader, false); } else { xmlWriter.WriteNode(this.xmlReader, false); } } if (!flag) { if (hashSet.Count > 0) { ExTraceGlobals.StorageTracer.TraceDebug((long)mcl.GetHashCode(), "The source XML didn't contain the root element we expect. Reverting to source-less serialization."); throw new CorruptDataException(ServerStrings.ExInvalidMclXml); } this.ReportFault(); } }); }