| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291 |
- //
- // System.Xml.XmlTextReader
- //
- // Author:
- // Jason Diamond ([email protected])
- // Adam Treat ([email protected])
- //
- // (C) 2001, 2002 Jason Diamond http://injektilo.org/
- //
- // FIXME:
- // This can only parse basic XML: elements, attributes, processing
- // instructions, and comments are OK.
- //
- // It barfs on DOCTYPE declarations.
- //
- // There's also no checking being done for either well-formedness
- // or validity.
- //
- // NameTables aren't being used everywhere yet.
- //
- // Some thought needs to be given to performance. There's too many
- // strings being allocated.
- //
- // Some of the MoveTo methods haven't been implemented yet.
- //
- // LineNumber and LinePosition aren't being tracked.
- //
- // xml:space, xml:lang, and xml:base aren't being tracked.
- //
- using System;
- using System.Collections;
- using System.IO;
- using System.Text;
- namespace System.Xml
- {
- public class XmlTextReader : XmlReader, IXmlLineInfo
- {
- #region Constructors
- [MonoTODO]
- protected XmlTextReader ()
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public XmlTextReader (Stream input)
- {
- // We can share some code in the constructors (at least for this one and next 2)
- XmlNameTable nt = new NameTable ();
- XmlNamespaceManager nsMgr = new XmlNamespaceManager (nt);
- parserContext = new XmlParserContext (null, nsMgr, null, XmlSpace.None);
- Init ();
- reader = new StreamReader (input);
- }
- [MonoTODO]
- public XmlTextReader (string url)
- {
- XmlNameTable nt = new NameTable ();
- XmlNamespaceManager nsMgr = new XmlNamespaceManager (nt);
- parserContext = new XmlParserContext (null, nsMgr, null, XmlSpace.None);
- Init ();
- reader = new StreamReader(url);
- }
- [MonoTODO]
- public XmlTextReader (TextReader input)
- {
- XmlNameTable nt = new NameTable ();
- XmlNamespaceManager nsMgr = new XmlNamespaceManager (nt);
- parserContext = new XmlParserContext (null, nsMgr, null, XmlSpace.None);
- Init ();
- reader = input;
- }
- [MonoTODO]
- protected XmlTextReader (XmlNameTable nt)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public XmlTextReader (Stream input, XmlNameTable nt)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public XmlTextReader (string url, Stream input)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public XmlTextReader (string url, TextReader input)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public XmlTextReader (string url, XmlNameTable nt)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public XmlTextReader (TextReader input, XmlNameTable nt)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public XmlTextReader (Stream xmlFragment, XmlNodeType fragType, XmlParserContext context)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public XmlTextReader (string url, Stream input, XmlNameTable nt)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public XmlTextReader (string url, TextReader input, XmlNameTable nt)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public XmlTextReader (string xmlFragment, XmlNodeType fragType, XmlParserContext context)
- {
- //Waiting for Validating reader for fragType rules.
- parserContext = context;
- Init ();
- reader = new StringReader(xmlFragment);
- }
- #endregion
- #region Properties
- public override int AttributeCount
- {
- get { return attributes.Count; }
- }
- [MonoTODO]
- public override string BaseURI
- {
- get { throw new NotImplementedException (); }
- }
- public override int Depth
- {
- get { return depth > 0 ? depth : 0; }
- }
- [MonoTODO]
- public Encoding Encoding
- {
- get { throw new NotImplementedException (); }
- }
- public override bool EOF
- {
- get
- {
- return
- readState == ReadState.EndOfFile ||
- readState == ReadState.Closed;
- }
- }
- public override bool HasValue
- {
- get { 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 line; }
- }
- public int LinePosition
- {
- get { return column; }
- }
- public override string LocalName
- {
- get { return localName; }
- }
- public override string Name
- {
- get { return name; }
- }
- [MonoTODO]
- public bool Namespaces
- {
- get { throw new NotImplementedException (); }
- set { throw new NotImplementedException (); }
- }
- 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 { throw new NotImplementedException (); }
- set { throw new NotImplementedException (); }
- }
- public override string Prefix
- {
- get { return prefix; }
- }
- [MonoTODO]
- public override char QuoteChar
- {
- get { throw new NotImplementedException (); }
- }
- public override ReadState ReadState
- {
- get { return readState; }
- }
- public override string Value
- {
- get { return value; }
- }
- [MonoTODO]
- public WhitespaceHandling WhitespaceHandling
- {
- get { throw new NotImplementedException (); }
- set { throw new NotImplementedException (); }
- }
- [MonoTODO]
- public override string XmlLang
- {
- get { throw new NotImplementedException (); }
- }
- [MonoTODO]
- public XmlResolver XmlResolver
- {
- set { throw new NotImplementedException (); }
- }
- [MonoTODO]
- public override XmlSpace XmlSpace
- {
- get { throw new NotImplementedException (); }
- }
- #endregion
- #region Methods
- [MonoTODO]
- public override void Close ()
- {
- readState = ReadState.Closed;
- }
- [MonoTODO]
- public override string GetAttribute (int i)
- {
- throw new NotImplementedException ();
- }
- public override string GetAttribute (string name)
- {
- return attributes [name] as string;
- }
- public override string GetAttribute (string localName, string namespaceURI)
- {
- foreach (DictionaryEntry entry in attributes)
- {
- string thisName = entry.Key as string;
- 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 attributes [thisName] as string;
- }
- } else if (localName == "xmlns" && namespaceURI == "http://www.w3.org/2000/xmlns/" && thisName == "xmlns")
- return attributes [thisName] as string;
- }
- return String.Empty;
- }
- [MonoTODO]
- public TextReader GetRemainder ()
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- bool IXmlLineInfo.HasLineInfo ()
- {
- return false;
- }
- public override string LookupNamespace (string prefix)
- {
- return parserContext.NamespaceManager.LookupNamespace (prefix);
- }
- [MonoTODO]
- public override void MoveToAttribute (int i)
- {
- throw new NotImplementedException ();
- }
- 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
- );
- }
-
- return match;
- }
- [MonoTODO]
- public override bool MoveToAttribute (string localName, string namespaceName)
- {
- throw new NotImplementedException ();
- }
- public override bool MoveToElement ()
- {
- if (orderedAttributesEnumerator != null) {
- orderedAttributesEnumerator = null;
- 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
- );
- return true;
- }
- return false;
- }
- public override bool Read ()
- {
- bool more = false;
- readState = ReadState.Interactive;
- more = ReadContent ();
- return more;
- }
- [MonoTODO]
- public override bool ReadAttributeValue ()
- {
- throw new NotImplementedException ();
- }
- [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 ();
- }
- [MonoTODO]
- public override string ReadInnerXml ()
- {
- // Still need a Well Formedness check.
- // Will wait for Validating reader ;-)
- if (NodeType == XmlNodeType.Attribute) {
- return Value;
- } else {
- saveToXmlBuffer = true;
- string startname = this.Name;
- string endname = string.Empty;
- readState = ReadState.Interactive;
- while (startname != endname) {
- ReadContent ();
- endname = this.Name;
- }
- xmlBuffer.Replace(currentTag.ToString (), "");
- saveToXmlBuffer = false;
- string InnerXml = xmlBuffer.ToString ();
- xmlBuffer.Length = 0;
- return InnerXml;
- }
- }
- [MonoTODO]
- public override string ReadOuterXml ()
- {
- // Still need a Well Formedness check.
- // Will wait for Validating reader ;-)
- if (NodeType == XmlNodeType.Attribute) {
- return Name+"=\""+Value+"\"";
- } else {
- saveToXmlBuffer = true;
- xmlBuffer.Append(currentTag.ToString ());
- string startname = this.Name;
- string endname = string.Empty;
- readState = ReadState.Interactive;
- while (startname != endname) {
- ReadContent ();
- endname = this.Name;
- }
- saveToXmlBuffer = false;
- string OuterXml = xmlBuffer.ToString ();
- xmlBuffer.Length = 0;
- return OuterXml;
- }
- }
- [MonoTODO]
- public override string ReadString ()
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public void ResetState ()
- {
- throw new NotImplementedException ();
- }
- public override void ResolveEntity ()
- {
- // XmlTextReaders don't resolve entities.
- throw new InvalidOperationException ("XmlTextReaders don't resolve entities.");
- }
- #endregion
- // privates
- private XmlParserContext parserContext;
- private TextReader reader;
- private ReadState readState;
- private int depth;
- private bool depthDown;
- private bool popScope;
- private XmlNodeType nodeType;
- private string name;
- private string prefix;
- private string localName;
- private string namespaceURI;
- private bool isEmptyElement;
- private string value;
- 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 char [] valueBuffer;
- private int valueLength;
- private int valueCapacity;
- private const int initialValueCapacity = 8192;
- private StringBuilder xmlBuffer; // This is for Read(Inner|Outer)Xml
- private StringBuilder currentTag; // A buffer for ReadContent for ReadOuterXml
- private bool saveToXmlBuffer;
- private int line;
- private int column;
- private void Init ()
- {
- readState = ReadState.Initial;
- depth = -1;
- depthDown = false;
- popScope = false;
- nodeType = XmlNodeType.None;
- name = String.Empty;
- prefix = String.Empty;
- localName = string.Empty;
- isEmptyElement = false;
- value = String.Empty;
- attributes = new Hashtable ();
- orderedAttributes = new ArrayList ();
- orderedAttributesEnumerator = null;
- returnEntityReference = false;
- entityReferenceName = String.Empty;
- nameBuffer = new char [initialNameCapacity];
- nameLength = 0;
- nameCapacity = initialNameCapacity;
- valueBuffer = new char [initialValueCapacity];
- valueLength = 0;
- valueCapacity = initialValueCapacity;
- xmlBuffer = new StringBuilder ();
- currentTag = new StringBuilder ();
- }
- // 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;
- if (clearAttributes)
- ClearAttributes ();
- int indexOfColon = name.IndexOf (':');
- if (indexOfColon == -1) {
- prefix = String.Empty;
- localName = name;
- } else {
- prefix = name.Substring (0, indexOfColon);
- localName = name.Substring (indexOfColon + 1);
- }
- namespaceURI = LookupNamespace (prefix);
- }
- private void SaveProperties ()
- {
- saveNodeType = nodeType;
- saveName = name;
- savePrefix = prefix;
- saveLocalName = localName;
- saveNamespaceURI = namespaceURI;
- saveIsEmptyElement = isEmptyElement;
- // An element's value is always String.Empty.
- }
- private void RestoreProperties ()
- {
- nodeType = saveNodeType;
- name = saveName;
- prefix = savePrefix;
- localName = saveLocalName;
- namespaceURI = saveNamespaceURI;
- isEmptyElement = saveIsEmptyElement;
- value = String.Empty;
- }
- private void AddAttribute (string name, string value)
- {
- attributes.Add (name, value);
- orderedAttributes.Add (name);
- }
- private void ClearAttributes ()
- {
- if (attributes.Count > 0) {
- attributes.Clear ();
- orderedAttributes.Clear ();
- }
- orderedAttributesEnumerator = null;
- }
- private int PeekChar ()
- {
- return reader.Peek ();
- }
- private int ReadChar ()
- {
- int ch = reader.Read ();
- if (ch == '\n') {
- line++;
- column = 1;
- } else {
- column++;
- }
- if (saveToXmlBuffer) {
- xmlBuffer.Append ((char) ch);
- }
- currentTag.Append ((char) ch);
- return ch;
- }
- // 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 ()
- {
- bool more = false;
- currentTag.Length = 0;
- if (popScope) {
- parserContext.NamespaceManager.PopScope ();
- popScope = false;
- }
- if (depthDown)
- --depth;
- if (returnEntityReference) {
- ++depth;
- SetEntityReferenceProperties ();
- more = true;
- } else {
- switch (PeekChar ())
- {
- case '<':
- ReadChar ();
- ReadTag ();
- more = true;
- break;
- case -1:
- readState = ReadState.EndOfFile;
- SetProperties (
- XmlNodeType.None, // nodeType
- String.Empty, // name
- false, // isEmptyElement
- String.Empty, // value
- true // clearAttributes
- );
- more = false;
- break;
- default:
- ReadText ();
- more = true;
- break;
- }
- }
- return more;
- }
- private void SetEntityReferenceProperties ()
- {
- 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 ()
- {
- parserContext.NamespaceManager.PushScope ();
- string name = ReadName ();
- SkipWhitespace ();
- bool isEmptyElement = false;
- ClearAttributes ();
- if (XmlChar.IsFirstNameChar (PeekChar ()))
- ReadAttributes ();
- if (PeekChar () == '/') {
- ReadChar ();
- isEmptyElement = true;
- depthDown = true;
- popScope = true;
- }
- Expect ('>');
- ++depth;
- 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 ()
- {
- string name = ReadName ();
- 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 new String (nameBuffer, 0, nameLength);
- }
- private void AppendValueChar (int ch)
- {
- CheckValueCapacity ();
- valueBuffer [valueLength++] = (char)ch;
- }
- private void CheckValueCapacity ()
- {
- if (valueLength == valueCapacity) {
- valueCapacity = valueCapacity * 2;
- char [] oldValueBuffer = valueBuffer;
- valueBuffer = new char [valueCapacity];
- Array.Copy (oldValueBuffer, valueBuffer, valueLength);
- }
- }
- private string CreateValueString ()
- {
- return new String (valueBuffer, 0, valueLength);
- }
- // The reader is positioned on the first character
- // of the text.
- private void ReadText ()
- {
- valueLength = 0;
- int ch = PeekChar ();
- while (ch != '<' && ch != -1) {
- if (ch == '&') {
- ReadChar ();
- if (ReadReference (false))
- break;
- } else
- AppendValueChar (ReadChar ());
- ch = PeekChar ();
- }
- if (returnEntityReference && valueLength == 0) {
- ++depth;
- SetEntityReferenceProperties ();
- } else {
- if (depth >= 0) {
- ++depth;
- depthDown = true;
- }
- SetProperties (
- XmlNodeType.Text, // nodeType
- String.Empty, // name
- false, // isEmptyElement
- CreateValueString (), // 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 (
- 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 (
- String.Format (
- "invalid decimal digit: {0} (#x{1:X})",
- (char)ch,
- ch));
- }
- }
- ReadChar (); // ';'
- AppendValueChar (value);
- }
- private void ReadEntityReference (bool ignoreEntityReferences)
- {
- nameLength = 0;
- int ch = PeekChar ();
- while (ch != ';' && ch != -1) {
- AppendNameChar (ReadChar ());
- ch = PeekChar ();
- }
- Expect (';');
- string name = CreateNameString ();
- switch (name)
- {
- case "lt":
- AppendValueChar ('<');
- break;
- case "gt":
- AppendValueChar ('>');
- break;
- case "amp":
- AppendValueChar ('&');
- break;
- case "apos":
- AppendValueChar ('\'');
- break;
- case "quot":
- AppendValueChar ('"');
- break;
- default:
- if (ignoreEntityReferences) {
- AppendValueChar ('&');
- foreach (char ch2 in name) {
- AppendValueChar (ch2);
- }
- AppendValueChar (';');
- } else {
- returnEntityReference = true;
- entityReferenceName = name;
- }
- break;
- }
- }
- // The reader is positioned on the first character of
- // the attribute name.
- private void ReadAttributes ()
- {
- do {
- string name = ReadName ();
- SkipWhitespace ();
- Expect ('=');
- SkipWhitespace ();
- string value = ReadAttribute ();
- SkipWhitespace ();
- if (name == "xmlns")
- parserContext.NamespaceManager.AddNamespace (String.Empty, value);
- else if (name.StartsWith ("xmlns:"))
- parserContext.NamespaceManager.AddNamespace (name.Substring (6), value);
- AddAttribute (name, value);
- } while (PeekChar () != '/' && PeekChar () != '>' && PeekChar () != -1);
- }
- // The reader is positioned on the quote character.
- private string ReadAttribute ()
- {
- int quoteChar = ReadChar ();
- if (quoteChar != '\'' && quoteChar != '\"')
- throw new XmlException ("an attribute value was not quoted");
- valueLength = 0;
- while (PeekChar () != quoteChar) {
- int ch = ReadChar ();
- switch (ch)
- {
- case '<':
- throw new XmlException ("attribute values cannot contain '<'");
- case '&':
- ReadReference (true);
- break;
- case -1:
- throw new XmlException ("unexpected end of file in an attribute value");
- default:
- AppendValueChar (ch);
- break;
- }
- }
- ReadChar (); // quoteChar
- return CreateValueString ();
- }
- // The reader is positioned on the first character
- // of the target.
- private void ReadProcessingInstruction ()
- {
- string target = ReadName ();
- SkipWhitespace ();
- valueLength = 0;
- while (PeekChar () != -1) {
- int ch = ReadChar ();
- if (ch == '?' && PeekChar () == '>') {
- ReadChar ();
- break;
- }
- AppendValueChar ((char)ch);
- }
- SetProperties (
- XmlNodeType.ProcessingInstruction, // nodeType
- target, // name
- false, // isEmptyElement
- CreateValueString (), // value
- true // clearAttributes
- );
- }
- // The reader is positioned on the first character after
- // the leading '<!'.
- private void ReadDeclaration ()
- {
- int ch = PeekChar ();
- switch (ch)
- {
- case '-':
- Expect ('-');
- Expect ('-');
- ReadComment ();
- break;
- case '[':
- ReadChar ();
- Expect ('C');
- Expect ('D');
- Expect ('A');
- Expect ('T');
- Expect ('A');
- Expect ('[');
- ReadCDATA ();
- break;
- }
- }
- // The reader is positioned on the first character after
- // the leading '<!--'.
- private void ReadComment ()
- {
- valueLength = 0;
- while (PeekChar () != -1) {
- int ch = ReadChar ();
- if (ch == '-' && PeekChar () == '-') {
- ReadChar ();
- if (PeekChar () != '>')
- throw new XmlException ("comments cannot contain '--'");
- ReadChar ();
- break;
- }
- AppendValueChar ((char)ch);
- }
- SetProperties (
- XmlNodeType.Comment, // nodeType
- String.Empty, // name
- false, // isEmptyElement
- CreateValueString (), // value
- true // clearAttributes
- );
- }
- // The reader is positioned on the first character after
- // the leading '<![CDATA['.
- private void ReadCDATA ()
- {
- valueLength = 0;
- while (PeekChar () != -1) {
- int ch = ReadChar ();
- if (ch == ']' && PeekChar () == ']') {
- ch = ReadChar (); // ']'
- if (PeekChar () == '>') {
- ReadChar (); // '>'
- break;
- } else {
- AppendValueChar (']');
- AppendValueChar (']');
- ch = ReadChar ();
- }
- }
- AppendValueChar ((char)ch);
- }
- ++depth;
- SetProperties (
- XmlNodeType.CDATA, // nodeType
- String.Empty, // name
- false, // isEmptyElement
- CreateValueString (), // value
- true // clearAttributes
- );
- }
- // The reader is positioned on the first character
- // of the name.
- private string ReadName ()
- {
- if (!XmlChar.IsFirstNameChar (PeekChar ()))
- throw new XmlException ("a name did not start with a legal character");
- 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 (
- String.Format (
- "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
- (char)expected,
- expected,
- (char)ch,
- ch));
- }
- }
- // Does not consume the first non-whitespace character.
- private void SkipWhitespace ()
- {
- while (XmlChar.IsWhitespace (PeekChar ()))
- ReadChar ();
- }
- }
- }
|