| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648 |
- //------------------------------------------------------------
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //------------------------------------------------------------
- namespace System.Xml
- {
- using System.Globalization;
- using System.IO;
- using System.Runtime;
- using System.Runtime.Serialization;
- using System.Text;
- using System.Threading.Tasks;
- abstract class XmlBaseWriter : XmlDictionaryWriter, IFragmentCapableXmlDictionaryWriter
- {
- XmlNodeWriter writer;
- NamespaceManager nsMgr;
- Element[] elements;
- int depth;
- string attributeLocalName;
- string attributeValue;
- bool isXmlAttribute;
- bool isXmlnsAttribute;
- WriteState writeState;
- DocumentState documentState;
- byte[] trailBytes;
- int trailByteCount;
- XmlStreamNodeWriter nodeWriter;
- XmlSigningNodeWriter signingWriter;
- XmlUTF8NodeWriter textFragmentWriter;
- XmlNodeWriter oldWriter;
- Stream oldStream;
- int oldNamespaceBoundary;
- bool inList;
- const string xmlnsNamespace = "http://www.w3.org/2000/xmlns/";
- const string xmlNamespace = "http://www.w3.org/XML/1998/namespace";
- static BinHexEncoding binhexEncoding;
- static string[] prefixes = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
- XmlBaseWriterNodeWriterAsyncHelper nodeWriterAsyncHelper;
- protected XmlBaseWriter()
- {
- this.nsMgr = new NamespaceManager();
- this.writeState = WriteState.Start;
- this.documentState = DocumentState.None;
- }
- protected void SetOutput(XmlStreamNodeWriter writer)
- {
- this.inList = false;
- this.writer = writer;
- this.nodeWriter = writer;
- this.writeState = WriteState.Start;
- this.documentState = DocumentState.None;
- this.nsMgr.Clear();
- if (this.depth != 0)
- {
- this.elements = null;
- this.depth = 0;
- }
- this.attributeLocalName = null;
- this.attributeValue = null;
- this.oldWriter = null;
- this.oldStream = null;
- }
- public override void Flush()
- {
- if (IsClosed)
- ThrowClosed();
- writer.Flush();
- }
- public override void Close()
- {
- if (IsClosed)
- return;
- try
- {
- FinishDocument();
- AutoComplete(WriteState.Closed);
- writer.Flush();
- }
- finally
- {
- nsMgr.Close();
- if (depth != 0)
- {
- elements = null;
- depth = 0;
- }
- attributeValue = null;
- attributeLocalName = null;
- nodeWriter.Close();
- if (signingWriter != null)
- {
- signingWriter.Close();
- }
- if (textFragmentWriter != null)
- {
- textFragmentWriter.Close();
- }
- oldWriter = null;
- oldStream = null;
- }
- }
- protected bool IsClosed
- {
- get { return writeState == WriteState.Closed; }
- }
- protected void ThrowClosed()
- {
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlWriterClosed)));
- }
- static BinHexEncoding BinHexEncoding
- {
- get
- {
- if (binhexEncoding == null)
- binhexEncoding = new BinHexEncoding();
- return binhexEncoding;
- }
- }
- public override string XmlLang
- {
- get
- {
- return nsMgr.XmlLang;
- }
- }
- public override XmlSpace XmlSpace
- {
- get
- {
- return nsMgr.XmlSpace;
- }
- }
- public override WriteState WriteState
- {
- get
- {
- return writeState;
- }
- }
- public override void WriteXmlnsAttribute(string prefix, string ns)
- {
- if (IsClosed)
- ThrowClosed();
- if (ns == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("ns");
- if (writeState != WriteState.Element)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidWriteState, "WriteXmlnsAttribute", WriteState.ToString())));
- if (prefix == null)
- {
- prefix = nsMgr.LookupPrefix(ns);
- if (prefix == null)
- {
- GeneratePrefix(ns, null);
- }
- }
- else
- {
- nsMgr.AddNamespaceIfNotDeclared(prefix, ns, null);
- }
- }
- public override void WriteXmlnsAttribute(string prefix, XmlDictionaryString ns)
- {
- if (IsClosed)
- ThrowClosed();
- if (ns == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("ns");
- if (writeState != WriteState.Element)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidWriteState, "WriteXmlnsAttribute", WriteState.ToString())));
- if (prefix == null)
- {
- prefix = nsMgr.LookupPrefix(ns.Value);
- if (prefix == null)
- {
- GeneratePrefix(ns.Value, ns);
- }
- }
- else
- {
- nsMgr.AddNamespaceIfNotDeclared(prefix, ns.Value, ns);
- }
- }
- void StartAttribute(ref string prefix, string localName, string ns, XmlDictionaryString xNs)
- {
- if (IsClosed)
- ThrowClosed();
- if (writeState == WriteState.Attribute)
- WriteEndAttribute();
- if (localName == null || (localName.Length == 0 && prefix != "xmlns"))
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("localName"));
- if (writeState != WriteState.Element)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidWriteState, "WriteStartAttribute", WriteState.ToString())));
- if (prefix == null)
- {
- if (ns == xmlnsNamespace && localName != "xmlns")
- prefix = "xmlns";
- else if (ns == xmlNamespace)
- prefix = "xml";
- else
- prefix = string.Empty;
- }
- // Normalize a (prefix,localName) of (null, "xmlns") to ("xmlns", string.Empty).
- if (prefix.Length == 0 && localName == "xmlns")
- {
- prefix = "xmlns";
- localName = string.Empty;
- }
- isXmlnsAttribute = false;
- isXmlAttribute = false;
- if (prefix == "xml")
- {
- if (ns != null && ns != xmlNamespace)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlPrefixBoundToNamespace, "xml", xmlNamespace, ns), "ns"));
- isXmlAttribute = true;
- attributeValue = string.Empty;
- attributeLocalName = localName;
- }
- else if (prefix == "xmlns")
- {
- if (ns != null && ns != xmlnsNamespace)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlPrefixBoundToNamespace, "xmlns", xmlnsNamespace, ns), "ns"));
- isXmlnsAttribute = true;
- attributeValue = string.Empty;
- attributeLocalName = localName;
- }
- else if (ns == null)
- {
- // A null namespace means the namespace of the given prefix.
- if (prefix.Length == 0)
- {
- // An empty prefix on an attribute means no namespace (not the default namespace)
- ns = string.Empty;
- }
- else
- {
- ns = nsMgr.LookupNamespace(prefix);
- if (ns == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlUndefinedPrefix, prefix), "prefix"));
- }
- }
- else if (ns.Length == 0)
- {
- // An empty namespace means no namespace; prefix must be empty
- if (prefix.Length != 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlEmptyNamespaceRequiresNullPrefix), "prefix"));
- }
- else if (prefix.Length == 0)
- {
- // No prefix specified - try to find a prefix corresponding to the given namespace
- prefix = nsMgr.LookupAttributePrefix(ns);
- // If we didn't find anything with the right namespace, generate one.
- if (prefix == null)
- {
- // Watch for special values
- if (ns.Length == xmlnsNamespace.Length && ns == xmlnsNamespace)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlSpecificBindingNamespace, "xmlns", ns)));
- if (ns.Length == xmlNamespace.Length && ns == xmlNamespace)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlSpecificBindingNamespace, "xml", ns)));
- prefix = GeneratePrefix(ns, xNs);
- }
- }
- else
- {
- nsMgr.AddNamespaceIfNotDeclared(prefix, ns, xNs);
- }
- writeState = WriteState.Attribute;
- }
- public override void WriteStartAttribute(string prefix, string localName, string namespaceUri)
- {
- StartAttribute(ref prefix, localName, namespaceUri, null);
- if (!isXmlnsAttribute)
- {
- writer.WriteStartAttribute(prefix, localName);
- }
- }
- public override void WriteStartAttribute(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri)
- {
- StartAttribute(ref prefix, (localName != null ? localName.Value : null), (namespaceUri != null ? namespaceUri.Value : null), namespaceUri);
- if (!isXmlnsAttribute)
- {
- writer.WriteStartAttribute(prefix, localName);
- }
- }
- public override void WriteEndAttribute()
- {
- if (IsClosed)
- ThrowClosed();
- if (writeState != WriteState.Attribute)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidWriteState, "WriteEndAttribute", WriteState.ToString())));
- FlushBase64();
- try
- {
- if (isXmlAttribute)
- {
- if (attributeLocalName == "lang")
- {
- nsMgr.AddLangAttribute(attributeValue);
- }
- else if (attributeLocalName == "space")
- {
- if (attributeValue == "preserve")
- {
- nsMgr.AddSpaceAttribute(XmlSpace.Preserve);
- }
- else if (attributeValue == "default")
- {
- nsMgr.AddSpaceAttribute(XmlSpace.Default);
- }
- else
- {
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlInvalidXmlSpace, attributeValue)));
- }
- }
- else
- {
- // XmlTextWriter specifically allows for other localNames
- }
- isXmlAttribute = false;
- attributeLocalName = null;
- attributeValue = null;
- }
- if (isXmlnsAttribute)
- {
- nsMgr.AddNamespaceIfNotDeclared(attributeLocalName, attributeValue, null);
- isXmlnsAttribute = false;
- attributeLocalName = null;
- attributeValue = null;
- }
- else
- {
- writer.WriteEndAttribute();
- }
- }
- finally
- {
- writeState = WriteState.Element;
- }
- }
- public override void WriteComment(string text)
- {
- if (IsClosed)
- ThrowClosed();
- if (writeState == WriteState.Attribute)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidWriteState, "WriteComment", WriteState.ToString())));
- if (text == null)
- {
- text = string.Empty;
- }
- else if (text.IndexOf("--", StringComparison.Ordinal) != -1 || (text.Length > 0 && text[text.Length - 1] == '-'))
- {
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlInvalidCommentChars), "text"));
- }
- StartComment();
- FlushBase64();
- writer.WriteComment(text);
- EndComment();
- }
- public override void WriteFullEndElement()
- {
- if (IsClosed)
- ThrowClosed();
- if (writeState == WriteState.Attribute)
- WriteEndAttribute();
- if (writeState != WriteState.Element && writeState != WriteState.Content)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidWriteState, "WriteFullEndElement", WriteState.ToString())));
- AutoComplete(WriteState.Content);
- WriteEndElement();
- }
- public override void WriteCData(string text)
- {
- if (IsClosed)
- ThrowClosed();
- if (writeState == WriteState.Attribute)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidWriteState, "WriteCData", WriteState.ToString())));
- if (text == null)
- text = string.Empty;
- if (text.Length > 0)
- {
- StartContent();
- FlushBase64();
- writer.WriteCData(text);
- EndContent();
- }
- }
- public override void WriteDocType(string name, string pubid, string sysid, string subset)
- {
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.XmlMethodNotSupported, "WriteDocType")));
- }
- void StartElement(ref string prefix, string localName, string ns, XmlDictionaryString xNs)
- {
- if (IsClosed)
- ThrowClosed();
- if (this.documentState == DocumentState.Epilog)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlOnlyOneRoot)));
- if (localName == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("localName"));
- if (localName.Length == 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.InvalidLocalNameEmpty), "localName"));
- if (writeState == WriteState.Attribute)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidWriteState, "WriteStartElement", WriteState.ToString())));
- FlushBase64();
- AutoComplete(WriteState.Element);
- Element element = EnterScope();
- if (ns == null)
- {
- if (prefix == null)
- prefix = string.Empty;
- ns = nsMgr.LookupNamespace(prefix);
- if (ns == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlUndefinedPrefix, prefix), "prefix"));
- }
- else if (prefix == null)
- {
- prefix = nsMgr.LookupPrefix(ns);
- if (prefix == null)
- {
- prefix = string.Empty;
- nsMgr.AddNamespace(string.Empty, ns, xNs);
- }
- }
- else
- {
- nsMgr.AddNamespaceIfNotDeclared(prefix, ns, xNs);
- }
- element.Prefix = prefix;
- element.LocalName = localName;
- }
- public override void WriteStartElement(string prefix, string localName, string namespaceUri)
- {
- StartElement(ref prefix, localName, namespaceUri, null);
- writer.WriteStartElement(prefix, localName);
- }
- public override void WriteStartElement(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri)
- {
- StartElement(ref prefix, (localName != null ? localName.Value : null), (namespaceUri != null ? namespaceUri.Value : null), namespaceUri);
- writer.WriteStartElement(prefix, localName);
- }
- public override void WriteEndElement()
- {
- if (IsClosed)
- ThrowClosed();
- if (depth == 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidDepth, "WriteEndElement", depth.ToString(CultureInfo.InvariantCulture))));
- if (writeState == WriteState.Attribute)
- WriteEndAttribute();
- FlushBase64();
- if (writeState == WriteState.Element)
- {
- nsMgr.DeclareNamespaces(writer);
- writer.WriteEndStartElement(true);
- }
- else
- {
- Element element = elements[depth];
- writer.WriteEndElement(element.Prefix, element.LocalName);
- }
- ExitScope();
- writeState = WriteState.Content;
- }
- Element EnterScope()
- {
- nsMgr.EnterScope();
- depth++;
- if (elements == null)
- {
- elements = new Element[4];
- }
- else if (elements.Length == depth)
- {
- Element[] newElementNodes = new Element[depth * 2];
- Array.Copy(elements, newElementNodes, depth);
- elements = newElementNodes;
- }
- Element element = elements[depth];
- if (element == null)
- {
- element = new Element();
- elements[depth] = element;
- }
- return element;
- }
- void ExitScope()
- {
- elements[depth].Clear();
- depth--;
- if (depth == 0 && documentState == DocumentState.Document)
- this.documentState = DocumentState.Epilog;
- nsMgr.ExitScope();
- }
- protected void FlushElement()
- {
- if (this.writeState == WriteState.Element)
- {
- AutoComplete(WriteState.Content);
- }
- }
- protected void StartComment()
- {
- FlushElement();
- }
- protected void EndComment()
- {
- }
- protected void StartContent()
- {
- FlushElement();
- if (depth == 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlIllegalOutsideRoot)));
- }
- protected void StartContent(char ch)
- {
- FlushElement();
- if (depth == 0)
- VerifyWhitespace(ch);
- }
- protected void StartContent(string s)
- {
- FlushElement();
- if (depth == 0)
- VerifyWhitespace(s);
- }
- protected void StartContent(char[] chars, int offset, int count)
- {
- FlushElement();
- if (depth == 0)
- VerifyWhitespace(chars, offset, count);
- }
- void VerifyWhitespace(char ch)
- {
- if (!IsWhitespace(ch))
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlIllegalOutsideRoot)));
- }
- void VerifyWhitespace(string s)
- {
- for (int i = 0; i < s.Length; i++)
- if (!IsWhitespace(s[i]))
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlIllegalOutsideRoot)));
- }
- void VerifyWhitespace(char[] chars, int offset, int count)
- {
- for (int i = 0; i < count; i++)
- if (!IsWhitespace(chars[offset + i]))
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlIllegalOutsideRoot)));
- }
- bool IsWhitespace(char ch)
- {
- return (ch == ' ' || ch == '\n' || ch == '\r' || ch == 't');
- }
- protected void EndContent()
- {
- }
- void AutoComplete(WriteState writeState)
- {
- if (this.writeState == WriteState.Element)
- {
- EndStartElement();
- }
- this.writeState = writeState;
- }
- void EndStartElement()
- {
- nsMgr.DeclareNamespaces(writer);
- writer.WriteEndStartElement(false);
- }
- public override string LookupPrefix(string ns)
- {
- if (ns == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("ns"));
- if (IsClosed)
- ThrowClosed();
- return nsMgr.LookupPrefix(ns);
- }
- internal string LookupNamespace(string prefix)
- {
- if (prefix == null)
- return null;
- return nsMgr.LookupNamespace(prefix);
- }
- string GetQualifiedNamePrefix(string namespaceUri, XmlDictionaryString xNs)
- {
- string prefix = nsMgr.LookupPrefix(namespaceUri);
- if (prefix == null)
- {
- if (writeState != WriteState.Attribute)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlNamespaceNotFound, namespaceUri), "namespaceUri"));
- prefix = GeneratePrefix(namespaceUri, xNs);
- }
- return prefix;
- }
- public override void WriteQualifiedName(string localName, string namespaceUri)
- {
- if (IsClosed)
- ThrowClosed();
- if (localName == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("localName"));
- if (localName.Length == 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.InvalidLocalNameEmpty), "localName"));
- if (namespaceUri == null)
- namespaceUri = string.Empty;
- string prefix = GetQualifiedNamePrefix(namespaceUri, null);
- if (prefix.Length != 0)
- {
- WriteString(prefix);
- WriteString(":");
- }
- WriteString(localName);
- }
- public override void WriteQualifiedName(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
- {
- if (IsClosed)
- ThrowClosed();
- if (localName == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("localName"));
- if (localName.Value.Length == 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.InvalidLocalNameEmpty), "localName"));
- if (namespaceUri == null)
- namespaceUri = XmlDictionaryString.Empty;
- string prefix = GetQualifiedNamePrefix(namespaceUri.Value, namespaceUri);
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(string.Concat(prefix, ":", namespaceUri.Value));
- if (!isXmlnsAttribute)
- {
- StartContent();
- writer.WriteQualifiedName(prefix, localName);
- EndContent();
- }
- }
- public override void WriteStartDocument()
- {
- if (IsClosed)
- ThrowClosed();
- if (writeState != WriteState.Start)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidWriteState, "WriteStartDocument", WriteState.ToString())));
- writeState = WriteState.Prolog;
- documentState = DocumentState.Document;
- writer.WriteDeclaration();
- }
- public override void WriteStartDocument(bool standalone)
- {
- if (IsClosed)
- ThrowClosed();
- WriteStartDocument();
- }
- public override void WriteProcessingInstruction(string name, string text)
- {
- if (IsClosed)
- ThrowClosed();
- if (name != "xml")
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlProcessingInstructionNotSupported), "name"));
- if (writeState != WriteState.Start)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidDeclaration)));
- // The only thing the text can legitimately contain is version, encoding, and standalone.
- // We only support version 1.0, we can only write whatever encoding we were supplied,
- // and we don't support DTDs, so whatever values are supplied in the text argument are irrelevant.
- writer.WriteDeclaration();
- }
- void FinishDocument()
- {
- if (this.writeState == WriteState.Attribute)
- {
- WriteEndAttribute();
- }
- while (this.depth > 0)
- {
- WriteEndElement();
- }
- }
- public override void WriteEndDocument()
- {
- if (IsClosed)
- ThrowClosed();
- if (writeState == WriteState.Start || writeState == WriteState.Prolog)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlNoRootElement)));
- FinishDocument();
- writeState = WriteState.Start;
- documentState = DocumentState.End;
- }
- protected int NamespaceBoundary
- {
- get
- {
- return nsMgr.NamespaceBoundary;
- }
- set
- {
- nsMgr.NamespaceBoundary = value;
- }
- }
- public override void WriteEntityRef(string name)
- {
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.XmlMethodNotSupported, "WriteEntityRef")));
- }
- public override void WriteName(string name)
- {
- if (IsClosed)
- ThrowClosed();
- WriteString(name);
- }
- public override void WriteNmToken(string name)
- {
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.XmlMethodNotSupported, "WriteNmToken")));
- }
- public override void WriteWhitespace(string whitespace)
- {
- if (IsClosed)
- ThrowClosed();
- if (whitespace == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("whitespace");
- for (int i = 0; i < whitespace.Length; ++i)
- {
- char c = whitespace[i];
- if (c != ' ' &&
- c != '\t' &&
- c != '\n' &&
- c != '\r')
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlOnlyWhitespace), "whitespace"));
- }
- WriteString(whitespace);
- }
- public override void WriteString(string value)
- {
- if (IsClosed)
- ThrowClosed();
- if (value == null)
- value = string.Empty;
- if (value.Length > 0 || this.inList)
- {
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(value);
- if (!isXmlnsAttribute)
- {
- StartContent(value);
- writer.WriteEscapedText(value);
- EndContent();
- }
- }
- }
- public override void WriteString(XmlDictionaryString value)
- {
- if (IsClosed)
- ThrowClosed();
- if (value == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
- if (value.Value.Length > 0)
- {
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(value.Value);
- if (!isXmlnsAttribute)
- {
- StartContent(value.Value);
- writer.WriteEscapedText(value);
- EndContent();
- }
- }
- }
- public override void WriteChars(char[] chars, int offset, int count)
- {
- if (IsClosed)
- ThrowClosed();
- if (chars == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("chars"));
- // Not checking upper bound because it will be caught by "count". This is what XmlTextWriter does.
- if (offset < 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.ValueMustBeNonNegative)));
- if (count < 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.ValueMustBeNonNegative)));
- if (count > chars.Length - offset)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.SizeExceedsRemainingBufferSpace, chars.Length - offset)));
- if (count > 0)
- {
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(new string(chars, offset, count));
- if (!isXmlnsAttribute)
- {
- StartContent(chars, offset, count);
- writer.WriteEscapedText(chars, offset, count);
- EndContent();
- }
- }
- }
- public override void WriteRaw(string value)
- {
- if (IsClosed)
- ThrowClosed();
- if (value == null)
- value = string.Empty;
- if (value.Length > 0)
- {
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(value);
- if (!isXmlnsAttribute)
- {
- StartContent(value);
- writer.WriteText(value);
- EndContent();
- }
- }
- }
- public override void WriteRaw(char[] chars, int offset, int count)
- {
- if (IsClosed)
- ThrowClosed();
- if (chars == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("chars"));
- // Not checking upper bound because it will be caught by "count". This is what XmlTextWriter does.
- if (offset < 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.ValueMustBeNonNegative)));
- if (count < 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.ValueMustBeNonNegative)));
- if (count > chars.Length - offset)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.SizeExceedsRemainingBufferSpace, chars.Length - offset)));
- if (count > 0)
- {
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(new string(chars, offset, count));
- if (!isXmlnsAttribute)
- {
- StartContent(chars, offset, count);
- writer.WriteText(chars, offset, count);
- EndContent();
- }
- }
- }
- public override void WriteCharEntity(char ch)
- {
- if (IsClosed)
- ThrowClosed();
- if (ch >= 0xd800 && ch <= 0xdfff)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlMissingLowSurrogate), "ch"));
- if (attributeValue != null)
- WriteAttributeText(ch.ToString());
- if (!isXmlnsAttribute)
- {
- StartContent(ch);
- FlushBase64();
- writer.WriteCharEntity(ch);
- EndContent();
- }
- }
- public override void WriteSurrogateCharEntity(char lowChar, char highChar)
- {
- if (IsClosed)
- ThrowClosed();
- SurrogateChar ch = new SurrogateChar(lowChar, highChar);
- if (attributeValue != null)
- {
- char[] chars = new char[2] { highChar, lowChar };
- WriteAttributeText(new string(chars));
- }
- if (!isXmlnsAttribute)
- {
- StartContent();
- FlushBase64();
- writer.WriteCharEntity(ch.Char);
- EndContent();
- }
- }
- public override void WriteValue(object value)
- {
- if (IsClosed)
- ThrowClosed();
- if (value == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("value"));
- else if (value is object[])
- {
- WriteValue((object[])value);
- }
- else if (value is Array)
- {
- WriteValue((Array)value);
- }
- else if (value is IStreamProvider)
- {
- WriteValue((IStreamProvider)value);
- }
- else
- {
- WritePrimitiveValue(value);
- }
- }
- protected void WritePrimitiveValue(object value)
- {
- if (IsClosed)
- ThrowClosed();
- if (value == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("value"));
- if (value is ulong)
- {
- WriteValue((ulong)value);
- }
- else if (value is string)
- {
- WriteValue((string)value);
- }
- else if (value is int)
- {
- WriteValue((int)value);
- }
- else if (value is long)
- {
- WriteValue((long)value);
- }
- else if (value is bool)
- {
- WriteValue((bool)value);
- }
- else if (value is double)
- {
- WriteValue((double)value);
- }
- else if (value is DateTime)
- {
- WriteValue((DateTime)value);
- }
- else if (value is float)
- {
- WriteValue((float)value);
- }
- else if (value is decimal)
- {
- WriteValue((decimal)value);
- }
- else if (value is XmlDictionaryString)
- {
- WriteValue((XmlDictionaryString)value);
- }
- else if (value is UniqueId)
- {
- WriteValue((UniqueId)value);
- }
- else if (value is Guid)
- {
- WriteValue((Guid)value);
- }
- else if (value is TimeSpan)
- {
- WriteValue((TimeSpan)value);
- }
- else if (value.GetType().IsArray)
- {
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlNestedArraysNotSupported), "value"));
- }
- else
- {
- base.WriteValue(value);
- }
- }
- public override void WriteValue(string value)
- {
- if (IsClosed)
- ThrowClosed();
- WriteString(value);
- }
- public override void WriteValue(int value)
- {
- if (IsClosed)
- ThrowClosed();
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(XmlConverter.ToString(value));
- if (!isXmlnsAttribute)
- {
- StartContent();
- writer.WriteInt32Text(value);
- EndContent();
- }
- }
- public override void WriteValue(long value)
- {
- if (IsClosed)
- ThrowClosed();
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(XmlConverter.ToString(value));
- if (!isXmlnsAttribute)
- {
- StartContent();
- writer.WriteInt64Text(value);
- EndContent();
- }
- }
- void WriteValue(ulong value)
- {
- if (IsClosed)
- ThrowClosed();
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(XmlConverter.ToString(value));
- if (!isXmlnsAttribute)
- {
- StartContent();
- writer.WriteUInt64Text(value);
- EndContent();
- }
- }
- public override void WriteValue(bool value)
- {
- if (IsClosed)
- ThrowClosed();
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(XmlConverter.ToString(value));
- if (!isXmlnsAttribute)
- {
- StartContent();
- writer.WriteBoolText(value);
- EndContent();
- }
- }
- public override void WriteValue(decimal value)
- {
- if (IsClosed)
- ThrowClosed();
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(XmlConverter.ToString(value));
- if (!isXmlnsAttribute)
- {
- StartContent();
- writer.WriteDecimalText(value);
- EndContent();
- }
- }
- public override void WriteValue(float value)
- {
- if (IsClosed)
- ThrowClosed();
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(XmlConverter.ToString(value));
- if (!isXmlnsAttribute)
- {
- StartContent();
- writer.WriteFloatText(value);
- EndContent();
- }
- }
- public override void WriteValue(double value)
- {
- if (IsClosed)
- ThrowClosed();
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(XmlConverter.ToString(value));
- if (!isXmlnsAttribute)
- {
- StartContent();
- writer.WriteDoubleText(value);
- EndContent();
- }
- }
- public override void WriteValue(XmlDictionaryString value)
- {
- WriteString(value);
- }
- public override void WriteValue(DateTime value)
- {
- if (IsClosed)
- ThrowClosed();
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(XmlConverter.ToString(value));
- if (!isXmlnsAttribute)
- {
- StartContent();
- writer.WriteDateTimeText(value);
- EndContent();
- }
- }
- public override void WriteValue(UniqueId value)
- {
- if (IsClosed)
- ThrowClosed();
- if (value == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(XmlConverter.ToString(value));
- if (!isXmlnsAttribute)
- {
- StartContent();
- writer.WriteUniqueIdText(value);
- EndContent();
- }
- }
- public override void WriteValue(Guid value)
- {
- if (IsClosed)
- ThrowClosed();
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(XmlConverter.ToString(value));
- if (!isXmlnsAttribute)
- {
- StartContent();
- writer.WriteGuidText(value);
- EndContent();
- }
- }
- public override void WriteValue(TimeSpan value)
- {
- if (IsClosed)
- ThrowClosed();
- FlushBase64();
- if (attributeValue != null)
- WriteAttributeText(XmlConverter.ToString(value));
- if (!isXmlnsAttribute)
- {
- StartContent();
- writer.WriteTimeSpanText(value);
- EndContent();
- }
- }
- public override void WriteBase64(byte[] buffer, int offset, int count)
- {
- if (IsClosed)
- ThrowClosed();
- EnsureBufferBounds(buffer, offset, count);
- if (count > 0)
- {
- if (trailByteCount > 0)
- {
- while (trailByteCount < 3 && count > 0)
- {
- trailBytes[trailByteCount++] = buffer[offset++];
- count--;
- }
- }
- int totalByteCount = trailByteCount + count;
- int actualByteCount = totalByteCount - (totalByteCount % 3);
- if (trailBytes == null)
- {
- trailBytes = new byte[3];
- }
- if (actualByteCount >= 3)
- {
- if (attributeValue != null)
- {
- WriteAttributeText(XmlConverter.Base64Encoding.GetString(trailBytes, 0, trailByteCount));
- WriteAttributeText(XmlConverter.Base64Encoding.GetString(buffer, offset, actualByteCount - trailByteCount));
- }
- if (!isXmlnsAttribute)
- {
- StartContent();
- writer.WriteBase64Text(trailBytes, trailByteCount, buffer, offset, actualByteCount - trailByteCount);
- EndContent();
- }
- trailByteCount = (totalByteCount - actualByteCount);
- if (trailByteCount > 0)
- {
- int trailOffset = offset + count - trailByteCount;
- for (int i = 0; i < trailByteCount; i++)
- trailBytes[i] = buffer[trailOffset++];
- }
- }
- else
- {
- Buffer.BlockCopy(buffer, offset, trailBytes, trailByteCount, count);
- trailByteCount += count;
- }
- }
- }
- internal override IAsyncResult BeginWriteBase64(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
- {
- if (IsClosed)
- ThrowClosed();
- EnsureBufferBounds(buffer, offset, count);
- return new WriteBase64AsyncResult(buffer, offset, count, this, callback, state);
- }
- internal override void EndWriteBase64(IAsyncResult result)
- {
- WriteBase64AsyncResult.End(result);
- }
- internal override AsyncCompletionResult WriteBase64Async(AsyncEventArgs<XmlWriteBase64AsyncArguments> state)
- {
- if (this.nodeWriterAsyncHelper == null)
- {
- this.nodeWriterAsyncHelper = new XmlBaseWriterNodeWriterAsyncHelper(this);
- }
- this.nodeWriterAsyncHelper.SetArguments(state);
- if (this.nodeWriterAsyncHelper.StartAsync() == AsyncCompletionResult.Completed)
- {
- return AsyncCompletionResult.Completed;
- }
- return AsyncCompletionResult.Queued;
- }
- class WriteBase64AsyncResult : AsyncResult
- {
- static AsyncCompletion onComplete = new AsyncCompletion(OnComplete);
- XmlBaseWriter writer;
- byte[] buffer;
- int offset;
- int count;
- int actualByteCount;
- int totalByteCount;
- public WriteBase64AsyncResult(byte[] buffer, int offset, int count, XmlBaseWriter writer, AsyncCallback callback, object state)
- : base(callback, state)
- {
- this.writer = writer;
- this.buffer = buffer;
- this.offset = offset;
- this.count = count;
- bool completeSelf = true;
- if (this.count > 0)
- {
- if (writer.trailByteCount > 0)
- {
- while (writer.trailByteCount < 3 && this.count > 0)
- {
- writer.trailBytes[writer.trailByteCount++] = buffer[this.offset++];
- this.count--;
- }
- }
- this.totalByteCount = writer.trailByteCount + this.count;
- this.actualByteCount = totalByteCount - (totalByteCount % 3);
- if (writer.trailBytes == null)
- {
- writer.trailBytes = new byte[3];
- }
- if (actualByteCount >= 3)
- {
- if (writer.attributeValue != null)
- {
- writer.WriteAttributeText(XmlConverter.Base64Encoding.GetString(writer.trailBytes, 0, writer.trailByteCount));
- writer.WriteAttributeText(XmlConverter.Base64Encoding.GetString(buffer, this.offset, actualByteCount - writer.trailByteCount));
- }
- // StartContent/WriteBase64Text/EndContent will be called from HandleWriteBase64 as appropriate
- completeSelf = HandleWriteBase64Text(null);
- }
- else
- {
- Buffer.BlockCopy(buffer, this.offset, writer.trailBytes, writer.trailByteCount, this.count);
- writer.trailByteCount += this.count;
- }
- }
- if (completeSelf)
- {
- this.Complete(true);
- }
- }
- static bool OnComplete(IAsyncResult result)
- {
- WriteBase64AsyncResult thisPtr = (WriteBase64AsyncResult)result.AsyncState;
- return thisPtr.HandleWriteBase64Text(result);
- }
- bool HandleWriteBase64Text(IAsyncResult result)
- {
- // in this code block if count > 0 && actualByteCount >= 3
- if (!writer.isXmlnsAttribute)
- {
- if (result == null)
- {
- this.writer.StartContent();
- result = this.writer.writer.BeginWriteBase64Text(this.writer.trailBytes,
- this.writer.trailByteCount,
- this.buffer,
- this.offset,
- this.actualByteCount - this.writer.trailByteCount,
- PrepareAsyncCompletion(onComplete),
- this);
- if (!result.CompletedSynchronously)
- {
- return false;
- }
- }
- this.writer.writer.EndWriteBase64Text(result);
- this.writer.EndContent();
- }
- this.writer.trailByteCount = (totalByteCount - actualByteCount);
- if (this.writer.trailByteCount > 0)
- {
- int trailOffset = offset + count - this.writer.trailByteCount;
- for (int i = 0; i < this.writer.trailByteCount; i++)
- this.writer.trailBytes[i] = this.buffer[trailOffset++];
- }
- return true;
- }
- public static void End(IAsyncResult result)
- {
- AsyncResult.End<WriteBase64AsyncResult>(result);
- }
- }
- public override void WriteBinHex(byte[] buffer, int offset, int count)
- {
- if (IsClosed)
- ThrowClosed();
- EnsureBufferBounds(buffer, offset, count);
- WriteRaw(BinHexEncoding.GetString(buffer, offset, count));
- }
- public override bool CanCanonicalize
- {
- get
- {
- return true;
- }
- }
- protected bool Signing
- {
- get
- {
- return writer == signingWriter;
- }
- }
- public override void StartCanonicalization(Stream stream, bool includeComments, string[] inclusivePrefixes)
- {
- if (IsClosed)
- ThrowClosed();
- if (Signing)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlCanonicalizationStarted)));
- FlushElement();
- if (signingWriter == null)
- signingWriter = CreateSigningNodeWriter();
- signingWriter.SetOutput(writer, stream, includeComments, inclusivePrefixes);
- writer = signingWriter;
- SignScope(signingWriter.CanonicalWriter);
- }
- public override void EndCanonicalization()
- {
- if (IsClosed)
- ThrowClosed();
- if (!Signing)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlCanonicalizationNotStarted)));
- signingWriter.Flush();
- writer = signingWriter.NodeWriter;
- }
- protected abstract XmlSigningNodeWriter CreateSigningNodeWriter();
- public virtual bool CanFragment
- {
- get
- {
- return true;
- }
- }
- public void StartFragment(Stream stream, bool generateSelfContainedTextFragment)
- {
- if (!CanFragment)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
- if (IsClosed)
- ThrowClosed();
- if (stream == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("stream"));
- if (oldStream != null || oldWriter != null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException());
- if (WriteState == WriteState.Attribute)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidWriteState, "StartFragment", WriteState.ToString())));
- FlushElement();
- writer.Flush();
- oldNamespaceBoundary = NamespaceBoundary;
- XmlStreamNodeWriter fragmentWriter = null;
- if (generateSelfContainedTextFragment)
- {
- this.NamespaceBoundary = depth + 1;
- if (textFragmentWriter == null)
- textFragmentWriter = new XmlUTF8NodeWriter();
- textFragmentWriter.SetOutput(stream, false, Encoding.UTF8);
- fragmentWriter = textFragmentWriter;
- }
- if (Signing)
- {
- if (fragmentWriter != null)
- {
- oldWriter = signingWriter.NodeWriter;
- signingWriter.NodeWriter = fragmentWriter;
- }
- else
- {
- oldStream = ((XmlStreamNodeWriter)signingWriter.NodeWriter).Stream;
- ((XmlStreamNodeWriter)signingWriter.NodeWriter).Stream = stream;
- }
- }
- else
- {
- if (fragmentWriter != null)
- {
- oldWriter = writer;
- writer = fragmentWriter;
- }
- else
- {
- oldStream = nodeWriter.Stream;
- nodeWriter.Stream = stream;
- }
- }
- }
- public void EndFragment()
- {
- if (IsClosed)
- ThrowClosed();
- if (oldStream == null && oldWriter == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException());
- if (WriteState == WriteState.Attribute)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidWriteState, "EndFragment", WriteState.ToString())));
- FlushElement();
- writer.Flush();
- if (Signing)
- {
- if (oldWriter != null)
- signingWriter.NodeWriter = oldWriter;
- else
- ((XmlStreamNodeWriter)signingWriter.NodeWriter).Stream = oldStream;
- }
- else
- {
- if (oldWriter != null)
- writer = oldWriter;
- else
- nodeWriter.Stream = oldStream;
- }
- NamespaceBoundary = oldNamespaceBoundary;
- oldWriter = null;
- oldStream = null;
- }
- public void WriteFragment(byte[] buffer, int offset, int count)
- {
- if (!CanFragment)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
- if (IsClosed)
- ThrowClosed();
- if (buffer == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("buffer"));
- if (offset < 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.ValueMustBeNonNegative)));
- if (count < 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.ValueMustBeNonNegative)));
- if (count > buffer.Length - offset)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.SizeExceedsRemainingBufferSpace, buffer.Length - offset)));
- if (WriteState == WriteState.Attribute)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidWriteState, "WriteFragment", WriteState.ToString())));
- if (writer != nodeWriter)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException());
- FlushElement();
- FlushBase64();
- nodeWriter.Flush();
- nodeWriter.Stream.Write(buffer, offset, count);
- }
- void FlushBase64()
- {
- if (trailByteCount > 0)
- {
- FlushTrailBytes();
- }
- }
- void FlushTrailBytes()
- {
- if (attributeValue != null)
- WriteAttributeText(XmlConverter.Base64Encoding.GetString(trailBytes, 0, trailByteCount));
- if (!isXmlnsAttribute)
- {
- StartContent();
- writer.WriteBase64Text(trailBytes, trailByteCount, trailBytes, 0, 0);
- EndContent();
- }
- trailByteCount = 0;
- }
- void WriteValue(object[] array)
- {
- FlushBase64();
- StartContent();
- writer.WriteStartListText();
- this.inList = true;
- for (int i = 0; i < array.Length; i++)
- {
- if (i != 0)
- {
- writer.WriteListSeparator();
- }
- WritePrimitiveValue(array[i]);
- }
- this.inList = false;
- writer.WriteEndListText();
- EndContent();
- }
- void WriteValue(Array array)
- {
- FlushBase64();
- StartContent();
- writer.WriteStartListText();
- this.inList = true;
- for (int i = 0; i < array.Length; i++)
- {
- if (i != 0)
- {
- writer.WriteListSeparator();
- }
- WritePrimitiveValue(array.GetValue(i));
- }
- this.inList = false;
- writer.WriteEndListText();
- EndContent();
- }
- protected void StartArray(int count)
- {
- FlushBase64();
- if (this.documentState == DocumentState.Epilog)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlOnlyOneRoot)));
- if (this.documentState == DocumentState.Document && count > 1 && depth == 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlOnlyOneRoot)));
- if (writeState == WriteState.Attribute)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidWriteState, "WriteStartElement", WriteState.ToString())));
- AutoComplete(WriteState.Content);
- }
- protected void EndArray()
- {
- }
- void EnsureBufferBounds(byte[] buffer, int offset, int count)
- {
- if (buffer == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("buffer");
- // Not checking upper bound because it will be caught by "count". This is what XmlTextWriter does.
- if (offset < 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.ValueMustBeNonNegative)));
- if (count < 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.ValueMustBeNonNegative)));
- if (count > buffer.Length - offset)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.SizeExceedsRemainingBufferSpace, buffer.Length - offset)));
- }
- string GeneratePrefix(string ns, XmlDictionaryString xNs)
- {
- if (writeState != WriteState.Element && writeState != WriteState.Attribute)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidPrefixState, WriteState.ToString())));
- string prefix = nsMgr.AddNamespace(ns, xNs);
- if (prefix != null)
- return prefix;
- while (true)
- {
- int prefixId = elements[depth].PrefixId++;
- prefix = string.Concat("d", depth.ToString(CultureInfo.InvariantCulture), "p", prefixId.ToString(CultureInfo.InvariantCulture));
- if (nsMgr.LookupNamespace(prefix) == null)
- {
- nsMgr.AddNamespace(prefix, ns, xNs);
- return prefix;
- }
- }
- }
- protected void SignScope(XmlCanonicalWriter signingWriter)
- {
- nsMgr.Sign(signingWriter);
- }
- void WriteAttributeText(string value)
- {
- if (attributeValue.Length == 0)
- attributeValue = value;
- else
- attributeValue += value;
- }
- class Element
- {
- string prefix;
- string localName;
- int prefixId;
- public string Prefix
- {
- get
- {
- return prefix;
- }
- set
- {
- prefix = value;
- }
- }
- public string LocalName
- {
- get
- {
- return localName;
- }
- set
- {
- localName = value;
- }
- }
- public int PrefixId
- {
- get
- {
- return prefixId;
- }
- set
- {
- prefixId = value;
- }
- }
- public void Clear()
- {
- this.prefix = null;
- this.localName = null;
- this.prefixId = 0;
- }
- }
- enum DocumentState : byte
- {
- None, // Not inside StartDocument/EndDocument - Allows multiple root elemnts
- Document, // Inside StartDocument/EndDocument
- Epilog, // EndDocument must be called
- End // Nothing further to write
- }
- class NamespaceManager
- {
- Namespace[] namespaces;
- Namespace lastNameSpace;
- int nsCount;
- int depth;
- XmlAttribute[] attributes;
- int attributeCount;
- XmlSpace space;
- string lang;
- int namespaceBoundary;
- int nsTop;
- Namespace defaultNamespace;
- public NamespaceManager()
- {
- defaultNamespace = new Namespace();
- defaultNamespace.Depth = 0;
- defaultNamespace.Prefix = string.Empty;
- defaultNamespace.Uri = string.Empty;
- defaultNamespace.UriDictionaryString = null;
- }
- public string XmlLang
- {
- get
- {
- return lang;
- }
- }
- public XmlSpace XmlSpace
- {
- get
- {
- return space;
- }
- }
- public void Clear()
- {
- if (this.namespaces == null)
- {
- this.namespaces = new Namespace[4];
- this.namespaces[0] = defaultNamespace;
- }
- this.nsCount = 1;
- this.nsTop = 0;
- this.depth = 0;
- this.attributeCount = 0;
- this.space = XmlSpace.None;
- this.lang = null;
- this.lastNameSpace = null;
- this.namespaceBoundary = 0;
- }
- public int NamespaceBoundary
- {
- get
- {
- return namespaceBoundary;
- }
- set
- {
- int i;
- for (i = 0; i < nsCount; i++)
- if (namespaces[i].Depth >= value)
- break;
- nsTop = i;
- namespaceBoundary = value;
- lastNameSpace = null;
- }
- }
- public void Close()
- {
- if (depth == 0)
- {
- if (namespaces != null && namespaces.Length > 32)
- namespaces = null;
- if (attributes != null && attributes.Length > 4)
- attributes = null;
- }
- else
- {
- namespaces = null;
- attributes = null;
- }
- lang = null;
- }
- public void DeclareNamespaces(XmlNodeWriter writer)
- {
- int i = this.nsCount;
- while (i > 0)
- {
- Namespace nameSpace = namespaces[i - 1];
- if (nameSpace.Depth != depth)
- break;
- i--;
- }
- while (i < this.nsCount)
- {
- Namespace nameSpace = namespaces[i];
- if (nameSpace.UriDictionaryString != null)
- writer.WriteXmlnsAttribute(nameSpace.Prefix, nameSpace.UriDictionaryString);
- else
- writer.WriteXmlnsAttribute(nameSpace.Prefix, nameSpace.Uri);
- i++;
- }
- }
- public void EnterScope()
- {
- depth++;
- }
- public void ExitScope()
- {
- while (nsCount > 0)
- {
- Namespace nameSpace = namespaces[nsCount - 1];
- if (nameSpace.Depth != depth)
- break;
- if (lastNameSpace == nameSpace)
- lastNameSpace = null;
- nameSpace.Clear();
- nsCount--;
- }
- while (attributeCount > 0)
- {
- XmlAttribute attribute = attributes[attributeCount - 1];
- if (attribute.Depth != depth)
- break;
- space = attribute.XmlSpace;
- lang = attribute.XmlLang;
- attribute.Clear();
- attributeCount--;
- }
- depth--;
- }
- public void AddLangAttribute(string lang)
- {
- AddAttribute();
- this.lang = lang;
- }
- public void AddSpaceAttribute(XmlSpace space)
- {
- AddAttribute();
- this.space = space;
- }
- void AddAttribute()
- {
- if (attributes == null)
- {
- attributes = new XmlAttribute[1];
- }
- else if (attributes.Length == attributeCount)
- {
- XmlAttribute[] newAttributes = new XmlAttribute[attributeCount * 2];
- Array.Copy(attributes, newAttributes, attributeCount);
- attributes = newAttributes;
- }
- XmlAttribute attribute = attributes[attributeCount];
- if (attribute == null)
- {
- attribute = new XmlAttribute();
- attributes[attributeCount] = attribute;
- }
- attribute.XmlLang = this.lang;
- attribute.XmlSpace = this.space;
- attribute.Depth = depth;
- attributeCount++;
- }
- public string AddNamespace(string uri, XmlDictionaryString uriDictionaryString)
- {
- if (uri.Length == 0)
- {
- // Empty namespace can only be bound to the empty prefix
- AddNamespaceIfNotDeclared(string.Empty, uri, uriDictionaryString);
- return string.Empty;
- }
- else
- {
- for (int i = 0; i < prefixes.Length; i++)
- {
- string prefix = prefixes[i];
- bool declared = false;
- for (int j = nsCount - 1; j >= nsTop; j--)
- {
- Namespace nameSpace = namespaces[j];
- if (nameSpace.Prefix == prefix)
- {
- declared = true;
- break;
- }
- }
- if (!declared)
- {
- AddNamespace(prefix, uri, uriDictionaryString);
- return prefix;
- }
- }
- }
- return null;
- }
- public void AddNamespaceIfNotDeclared(string prefix, string uri, XmlDictionaryString uriDictionaryString)
- {
- if (LookupNamespace(prefix) != uri)
- {
- AddNamespace(prefix, uri, uriDictionaryString);
- }
- }
- public void AddNamespace(string prefix, string uri, XmlDictionaryString uriDictionaryString)
- {
- if (prefix.Length >= 3)
- {
- // Upper and lower case letter differ by a bit.
- if ((prefix[0] & ~32) == 'X' && (prefix[1] & ~32) == 'M' && (prefix[2] & ~32) == 'L')
- {
- if (prefix == "xml" && uri == xmlNamespace)
- return;
- if (prefix == "xmlns" && uri == xmlnsNamespace)
- return;
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlReservedPrefix), "prefix"));
- }
- }
- Namespace nameSpace;
- for (int i = nsCount - 1; i >= 0; i--)
- {
- nameSpace = namespaces[i];
- if (nameSpace.Depth != depth)
- break;
- if (nameSpace.Prefix == prefix)
- {
- if (nameSpace.Uri == uri)
- return;
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlPrefixBoundToNamespace, prefix, nameSpace.Uri, uri), "prefix"));
- }
- }
- if (prefix.Length != 0 && uri.Length == 0)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlEmptyNamespaceRequiresNullPrefix), "prefix"));
- if (uri.Length == xmlnsNamespace.Length && uri == xmlnsNamespace)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlSpecificBindingNamespace, "xmlns", uri)));
- // The addressing namespace and the xmlNamespace are the same length, so add a quick check to try to disambiguate
- if (uri.Length == xmlNamespace.Length && uri[18] == 'X' && uri == xmlNamespace)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.XmlSpecificBindingNamespace, "xml", uri)));
- if (namespaces.Length == nsCount)
- {
- Namespace[] newNamespaces = new Namespace[nsCount * 2];
- Array.Copy(namespaces, newNamespaces, nsCount);
- namespaces = newNamespaces;
- }
- nameSpace = namespaces[nsCount];
- if (nameSpace == null)
- {
- nameSpace = new Namespace();
- namespaces[nsCount] = nameSpace;
- }
- nameSpace.Depth = depth;
- nameSpace.Prefix = prefix;
- nameSpace.Uri = uri;
- nameSpace.UriDictionaryString = uriDictionaryString;
- nsCount++;
- lastNameSpace = null;
- }
- public string LookupPrefix(string ns)
- {
- if (lastNameSpace != null && lastNameSpace.Uri == ns)
- return lastNameSpace.Prefix;
- int nsCount = this.nsCount;
- for (int i = nsCount - 1; i >= nsTop; i--)
- {
- Namespace nameSpace = namespaces[i];
- if (object.ReferenceEquals(nameSpace.Uri, ns))
- {
- string prefix = nameSpace.Prefix;
- // Make sure that the prefix refers to the namespace in scope
- bool declared = false;
- for (int j = i + 1; j < nsCount; j++)
- {
- if (namespaces[j].Prefix == prefix)
- {
- declared = true;
- break;
- }
- }
- if (!declared)
- {
- lastNameSpace = nameSpace;
- return prefix;
- }
- }
- }
- for (int i = nsCount - 1; i >= nsTop; i--)
- {
- Namespace nameSpace = namespaces[i];
- if (nameSpace.Uri == ns)
- {
- string prefix = nameSpace.Prefix;
- // Make sure that the prefix refers to the namespace in scope
- bool declared = false;
- for (int j = i + 1; j < nsCount; j++)
- {
- if (namespaces[j].Prefix == prefix)
- {
- declared = true;
- break;
- }
- }
- if (!declared)
- {
- lastNameSpace = nameSpace;
- return prefix;
- }
- }
- }
- if (ns.Length == 0)
- {
- // Make sure the default binding is still valid
- bool emptyPrefixUnassigned = true;
- for (int i = nsCount - 1; i >= nsTop; i--)
- {
- if (namespaces[i].Prefix.Length == 0)
- {
- emptyPrefixUnassigned = false;
- break;
- }
- }
- if (emptyPrefixUnassigned)
- return string.Empty;
- }
- if (ns == xmlnsNamespace)
- return "xmlns";
- if (ns == xmlNamespace)
- return "xml";
- return null;
- }
- public string LookupAttributePrefix(string ns)
- {
- if (lastNameSpace != null && lastNameSpace.Uri == ns && lastNameSpace.Prefix.Length != 0)
- return lastNameSpace.Prefix;
- int nsCount = this.nsCount;
- for (int i = nsCount - 1; i >= nsTop; i--)
- {
- Namespace nameSpace = namespaces[i];
- if (object.ReferenceEquals(nameSpace.Uri, ns))
- {
- string prefix = nameSpace.Prefix;
- if (prefix.Length != 0)
- {
- // Make sure that the prefix refers to the namespace in scope
- bool declared = false;
- for (int j = i + 1; j < nsCount; j++)
- {
- if (namespaces[j].Prefix == prefix)
- {
- declared = true;
- break;
- }
- }
- if (!declared)
- {
- lastNameSpace = nameSpace;
- return prefix;
- }
- }
- }
- }
- for (int i = nsCount - 1; i >= nsTop; i--)
- {
- Namespace nameSpace = namespaces[i];
- if (nameSpace.Uri == ns)
- {
- string prefix = nameSpace.Prefix;
- if (prefix.Length != 0)
- {
- // Make sure that the prefix refers to the namespace in scope
- bool declared = false;
- for (int j = i + 1; j < nsCount; j++)
- {
- if (namespaces[j].Prefix == prefix)
- {
- declared = true;
- break;
- }
- }
- if (!declared)
- {
- lastNameSpace = nameSpace;
- return prefix;
- }
- }
- }
- }
- if (ns.Length == 0)
- return string.Empty;
- return null;
- }
- public string LookupNamespace(string prefix)
- {
- int nsCount = this.nsCount;
- if (prefix.Length == 0)
- {
- for (int i = nsCount - 1; i >= nsTop; i--)
- {
- Namespace nameSpace = namespaces[i];
- if (nameSpace.Prefix.Length == 0)
- return nameSpace.Uri;
- }
- return string.Empty;
- }
- if (prefix.Length == 1)
- {
- char prefixChar = prefix[0];
- for (int i = nsCount - 1; i >= nsTop; i--)
- {
- Namespace nameSpace = namespaces[i];
- if (nameSpace.PrefixChar == prefixChar)
- return nameSpace.Uri;
- }
- return null;
- }
- for (int i = nsCount - 1; i >= nsTop; i--)
- {
- Namespace nameSpace = namespaces[i];
- if (nameSpace.Prefix == prefix)
- return nameSpace.Uri;
- }
- if (prefix == "xmlns")
- return xmlnsNamespace;
- if (prefix == "xml")
- return xmlNamespace;
- return null;
- }
- public void Sign(XmlCanonicalWriter signingWriter)
- {
- int nsCount = this.nsCount;
- Fx.Assert(nsCount >= 1 && namespaces[0].Prefix.Length == 0 && namespaces[0].Uri.Length == 0, "");
- for (int i = 1; i < nsCount; i++)
- {
- Namespace nameSpace = namespaces[i];
- bool found = false;
- for (int j = i + 1; j < nsCount && !found; j++)
- {
- found = (nameSpace.Prefix == namespaces[j].Prefix);
- }
- if (!found)
- {
- signingWriter.WriteXmlnsAttribute(nameSpace.Prefix, nameSpace.Uri);
- }
- }
- }
- class XmlAttribute
- {
- XmlSpace space;
- string lang;
- int depth;
- public XmlAttribute()
- {
- }
- public int Depth
- {
- get
- {
- return depth;
- }
- set
- {
- depth = value;
- }
- }
- public string XmlLang
- {
- get
- {
- return lang;
- }
- set
- {
- lang = value;
- }
- }
- public XmlSpace XmlSpace
- {
- get
- {
- return space;
- }
- set
- {
- space = value;
- }
- }
- public void Clear()
- {
- this.lang = null;
- }
- }
- class Namespace
- {
- string prefix;
- string ns;
- XmlDictionaryString xNs;
- int depth;
- char prefixChar;
- public Namespace()
- {
- }
- public void Clear()
- {
- this.prefix = null;
- this.prefixChar = (char)0;
- this.ns = null;
- this.xNs = null;
- this.depth = 0;
- }
- public int Depth
- {
- get
- {
- return depth;
- }
- set
- {
- depth = value;
- }
- }
- public char PrefixChar
- {
- get
- {
- return prefixChar;
- }
- }
- public string Prefix
- {
- get
- {
- return prefix;
- }
- set
- {
- if (value.Length == 1)
- prefixChar = value[0];
- else
- prefixChar = (char)0;
- prefix = value;
- }
- }
- public string Uri
- {
- get
- {
- return ns;
- }
- set
- {
- ns = value;
- }
- }
- public XmlDictionaryString UriDictionaryString
- {
- get
- {
- return xNs;
- }
- set
- {
- xNs = value;
- }
- }
- }
- }
- class XmlBaseWriterNodeWriterAsyncHelper
- {
- static AsyncEventArgsCallback onWriteComplete;
- XmlBaseWriter writer;
- byte[] buffer;
- int offset;
- int count;
- int actualByteCount;
- int totalByteCount;
- AsyncEventArgs<XmlNodeWriterWriteBase64TextArgs> nodeWriterAsyncState;
- XmlNodeWriterWriteBase64TextArgs nodeWriterArgs;
- AsyncEventArgs<XmlWriteBase64AsyncArguments> inputState;
- public XmlBaseWriterNodeWriterAsyncHelper(XmlBaseWriter writer)
- {
- this.writer = writer;
- }
- public void SetArguments(AsyncEventArgs<XmlWriteBase64AsyncArguments> inputState)
- {
- Fx.Assert(inputState != null, "InputState cannot be null.");
- this.inputState = inputState;
- this.buffer = inputState.Arguments.Buffer;
- this.offset = inputState.Arguments.Offset;
- this.count = inputState.Arguments.Count;
- }
- public AsyncCompletionResult StartAsync()
- {
- bool completeSelf = true;
- if (this.count > 0)
- {
- // Bytes that have been already been read.
- if (this.writer.trailByteCount > 0)
- {
- // Copy over up to 3 trailing bytes into the trailBytes buffer.
- while (this.writer.trailByteCount < 3 && this.count > 0)
- {
- this.writer.trailBytes[this.writer.trailByteCount++] = this.buffer[this.offset++];
- this.count--;
- }
- }
- this.totalByteCount = this.writer.trailByteCount + this.count;
- this.actualByteCount = this.totalByteCount - (this.totalByteCount % 3);
- if (this.writer.trailBytes == null)
- {
- this.writer.trailBytes = new byte[3];
- }
- if (actualByteCount >= 3)
- {
- if (this.writer.attributeValue != null)
- {
- this.writer.WriteAttributeText(XmlConverter.Base64Encoding.GetString(this.writer.trailBytes, 0, this.writer.trailByteCount));
- this.writer.WriteAttributeText(XmlConverter.Base64Encoding.GetString(this.buffer, this.offset, actualByteCount - this.writer.trailByteCount));
- }
- // StartContent/WriteBase64Text/EndContent will be called from HandleWriteBase64 as appropriate
- completeSelf = HandleWriteBase64Text(false);
- }
- else
- {
- Buffer.BlockCopy(this.buffer, this.offset, this.writer.trailBytes, this.writer.trailByteCount, this.count);
- this.writer.trailByteCount += this.count;
- }
- }
- if (completeSelf)
- {
- this.Clear();
- return AsyncCompletionResult.Completed;
- }
- return AsyncCompletionResult.Queued;
- }
- static void OnWriteComplete(IAsyncEventArgs asyncEventArgs)
- {
- bool completeSelf = false;
- Exception completionException = null;
- XmlBaseWriterNodeWriterAsyncHelper thisPtr = (XmlBaseWriterNodeWriterAsyncHelper)asyncEventArgs.AsyncState;
- AsyncEventArgs<XmlWriteBase64AsyncArguments> inputState = thisPtr.inputState;
- try
- {
- if (asyncEventArgs.Exception != null)
- {
- completionException = asyncEventArgs.Exception;
- completeSelf = true;
- }
- else
- {
- completeSelf = thisPtr.HandleWriteBase64Text(true);
- }
- }
- catch (Exception exception)
- {
- if (Fx.IsFatal(exception))
- {
- throw;
- }
- completionException = exception;
- completeSelf = true;
- }
- if (completeSelf)
- {
- thisPtr.Clear();
- inputState.Complete(false, completionException);
- }
- }
- bool HandleWriteBase64Text(bool isAsyncCallback)
- {
- Fx.Assert(this.count > 0 && this.actualByteCount >= 3, "HandleWriteBase64Text cannot be invoked with less than 3 bytes.");
- if (!writer.isXmlnsAttribute)
- {
- if (!isAsyncCallback)
- {
- if (this.nodeWriterAsyncState == null)
- {
- this.nodeWriterAsyncState = new AsyncEventArgs<XmlNodeWriterWriteBase64TextArgs>();
- this.nodeWriterArgs = new XmlNodeWriterWriteBase64TextArgs();
- }
- if (onWriteComplete == null)
- {
- onWriteComplete = new AsyncEventArgsCallback(OnWriteComplete);
- }
- this.writer.StartContent();
- this.nodeWriterArgs.TrailBuffer = this.writer.trailBytes;
- this.nodeWriterArgs.TrailCount = this.writer.trailByteCount;
- this.nodeWriterArgs.Buffer = this.buffer;
- this.nodeWriterArgs.Offset = this.offset;
- this.nodeWriterArgs.Count = this.actualByteCount - this.writer.trailByteCount;
- this.nodeWriterAsyncState.Set(onWriteComplete, this.nodeWriterArgs, this);
- if (this.writer.writer.WriteBase64TextAsync(this.nodeWriterAsyncState) != AsyncCompletionResult.Completed)
- {
- return false;
- }
- this.nodeWriterAsyncState.Complete(true);
- }
- this.writer.EndContent();
- }
- this.writer.trailByteCount = (this.totalByteCount - this.actualByteCount);
- if (this.writer.trailByteCount > 0)
- {
- int trailOffset = offset + count - this.writer.trailByteCount;
- for (int i = 0; i < this.writer.trailByteCount; i++)
- this.writer.trailBytes[i] = this.buffer[trailOffset++];
- }
- return true;
- }
- void Clear()
- {
- this.inputState = null;
- this.buffer = null;
- this.offset = 0;
- this.count = 0;
- this.actualByteCount = 0;
- this.totalByteCount = 0;
- }
- }
- }
- }
|