1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Syndication
{
using System.Runtime;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.CompilerServices;
// NOTE: This class implements Clone so if you add any members, please update the copy ctor
[TypeForwardedFrom("System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
public class XmlSyndicationContent : SyndicationContent
{
XmlBuffer contentBuffer;
SyndicationElementExtension extension;
string type;
// Saves the element in the reader to the buffer (attributes preserved)
// Type is populated from type attribute on reader
// Reader must be positioned at an element
public XmlSyndicationContent(XmlReader reader)
{
if (reader == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
}
SyndicationFeedFormatter.MoveToStartElement(reader);
if (reader.HasAttributes)
{
while (reader.MoveToNextAttribute())
{
string name = reader.LocalName;
string ns = reader.NamespaceURI;
string value = reader.Value;
if (name == Atom10Constants.TypeTag && ns == string.Empty)
{
this.type = value;
}
else if (!FeedUtils.IsXmlns(name, ns))
{
base.AttributeExtensions.Add(new XmlQualifiedName(name, ns), value);
}
}
reader.MoveToElement();
}
this.type = string.IsNullOrEmpty(this.type) ? Atom10Constants.XmlMediaType : this.type;
this.contentBuffer = new XmlBuffer(int.MaxValue);
using (XmlDictionaryWriter writer = this.contentBuffer.OpenSection(XmlDictionaryReaderQuotas.Max))
{
writer.WriteNode(reader, false);
}
contentBuffer.CloseSection();
contentBuffer.Close();
}
public XmlSyndicationContent(string type, object dataContractExtension, XmlObjectSerializer dataContractSerializer)
{
this.type = string.IsNullOrEmpty(type) ? Atom10Constants.XmlMediaType : type;
this.extension = new SyndicationElementExtension(dataContractExtension, dataContractSerializer);
}
public XmlSyndicationContent(string type, object xmlSerializerExtension, XmlSerializer serializer)
{
this.type = string.IsNullOrEmpty(type) ? Atom10Constants.XmlMediaType : type;
this.extension = new SyndicationElementExtension(xmlSerializerExtension, serializer);
}
public XmlSyndicationContent(string type, SyndicationElementExtension extension)
{
if (extension == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("extension");
}
this.type = string.IsNullOrEmpty(type) ? Atom10Constants.XmlMediaType : type;
this.extension = extension;
}
protected XmlSyndicationContent(XmlSyndicationContent source)
: base(source)
{
if (source == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("source");
}
this.contentBuffer = source.contentBuffer;
this.extension = source.extension;
this.type = source.type;
}
public SyndicationElementExtension Extension
{
get
{
return this.extension;
}
}
public override string Type
{
get { return this.type; }
}
public override SyndicationContent Clone()
{
return new XmlSyndicationContent(this);
}
public XmlDictionaryReader GetReaderAtContent()
{
EnsureContentBuffer();
return this.contentBuffer.GetReader(0);
}
public TContent ReadContent<TContent>()
{
return ReadContent<TContent>((DataContractSerializer) null);
}
public TContent ReadContent<TContent>(XmlObjectSerializer dataContractSerializer)
{
if (dataContractSerializer == null)
{
dataContractSerializer = new DataContractSerializer(typeof(TContent));
}
if (this.extension != null)
{
return this.extension.GetObject<TContent>(dataContractSerializer);
}
else
{
Fx.Assert(this.contentBuffer != null, "contentBuffer cannot be null");
using (XmlDictionaryReader reader = this.contentBuffer.GetReader(0))
{
// skip past the content element
reader.ReadStartElement();
return (TContent) dataContractSerializer.ReadObject(reader, false);
}
}
}
public TContent ReadContent<TContent>(XmlSerializer serializer)
{
if (serializer == null)
{
serializer = new XmlSerializer(typeof(TContent));
}
if (this.extension != null)
{
return this.extension.GetObject<TContent>(serializer);
}
else
{
Fx.Assert(this.contentBuffer != null, "contentBuffer cannot be null");
using (XmlDictionaryReader reader = this.contentBuffer.GetReader(0))
{
// skip past the content element
reader.ReadStartElement();
return (TContent) serializer.Deserialize(reader);
}
}
}
// does not write start element or type attribute, writes other attributes and rest of content
protected override void WriteContentsTo(XmlWriter writer)
{
if (writer == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
}
if (this.extension != null)
{
this.extension.WriteTo(writer);
}
else if (this.contentBuffer != null)
{
using (XmlDictionaryReader reader = this.contentBuffer.GetReader(0))
{
reader.MoveToStartElement();
if (!reader.IsEmptyElement)
{
reader.ReadStartElement();
while (reader.Depth >= 1 && reader.ReadState == ReadState.Interactive)
{
writer.WriteNode(reader, false);
}
}
}
}
}
void EnsureContentBuffer()
{
if (this.contentBuffer == null)
{
XmlBuffer tmp = new XmlBuffer(int.MaxValue);
using (XmlDictionaryWriter writer = tmp.OpenSection(XmlDictionaryReaderQuotas.Max))
{
this.WriteTo(writer, Atom10Constants.ContentTag, Atom10Constants.Atom10Namespace);
}
tmp.CloseSection();
tmp.Close();
this.contentBuffer = tmp;
}
}
}
}
|