| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019 |
- //
- // 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.
- //
- 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)
- {
- }
- [MonoTODO("TODO as same as private XmlTextReader(TextReader, XmlNodeType, XmlParserContext)")]
- public XmlTextReader (string xmlFragment, XmlNodeType fragType, XmlParserContext context)
- : this (context != null ? context.BaseURI : String.Empty,
- new StringReader (xmlFragment),
- fragType,
- context)
- {
- }
- // TODO still remains as described at head of this file,
- // but it might not be TODO of the constructors...
- XmlTextReader (string url, TextReader fragment, XmlNodeType fragType, XmlParserContext context)
- {
- InitializeContext (url, context, fragment, fragType);
- }
- #endregion
- #region Properties
- public override int AttributeCount
- {
- get { return attributes.Count; }
- }
- public override string BaseURI
- {
- get { return parserContext.BaseURI; }
- }
- public override int Depth
- {
- get {
- 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 value != String.Empty;
- }
- }
- public override bool IsDefault
- {
- get
- {
- // XmlTextReader does not expand default attributes.
- return false;
- }
- }
- public override bool IsEmptyElement
- {
- get { return 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 { return currentInput.LineNumber; }
- }
- public int LinePosition
- {
- get { return currentInput.LinePosition; }
- }
- public override string LocalName
- {
- get { return localName; }
- }
- public override string Name
- {
- get { return 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 namespaceURI; }
- }
- public override XmlNameTable NameTable
- {
- get { return parserContext.NameTable; }
- }
- public override XmlNodeType NodeType
- {
- get { return nodeType; }
- }
- [MonoTODO]
- public bool Normalization
- {
- get { return normalization; }
- set { normalization = value; }
- }
- public override string Prefix
- {
- get { return prefix; }
- }
- public override char QuoteChar
- {
- get {
- // value string holds attribute quotation char.
- if (NodeType == XmlNodeType.Attribute)
- return value [0];
- else
- return '"';
- }
- }
- public override ReadState ReadState
- {
- get { return readState; }
- }
- public override string Value
- {
- get {
-
- string v = value;
- if (valueBuilderAvailable)
- v = valueBuilder.ToString ();
- if(NodeType == XmlNodeType.Attribute)
- return UnescapeAttributeValue(v);
- else
- return v;
- }
- }
- 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 ();
- }
- public override string GetAttribute (int i)
- {
- if (i > attributes.Count)
- throw new ArgumentOutOfRangeException ("i is smaller than AttributeCount");
- else
- return UnescapeAttributeValue (attributes [orderedAttributes [i]] as string);
- }
- // MS.NET 1.0 documentation says that this method returns String.Empty for
- // not-exist attribute, but in fact it returns null.
- // That description is corrected in MS.NET 1.1 documentation.
- public override string GetAttribute (string name)
- {
- return UnescapeAttributeValue (attributes [name] as string);
- }
- private int GetIndexOfQualifiedAttribute (string localName, string namespaceURI)
- {
- for(int i = 0; i < orderedAttributes.Count; i++)
- {
- string thisName = (string) orderedAttributes [i];
- int indexOfColon = thisName.IndexOf (':');
- if (indexOfColon != -1) {
- string thisLocalName = thisName.Substring (indexOfColon + 1);
- if (localName == thisLocalName) {
- string thisPrefix = thisName.Substring (0, indexOfColon);
- string thisNamespaceURI = LookupNamespace (thisPrefix);
- if (namespaceURI == thisNamespaceURI)
- return i;
- }
- } else if (localName == "xmlns" && namespaceURI == "http://www.w3.org/2000/xmlns/" && thisName == "xmlns")
- 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 UnescapeAttributeValue (attributes [orderedAttributes [idx]] as string);
- }
- [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)
- {
- MoveToElement ();
- if (attributes == null || orderedAttributes.Count < i || i < 0)
- throw new ArgumentOutOfRangeException ("attribute index out of range.");
- if (orderedAttributesEnumerator == null) {
- SaveProperties ();
- }
- orderedAttributesEnumerator = orderedAttributes.GetEnumerator ();
- for (int n=0; n<=i; n++)
- orderedAttributesEnumerator.MoveNext();
- string name = orderedAttributes [i] as string;
- string value = attributes [name] as string;
- SetProperties (
- XmlNodeType.Attribute, // nodeType
- name, // name
- false, // isEmptyElement
- value, // value
- false // clearAttributes
- );
- attributeValuePos = 0;
- }
- public override bool MoveToAttribute (string name)
- {
- MoveToElement ();
- bool match = false;
- if (attributes == null)
- return false;
- if (orderedAttributesEnumerator == null) {
- SaveProperties ();
- }
- orderedAttributesEnumerator = orderedAttributes.GetEnumerator ();
- while (orderedAttributesEnumerator.MoveNext ()) {
- if(name == orderedAttributesEnumerator.Current as string) {
- match = true;
- break;
- }
- }
- if (match) {
- string value = attributes [name] as string;
- SetProperties (
- XmlNodeType.Attribute, // nodeType
- name, // name
- false, // isEmptyElement
- value, // value
- false // clearAttributes
- );
- attributeValuePos = 0;
- }
- return match;
- }
- public override bool MoveToAttribute (string localName, string namespaceName)
- {
- MoveToElement ();
- if (attributes == null)
- return false;
- int idx = GetIndexOfQualifiedAttribute (localName, namespaceName);
- if (idx < 0)
- return false;
- MoveToAttribute (idx);
- return true;
- }
- public override bool MoveToElement ()
- {
- if (orderedAttributesEnumerator != null) {
- orderedAttributesEnumerator = null;
- if (isPropertySaved)
- RestoreProperties ();
- return true;
- }
- return false;
- }
- public override bool MoveToFirstAttribute ()
- {
- MoveToElement ();
- return MoveToNextAttribute ();
- }
- public override bool MoveToNextAttribute ()
- {
- if (attributes == null)
- return false;
- if (orderedAttributesEnumerator == null) {
- SaveProperties ();
- orderedAttributesEnumerator = orderedAttributes.GetEnumerator ();
- }
- if (orderedAttributesEnumerator.MoveNext ()) {
- string name = orderedAttributesEnumerator.Current as string;
- string value = attributes [name] as string;
- SetProperties (
- XmlNodeType.Attribute, // nodeType
- name, // name
- false, // isEmptyElement
- value, // value
- false // clearAttributes
- );
- attributeValuePos = 0;
- return true;
- }
- return false;
- }
- public override bool Read ()
- {
- bool more = false;
- isPropertySaved = false;
- readState = ReadState.Interactive;
- // 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 was not appeared.");
- return more;
- }
- public override bool ReadAttributeValue ()
- {
- // 'attributeString' holds real string value (without their
- // quotation characters).
- //
- // 'attributeValuePos' holds current position
- // of 'attributeString' while iterating ReadAttribute().
- // It may be:
- // -1 if ReadAttributeValue() has already finished.
- // 0 if ReadAttributeValue() ready to start reading.
- // >0 if ReadAttributeValue() already got 1 or more values
- //
- // local 'refPosition' holds the position on the
- // attributeString which may be used next time.
- if (attributeValuePos < 0)
- return false;
- // If not started, then initialize attributeString when parsing is at start.
- if (attributeValuePos == 0)
- attributeString =
- value.Substring (1, value.Length - 2);
- // It occurs when attribute dully consists of entity reference.
- if (attributeValuePos == attributeString.Length)
- return false;
- returnEntityReference = false;
- value = String.Empty;
- int refPosition;
- int loop = 0;
- do {
- refPosition = attributeString.IndexOf ('&', attributeValuePos);
- if (refPosition < 0) {
- // Reached to the end of value string.
- value += attributeString.Substring (attributeValuePos);
- attributeValuePos = -1;
- break;
- } else if (refPosition == attributeValuePos) {
- string parsed = ReadAttributeValueReference ();
- if (parsed != null)
- value += parsed;
- else {
- // Found that an entity reference starts from this point.
- // reset position to after '&'.
- attributeValuePos = refPosition;
- if (value.Length <= 0) {
- int endNamePos = attributeString.IndexOf (";", attributeValuePos);
- value = attributeString.Substring (attributeValuePos+1, endNamePos - attributeValuePos - 1);
- attributeValuePos += value.Length + 2;
- returnEntityReference = true;
- }
- break;
- }
- } else {
- value += attributeString.Substring (attributeValuePos,
- refPosition - attributeValuePos);
- attributeValuePos = refPosition;
- continue;
- }
- } while (++loop > 0);
- if (returnEntityReference)
- SetProperties (XmlNodeType.EntityReference,
- value,
- false,
- String.Empty,
- false);
- else
- SetProperties (XmlNodeType.Text,
- "",
- false,
- value,
- false);
- returnEntityReference = false;
- return true;
- }
- [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
- 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 XmlNodeType nodeType;
- private string name;
- private string prefix;
- private string localName;
- private string namespaceURI;
- private bool isEmptyElement;
- private string value;
- private StringBuilder valueBuilder;
- private bool valueBuilderAvailable = false;
- private bool isPropertySaved;
- private XmlNodeType saveNodeType;
- private string saveName;
- private string savePrefix;
- private string saveLocalName;
- private string saveNamespaceURI;
- private bool saveIsEmptyElement;
- private Hashtable attributes;
- private ArrayList orderedAttributes;
- private IEnumerator orderedAttributesEnumerator;
- private bool returnEntityReference;
- private string entityReferenceName;
- private char [] nameBuffer;
- private int nameLength;
- private int nameCapacity;
- private const int initialNameCapacity = 256;
- private StringBuilder valueBuffer;
- // A buffer for ReadContent for ReadOuterXml
- private StringBuilder currentTag {
- get {
- return currentInput.CurrentMarkup;
- }
- }
- private string attributeString;
- private int attributeValuePos;
- // This should be only referenced(used) by ReadInnerXml(). Kind of flyweight pattern.
- private StringBuilder innerXmlBuilder;
- // 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;
- // These values are never re-initialized.
- private XmlResolver resolver = new XmlUrlResolver ();
- 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();
- nodeType = XmlNodeType.None;
- name = String.Empty;
- prefix = String.Empty;
- localName = string.Empty;
- isEmptyElement = false;
- value = String.Empty;
- attributes = new Hashtable ();
- attributeString = String.Empty;
- orderedAttributes = new ArrayList ();
- orderedAttributesEnumerator = null;
- returnEntityReference = false;
- entityReferenceName = String.Empty;
- nameBuffer = new char [initialNameCapacity];
- nameLength = 0;
- nameCapacity = initialNameCapacity;
-
- valueBuffer = new StringBuilder (8192);
- parameterEntities = new Hashtable ();
- }
- 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:
- value = String.Format ("{0}{1}{0}", "'", 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);
- StreamReader sr = fragment as StreamReader;
- }
- // 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)
- {
- this.nodeType = nodeType;
- this.name = name;
- this.isEmptyElement = isEmptyElement;
- this.value = value;
- this.elementDepth = depth;
- this.valueBuilderAvailable = false;
- if (clearAttributes)
- ClearAttributes ();
- if (namespaces) {
- int indexOfColon = name.IndexOf (':');
- if (indexOfColon == -1) {
- prefix = String.Empty;
- localName = name;
- } else {
- prefix = name.Substring (0, indexOfColon);
- localName = name.Substring (indexOfColon + 1);
- }
- } else {
- prefix = String.Empty;
- localName = name;
- }
- switch (nodeType) {
- case XmlNodeType.Attribute:
- if (prefix == string.Empty) namespaceURI = string.Empty;
- else namespaceURI = LookupNamespace (prefix);
- if (localName == "xmlns" && prefix == "")
- namespaceURI = "http://www.w3.org/2000/xmlns/";
- break;
- case XmlNodeType.Element:
- case XmlNodeType.EndElement:
- namespaceURI = LookupNamespace (prefix);
- break;
- default:
- namespaceURI = "";
- break;
- }
- }
-
- private void SetProperties (
- XmlNodeType nodeType,
- string name,
- bool isEmptyElement,
- StringBuilder value,
- bool clearAttributes) {
- SetProperties (nodeType, name, isEmptyElement, (string)null, clearAttributes);
- this.valueBuilderAvailable = true;
- this.valueBuilder = value;
- }
- private void SaveProperties ()
- {
- // If already saved, then return.
- if (isPropertySaved)
- return;
- saveNodeType = nodeType;
- saveName = name;
- savePrefix = prefix;
- saveLocalName = localName;
- saveNamespaceURI = namespaceURI;
- saveIsEmptyElement = isEmptyElement;
- // An element's value is always String.Empty.
- isPropertySaved = true;
- }
- private void RestoreProperties ()
- {
- nodeType = saveNodeType;
- name = saveName;
- prefix = savePrefix;
- localName = saveLocalName;
- namespaceURI = saveNamespaceURI;
- isEmptyElement = saveIsEmptyElement;
- value = String.Empty;
- isPropertySaved = false;
- }
- private void AddAttribute (string name, string value)
- {
- if (attributes.ContainsKey (name))
- throw new XmlException (this as IXmlLineInfo,
- String.Format ("Attribute {0} already exists.", name));
- attributes.Add (name, value);
- orderedAttributes.Add (name);
- }
- private void ClearAttributes ()
- {
- if (attributes.Count > 0) {
- attributes.Clear ();
- orderedAttributes.Clear ();
- }
- orderedAttributesEnumerator = null;
- }
- 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:
- if (depth > 0)
- throw new XmlException ("unexpected end of file. Current depth is " + depth);
- readState = ReadState.EndOfFile;
- SetProperties (
- XmlNodeType.None, // nodeType
- String.Empty, // name
- false, // isEmptyElement
- String.Empty, // value
- true // clearAttributes
- );
- break;
- 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.Empty, // 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);
- 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.Empty, // 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.Empty, // 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 : XmlNodeType.Whitespace;
- SetProperties (
- nodeType, // nodeType
- String.Empty, // name
- false, // isEmptyElement
- valueBuffer, // value
- true // clearAttributes
- );
- }
- }
- // 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 allowPIEnd)
- {
- int peekChar = -1;
- bool requireWhitespace = false;
- do {
- if (!SkipWhitespace () && requireWhitespace)
- throw new XmlException ("Unexpected token. Name is required here.");
- string name = ReadName ();
- SkipWhitespace ();
- Expect ('=');
- SkipWhitespace ();
- string value = ReadAttribute (false);
- if (name == "xmlns")
- parserContext.NamespaceManager.AddNamespace (String.Empty, UnescapeAttributeValue (value));
- else if (name.StartsWith ("xmlns:"))
- parserContext.NamespaceManager.AddNamespace (name.Substring (6), UnescapeAttributeValue (value));
- AddAttribute (name, value);
- if (!SkipWhitespace ())
- requireWhitespace = true;
- peekChar = PeekChar ();
- if (peekChar == '?' && allowPIEnd)
- break;
- } while (peekChar != '/' && peekChar != '>' && peekChar != -1);
- }
- // The reader is positioned on the quote character.
- // *Keeps quote char* to value to get_QuoteChar() correctly.
- 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
- valueBuffer, // value
- true // clearAttributes
- );
- }
- // 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 = (string) attributes ["version"];
- if (version != null)
- version = version.Substring (1, version.Length - 2);
- string message = null;
- if (parserInputStack.Count == 0) {
- if (maybeTextDecl == 0 && (orderedAttributes [0] as string != "version" || version != "1.0"))
- message = "Version 1.0 declaration is required in XML Declaration.";
- else if (orderedAttributes.Count > 1 &&
- (orderedAttributes [1] as string != "encoding" &&
- orderedAttributes [1] as string != "standalone"))
- message = "Invalid Xml Declaration markup was found.";
- else if (orderedAttributes.Count > 2 && orderedAttributes [2] as string != "standalone")
- message = "Invalid Xml Declaration markup was found.";
- string sa = attributes ["standalone"] as string;
- if (sa != null)
- sa = sa.Substring (1, sa.Length - 2);
- 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 (orderedAttributes [0] as string == "version") {
- if (version != "1.0")
- message = "Version 1.0 declaration is required in Text Declaration.";
- currentCheck = 1;
- }
- if (orderedAttributes.Count <= currentCheck || orderedAttributes [currentCheck] as string != "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
- valueBuffer, // value
- true // clearAttributes
- );
- }
- // 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
- valueBuffer, // value
- true // clearAttributes
- );
- }
- // 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
- );
- }
- 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;
- if (internalSubset != null && internalSubset.Length > 0) {
- XmlParserInput original = currentInput;
- currentInput = new XmlParserInput (new StringReader (internalSubset), BaseURI, intSubsetStartLine, intSubsetStartColumn);
- do {
- CompileDTDSubset ();
- if (PeekChar () == -1 && parserInputStack.Count > 0)
- PopParserInput ();
- } while (nodeType != XmlNodeType.None || 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 {
- this.CompileDTDSubset ();
- if (PeekChar () == -1 && parserInputStack.Count > 1)
- PopParserInput ();
- } while (nodeType != XmlNodeType.None || 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 a node of some nodeType or null, setting nodeType.
- // (if None then ']' was found.)
- private void CompileDTDSubset()
- {
- SkipWhitespace ();
- switch(PeekChar ())
- {
- case -1:
- nodeType = XmlNodeType.None;
- break;
- 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 ();
- break;
- default:
- throw new XmlException (this as IXmlLineInfo,String.Format ("Syntax Error inside doctypedecl markup : {0}({1})", PeekChar (), (char) PeekChar ()));
- }
- }
- private void CompileDeclaration ()
- {
- nodeType = XmlNodeType.DocumentType; // Hack!!
- 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 = attributes ["PUBLIC"] as string;
- decl.SystemId = attributes ["SYSTEM"] as string;
- 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 = attributes ["PUBLIC"] as string;
- decl.SystemId = attributes ["SYSTEM"] as string;
- 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
- }
- }
- }
- 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 (currentTag.ToString (start, currentTag.Length - start), false);
- 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() {
- switch(PeekChar ()) {
- case 'S':
- attributes ["PUBLIC"] = null;
- attributes ["SYSTEM"] = ReadSystemLiteral (true);
- break;
- case 'P':
- attributes ["PUBLIC"] = ReadPubidLiteral ();
- if (!SkipWhitespace ())
- throw new XmlException (this as IXmlLineInfo,
- "Whitespace is required between PUBLIC id and SYSTEM id.");
- attributes ["SYSTEM"] = ReadSystemLiteral (false);
- 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;
- while(c != quoteChar) {
- c = ReadChar ();
- if(c < 0) throw new XmlException (this as IXmlLineInfo,"Unexpected end of stream in ExternalID.");
- }
- return 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;
- 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");
- }
- return 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
- SetProperties (XmlNodeType.Whitespace,
- String.Empty,
- false,
- valueBuffer,
- true);
- return; // (PeekChar () != -1);
- }
- // read entity reference from attribute string and if parsable then return the value.
- private string ReadAttributeValueReference ()
- {
- int endEntityPosition = attributeString.IndexOf(';',
- attributeValuePos);
- if (endEntityPosition < 0)
- throw new XmlException ("Insufficient markup of entity reference");
- string entityName = attributeString.Substring (attributeValuePos + 1,
- endEntityPosition - attributeValuePos - 1);
- attributeValuePos = endEntityPosition + 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));
- }
- return c.ToString();
- }
- else {
- switch(entityName)
- {
- case "lt": return "<";
- case "gt": return ">";
- case "amp": return "&";
- case "quot": return "\"";
- case "apos": return "'";
- default: return null;
- }
- }
- }
- private string UnescapeAttributeValue (string unresolved)
- {
- if(unresolved == null) return null;
- // trim start/end edge of quotation character.
- return Dereference (unresolved.Substring (1, unresolved.Length - 2), true);
- }
- 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
- }
- }
|