| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125 |
- //
- // System.Xml.XmlTextReader
- //
- // Author:
- // Jason Diamond ([email protected])
- // Adam Treat ([email protected])
- // Atsushi Enomoto ([email protected])
- //
- // (C) 2001, 2002 Jason Diamond http://injektilo.org/
- //
- // FIXME:
- //
- // NameTables aren't being used completely yet.
- //
- // Some thought needs to be given to performance. There's too many
- // strings being allocated.
- //
- // If current node is on an Attribute, Prefix might be null, and
- // in several fields which uses XmlReader, it should be considered.
- //
- using System;
- using System.Collections;
- using System.Collections.Specialized;
- using System.IO;
- using System.Text;
- using System.Xml.Schema;
- using Mono.Xml;
- using Mono.Xml.Native;
- namespace System.Xml
- {
- public class XmlTextReader : XmlReader, IXmlLineInfo
- {
- #region Constructors
- protected XmlTextReader ()
- {
- }
- public XmlTextReader (Stream input)
- : this (new XmlStreamReader (input))
- {
- }
- public XmlTextReader (string url)
- : this(url, new NameTable ())
- {
- }
- public XmlTextReader (TextReader input)
- : this (input, new NameTable ())
- {
- }
- protected XmlTextReader (XmlNameTable nt)
- : this (String.Empty, null, XmlNodeType.None, null)
- {
- }
- public XmlTextReader (Stream input, XmlNameTable nt)
- : this(new XmlStreamReader (input), nt)
- {
- }
- public XmlTextReader (string url, Stream input)
- : this (url, new XmlStreamReader (input))
- {
- }
- public XmlTextReader (string url, TextReader input)
- : this (url, input, new NameTable ())
- {
- }
- public XmlTextReader (string url, XmlNameTable nt)
- : this (url, new XmlStreamReader (url, null, null), nt)
- {
- }
- public XmlTextReader (TextReader input, XmlNameTable nt)
- : this (String.Empty, input, nt)
- {
- }
- public XmlTextReader (Stream xmlFragment, XmlNodeType fragType, XmlParserContext context)
- : this (context != null ? context.BaseURI : String.Empty,
- new XmlStreamReader (xmlFragment),
- fragType,
- context)
- {
- }
- public XmlTextReader (string url, Stream input, XmlNameTable nt)
- : this (url, new XmlStreamReader (input), nt)
- {
- }
- public XmlTextReader (string url, TextReader input, XmlNameTable nt)
- : this (url, input, XmlNodeType.Document, null)
- {
- }
- public XmlTextReader (string xmlFragment, XmlNodeType fragType, XmlParserContext context)
- : this (context != null ? context.BaseURI : String.Empty,
- new StringReader (xmlFragment),
- fragType,
- context)
- {
- }
- XmlTextReader (string url, TextReader fragment, XmlNodeType fragType, XmlParserContext context)
- {
- InitializeContext (url, context, fragment, fragType);
- }
- #endregion
- #region Properties
- public override int AttributeCount
- {
- get { return attributeCount; }
- }
- public override string BaseURI
- {
- get { return parserContext.BaseURI; }
- }
- public override int Depth
- {
- get {
- if (currentAttributeValue >= 0)
- return elementDepth + 2; // inside attribute value.
- else if (currentAttribute >= 0)
- return elementDepth + 1;
- return elementDepth;
- }
- }
- public Encoding Encoding
- {
- get { return parserContext.Encoding; }
- }
- public override bool EOF
- {
- get
- {
- return
- readState == ReadState.EndOfFile ||
- readState == ReadState.Closed;
- }
- }
- public override bool HasValue
- {
- get {
- if (this.valueBuilderAvailable)
- return valueBuilder.Length != 0;
- else
- return cursorToken.Value != null;
- }
- }
- public override bool IsDefault
- {
- get
- {
- // XmlTextReader does not expand default attributes.
- return false;
- }
- }
- public override bool IsEmptyElement
- {
- get { return cursorToken.IsEmptyElement; }
- }
- public override string this [int i]
- {
- get { return GetAttribute (i); }
- }
- public override string this [string name]
- {
- get { return GetAttribute (name); }
- }
- public override string this [string localName, string namespaceName]
- {
- get { return GetAttribute (localName, namespaceName); }
- }
- public int LineNumber
- {
- get {
- if (useProceedingLineInfo)
- return currentInput.LineNumber;
- else
- return cursorToken.LineNumber;
- }
- }
- public int LinePosition
- {
- get {
- if (useProceedingLineInfo)
- return currentInput.LinePosition;
- else
- return cursorToken.LinePosition;
- }
- }
- public override string LocalName
- {
- get { return cursorToken.LocalName; }
- }
- public override string Name
- {
- get { return cursorToken.Name; }
- }
- public bool Namespaces
- {
- get { return namespaces; }
- set {
- if (readState != ReadState.Initial)
- throw new InvalidOperationException ("Namespaces have to be set before reading.");
- namespaces = value;
- }
- }
- public override string NamespaceURI
- {
- get { return cursorToken.NamespaceURI; }
- }
- public override XmlNameTable NameTable
- {
- get { return parserContext.NameTable; }
- }
- public override XmlNodeType NodeType
- {
- get { return cursorToken.NodeType; }
- }
- [MonoTODO]
- public bool Normalization
- {
- get { return normalization; }
- set { normalization = value; }
- }
- public override string Prefix
- {
- get { return cursorToken.Prefix; }
- }
- public override char QuoteChar
- {
- get { return cursorToken.QuoteChar; }
- }
- public override ReadState ReadState
- {
- get { return readState; }
- }
- public override string Value
- {
- get { return cursorToken.Value != null ? cursorToken.Value : String.Empty; }
- }
- public WhitespaceHandling WhitespaceHandling
- {
- get { return whitespaceHandling; }
- set { whitespaceHandling = value; }
- }
- public override string XmlLang
- {
- get { return parserContext.XmlLang; }
- }
- public XmlResolver XmlResolver
- {
- set { resolver = value; }
- }
- public override XmlSpace XmlSpace
- {
- get { return parserContext.XmlSpace; }
- }
- #endregion
- #region Methods
- public override void Close ()
- {
- readState = ReadState.Closed;
- foreach (XmlParserInput input in parserInputStack.ToArray ())
- input.Close ();
- this.currentInput.Close ();
- cursorToken.Clear ();
- currentToken.Clear ();
- attributeCount = 0;
- }
- public override string GetAttribute (int i)
- {
- if (i > attributeCount)
- throw new ArgumentOutOfRangeException ("i is smaller than AttributeCount");
- else {
- return attributeTokens [i].Value;
- }
- }
- // MS.NET 1.0 msdn says that this method returns String.Empty
- // for absent attribute, but in fact it returns null.
- // This description is corrected in MS.NET 1.1 msdn.
- public override string GetAttribute (string name)
- {
- for (int i = 0; i < attributeCount; i++)
- if (attributeTokens [i].Name == name)
- return attributeTokens [i].Value;
- return null;
- }
- private int GetIndexOfQualifiedAttribute (string localName, string namespaceURI)
- {
- for (int i = 0; i < attributeCount; i++) {
- XmlAttributeTokenInfo ti = attributeTokens [i];
- if (ti.LocalName == localName && ti.NamespaceURI == namespaceURI)
- return i;
- }
- return -1;
- }
- internal XmlParserContext GetInternalParserContext ()
- {
- return parserContext;
- }
- public override string GetAttribute (string localName, string namespaceURI)
- {
- int idx = this.GetIndexOfQualifiedAttribute (localName, namespaceURI);
- if (idx < 0)
- return null;
- return attributeTokens [idx].Value;
- }
- [MonoTODO]
- public TextReader GetRemainder ()
- {
- throw new NotImplementedException ();
- }
- bool IXmlLineInfo.HasLineInfo ()
- {
- return true;
- }
- public override string LookupNamespace (string prefix)
- {
- return parserContext.NamespaceManager.LookupNamespace (prefix);
- }
- public override void MoveToAttribute (int i)
- {
- if (i >= attributeCount)
- throw new ArgumentOutOfRangeException ("attribute index out of range.");
- currentAttribute = i;
- currentAttributeValue = -1;
- cursorToken = attributeTokens [i];
- }
- public override bool MoveToAttribute (string name)
- {
- for (int i = 0; i < attributeCount; i++) {
- XmlAttributeTokenInfo ti = attributeTokens [i];
- if (ti.Name == name) {
- MoveToAttribute (i);
- return true;
- }
- }
- return false;
- }
- public override bool MoveToAttribute (string localName, string namespaceName)
- {
- int idx = GetIndexOfQualifiedAttribute (localName, namespaceName);
- if (idx < 0)
- return false;
- MoveToAttribute (idx);
- return true;
- }
- public override bool MoveToElement ()
- {
- if (currentToken == null) // for attribute .ctor()
- return false;
- if (cursorToken == currentToken)
- return false;
- if (currentAttribute >= 0) {
- currentAttribute = -1;
- currentAttributeValue = -1;
- cursorToken = currentToken;
- return true;
- }
- else
- return false;
- }
- public override bool MoveToFirstAttribute ()
- {
- if (attributeCount == 0)
- return false;
- MoveToElement ();
- return MoveToNextAttribute ();
- }
- public override bool MoveToNextAttribute ()
- {
- if (currentAttribute == 0 && attributeCount == 0)
- return false;
- if (currentAttribute + 1 < attributeCount) {
- currentAttribute++;
- currentAttributeValue = -1;
- cursorToken = attributeTokens [currentAttribute];
- return true;
- }
- else
- return false;
- }
- public override bool Read ()
- {
- if (startNodeType == XmlNodeType.Attribute) {
- if (currentAttribute == 0)
- return false; // already read.
- ClearAttributes ();
- IncrementAttributeToken ();
- ReadAttributeValueTokens ('"');
- cursorToken = attributeTokens [0];
- currentAttributeValue = -1;
- readState = ReadState.Interactive;
- return true;
- }
- bool more = false;
- readState = ReadState.Interactive;
- currentLinkedNodeLineNumber = currentInput.LineNumber;
- currentLinkedNodeLinePosition = currentInput.LinePosition;
- useProceedingLineInfo = true;
- cursorToken = currentToken;
- attributeCount = 0;
- currentAttribute = currentAttributeValue = -1;
- currentToken.Clear ();
- // It was moved from end of ReadStartTag ().
- if (depthUp)
- ++depth;
- depthUp = false;
- more = ReadContent ();
- if (depth == 0 && !allowMultipleRoot && (IsEmptyElement || NodeType == XmlNodeType.EndElement))
- currentState = XmlNodeType.EndElement;
- if (maybeTextDecl != 0)
- maybeTextDecl--;
- if (!more && startNodeType == XmlNodeType.Document && currentState != XmlNodeType.EndElement)
- throw new XmlException ("Document element did not appear.");
- useProceedingLineInfo = false;
- return more;
- }
- public override bool ReadAttributeValue ()
- {
- if (readState == ReadState.Initial && startNodeType == XmlNodeType.Attribute) {
- Read ();
- }
- if (currentAttribute < 0)
- return false;
- XmlAttributeTokenInfo ti = attributeTokens [currentAttribute];
- if (currentAttributeValue < 0)
- currentAttributeValue = ti.ValueTokenStartIndex - 1;
- if (currentAttributeValue < ti.ValueTokenEndIndex) {
- currentAttributeValue++;
- cursorToken = attributeValueTokens [currentAttributeValue];
- return true;
- }
- else
- return false;
- }
- [MonoTODO]
- public int ReadBase64 (byte [] buffer, int offset, int length)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public int ReadBinHex (byte [] buffer, int offset, int length)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public int ReadChars (char [] buffer, int offset, int length)
- {
- throw new NotImplementedException ();
- }
- #if NET_1_0
- public override string ReadInnerXml ()
- {
- if (readState != ReadState.Interactive)
- return String.Empty;
- switch (NodeType) {
- case XmlNodeType.Attribute:
- return value.Substring (1, value.Length - 2);
- case XmlNodeType.Element:
- if (IsEmptyElement)
- return String.Empty;
- int startDepth = depth;
- if (innerXmlBuilder == null)
- innerXmlBuilder = new StringBuilder ();
- innerXmlBuilder.Length = 0;
- bool loop = true;
- do {
- Read ();
- if (NodeType ==XmlNodeType.None)
- throw new XmlException ("unexpected end of xml.");
- else if (NodeType == XmlNodeType.EndElement && depth == startDepth) {
- loop = false;
- Read ();
- }
- else
- innerXmlBuilder.Append (currentTag);
- } while (loop);
- string xml = innerXmlBuilder.ToString ();
- innerXmlBuilder.Length = 0;
- return xml;
- case XmlNodeType.None:
- // MS document is incorrect. Seems not to progress.
- return String.Empty;
- default:
- Read ();
- return String.Empty;
- }
- }
- public override string ReadOuterXml ()
- {
- if (readState != ReadState.Interactive)
- return String.Empty;
- switch (NodeType) {
- case XmlNodeType.Attribute:
- // strictly incompatible with MS... (it holds spaces attribute between name, value and "=" char (very trivial).
- return String.Format ("{0}={1}{2}{1}", Name, QuoteChar, ReadInnerXml ());
- case XmlNodeType.Element:
- bool isEmpty = IsEmptyElement;
- string startTag = currentTag.ToString ();
- string name = Name;
- if (NodeType == XmlNodeType.Element && !isEmpty)
- return String.Format ("{0}{1}</{2}>", startTag, ReadInnerXml (), name);
- else
- return currentTag.ToString ();
- case XmlNodeType.None:
- // MS document is incorrect. Seems not to progress.
- return String.Empty;
- default:
- Read ();
- return String.Empty;
- }
- }
- #endif
- public override string ReadString ()
- {
- return ReadStringInternal ();
- }
- public void ResetState ()
- {
- Init ();
- }
- public override void ResolveEntity ()
- {
- // XmlTextReader does not resolve entities.
- throw new InvalidOperationException ("XmlTextReader cannot resolve external entities.");
- }
- #endregion
- #region Internals
- // Parsed DTD Objects
- internal DTDObjectModel DTD {
- get { return parserContext.Dtd; }
- }
- internal bool MaybeTextDecl {
- set { if (value) this.maybeTextDecl = 2; }
- }
- #endregion
- #region Privates
- internal class XmlTokenInfo
- {
- public XmlTokenInfo (XmlTextReader xtr)
- {
- Reader = xtr;
- Clear ();
- }
- string valueCache;
- protected XmlTextReader Reader;
- public string Name;
- public string LocalName;
- public string Prefix;
- public string NamespaceURI;
- public bool IsEmptyElement;
- public char QuoteChar;
- public int LineNumber;
- public int LinePosition;
- public XmlNodeType NodeType;
- public virtual string Value {
- get {
- if (valueCache != null)
- return valueCache;
- else if (Reader.valueBuilderAvailable) {
- valueCache = Reader.valueBuilder.ToString ();
- return valueCache;
- }
- return valueCache;
- }
- set {
- valueCache = value;
- }
- }
- public virtual void Clear ()
- {
- valueCache = null;
- NodeType = XmlNodeType.None;
- Name = LocalName = Prefix = NamespaceURI = String.Empty;
- IsEmptyElement = false;
- QuoteChar = '"';
- LineNumber = LinePosition = 0;
- }
- internal virtual void FillNames ()
- {
- if (Reader.Namespaces) {
- int indexOfColon = Name.IndexOf (':');
- if (indexOfColon == -1) {
- Prefix = String.Empty;
- LocalName = Name;
- } else {
- Prefix = Reader.NameTable.Add (Name.Substring (0, indexOfColon));
- LocalName = Reader.NameTable.Add (Name.Substring (indexOfColon + 1));
- }
- // NamespaceURI
- switch (NodeType) {
- case XmlNodeType.Attribute:
- if (Prefix == string.Empty)
- NamespaceURI = string.Empty;
- else
- NamespaceURI = Reader.LookupNamespace (Prefix);
- break;
- case XmlNodeType.Element:
- case XmlNodeType.EndElement:
- NamespaceURI = Reader.LookupNamespace (Prefix);
- break;
- default:
- NamespaceURI = "";
- break;
- }
- } else {
- Prefix = String.Empty;
- LocalName = Name;
- }
- }
- }
- internal class XmlAttributeTokenInfo : XmlTokenInfo
- {
- public XmlAttributeTokenInfo (XmlTextReader reader)
- : base (reader)
- {
- NodeType = XmlNodeType.Attribute;
- }
- public int ValueTokenStartIndex;
- public int ValueTokenEndIndex;
- string valueCache;
- public override string Value {
- get {
- if (valueCache != null)
- return valueCache;
- // An empty value should return String.Empty.
- if (ValueTokenStartIndex == ValueTokenEndIndex) {
- XmlTokenInfo ti = Reader.attributeValueTokens [ValueTokenStartIndex];
- if (ti.NodeType == XmlNodeType.Text)
- valueCache = ti.Value;
- else
- valueCache = String.Concat ("&", ti.Name, ";");
- return valueCache;
- }
- StringBuilder sb = new StringBuilder ();
- for (int i = ValueTokenStartIndex; i <= ValueTokenEndIndex; i++) {
- XmlTokenInfo ti = Reader.attributeValueTokens [i];
- if (ti.NodeType == XmlNodeType.Text)
- sb.Append (ti.Value);
- else {
- sb.Append ('&');
- sb.Append (ti.Name);
- sb.Append (';');
- }
- }
- valueCache = sb.ToString ();
- return valueCache;
- }
- set {
- valueCache = value;
- }
- }
- public override void Clear ()
- {
- base.Clear ();
- valueCache = null;
- NodeType = XmlNodeType.Attribute;
- ValueTokenStartIndex = ValueTokenEndIndex = 0;
- }
- internal override void FillNames ()
- {
- base.FillNames ();
- if (Prefix == "xmlns" || Name == "xmlns")
- NamespaceURI = XmlNamespaceManager.XmlnsXmlns;
- }
- }
- private XmlTokenInfo cursorToken;
- private XmlTokenInfo currentToken;
- private XmlAttributeTokenInfo currentAttributeToken;
- private XmlTokenInfo currentAttributeValueToken;
- private XmlAttributeTokenInfo [] attributeTokens = new XmlAttributeTokenInfo [10];
- private XmlTokenInfo [] attributeValueTokens = new XmlTokenInfo [10];
- private int currentAttribute;
- private int currentAttributeValue;
- private int attributeCount;
- private XmlParserContext parserContext;
- private XmlParserInput currentInput;
- private Stack parserInputStack;
- private ReadState readState;
- private int depth;
- private int elementDepth;
- private bool depthUp;
- private bool popScope;
- private Stack elementStack;
- private bool allowMultipleRoot;
- private bool isStandalone;
- private StringBuilder valueBuilder;
- private bool valueBuilderAvailable = false;
- private bool returnEntityReference;
- private string entityReferenceName;
- private char [] nameBuffer;
- private int nameLength;
- private int nameCapacity;
- private const int initialNameCapacity = 256;
- private StringBuilder valueBuffer;
- private int currentLinkedNodeLineNumber;
- private int currentLinkedNodeLinePosition;
- private bool useProceedingLineInfo;
- // A buffer for ReadContent for ReadOuterXml
- private StringBuilder currentTag {
- get {
- return currentInput.CurrentMarkup;
- }
- }
- // Parameter entity placeholder
- private Hashtable parameterEntities;
- private int dtdIncludeSect;
- private XmlNodeType startNodeType;
- // State machine attribute.
- // XmlDeclaration: after the first node.
- // DocumentType: after doctypedecl
- // Element: inside document element
- // EndElement: after document element
- private XmlNodeType currentState;
- private int maybeTextDecl;
- private XmlResolver resolver = new XmlUrlResolver ();
- // These values are never re-initialized.
- private bool namespaces = true;
- private WhitespaceHandling whitespaceHandling = WhitespaceHandling.All;
- private bool normalization = false;
- private void Init ()
- {
- readState = ReadState.Initial;
- currentState = XmlNodeType.None;
- maybeTextDecl = 0;
- allowMultipleRoot = false;
- depth = 0;
- depthUp = false;
- popScope = false;
- parserInputStack = new Stack ();
- elementStack = new Stack();
- currentAttribute = -1;
- currentAttributeValue = -1;
- returnEntityReference = false;
- entityReferenceName = String.Empty;
- nameBuffer = new char [initialNameCapacity];
- nameLength = 0;
- nameCapacity = initialNameCapacity;
-
- valueBuffer = new StringBuilder (512);
- parameterEntities = new Hashtable ();
- currentToken = new XmlTokenInfo (this);
- cursorToken = currentToken;
- }
- private void InitializeContext (string url, XmlParserContext context, TextReader fragment, XmlNodeType fragType)
- {
- startNodeType = fragType;
- parserContext = context;
- if (context == null) {
- XmlNameTable nt = new NameTable ();
- parserContext = new XmlParserContext (nt,
- new XmlNamespaceManager (nt),
- String.Empty,
- XmlSpace.None);
- }
- if (url != null && url != String.Empty) {
- string path = Path.GetFullPath ("./a");
- Uri uri = new Uri (new Uri (path), url);
- parserContext.BaseURI = uri.ToString ();
- }
- Init ();
- switch (fragType) {
- case XmlNodeType.Attribute:
- fragment = new StringReader (fragment.ReadToEnd ().Replace ("\"", """));
- break;
- case XmlNodeType.Element:
- currentState = XmlNodeType.Element;
- allowMultipleRoot = true;
- break;
- case XmlNodeType.Document:
- break;
- default:
- throw new XmlException (String.Format ("NodeType {0} is not allowed to create XmlTextReader.", fragType));
- }
- this.currentInput = new XmlParserInput (fragment, url);
- }
- // Use this method rather than setting the properties
- // directly so that all the necessary properties can
- // be changed in harmony with each other. Maybe the
- // fields should be in a seperate class to help enforce
- // this.
- private void SetProperties (
- XmlNodeType nodeType,
- string name,
- bool isEmptyElement,
- string value,
- bool clearAttributes)
- {
- SetProperties (currentToken, nodeType, name, isEmptyElement, value, clearAttributes);
- currentToken.LineNumber = this.currentLinkedNodeLineNumber;
- currentToken.LinePosition = this.currentLinkedNodeLinePosition;
- }
- private void SetProperties (
- XmlTokenInfo token,
- XmlNodeType nodeType,
- string name,
- bool isEmptyElement,
- string value,
- bool clearAttributes)
- {
- this.valueBuilderAvailable = false;
- token.Clear ();
- token.NodeType = nodeType;
- token.Name = name;
- token.IsEmptyElement = isEmptyElement;
- token.Value = value;
- this.elementDepth = depth;
- if (clearAttributes)
- ClearAttributes ();
- token.FillNames ();
- }
- private void SetProperties (
- XmlNodeType nodeType,
- string name,
- bool isEmptyElement,
- bool clearAttributes,
- StringBuilder value) {
- SetProperties (nodeType, name, isEmptyElement, (string)null, clearAttributes);
- this.valueBuilderAvailable = true;
- this.valueBuilder = value;
- }
- private void ClearAttributes ()
- {
- for (int i = 0; i < attributeCount; i++)
- attributeTokens [i].Clear ();
- attributeCount = 0;
- currentAttribute = -1;
- currentAttributeValue = -1;
- }
- private int PeekChar ()
- {
- return currentInput.PeekChar ();
- }
- private int ReadChar ()
- {
- return currentInput.ReadChar ();
- }
- // This should really keep track of some state so
- // that it's not possible to have more than one document
- // element or text outside of the document element.
- private bool ReadContent ()
- {
- currentTag.Length = 0;
- if (popScope) {
- parserContext.NamespaceManager.PopScope ();
- popScope = false;
- }
- if (returnEntityReference)
- SetEntityReferenceProperties ();
- else {
- switch (PeekChar ()) {
- case '<':
- ReadChar ();
- ReadTag ();
- break;
- case '\r': goto case ' ';
- case '\n': goto case ' ';
- case '\t': goto case ' ';
- case ' ':
- if (whitespaceHandling == WhitespaceHandling.All ||
- whitespaceHandling == WhitespaceHandling.Significant)
- ReadWhitespace ();
- else {
- SkipWhitespace ();
- return ReadContent ();
- }
- break;
- case -1:
- readState = ReadState.EndOfFile;
- SetProperties (
- XmlNodeType.None, // nodeType
- String.Empty, // name
- false, // isEmptyElement
- (string) null, // value
- true // clearAttributes
- );
- if (depth > 0)
- throw new XmlException ("unexpected end of file. Current depth is " + depth);
- return false;
- default:
- ReadText (true);
- break;
- }
- }
- if (NodeType == XmlNodeType.XmlDeclaration && maybeTextDecl == 1)
- return ReadContent ();
- return this.ReadState != ReadState.EndOfFile;
- }
- private void SetEntityReferenceProperties ()
- {
- /*
- if (resolver != null) {
- if (DTD == null)
- throw new XmlException (this as IXmlLineInfo,
- "Entity reference is not allowed without document type declaration.");
- else if((!DTD.InternalSubsetHasPEReference || isStandalone) &&
- DTD.EntityDecls [entityReferenceName] == null)
- throw new XmlException (this as IXmlLineInfo,
- "Required entity declaration for '" + entityReferenceName + "' was not found.");
- string dummy = DTD.EntityDecls [entityReferenceName].EntityValue;
- }
- */
- SetProperties (
- XmlNodeType.EntityReference, // nodeType
- entityReferenceName, // name
- false, // isEmptyElement
- (string) null, // value
- true // clearAttributes
- );
- returnEntityReference = false;
- entityReferenceName = String.Empty;
- }
- // The leading '<' has already been consumed.
- private void ReadTag ()
- {
- switch (PeekChar ())
- {
- case '/':
- ReadChar ();
- ReadEndTag ();
- break;
- case '?':
- ReadChar ();
- ReadProcessingInstruction ();
- break;
- case '!':
- ReadChar ();
- ReadDeclaration ();
- break;
- default:
- ReadStartTag ();
- break;
- }
- }
- // The leading '<' has already been consumed.
- private void ReadStartTag ()
- {
- if (currentState == XmlNodeType.EndElement)
- throw new XmlException (this as IXmlLineInfo,
- "Element cannot appear in this state.");
- currentState = XmlNodeType.Element;
- parserContext.NamespaceManager.PushScope ();
- string name = ReadName ();
- if (currentState == XmlNodeType.EndElement)
- throw new XmlException (this as IXmlLineInfo,"document has terminated, cannot open new element");
- bool isEmptyElement = false;
- ClearAttributes ();
- SkipWhitespace ();
- if (XmlChar.IsFirstNameChar (PeekChar ()))
- ReadAttributes (false);
- cursorToken = this.currentToken;
- // fill namespaces
- for (int i = 0; i < attributeCount; i++)
- attributeTokens [i].FillNames ();
- // quick name check
- for (int i = 0; i < attributeCount; i++)
- for (int j = i + 1; j < attributeCount; j++)
- if (Object.ReferenceEquals (attributeTokens [i].Name, attributeTokens [j].Name) ||
- (Object.ReferenceEquals (attributeTokens [i].LocalName, attributeTokens [j].LocalName) &&
- Object.ReferenceEquals (attributeTokens [i].NamespaceURI, attributeTokens [j].NamespaceURI)))
- throw new XmlException (this as IXmlLineInfo,
- "Attribute name and qualified name must be identical.");
- string baseUri = GetAttribute ("xml:base");
- if (baseUri != null)
- parserContext.BaseURI = baseUri;
- string xmlLang = GetAttribute ("xml:lang");
- if (xmlLang != null)
- parserContext.XmlLang = xmlLang;
- string xmlSpaceAttr = GetAttribute ("xml:space");
- if (xmlSpaceAttr != null) {
- if (xmlSpaceAttr == "preserve")
- parserContext.XmlSpace = XmlSpace.Preserve;
- else if (xmlSpaceAttr == "default")
- parserContext.XmlSpace = XmlSpace.Default;
- else
- throw new XmlException (this as IXmlLineInfo,String.Format ("Invalid xml:space value: {0}", xmlSpaceAttr));
- }
- if (PeekChar () == '/') {
- ReadChar ();
- isEmptyElement = true;
- popScope = true;
- }
- else {
- depthUp = true;
- elementStack.Push (name);
- parserContext.PushScope ();
- }
- Expect ('>');
- SetProperties (
- XmlNodeType.Element, // nodeType
- name, // name
- isEmptyElement, // isEmptyElement
- (string) null, // value
- false // clearAttributes
- );
- }
- // The reader is positioned on the first character
- // of the element's name.
- private void ReadEndTag ()
- {
- if (currentState != XmlNodeType.Element)
- throw new XmlException (this as IXmlLineInfo,
- "End tag cannot appear in this state.");
- string name = ReadName ();
- if (elementStack.Count == 0)
- throw new XmlException (this as IXmlLineInfo,"closing element without matching opening element");
- string expected = (string)elementStack.Pop();
- if (expected != name)
- throw new XmlException (this as IXmlLineInfo,String.Format ("unmatched closing element: expected {0} but found {1}", expected, name));
- parserContext.PopScope ();
- SkipWhitespace ();
- Expect ('>');
- --depth;
- SetProperties (
- XmlNodeType.EndElement, // nodeType
- name, // name
- false, // isEmptyElement
- (string) null, // value
- true // clearAttributes
- );
- popScope = true;
- }
- private void AppendNameChar (int ch)
- {
- CheckNameCapacity ();
- nameBuffer [nameLength++] = (char)ch;
- }
- private void CheckNameCapacity ()
- {
- if (nameLength == nameCapacity) {
- nameCapacity = nameCapacity * 2;
- char [] oldNameBuffer = nameBuffer;
- nameBuffer = new char [nameCapacity];
- Array.Copy (oldNameBuffer, nameBuffer, nameLength);
- }
- }
- private string CreateNameString ()
- {
- return parserContext.NameTable.Add (nameBuffer, 0, nameLength);
- }
- private void AppendValueChar (int ch)
- {
- valueBuffer.Append ((char)ch);
- }
- private string CreateValueString ()
- {
- return valueBuffer.ToString ();
- }
-
- private void ClearValueBuffer ()
- {
- valueBuffer.Length = 0;
- }
- // The reader is positioned on the first character
- // of the text.
- private void ReadText (bool notWhitespace)
- {
- if (currentState != XmlNodeType.Element)
- throw new XmlException (this as IXmlLineInfo,
- "Text node cannot appear in this state.");
- if (notWhitespace)
- ClearValueBuffer ();
- int ch = PeekChar ();
- int previousCloseBracketLine = 0;
- int previousCloseBracketColumn = 0;
- while (ch != '<' && ch != -1) {
- if (ch == '&') {
- ReadChar ();
- if (ReadReference (false))
- break;
- } else {
- if (XmlConstructs.IsInvalid (ch))
- throw new XmlException (this as IXmlLineInfo,
- "Not allowed character was found.");
- AppendValueChar (ReadChar ());
- if (ch == ']') {
- if (previousCloseBracketColumn == LinePosition - 1 &&
- previousCloseBracketLine == LineNumber)
- if (PeekChar () == '>')
- throw new XmlException (this as IXmlLineInfo,
- "Inside text content, character sequence ']]>' is not allowed.");
- previousCloseBracketColumn = LinePosition;
- previousCloseBracketLine = LineNumber;
- }
- }
- ch = PeekChar ();
- notWhitespace = true;
- }
- if (returnEntityReference && valueBuffer.Length == 0) {
- SetEntityReferenceProperties ();
- } else {
- XmlNodeType nodeType = notWhitespace ? XmlNodeType.Text :
- this.XmlSpace == XmlSpace.Preserve ? XmlNodeType.SignificantWhitespace : XmlNodeType.Whitespace;
- SetProperties (
- nodeType, // nodeType
- String.Empty, // name
- false, // isEmptyElement
- true, // clearAttributes
- valueBuffer // value
- );
- }
- }
- // The leading '&' has already been consumed.
- // Returns true if the entity reference isn't a simple
- // character reference or one of the predefined entities.
- // This allows the ReadText method to break so that the
- // next call to Read will return the EntityReference node.
- private bool ReadReference (bool ignoreEntityReferences)
- {
- if (PeekChar () == '#') {
- ReadChar ();
- ReadCharacterReference ();
- } else
- ReadEntityReference (ignoreEntityReferences);
- return returnEntityReference;
- }
- private void ReadCharacterReference ()
- {
- int value = 0;
- if (PeekChar () == 'x') {
- ReadChar ();
- while (PeekChar () != ';' && PeekChar () != -1) {
- int ch = ReadChar ();
- if (ch >= '0' && ch <= '9')
- value = (value << 4) + ch - '0';
- else if (ch >= 'A' && ch <= 'F')
- value = (value << 4) + ch - 'A' + 10;
- else if (ch >= 'a' && ch <= 'f')
- value = (value << 4) + ch - 'a' + 10;
- else
- throw new XmlException (this as IXmlLineInfo,
- String.Format (
- "invalid hexadecimal digit: {0} (#x{1:X})",
- (char)ch,
- ch));
- }
- } else {
- while (PeekChar () != ';' && PeekChar () != -1) {
- int ch = ReadChar ();
- if (ch >= '0' && ch <= '9')
- value = value * 10 + ch - '0';
- else
- throw new XmlException (this as IXmlLineInfo,
- String.Format (
- "invalid decimal digit: {0} (#x{1:X})",
- (char)ch,
- ch));
- }
- }
- ReadChar (); // ';'
- // FIXME: how to handle such chars larger than 0xffff?
- if (value < 0xffff && !XmlConstructs.IsValid (value))
- throw new XmlException (this as IXmlLineInfo,
- "Referenced character was not allowed in XML.");
- AppendValueChar (value);
- }
- private void ReadEntityReference (bool ignoreEntityReferences)
- {
- nameLength = 0;
- int ch = PeekChar ();
- while (ch != ';' && ch != -1) {
- AppendNameChar (ReadChar ());
- ch = PeekChar ();
- }
- Expect (';');
- string name = CreateNameString ();
- if (!XmlChar.IsName (name))
- throw new XmlException (this as IXmlLineInfo,
- "Invalid entity reference name was found.");
- char predefined = XmlChar.GetPredefinedEntity (name);
- if (predefined != 0)
- AppendValueChar (predefined);
- else {
- if (ignoreEntityReferences) {
- AppendValueChar ('&');
- foreach (char ch2 in name) {
- AppendValueChar (ch2);
- }
- AppendValueChar (';');
- } else {
- returnEntityReference = true;
- entityReferenceName = name;
- }
- }
- }
- // The reader is positioned on the first character of
- // the attribute name.
- private void ReadAttributes (bool endsWithQuestion)
- {
- int peekChar = -1;
- bool requireWhitespace = false;
- currentAttribute = -1;
- currentAttributeValue = -1;
- do {
- if (!SkipWhitespace () && requireWhitespace)
- throw new XmlException ("Unexpected token. Name is required here.");
- IncrementAttributeToken ();
- currentAttributeToken.LineNumber = currentInput.LineNumber;
- currentAttributeToken.LinePosition = currentInput.LinePosition;
- currentAttributeToken.Name = ReadName ();
- SkipWhitespace ();
- Expect ('=');
- SkipWhitespace ();
- ReadAttributeValueTokens (-1);
- attributeCount++;
- if (currentAttributeToken.Name == "xmlns")
- parserContext.NamespaceManager.AddNamespace (String.Empty, GetAttribute (currentAttribute));
- else if (currentAttributeToken.Name.StartsWith ("xmlns:")) {
- string nsPrefix = NameTable.Add (currentAttributeToken.Name.Substring (6));
- parserContext.NamespaceManager.AddNamespace (nsPrefix, GetAttribute (currentAttribute));
- }
- if (!SkipWhitespace ())
- requireWhitespace = true;
- peekChar = PeekChar ();
- if (endsWithQuestion) {
- if (peekChar == '?')
- break;
- }
- else if (peekChar == '/' || peekChar == '>')
- break;
- } while (peekChar != -1);
- currentAttribute = -1;
- currentAttributeValue = -1;
- }
- private void AddAttribute (string name, string value)
- {
- IncrementAttributeToken ();
- XmlAttributeTokenInfo ati = attributeTokens [currentAttribute];
- ati.Name = "SYSTEM";
- ati.FillNames ();
- IncrementAttributeValueToken ();
- XmlTokenInfo vti = attributeValueTokens [currentAttributeValue];
- vti.Value = value;
- SetProperties (vti, XmlNodeType.Text, name, false, value, false);
- attributeCount++;
- }
- private void IncrementAttributeToken ()
- {
- currentAttribute++;
- if (attributeTokens.Length == currentAttribute) {
- XmlAttributeTokenInfo [] newArray =
- new XmlAttributeTokenInfo [attributeTokens.Length * 2];
- attributeTokens.CopyTo (newArray, 0);
- attributeTokens = newArray;
- }
- if (attributeTokens [currentAttribute] == null)
- attributeTokens [currentAttribute] = new XmlAttributeTokenInfo (this);
- currentAttributeToken = attributeTokens [currentAttribute];
- currentAttributeToken.Clear ();
- }
- private void IncrementAttributeValueToken ()
- {
- ClearValueBuffer ();
- currentAttributeValue++;
- if (attributeValueTokens.Length == currentAttributeValue) {
- XmlTokenInfo [] newArray = new XmlTokenInfo [attributeValueTokens.Length * 2];
- attributeValueTokens.CopyTo (newArray, 0);
- attributeValueTokens = newArray;
- }
- if (attributeValueTokens [currentAttributeValue] == null)
- attributeValueTokens [currentAttributeValue] = new XmlTokenInfo (this);
- currentAttributeValueToken = attributeValueTokens [currentAttributeValue];
- currentAttributeValueToken.Clear ();
- }
- private void ReadAttributeValueTokens (int dummyQuoteChar)
- {
- int quoteChar = (dummyQuoteChar < 0) ? ReadChar () : dummyQuoteChar;
- if (quoteChar != '\'' && quoteChar != '\"')
- throw new XmlException (this as IXmlLineInfo,"an attribute value was not quoted");
- currentAttributeToken.QuoteChar = (char) quoteChar;
- IncrementAttributeValueToken ();
- currentAttributeToken.ValueTokenStartIndex = currentAttributeValue;
- currentAttributeValueToken.LineNumber = currentInput.LineNumber;
- currentAttributeValueToken.LinePosition = currentInput.LinePosition;
- bool incrementToken = false;
- bool isNewToken = true;
- bool loop = true;
- while (loop && PeekChar () != quoteChar) {
- if (incrementToken) {
- IncrementAttributeValueToken ();
- currentAttributeValueToken.LineNumber = currentInput.LineNumber;
- currentAttributeValueToken.LinePosition = currentInput.LinePosition;
- incrementToken = false;
- isNewToken = true;
- }
- int ch = ReadChar ();
- switch (ch)
- {
- case '<':
- throw new XmlException (this as IXmlLineInfo,"attribute values cannot contain '<'");
- case -1:
- if (dummyQuoteChar < 0)
- throw new XmlException (this as IXmlLineInfo,"unexpected end of file in an attribute value");
- else // Attribute value constructor.
- loop = false;
- break;
- case '&':
- int startPosition = currentTag.Length - 1;
- if (PeekChar () == '#') {
- ReadChar ();
- this.ReadCharacterReference ();
- break;
- }
- // Check XML 1.0 section 3.1 WFC.
- string entName = ReadName ();
- Expect (';');
- int predefined = XmlChar.GetPredefinedEntity (entName);
- if (predefined == 0) {
- DTDEntityDeclaration entDecl =
- DTD == null ? null : DTD.EntityDecls [entName];
- if (entDecl != null && entDecl.SystemId != null)
- // if (!startNodeType == XmlNodeType.Attribute && (entDecl == null || entDecl.SystemId != null))
- throw new XmlException (this as IXmlLineInfo,
- "Reference to external entities is not allowed in the value of an attribute.");
- currentAttributeValueToken.Value = CreateValueString ();
- currentAttributeValueToken.NodeType = XmlNodeType.Text;
- if (!isNewToken)
- IncrementAttributeValueToken ();
- currentAttributeValueToken.Name = entName;
- currentAttributeValueToken.Value = String.Empty;
- currentAttributeValueToken.NodeType = XmlNodeType.EntityReference;
- incrementToken = true;
- }
- else
- AppendValueChar (predefined);
- break;
- default:
- AppendValueChar (ch);
- break;
- }
- isNewToken = false;
- }
- if (!incrementToken) {
- currentAttributeValueToken.Value = CreateValueString ();
- currentAttributeValueToken.NodeType = XmlNodeType.Text;
- currentAttributeToken.ValueTokenEndIndex = currentAttributeValue;
- }
- if (dummyQuoteChar < 0)
- ReadChar (); // quoteChar
- }
- // The reader is positioned on the quote character.
- // *Keeps quote char* to value to get_QuoteChar() correctly.
- // Not it is used only for DTD.
- private string ReadAttribute (bool isDefaultValue)
- {
- ClearValueBuffer ();
- int quoteChar = ReadChar ();
- if (quoteChar != '\'' && quoteChar != '\"')
- throw new XmlException (this as IXmlLineInfo,"an attribute value was not quoted");
- AppendValueChar (quoteChar);
- while (PeekChar () != quoteChar) {
- int ch = ReadChar ();
- switch (ch)
- {
- case '<':
- throw new XmlException (this as IXmlLineInfo,"attribute values cannot contain '<'");
- case -1:
- throw new XmlException (this as IXmlLineInfo,"unexpected end of file in an attribute value");
- /*
- case '&':
- if (isDefaultValue) {
- AppendValueChar (ch);
- break;
- }
- AppendValueChar (ch);
- if (PeekChar () == '#')
- break;
- // Check XML 1.0 section 3.1 WFC.
- string entName = ReadName ();
- Expect (';');
- if (XmlChar.GetPredefinedEntity (entName) == 0) {
- DTDEntityDeclaration entDecl =
- DTD == null ? null : DTD.EntityDecls [entName];
- if (entDecl == null || entDecl.SystemId != null)
- throw new XmlException (this as IXmlLineInfo,
- "Reference to external entities is not allowed in attribute value.");
- }
- valueBuffer.Append (entName);
- AppendValueChar (';');
- break;
- */
- default:
- AppendValueChar (ch);
- break;
- }
- }
- ReadChar (); // quoteChar
- AppendValueChar (quoteChar);
- return CreateValueString ();
- }
- // The reader is positioned on the first character
- // of the target.
- //
- // It may be xml declaration or processing instruction.
- private void ReadProcessingInstruction ()
- {
- string target = ReadName ();
- if (target == "xml") {
- ReadXmlDeclaration ();
- return;
- } else if (target.ToLower () == "xml")
- throw new XmlException (this as IXmlLineInfo,
- "Not allowed processing instruction name which starts with 'X', 'M', 'L' was found.");
- if (currentState == XmlNodeType.None)
- currentState = XmlNodeType.XmlDeclaration;
- if (!SkipWhitespace ())
- if (PeekChar () != '?')
- throw new XmlException (this as IXmlLineInfo,
- "Invalid processing instruction name was found.");
- ClearValueBuffer ();
- while (PeekChar () != -1) {
- int ch = ReadChar ();
- if (ch == '?' && PeekChar () == '>') {
- ReadChar ();
- break;
- }
- AppendValueChar ((char)ch);
- }
- SetProperties (
- XmlNodeType.ProcessingInstruction, // nodeType
- target, // name
- false, // isEmptyElement
- true, // clearAttributes
- valueBuffer // value
- );
- }
- // The reader is positioned after "<?xml "
- private void ReadXmlDeclaration ()
- {
- if (currentState != XmlNodeType.None) {
- if (maybeTextDecl == 0)
- throw new XmlException (this as IXmlLineInfo,
- "XML declaration cannot appear in this state.");
- }
- // Is this required?
- if (maybeTextDecl != 0)
- currentState = XmlNodeType.XmlDeclaration;
- ClearAttributes ();
- ReadAttributes (true); // They must have "version."
- string version = GetAttribute ("version");
- string message = null;
- if (parserInputStack.Count == 0) {
- if (maybeTextDecl == 0 && (attributeTokens [0].Name != "version" || version != "1.0"))
- message = "Version 1.0 declaration is required in XML Declaration.";
- else if (attributeCount > 1 &&
- (attributeTokens [1].Name != "encoding" &&
- attributeTokens [1].Name != "standalone"))
- message = "Invalid Xml Declaration markup was found.";
- else if (attributeCount > 2 && attributeTokens [2].Name != "standalone")
- message = "Invalid Xml Declaration markup was found.";
- string sa = GetAttribute ("standalone");
- if (sa != null && sa != "yes" && sa != "no")
- message = "Only 'yes' or 'no' is allowed for standalone.";
- this.isStandalone = (sa == "yes");
- } else {
- int currentCheck = 0;
- if (attributeTokens [0].Name == "version") {
- if (version != "1.0")
- message = "Version 1.0 declaration is required in Text Declaration.";
- currentCheck = 1;
- }
- if (attributeCount <= currentCheck || attributeTokens [currentCheck].Name != "encoding")
- message = "Invalid Text Declaration markup was found. encoding specification is required.";
- }
- if (message != null)
- throw new XmlException (this as IXmlLineInfo, message);
- Expect ("?>");
- if (maybeTextDecl != 0)
- if (this ["standalone"] != null)
- throw new XmlException (this as IXmlLineInfo,
- "Invalid text declaration.");
- if (maybeTextDecl == 2)
- maybeTextDecl = 1;
- SetProperties (
- XmlNodeType.XmlDeclaration, // nodeType
- "xml", // name
- false, // isEmptyElement
- currentInput.CurrentMarkup.ToString (6, currentInput.CurrentMarkup.Length - 6), // value
- false // clearAttributes
- );
- }
- // The reader is positioned on the first character after
- // the leading '<!'.
- private void ReadDeclaration ()
- {
- int ch = PeekChar ();
- switch (ch)
- {
- case '-':
- Expect ("--");
- ReadComment ();
- break;
- case '[':
- ReadChar ();
- Expect ("CDATA[");
- ReadCDATA ();
- break;
- case 'D':
- Expect ("DOCTYPE");
- ReadDoctypeDecl ();
- break;
- default:
- throw new XmlException (this as IXmlLineInfo,
- "Unexpected declaration markup was found.");
- }
- }
- // The reader is positioned on the first character after
- // the leading '<!--'.
- private void ReadComment ()
- {
- if (currentState == XmlNodeType.None)
- currentState = XmlNodeType.XmlDeclaration;
- ClearValueBuffer ();
- while (PeekChar () != -1) {
- int ch = ReadChar ();
- if (ch == '-' && PeekChar () == '-') {
- ReadChar ();
- if (PeekChar () != '>')
- throw new XmlException (this as IXmlLineInfo,"comments cannot contain '--'");
- ReadChar ();
- break;
- }
- if (XmlConstructs.IsInvalid (ch))
- throw new XmlException (this as IXmlLineInfo,
- "Not allowed character was found.");
- AppendValueChar ((char)ch);
- }
- SetProperties (
- XmlNodeType.Comment, // nodeType
- String.Empty, // name
- false, // isEmptyElement
- true, // clearAttributes
- valueBuffer // value
- );
- }
- // The reader is positioned on the first character after
- // the leading '<![CDATA['.
- private void ReadCDATA ()
- {
- if (currentState != XmlNodeType.Element)
- throw new XmlException (this as IXmlLineInfo,
- "CDATA section cannot appear in this state.");
- ClearValueBuffer ();
- bool skip = false;
- int ch = 0;
- while (PeekChar () != -1) {
- if (!skip)
- ch = ReadChar ();
- skip = false;
- if (ch == ']' && PeekChar () == ']') {
- ch = ReadChar (); // ']'
- if (PeekChar () == '>') {
- ReadChar (); // '>'
- break;
- } else {
- skip = true;
- // AppendValueChar (']');
- // AppendValueChar (']');
- // ch = ReadChar ();
- }
- }
- AppendValueChar ((char)ch);
- }
- SetProperties (
- XmlNodeType.CDATA, // nodeType
- String.Empty, // name
- false, // isEmptyElement
- true, // clearAttributes
- valueBuffer // value
- );
- }
- // The reader is positioned on the first character after
- // the leading '<!DOCTYPE'.
- private void ReadDoctypeDecl ()
- {
- switch (currentState) {
- case XmlNodeType.DocumentType:
- case XmlNodeType.Element:
- case XmlNodeType.EndElement:
- throw new XmlException (this as IXmlLineInfo,
- "Document type cannot appear in this state.");
- }
- currentState = XmlNodeType.DocumentType;
- string doctypeName = null;
- string publicId = null;
- string systemId = null;
- int intSubsetStartLine = 0;
- int intSubsetStartColumn = 0;
- SkipWhitespace ();
- doctypeName = ReadName ();
- SkipWhitespace ();
- switch(PeekChar ())
- {
- case 'S':
- systemId = ReadSystemLiteral (true);
- break;
- case 'P':
- publicId = ReadPubidLiteral ();
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required between PUBLIC id and SYSTEM id.");
- systemId = ReadSystemLiteral (false);
- break;
- }
- SkipWhitespace ();
- if(PeekChar () == '[')
- {
- // read markupdecl etc. or end of decl
- ReadChar ();
- intSubsetStartLine = this.LineNumber;
- intSubsetStartColumn = this.LinePosition;
- int startPos = currentTag.Length;
- ReadInternalSubset ();
- int endPos = currentTag.Length - 1;
- parserContext.InternalSubset = currentTag.ToString (startPos, endPos - startPos);
- }
- // end of DOCTYPE decl.
- SkipWhitespace ();
- Expect ('>');
- GenerateDTDObjectModel (doctypeName, publicId,
- systemId, parserContext.InternalSubset,
- intSubsetStartLine, intSubsetStartColumn);
- // set properties for <!DOCTYPE> node
- SetProperties (
- XmlNodeType.DocumentType, // nodeType
- doctypeName, // name
- false, // isEmptyElement
- parserContext.InternalSubset, // value
- true // clearAttributes
- );
- if (publicId != null)
- AddAttribute ("PUBLIC", publicId);
- if (systemId != null)
- AddAttribute ("SYSTEM", systemId);
- }
- internal DTDObjectModel GenerateDTDObjectModel (string name, string publicId,
- string systemId, string internalSubset)
- {
- return GenerateDTDObjectModel (name, publicId, systemId, internalSubset, 0, 0);
- }
- internal DTDObjectModel GenerateDTDObjectModel (string name, string publicId,
- string systemId, string internalSubset, int intSubsetStartLine, int intSubsetStartColumn)
- {
- // now compile DTD
- parserContext.Dtd = new DTDObjectModel (); // merges both internal and external subsets in the meantime,
- DTD.BaseURI = BaseURI;
- DTD.Name = name;
- DTD.PublicId = publicId;
- DTD.SystemId = systemId;
- DTD.InternalSubset = internalSubset;
- DTD.XmlResolver = resolver;
- int originalParserDepth = parserInputStack.Count;
- bool more;
- if (internalSubset != null && internalSubset.Length > 0) {
- XmlParserInput original = currentInput;
- currentInput = new XmlParserInput (new StringReader (internalSubset), BaseURI, intSubsetStartLine, intSubsetStartColumn);
- do {
- more = CompileDTDSubset ();
- if (PeekChar () == -1 && parserInputStack.Count > 0)
- PopParserInput ();
- } while (more || parserInputStack.Count > originalParserDepth);
- if (dtdIncludeSect != 0)
- throw new XmlException (this as IXmlLineInfo,"INCLUDE section is not ended correctly.");
- currentInput = original;
- }
- if (systemId != null && systemId != String.Empty && resolver != null) {
- PushParserInput (systemId);
- do {
- more = this.CompileDTDSubset ();
- if (PeekChar () == -1 && parserInputStack.Count > 1)
- PopParserInput ();
- } while (more || parserInputStack.Count > originalParserDepth + 1);
- PopParserInput ();
- }
- return DTD;
- }
- private void PushParserInput (string url)
- {
- Uri baseUri = null;
- try {
- baseUri = new Uri (BaseURI);
- } catch (UriFormatException) {
- }
- Uri absUri = resolver.ResolveUri (baseUri, url);
- string absPath = absUri.ToString ();
- foreach (XmlParserInput i in parserInputStack.ToArray ()) {
- if (i.BaseURI == absPath)
- throw new XmlException (this as IXmlLineInfo, "Nested inclusion is not allowed: " + url);
- }
- parserInputStack.Push (currentInput);
- currentInput = new XmlParserInput (new XmlStreamReader (url, false, resolver, BaseURI), absPath);
- parserContext.PushScope ();
- parserContext.BaseURI = absPath;
- maybeTextDecl = 2;
- }
- private void PopParserInput ()
- {
- currentInput = parserInputStack.Pop () as XmlParserInput;
- parserContext.PopScope ();
- }
- private enum DtdInputState
- {
- Free = 1,
- ElementDecl,
- AttlistDecl,
- EntityDecl,
- NotationDecl,
- PI,
- Comment,
- InsideSingleQuoted,
- InsideDoubleQuoted,
- }
- private class DtdInputStateStack
- {
- Stack intern = new Stack ();
- public DtdInputStateStack ()
- {
- Push (DtdInputState.Free);
- }
- public DtdInputState Peek ()
- {
- return (DtdInputState) intern.Peek ();
- }
- public DtdInputState Pop ()
- {
- return (DtdInputState) intern.Pop ();
- }
- public void Push (DtdInputState val)
- {
- intern.Push (val);
- }
- }
- DtdInputStateStack stateStack = new DtdInputStateStack ();
- DtdInputState State {
- get { return stateStack.Peek (); }
- }
- // Simply read but not generate any result.
- private void ReadInternalSubset ()
- {
- bool continueParse = true;
- while (continueParse) {
- switch (ReadChar ()) {
- case ']':
- switch (State) {
- case DtdInputState.Free:
- continueParse = false;
- break;
- case DtdInputState.InsideDoubleQuoted:
- continue;
- case DtdInputState.InsideSingleQuoted:
- continue;
- default:
- throw new XmlException (this as IXmlLineInfo,"unexpected end of file at DTD.");
- }
- break;
- case -1:
- throw new XmlException (this as IXmlLineInfo,"unexpected end of file at DTD.");
- case '<':
- if (State == DtdInputState.InsideDoubleQuoted ||
- State == DtdInputState.InsideSingleQuoted)
- continue; // well-formed
- switch (ReadChar ()) {
- case '?':
- stateStack.Push (DtdInputState.PI);
- break;
- case '!':
- switch (ReadChar ()) {
- case 'E':
- switch (ReadChar ()) {
- case 'L':
- Expect ("EMENT");
- stateStack.Push (DtdInputState.ElementDecl);
- break;
- case 'N':
- Expect ("TITY");
- stateStack.Push (DtdInputState.EntityDecl);
- break;
- default:
- throw new XmlException (this as IXmlLineInfo,"unexpected token '<!E'.");
- }
- break;
- case 'A':
- Expect ("TTLIST");
- stateStack.Push (DtdInputState.AttlistDecl);
- break;
- case 'N':
- Expect ("OTATION");
- stateStack.Push (DtdInputState.NotationDecl);
- break;
- case '-':
- Expect ("-");
- stateStack.Push (DtdInputState.Comment);
- break;
- }
- break;
- default:
- throw new XmlException (this as IXmlLineInfo,"unexpected '>'.");
- }
- break;
- case '\'':
- if (State == DtdInputState.InsideSingleQuoted)
- stateStack.Pop ();
- else if (State != DtdInputState.InsideDoubleQuoted && State != DtdInputState.Comment)
- stateStack.Push (DtdInputState.InsideSingleQuoted);
- break;
- case '"':
- if (State == DtdInputState.InsideDoubleQuoted)
- stateStack.Pop ();
- else if (State != DtdInputState.InsideSingleQuoted && State != DtdInputState.Comment)
- stateStack.Push (DtdInputState.InsideDoubleQuoted);
- break;
- case '>':
- switch (State) {
- case DtdInputState.ElementDecl:
- goto case DtdInputState.NotationDecl;
- case DtdInputState.AttlistDecl:
- goto case DtdInputState.NotationDecl;
- case DtdInputState.EntityDecl:
- goto case DtdInputState.NotationDecl;
- case DtdInputState.NotationDecl:
- stateStack.Pop ();
- break;
- case DtdInputState.InsideDoubleQuoted:
- continue;
- case DtdInputState.InsideSingleQuoted:
- continue; // well-formed
- case DtdInputState.Comment:
- continue;
- default:
- throw new XmlException (this as IXmlLineInfo,"unexpected token '>'");
- }
- break;
- case '?':
- if (State == DtdInputState.PI) {
- if (ReadChar () == '>')
- stateStack.Pop ();
- }
- break;
- case '-':
- if (State == DtdInputState.Comment) {
- if (PeekChar () == '-') {
- ReadChar ();
- Expect ('>');
- stateStack.Pop ();
- }
- }
- break;
- case '%':
- if (State != DtdInputState.Free && State != DtdInputState.EntityDecl && State != DtdInputState.Comment && State != DtdInputState.InsideDoubleQuoted && State != DtdInputState.InsideSingleQuoted)
- throw new XmlException (this as IXmlLineInfo,"Parameter Entity Reference cannot appear as a part of markupdecl (see XML spec 2.8).");
- break;
- }
- }
- }
- // Read any one of following:
- // elementdecl, AttlistDecl, EntityDecl, NotationDecl,
- // PI, Comment, Parameter Entity, or doctype termination char(']')
- //
- // Returns true if it may have any more contents, or false if not.
- private bool CompileDTDSubset()
- {
- SkipWhitespace ();
- switch(PeekChar ())
- {
- case -1:
- return false;
- case '%':
- // It affects on entity references' well-formedness
- if (this.parserInputStack.Count == 0)
- DTD.InternalSubsetHasPEReference = true;
- ReadChar ();
- string peName = ReadName ();
- Expect (';');
- currentInput.InsertParameterEntityBuffer (GetPEValue (peName));
- int currentLine = currentInput.LineNumber;
- int currentColumn = currentInput.LinePosition;
- while (currentInput.HasPEBuffer)
- CompileDTDSubset ();
- if (currentInput.LineNumber != currentLine ||
- currentInput.LinePosition != currentColumn)
- throw new XmlException (this as IXmlLineInfo,
- "Incorrectly nested parameter entity.");
- break;
- case '<':
- ReadChar ();
- switch(ReadChar ())
- {
- case '?':
- // Only read, no store.
- ReadProcessingInstruction ();
- break;
- case '!':
- CompileDeclaration ();
- break;
- default:
- throw new XmlException (this as IXmlLineInfo,"Syntax Error after '<' character.");
- }
- break;
- case ']':
- if (dtdIncludeSect == 0)
- throw new XmlException (this as IXmlLineInfo, "Unbalanced end of INCLUDE/IGNORE section.");
- // End of inclusion
- Expect ("]]>");
- dtdIncludeSect--;
- SkipWhitespace ();
- return false;
- default:
- throw new XmlException (this as IXmlLineInfo,String.Format ("Syntax Error inside doctypedecl markup : {0}({1})", PeekChar (), (char) PeekChar ()));
- }
- return true;
- }
- private void CompileDeclaration ()
- {
- switch(ReadChar ())
- {
- case '-':
- Expect ('-');
- // Only read, no store.
- ReadComment ();
- break;
- case 'E':
- switch(ReadChar ())
- {
- case 'N':
- Expect ("TITY");
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required after '<!ENTITY' in DTD entity declaration.");
- LOOPBACK:
- if (PeekChar () == '%') {
- ReadChar ();
- if (!SkipWhitespace ()) {
- ImportAsPERef ();
- goto LOOPBACK;
- } else {
- TryExpandPERef ();
- SkipWhitespace ();
- if (XmlChar.IsNameChar (PeekChar ()))
- ReadParameterEntityDecl ();
- else
- throw new XmlException (this as IXmlLineInfo,"expected name character");
- }
- break;
- }
- DTDEntityDeclaration ent = ReadEntityDecl ();
- if (DTD.EntityDecls [ent.Name] == null)
- DTD.EntityDecls.Add (ent.Name, ent);
- break;
- case 'L':
- Expect ("EMENT");
- DTDElementDeclaration el = ReadElementDecl ();
- DTD.ElementDecls.Add (el.Name, el);
- break;
- default:
- throw new XmlException (this as IXmlLineInfo,"Syntax Error after '<!E' (ELEMENT or ENTITY must be found)");
- }
- break;
- case 'A':
- Expect ("TTLIST");
- DTDAttListDeclaration atl = ReadAttListDecl ();
- // if (DTD.AttListDecls.ContainsKey (atl.Name))
- DTD.AttListDecls.Add (atl.Name, atl);
- break;
- case 'N':
- Expect ("OTATION");
- DTDNotationDeclaration not = ReadNotationDecl ();
- DTD.NotationDecls.Add (not.Name, not);
- break;
- case '[':
- // conditional sections
- SkipWhitespace ();
- TryExpandPERef ();
- SkipWhitespace ();
- Expect ('I');
- switch (ReadChar ()) {
- case 'N':
- Expect ("CLUDE");
- SkipWhitespace ();
- Expect ('[');
- dtdIncludeSect++;
- break;
- case 'G':
- Expect ("NORE");
- ReadIgnoreSect ();
- break;
- }
- break;
- default:
- throw new XmlException (this as IXmlLineInfo,"Syntax Error after '<!' characters.");
- }
- }
- private void ReadIgnoreSect ()
- {
- bool skip = false;
- SkipWhitespace ();
- Expect ('[');
- int dtdIgnoreSect = 1;
- while (dtdIgnoreSect > 0) {
- switch (skip ? PeekChar () : ReadChar ()) {
- case -1:
- throw new XmlException (this as IXmlLineInfo,"Unexpected IGNORE section end.");
- case '<':
- if (ReadChar () == '!' && ReadChar () == '[')
- dtdIgnoreSect++;
- break;
- case ']':
- if (ReadChar () == ']') {
- if (ReadChar () == '>')
- dtdIgnoreSect--;
- else
- skip = true;
- }
- break;
- }
- skip = false;
- }
- }
- // The reader is positioned on the head of the name.
- private DTDElementDeclaration ReadElementDecl ()
- {
- DTDElementDeclaration decl = new DTDElementDeclaration (DTD);
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required between '<!ELEMENT' and name in DTD element declaration.");
- TryExpandPERef ();
- SkipWhitespace ();
- decl.Name = ReadName ();
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required between name and content in DTD element declaration.");
- TryExpandPERef ();
- ReadContentSpec (decl);
- SkipWhitespace ();
- // This expanding is only allowed as a non-validating parser.
- TryExpandPERef ();
- SkipWhitespace ();
- Expect ('>');
- return decl;
- }
- // read 'children'(BNF) of contentspec
- private void ReadContentSpec (DTDElementDeclaration decl)
- {
- TryExpandPERef ();
- SkipWhitespace ();
- switch(PeekChar ())
- {
- case 'E':
- decl.IsEmpty = true;
- Expect ("EMPTY");
- break;
- case 'A':
- decl.IsAny = true;
- Expect ("ANY");
- break;
- case '(':
- DTDContentModel model = decl.ContentModel;
- ReadChar ();
- SkipWhitespace ();
- TryExpandPERef ();
- SkipWhitespace ();
- if(PeekChar () == '#') {
- // Mixed Contents. "#PCDATA" must appear first.
- decl.IsMixedContent = true;
- model.Occurence = DTDOccurence.ZeroOrMore;
- model.OrderType = DTDContentOrderType.Or;
- Expect ("#PCDATA");
- SkipWhitespace ();
- TryExpandPERef ();
- SkipWhitespace ();
- while(PeekChar () != ')') {
- Expect('|');
- SkipWhitespace ();
- TryExpandPERef ();
- SkipWhitespace ();
- DTDContentModel elem = new DTDContentModel (DTD, decl.Name);
- elem.ElementName = ReadName ();
- model.ChildModels.Add (elem);
- SkipWhitespace ();
- TryExpandPERef ();
- SkipWhitespace ();
- }
- Expect (')');
- if (model.ChildModels.Count > 0)
- Expect ('*');
- else if (PeekChar () == '*')
- Expect ('*');
- } else {
- // Non-Mixed Contents
- model.ChildModels.Add (ReadCP (decl));
- SkipWhitespace ();
- do { // copied from ReadCP() ...;-)
- TryExpandPERef ();
- SkipWhitespace ();
- if(PeekChar ()=='|') {
- // CPType=Or
- if (model.OrderType == DTDContentOrderType.Seq)
- throw new XmlException (this as IXmlLineInfo,
- "Inconsistent choice markup in sequence cp.");
- model.OrderType = DTDContentOrderType.Or;
- ReadChar ();
- SkipWhitespace ();
- model.ChildModels.Add (ReadCP (decl));
- SkipWhitespace ();
- }
- else if(PeekChar () == ',')
- {
- // CPType=Seq
- if (model.OrderType == DTDContentOrderType.Or)
- throw new XmlException (this as IXmlLineInfo,
- "Inconsistent sequence markup in choice cp.");
- model.OrderType = DTDContentOrderType.Seq;
- ReadChar ();
- SkipWhitespace ();
- model.ChildModels.Add (ReadCP (decl));
- SkipWhitespace ();
- }
- else
- break;
- }
- while(true);
- Expect (')');
- switch(PeekChar ())
- {
- case '?':
- model.Occurence = DTDOccurence.Optional;
- ReadChar ();
- break;
- case '*':
- model.Occurence = DTDOccurence.ZeroOrMore;
- ReadChar ();
- break;
- case '+':
- model.Occurence = DTDOccurence.OneOrMore;
- ReadChar ();
- break;
- }
- SkipWhitespace ();
- }
- SkipWhitespace ();
- break;
- }
- }
- // Read 'cp' (BNF) of contentdecl (BNF)
- private DTDContentModel ReadCP (DTDElementDeclaration elem)
- {
- DTDContentModel model = null;
- TryExpandPERef ();
- SkipWhitespace ();
- if(PeekChar () == '(') {
- model = new DTDContentModel (DTD, elem.Name);
- ReadChar ();
- SkipWhitespace ();
- model.ChildModels.Add (ReadCP (elem));
- SkipWhitespace ();
- do {
- TryExpandPERef ();
- SkipWhitespace ();
- if(PeekChar ()=='|') {
- // CPType=Or
- if (model.OrderType == DTDContentOrderType.Seq)
- throw new XmlException (this as IXmlLineInfo,
- "Inconsistent choice markup in sequence cp.");
- model.OrderType = DTDContentOrderType.Or;
- ReadChar ();
- SkipWhitespace ();
- model.ChildModels.Add (ReadCP (elem));
- SkipWhitespace ();
- }
- else if(PeekChar () == ',') {
- // CPType=Seq
- if (model.OrderType == DTDContentOrderType.Or)
- throw new XmlException (this as IXmlLineInfo,
- "Inconsistent sequence markup in choice cp.");
- model.OrderType = DTDContentOrderType.Seq;
- ReadChar ();
- SkipWhitespace ();
- model.ChildModels.Add (ReadCP (elem));
- SkipWhitespace ();
- }
- else
- break;
- }
- while(true);
- SkipWhitespace ();
- Expect (')');
- }
- else {
- TryExpandPERef ();
- model = new DTDContentModel (DTD, elem.Name);
- SkipWhitespace ();
- model.ElementName = ReadName ();
- }
- switch(PeekChar ()) {
- case '?':
- model.Occurence = DTDOccurence.Optional;
- ReadChar ();
- break;
- case '*':
- model.Occurence = DTDOccurence.ZeroOrMore;
- ReadChar ();
- break;
- case '+':
- model.Occurence = DTDOccurence.OneOrMore;
- ReadChar ();
- break;
- }
- return model;
- }
- // The reader is positioned on the first name char.
- private void ReadParameterEntityDecl ()
- {
- DTDParameterEntityDeclaration decl =
- new DTDParameterEntityDeclaration();
- decl.BaseURI = BaseURI;
- decl.Name = ReadName ();
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required after name in DTD parameter entity declaration.");
- if (PeekChar () == 'S' || PeekChar () == 'P') {
- // throw new NotImplementedException ("External parameter entity reference is not implemented yet.");
- // read publicId/systemId
- ReadExternalID ();
- decl.PublicId = GetAttribute ("PUBLIC");
- decl.SystemId = GetAttribute ("SYSTEM");
- SkipWhitespace ();
- decl.Resolve (resolver);
- }
- else {
- TryExpandPERef ();
- int quoteChar = ReadChar ();
- int start = currentTag.Length;
- ClearValueBuffer ();
- bool loop = true;
- while (loop) {
- int c = PeekChar ();
- switch (c) {
- case -1:
- throw new XmlException ("unexpected end of stream in entity value definition.");
- case '"':
- ReadChar ();
- if (quoteChar == '"')
- loop = false;
- else
- AppendValueChar ('"');
- break;
- case '\'':
- ReadChar ();
- if (quoteChar == '\'')
- loop = false;
- else
- AppendValueChar ('\'');
- break;
- case '&':
- ReadChar ();
- if (PeekChar () == '#') {
- ReadChar ();
- ReadCharacterReference ();
- }
- else
- AppendValueChar ('&');
- break;
- case '%':
- ReadChar ();
- string peName = ReadName ();
- Expect (';');
- valueBuffer.Append (GetPEValue (peName));
- break;
- default:
- AppendValueChar (ReadChar ());
- break;
- }
- }
- decl.LiteralValue = CreateValueString (); // currentTag.ToString (start, currentTag.Length - start - 1);
- ClearValueBuffer ();
- }
- SkipWhitespace ();
- Expect ('>');
- if (parameterEntities [decl.Name] == null) {
- parameterEntities.Add (decl.Name, decl);
- }
- }
- private string GetPEValue (string peName)
- {
- DTDParameterEntityDeclaration peDecl =
- this.parameterEntities [peName] as DTDParameterEntityDeclaration;
- if (peDecl != null)
- return peDecl.Value;
- // See XML 1.0 section 4.1 for both WFC and VC.
- if ((DTD.SystemId == null && !DTD.InternalSubsetHasPEReference) || this.isStandalone)
- throw new XmlException (this as IXmlLineInfo,
- "Parameter entity " + peName + " not found.");
- DTD.AddError (new XmlSchemaException (
- "Parameter entity " + peName + " not found.", null));
- return "";
- }
- private void TryExpandPERef ()
- {
- if (PeekChar () == '%') {
- // ReadChar ();
- // if (!XmlChar.IsNameChar (PeekChar ()))
- // return;
- // ExpandPERef ();
- ImportAsPERef ();
- }
- }
- // reader is positioned on '%'
- private void ImportAsPERef ()
- {
- ReadChar ();
- string peName = ReadName ();
- Expect (';');
- DTDParameterEntityDeclaration peDecl =
- this.parameterEntities [peName] as DTDParameterEntityDeclaration;
- if (peDecl == null) {
- DTD.AddError (new XmlSchemaException ("Parameter entity " + peName + " not found.", null));
- return; // do nothing
- }
- currentInput.InsertParameterEntityBuffer (" " + peDecl.Value + " ");
- }
- // The reader is positioned on the head of the name.
- private DTDEntityDeclaration ReadEntityDecl ()
- {
- DTDEntityDeclaration decl = new DTDEntityDeclaration (DTD);
- decl.IsInternalSubset = (parserInputStack.Count == 0);
- TryExpandPERef ();
- SkipWhitespace ();
- decl.Name = ReadName ();
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required between name and content in DTD entity declaration.");
- TryExpandPERef ();
- SkipWhitespace ();
- if (PeekChar () == 'S' || PeekChar () == 'P') {
- // external entity
- ReadExternalID ();
- decl.PublicId = GetAttribute ("PUBLIC");
- decl.SystemId = GetAttribute ("SYSTEM");
- if (SkipWhitespace ()) {
- if (PeekChar () == 'N') {
- // NDataDecl
- Expect ("NDATA");
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required after NDATA.");
- decl.NotationName = ReadName (); // ndata_name
- }
- }
- decl.ScanEntityValue (new StringCollection ());
- }
- else {
- // literal entity
- ReadEntityValueDecl (decl);
- }
- SkipWhitespace ();
- // This expanding is only allowed as a non-validating parser.
- TryExpandPERef ();
- SkipWhitespace ();
- Expect ('>');
- return decl;
- }
- private void ReadEntityValueDecl (DTDEntityDeclaration decl)
- {
- SkipWhitespace ();
- // quotation char will be finally removed on unescaping
- int quoteChar = ReadChar ();
- int start = currentTag.Length;
- if (quoteChar != '\'' && quoteChar != '"')
- throw new XmlException ("quotation char was expected.");
- ClearValueBuffer ();
- while (PeekChar () != quoteChar) {
- switch (PeekChar ()) {
- case '%':
- ReadChar ();
- string name = ReadName ();
- Expect (';');
- if (decl.IsInternalSubset)
- throw new XmlException (this as IXmlLineInfo,
- "Parameter entity is not allowed in internal subset entity '" + name + "'");
- valueBuffer.Append (GetPEValue (name));
- break;
- case -1:
- throw new XmlException ("unexpected end of stream.");
- default:
- AppendValueChar (ReadChar ());
- break;
- }
- }
- string value = Dereference (CreateValueString (), false);
- ClearValueBuffer ();
- Expect (quoteChar);
- decl.LiteralEntityValue = value;
- }
- private DTDAttListDeclaration ReadAttListDecl ()
- {
- SkipWhitespace ();
- TryExpandPERef ();
- SkipWhitespace ();
- string name = ReadName (); // target element name
- DTDAttListDeclaration decl =
- DTD.AttListDecls [name] as DTDAttListDeclaration;
- if (decl == null)
- decl = new DTDAttListDeclaration (DTD);
- decl.Name = name;
- if (!SkipWhitespace ())
- if (PeekChar () != '>')
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required between name and content in non-empty DTD attlist declaration.");
- TryExpandPERef ();
- SkipWhitespace ();
- while (XmlChar.IsNameChar ((char) PeekChar ())) {
- DTDAttributeDefinition def = ReadAttributeDefinition ();
- if (decl [def.Name] == null)
- decl.Add (def);
- SkipWhitespace ();
- TryExpandPERef ();
- SkipWhitespace ();
- }
- SkipWhitespace ();
- // This expanding is only allowed as a non-validating parser.
- TryExpandPERef ();
- SkipWhitespace ();
- Expect ('>');
- return decl;
- }
- private DTDAttributeDefinition ReadAttributeDefinition ()
- {
- DTDAttributeDefinition def = new DTDAttributeDefinition ();
- // attr_name
- TryExpandPERef ();
- SkipWhitespace ();
- def.Name = ReadName ();
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required between name and content in DTD attribute definition.");
- // attr_value
- TryExpandPERef ();
- SkipWhitespace ();
- switch(PeekChar ()) {
- case 'C': // CDATA
- Expect ("CDATA");
- def.Datatype = XmlSchemaDatatype.FromName ("normalizedString");
- break;
- case 'I': // ID, IDREF, IDREFS
- Expect ("ID");
- if(PeekChar () == 'R') {
- Expect ("REF");
- if(PeekChar () == 'S') {
- // IDREFS
- ReadChar ();
- def.Datatype = XmlSchemaDatatype.FromName ("IDREFS");
- }
- else // IDREF
- def.Datatype = XmlSchemaDatatype.FromName ("IDREF");
- }
- else // ID
- def.Datatype = XmlSchemaDatatype.FromName ("ID");
- break;
- case 'E': // ENTITY, ENTITIES
- Expect ("ENTIT");
- switch(ReadChar ()) {
- case 'Y': // ENTITY
- def.Datatype = XmlSchemaDatatype.FromName ("ENTITY");
- break;
- case 'I': // ENTITIES
- Expect ("ES");
- def.Datatype = XmlSchemaDatatype.FromName ("ENTITIES");
- break;
- }
- break;
- case 'N': // NMTOKEN, NMTOKENS, NOTATION
- ReadChar ();
- switch(PeekChar ()) {
- case 'M':
- Expect ("MTOKEN");
- if(PeekChar ()=='S') { // NMTOKENS
- ReadChar ();
- def.Datatype = XmlSchemaDatatype.FromName ("NMTOKENS");
- }
- else // NMTOKEN
- def.Datatype = XmlSchemaDatatype.FromName ("NMTOKEN");
- break;
- case 'O':
- Expect ("OTATION");
- def.Datatype = XmlSchemaDatatype.FromName ("NOTATION");
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required between name and content in DTD attribute definition.");
- Expect ('(');
- SkipWhitespace ();
- def.EnumeratedNotations.Add (ReadName ()); // notation name
- SkipWhitespace ();
- while(PeekChar () == '|') {
- ReadChar ();
- SkipWhitespace ();
- def.EnumeratedNotations.Add (ReadName ()); // notation name
- SkipWhitespace ();
- }
- Expect (')');
- break;
- default:
- throw new XmlException ("attribute declaration syntax error.");
- }
- break;
- default: // Enumerated Values
- def.Datatype = XmlSchemaDatatype.FromName ("NMTOKEN");
- TryExpandPERef ();
- SkipWhitespace ();
- Expect ('(');
- SkipWhitespace ();
- def.EnumeratedAttributeDeclaration.Add (
- def.Datatype.Normalize (ReadNmToken ())); // enum value
- SkipWhitespace ();
- while(PeekChar () == '|') {
- ReadChar ();
- SkipWhitespace ();
- def.EnumeratedAttributeDeclaration.Add (
- def.Datatype.Normalize (ReadNmToken ())); // enum value
- SkipWhitespace ();
- }
- Expect (')');
- break;
- }
- TryExpandPERef ();
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required between type and occurence in DTD attribute definition.");
- // def_value
- if(PeekChar () == '#')
- {
- ReadChar ();
- switch(PeekChar ())
- {
- case 'R':
- Expect ("REQUIRED");
- def.OccurenceType = DTDAttributeOccurenceType.Required;
- break;
- case 'I':
- Expect ("IMPLIED");
- def.OccurenceType = DTDAttributeOccurenceType.Optional;
- break;
- case 'F':
- Expect ("FIXED");
- def.OccurenceType = DTDAttributeOccurenceType.Fixed;
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required between FIXED and actual value in DTD attribute definition.");
- def.UnresolvedDefaultValue = ReadAttribute (true);
- break;
- }
- } else {
- // one of the enumerated value
- TryExpandPERef ();
- SkipWhitespace ();
- def.UnresolvedDefaultValue = ReadAttribute (true);
- }
- return def;
- }
- private DTDNotationDeclaration ReadNotationDecl()
- {
- DTDNotationDeclaration decl = new DTDNotationDeclaration ();
- TryExpandPERef ();
- SkipWhitespace ();
- decl.Name = ReadName (); // notation name
- if (namespaces) { // copy from SetProperties ;-)
- int indexOfColon = decl.Name.IndexOf (':');
- if (indexOfColon == -1) {
- decl.Prefix = String.Empty;
- decl.LocalName = decl.Name;
- } else {
- decl.Prefix = decl.Name.Substring (0, indexOfColon);
- decl.LocalName = decl.Name.Substring (indexOfColon + 1);
- }
- } else {
- decl.Prefix = String.Empty;
- decl.LocalName = decl.Name;
- }
- SkipWhitespace ();
- if(PeekChar () == 'P') {
- decl.PublicId = ReadPubidLiteral ();
- bool wsSkipped = SkipWhitespace ();
- if (PeekChar () == '\'' || PeekChar () == '"') {
- if (!wsSkipped)
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required between public id and system id.");
- decl.SystemId = ReadSystemLiteral (false);
- SkipWhitespace ();
- }
- } else if(PeekChar () == 'S') {
- decl.SystemId = ReadSystemLiteral (true);
- SkipWhitespace ();
- }
- if(decl.PublicId == null && decl.SystemId == null)
- throw new XmlException ("public or system declaration required for \"NOTATION\" declaration.");
- // This expanding is only allowed as a non-validating parser.
- TryExpandPERef ();
- SkipWhitespace ();
- Expect ('>');
- return decl;
- }
- private void ReadExternalID () {
- this.ClearAttributes ();
- switch (PeekChar ()) {
- case 'S':
- string systemId = ReadSystemLiteral (true);
- AddAttribute ("SYSTEM", systemId);
- break;
- case 'P':
- string publicId = ReadPubidLiteral ();
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required between PUBLIC id and SYSTEM id.");
- systemId = ReadSystemLiteral (false);
- AddAttribute ("PUBLIC", publicId);
- AddAttribute ("SYSTEM", systemId);
- break;
- }
- }
- // The reader is positioned on the first 'S' of "SYSTEM".
- private string ReadSystemLiteral (bool expectSYSTEM)
- {
- if(expectSYSTEM) {
- Expect ("SYSTEM");
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required after 'SYSTEM'.");
- }
- else
- SkipWhitespace ();
- int quoteChar = ReadChar (); // apos or quot
- int startPos = currentTag.Length;
- int c = 0;
- ClearValueBuffer ();
- while (c != quoteChar) {
- c = ReadChar ();
- if (c < 0)
- throw new XmlException (this as IXmlLineInfo,"Unexpected end of stream in ExternalID.");
- if (c != quoteChar)
- AppendValueChar (c);
- }
- return CreateValueString (); //currentTag.ToString (startPos, currentTag.Length - 1 - startPos);
- }
- private string ReadPubidLiteral()
- {
- Expect ("PUBLIC");
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required after 'PUBLIC'.");
- int quoteChar = ReadChar ();
- int startPos = currentTag.Length;
- int c = 0;
- ClearValueBuffer ();
- while(c != quoteChar)
- {
- c = ReadChar ();
- if(c < 0) throw new XmlException (this as IXmlLineInfo,"Unexpected end of stream in ExternalID.");
- if(c != quoteChar && !XmlChar.IsPubidChar (c))
- throw new XmlException (this as IXmlLineInfo,"character '" + (char)c + "' not allowed for PUBLIC ID");
- if (c != quoteChar)
- AppendValueChar (c);
- }
- return CreateValueString (); //currentTag.ToString (startPos, currentTag.Length - 1 - startPos);
- }
- // The reader is positioned on the first character
- // of the name.
- internal string ReadName ()
- {
- return ReadNameOrNmToken(false);
- }
- // The reader is positioned on the first character
- // of the name.
- private string ReadNmToken ()
- {
- return ReadNameOrNmToken(true);
- }
- private string ReadNameOrNmToken(bool isNameToken)
- {
- int ch = PeekChar ();
- if(isNameToken) {
- if (!XmlChar.IsNameChar ((char) ch))
- throw new XmlException (this as IXmlLineInfo,String.Format ("a nmtoken did not start with a legal character {0} ({1})", ch, (char)ch));
- }
- else {
- if (!XmlChar.IsFirstNameChar (ch))
- throw new XmlException (this as IXmlLineInfo,String.Format ("a name did not start with a legal character {0} ({1})", ch, (char)ch));
- }
- nameLength = 0;
- AppendNameChar (ReadChar ());
- while (XmlChar.IsNameChar (PeekChar ())) {
- AppendNameChar (ReadChar ());
- }
- return CreateNameString ();
- }
- // Read the next character and compare it against the
- // specified character.
- private void Expect (int expected)
- {
- int ch = ReadChar ();
- if (ch != expected) {
- throw new XmlException (this as IXmlLineInfo,
- String.Format (
- "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
- (char)expected,
- expected,
- (char)ch,
- ch));
- }
- }
- private void Expect (string expected)
- {
- int len = expected.Length;
- for(int i=0; i< len; i++)
- Expect (expected[i]);
- }
- // Does not consume the first non-whitespace character.
- private bool SkipWhitespace ()
- {
- //FIXME: Should not skip if whitespaceHandling == WhiteSpaceHandling.None
- bool skipped = XmlChar.IsWhitespace (PeekChar ());
- while (XmlChar.IsWhitespace (PeekChar ()))
- ReadChar ();
- return skipped;
- }
- private void ReadWhitespace ()
- {
- if (currentState == XmlNodeType.None)
- currentState = XmlNodeType.XmlDeclaration;
- ClearValueBuffer ();
- int ch = PeekChar ();
- do {
- AppendValueChar (ReadChar ());
- } while ((ch = PeekChar ()) != -1 && XmlChar.IsWhitespace (ch));
- if (currentState == XmlNodeType.Element && ch != -1 && ch != '<')
- ReadText (false);
- else {
- XmlNodeType nodeType = (this.XmlSpace == XmlSpace.Preserve) ?
- XmlNodeType.SignificantWhitespace : XmlNodeType.Whitespace;
- SetProperties (nodeType,
- String.Empty,
- false,
- true,
- valueBuffer);
- }
- return; // (PeekChar () != -1);
- }
- private string Dereference (string unresolved, bool expandPredefined)
- {
- StringBuilder resolved = new StringBuilder();
- int pos = 0;
- int next = unresolved.IndexOf ('&');
- if(next < 0)
- return unresolved;
- while(next >= 0) {
- if(pos < next)
- resolved.Append (unresolved.Substring (pos, next - pos));// - 1);
- int endPos = unresolved.IndexOf (';', next+1);
- string entityName =
- unresolved.Substring (next + 1, endPos - next - 1);
- if(entityName [0] == '#') {
- char c;
- // character entity
- if(entityName [1] == 'x') {
- // hexadecimal
- c = (char) int.Parse ("0" + entityName.Substring (2),
- System.Globalization.NumberStyles.HexNumber);
- } else {
- // decimal
- c = (char) int.Parse (entityName.Substring (1));
- }
- resolved.Append (c);
- } else {
- char predefined = XmlChar.GetPredefinedEntity (entityName);
- if (expandPredefined && predefined != 0)
- resolved.Append (predefined);
- else
- // With respect to "Value", MS document is helpless
- // and the implemention returns inconsistent value
- // (e.g. XML: "&ent; &ent;" ---> Value: "&ent; &ent;".)
- resolved.Append ("&" + entityName + ";");
- }
- pos = endPos + 1;
- if(pos > unresolved.Length)
- break;
- next = unresolved.IndexOf('&', pos);
- }
- resolved.Append (unresolved.Substring(pos));
- return resolved.ToString();
- }
- #endregion
- }
- }
|