| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662 |
- //
- // XmlDictionaryReader.cs
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- // Copyright (C) 2005, 2007 Novell, Inc. http://www.novell.com
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.IO;
- using System.Reflection;
- using System.Text;
- using System.Xml;
- namespace System.Xml
- {
- public abstract partial class XmlDictionaryReader : XmlReader
- {
- protected XmlDictionaryReader ()
- {
- }
- XmlDictionaryReaderQuotas quotas;
- public virtual bool CanCanonicalize {
- get { return false; }
- }
- public virtual XmlDictionaryReaderQuotas Quotas {
- get {
- if (quotas == null)
- quotas = new XmlDictionaryReaderQuotas ();
- return quotas;
- }
- }
- public virtual void EndCanonicalization ()
- {
- throw new NotSupportedException ();
- }
- public virtual string GetAttribute (
- XmlDictionaryString localName,
- XmlDictionaryString namespaceUri)
- {
- if (localName == null)
- throw new ArgumentNullException ("localName");
- if (namespaceUri == null)
- throw new ArgumentNullException ("namespaceUri");
- return GetAttribute (localName.Value, namespaceUri.Value);
- }
- public virtual int IndexOfLocalName (
- string [] localNames, string namespaceUri)
- {
- if (localNames == null)
- throw new ArgumentNullException ("localNames");
- if (namespaceUri == null)
- throw new ArgumentNullException ("namespaceUri");
- if (NamespaceURI != namespaceUri)
- return -1;
- for (int i = 0; i < localNames.Length; i++)
- if (localNames [i] == LocalName)
- return i;
- return -1;
- }
- public virtual int IndexOfLocalName (
- XmlDictionaryString [] localNames,
- XmlDictionaryString namespaceUri)
- {
- if (localNames == null)
- throw new ArgumentNullException ("localNames");
- if (namespaceUri == null)
- throw new ArgumentNullException ("namespaceUri");
- if (NamespaceURI != namespaceUri.Value)
- return -1;
- XmlDictionaryString localName;
- if (!TryGetLocalNameAsDictionaryString (out localName))
- return -1;
- IXmlDictionary dict = localName.Dictionary;
- XmlDictionaryString iter;
- for (int i = 0; i < localNames.Length; i++)
- if (dict.TryLookup (localNames [i], out iter) && object.ReferenceEquals (iter, localName))
- return i;
- return -1;
- }
- public virtual bool IsArray (out Type type)
- {
- type = null;
- return false;
- }
- public virtual bool IsLocalName (string localName)
- {
- return LocalName == localName;
- }
- public virtual bool IsLocalName (XmlDictionaryString localName)
- {
- if (localName == null)
- throw new ArgumentNullException ("localName");
- return LocalName == localName.Value;
- }
- public virtual bool IsNamespaceUri (string namespaceUri)
- {
- return NamespaceURI == namespaceUri;
- }
- public virtual bool IsNamespaceUri (XmlDictionaryString namespaceUri)
- {
- if (namespaceUri == null)
- throw new ArgumentNullException ("namespaceUri");
- return NamespaceURI == namespaceUri.Value;
- }
- public virtual bool IsStartArray (out Type type)
- {
- type = null;
- return false;
- }
- public virtual bool IsStartElement (
- XmlDictionaryString localName,
- XmlDictionaryString namespaceUri)
- {
- if (localName == null)
- throw new ArgumentNullException ("localName");
- if (namespaceUri == null)
- throw new ArgumentNullException ("namespaceUri");
- return IsStartElement (localName.Value, namespaceUri.Value);
- }
- protected bool IsTextNode (XmlNodeType nodeType)
- {
- switch (nodeType) {
- case XmlNodeType.Attribute: // wow, it isn't indeed.
- case XmlNodeType.Text:
- case XmlNodeType.CDATA:
- case XmlNodeType.Whitespace:
- case XmlNodeType.SignificantWhitespace:
- return true;
- default:
- return false;
- }
- }
- XmlException XmlError (string message)
- {
- IXmlLineInfo li = this as IXmlLineInfo;
- if (li == null || !li.HasLineInfo ())
- return new XmlException (message);
- else
- return new XmlException (String.Format ("{0} in {1} , at ({2},{3})", message, BaseURI, li.LineNumber, li.LinePosition));
- }
- public virtual void MoveToStartElement ()
- {
- MoveToContent ();
- if (NodeType != XmlNodeType.Element)
- throw XmlError (String.Format ("Element node is expected, but got {0} node.", NodeType));
- }
- public virtual void MoveToStartElement (string name)
- {
- if (name == null)
- throw new ArgumentNullException ("name");
- MoveToStartElement ();
- if (Name != name)
- throw XmlError (String.Format ("Element node '{0}' is expected, but got '{1}' element.", name, Name));
- }
- public virtual void MoveToStartElement (
- string localName, string namespaceUri)
- {
- if (localName == null)
- throw new ArgumentNullException ("localName");
- if (namespaceUri == null)
- throw new ArgumentNullException ("namespaceUri");
- MoveToStartElement ();
- if (LocalName != localName || NamespaceURI != namespaceUri)
- throw XmlError (String.Format ("Element node '{0}' in namespace '{1}' is expected, but got '{2}' in namespace '{3}' element.", localName, namespaceUri, LocalName, NamespaceURI));
- }
- public virtual void MoveToStartElement (
- XmlDictionaryString localName,
- XmlDictionaryString namespaceUri)
- {
- if (localName == null)
- throw new ArgumentNullException ("localName");
- if (namespaceUri == null)
- throw new ArgumentNullException ("namespaceUri");
- MoveToStartElement (localName.Value, namespaceUri.Value);
- }
- public virtual void StartCanonicalization (
- Stream stream, bool includeComments,
- string [] inclusivePrefixes)
- {
- throw new NotSupportedException ();
- }
- public virtual bool TryGetArrayLength (out int count)
- {
- count = -1;
- return false;
- }
- public virtual bool TryGetBase64ContentLength (out int count)
- {
- count = -1;
- return false;
- }
- public virtual bool TryGetLocalNameAsDictionaryString (
- out XmlDictionaryString localName)
- {
- localName = null;
- return false;
- }
- public virtual bool TryGetNamespaceUriAsDictionaryString (
- out XmlDictionaryString namespaceUri)
- {
- namespaceUri = null;
- return false;
- }
- #region Content Reader Methods
- public override object ReadContentAs (Type type, IXmlNamespaceResolver nsResolver)
- {
- return base.ReadContentAs (type, nsResolver);
- }
- public virtual byte [] ReadContentAsBase64 ()
- {
- int len;
- if (!TryGetBase64ContentLength (out len))
- return Convert.FromBase64String (ReadContentAsString ());
- byte [] bytes = new byte [len];
- ReadContentAsBase64 (bytes, 0, len);
- return bytes;
- }
- MethodInfo xmlconv_from_bin_hex = typeof (XmlConvert).GetMethod ("FromBinHexString", BindingFlags.Static | BindingFlags.NonPublic, null, new Type [] {typeof (string)}, null);
- byte [] FromBinHexString (string s)
- {
- return (byte []) xmlconv_from_bin_hex.Invoke (null, new object [] {s});
- }
- public virtual byte [] ReadContentAsBinHex ()
- {
- int len;
- if (!TryGetArrayLength (out len))
- return FromBinHexString (ReadContentAsString ());
- return ReadContentAsBinHex (len);
- }
- protected byte [] ReadContentAsBinHex (int maxByteArrayContentLength)
- {
- byte [] bytes = new byte [maxByteArrayContentLength];
- ReadContentAsBinHex (bytes, 0, maxByteArrayContentLength);
- return bytes;
- }
- [MonoTODO]
- public virtual int ReadContentAsChars (char [] chars, int offset, int count)
- {
- throw new NotImplementedException ();
- }
- public override decimal ReadContentAsDecimal ()
- {
- return base.ReadContentAsDecimal ();
- }
- public override float ReadContentAsFloat ()
- {
- return base.ReadContentAsFloat ();
- }
- public virtual Guid ReadContentAsGuid ()
- {
- return XmlConvert.ToGuid (ReadContentAsString ());
- }
- public virtual void ReadContentAsQualifiedName (out string localName, out string namespaceUri)
- {
- XmlQualifiedName qname = (XmlQualifiedName) ReadContentAs (typeof (XmlQualifiedName), this as IXmlNamespaceResolver);
- localName = qname.Name;
- namespaceUri = qname.Namespace;
- }
- public override string ReadContentAsString ()
- {
- return ReadContentAsString (Quotas.MaxStringContentLength);
- }
- [MonoTODO]
- protected string ReadContentAsString (int maxStringContentLength)
- {
- return base.ReadContentAsString ();
- }
- [MonoTODO ("there is exactly no information on the web")]
- public virtual string ReadContentAsString (string [] strings, out int index)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO ("there is exactly no information on the web")]
- public virtual string ReadContentAsString (XmlDictionaryString [] strings, out int index)
- {
- throw new NotImplementedException ();
- }
- public virtual TimeSpan ReadContentAsTimeSpan ()
- {
- return XmlConvert.ToTimeSpan (ReadContentAsString ());
- }
- public virtual UniqueId ReadContentAsUniqueId ()
- {
- return new UniqueId (ReadContentAsString ());
- }
- public virtual byte [] ReadElementContentAsBase64 ()
- {
- ReadStartElement ();
- byte [] ret = ReadContentAsBase64 ();
- ReadEndElement ();
- return ret;
- }
- public virtual byte [] ReadElementContentAsBinHex ()
- {
- ReadStartElement ();
- byte [] ret = ReadContentAsBinHex ();
- ReadEndElement ();
- return ret;
- }
- public virtual Guid ReadElementContentAsGuid ()
- {
- ReadStartElement ();
- Guid ret = ReadContentAsGuid ();
- ReadEndElement ();
- return ret;
- }
- public virtual TimeSpan ReadElementContentAsTimeSpan ()
- {
- ReadStartElement ();
- TimeSpan ret = ReadContentAsTimeSpan ();
- ReadEndElement ();
- return ret;
- }
- public virtual UniqueId ReadElementContentAsUniqueId ()
- {
- ReadStartElement ();
- UniqueId ret = ReadContentAsUniqueId ();
- ReadEndElement ();
- return ret;
- }
- public override string ReadElementContentAsString ()
- {
- if (IsEmptyElement) {
- Read ();
- return String.Empty;
- } else {
- ReadStartElement ();
- string s;
- if (NodeType == XmlNodeType.EndElement)
- s = String.Empty;
- else
- s = ReadContentAsString ();
- ReadEndElement ();
- return s;
- }
- }
- public virtual void ReadFullStartElement ()
- {
- if (!IsStartElement ())
- throw new XmlException ("Current node is not a start element");
- ReadStartElement ();
- }
- public virtual void ReadFullStartElement (string name)
- {
- if (!IsStartElement (name))
- throw new XmlException (String.Format ("Current node is not a start element '{0}'", name));
- ReadStartElement (name);
- }
- public virtual void ReadFullStartElement (string localName, string namespaceUri)
- {
- if (!IsStartElement (localName, namespaceUri))
- throw new XmlException (String.Format ("Current node is not a start element '{0}' in namesapce '{1}'", localName, namespaceUri));
- ReadStartElement (localName, namespaceUri);
- }
- public virtual void ReadFullStartElement (XmlDictionaryString localName, XmlDictionaryString namespaceUri)
- {
- if (!IsStartElement (localName, namespaceUri))
- throw new XmlException (String.Format ("Current node is not a start element '{0}' in namesapce '{1}'", localName, namespaceUri));
- ReadStartElement (localName.Value, namespaceUri.Value);
- }
- public virtual void ReadStartElement (XmlDictionaryString localName, XmlDictionaryString namespaceUri)
- {
- if (localName == null)
- throw new ArgumentNullException ("localName");
- if (namespaceUri == null)
- throw new ArgumentNullException ("namespaceUri");
- ReadStartElement (localName.Value, namespaceUri.Value);
- }
- public override string ReadString ()
- {
- return ReadString (Quotas.MaxStringContentLength);
- }
- [MonoTODO]
- protected string ReadString (int maxStringContentLength)
- {
- return base.ReadString ();
- }
- public virtual int ReadValueAsBase64 (byte [] bytes, int start, int length)
- {
- throw new NotSupportedException (); // as it is documented ...
- }
- public virtual bool TryGetValueAsDictionaryString (out XmlDictionaryString value)
- {
- throw new NotSupportedException (); // as documented
- }
- #endregion
- #region Factory Methods
- public static XmlDictionaryReader CreateBinaryReader (
- byte [] buffer, XmlDictionaryReaderQuotas quotas)
- {
- return CreateBinaryReader (buffer, 0, buffer.Length, quotas);
- }
- public static XmlDictionaryReader CreateBinaryReader (
- byte [] buffer, int offset, int count,
- XmlDictionaryReaderQuotas quotas)
- {
- return CreateBinaryReader (buffer, offset, count, new XmlDictionary (), quotas);
- }
- public static XmlDictionaryReader CreateBinaryReader (
- byte [] buffer, int offset, int count,
- IXmlDictionary dictionary,
- XmlDictionaryReaderQuotas quotas)
- {
- return CreateBinaryReader (buffer, offset, count,
- dictionary, quotas,
- new XmlBinaryReaderSession (), null);
- }
- public static XmlDictionaryReader CreateBinaryReader (
- byte [] buffer, int offset, int count,
- IXmlDictionary dictionary,
- XmlDictionaryReaderQuotas quotas,
- XmlBinaryReaderSession session)
- {
- return CreateBinaryReader (buffer, offset, count,
- dictionary, quotas,
- session, null);
- }
- public static XmlDictionaryReader CreateBinaryReader (
- byte [] buffer, int offset, int count,
- IXmlDictionary dictionary,
- XmlDictionaryReaderQuotas quotas,
- XmlBinaryReaderSession session,
- OnXmlDictionaryReaderClose onClose)
- {
- return new XmlBinaryDictionaryReader (buffer,
- offset, count,
- dictionary, quotas, session, onClose);
- }
- public static XmlDictionaryReader CreateBinaryReader (
- Stream stream, XmlDictionaryReaderQuotas quotas)
- {
- return CreateBinaryReader (stream, new XmlDictionary (), quotas);
- }
- public static XmlDictionaryReader CreateBinaryReader (
- Stream stream, IXmlDictionary dictionary,
- XmlDictionaryReaderQuotas quotas)
- {
- return CreateBinaryReader (stream, dictionary, quotas,
- new XmlBinaryReaderSession (), null);
- }
- public static XmlDictionaryReader CreateBinaryReader (
- Stream stream, IXmlDictionary dictionary,
- XmlDictionaryReaderQuotas quotas,
- XmlBinaryReaderSession session)
- {
- return CreateBinaryReader (stream, dictionary, quotas,
- session, null);
- }
- public static XmlDictionaryReader CreateBinaryReader (
- Stream stream, IXmlDictionary dictionary,
- XmlDictionaryReaderQuotas quotas,
- XmlBinaryReaderSession session,
- OnXmlDictionaryReaderClose onClose)
- {
- return new XmlBinaryDictionaryReader (stream,
- dictionary, quotas, session, onClose);
- }
- public static XmlDictionaryReader CreateDictionaryReader (
- XmlReader reader)
- {
- return new XmlSimpleDictionaryReader (reader);
- }
- #if !NET_2_1
- public static XmlDictionaryReader CreateMtomReader (
- Stream stream, Encoding encoding,
- XmlDictionaryReaderQuotas quotas)
- {
- return new XmlMtomDictionaryReader (stream, encoding, quotas);
- }
- public static XmlDictionaryReader CreateMtomReader (
- Stream stream, Encoding [] encodings,
- XmlDictionaryReaderQuotas quotas)
- {
- return CreateMtomReader (stream, encodings, null, quotas);
- }
- public static XmlDictionaryReader CreateMtomReader (
- Stream stream, Encoding [] encodings, string contentType,
- XmlDictionaryReaderQuotas quotas)
- {
- return CreateMtomReader (stream, encodings, contentType, quotas, int.MaxValue, null);
- }
- public static XmlDictionaryReader CreateMtomReader (
- Stream stream, Encoding [] encodings, string contentType,
- XmlDictionaryReaderQuotas quotas,
- int maxBufferSize,
- OnXmlDictionaryReaderClose onClose)
- {
- return new XmlMtomDictionaryReader (stream, encodings, contentType, quotas, maxBufferSize, onClose);
- }
- public static XmlDictionaryReader CreateMtomReader (
- byte [] buffer, int offset, int count,
- Encoding encoding, XmlDictionaryReaderQuotas quotas)
- {
- return CreateMtomReader (new MemoryStream (buffer, offset, count), encoding, quotas);
- }
- public static XmlDictionaryReader CreateMtomReader (
- byte [] buffer, int offset, int count,
- Encoding [] encodings, XmlDictionaryReaderQuotas quotas)
- {
- return CreateMtomReader (new MemoryStream (buffer, offset, count), encodings, quotas);
- }
- public static XmlDictionaryReader CreateMtomReader (
- byte [] buffer, int offset, int count,
- Encoding [] encodings, string contentType,
- XmlDictionaryReaderQuotas quotas)
- {
- return CreateMtomReader (new MemoryStream (buffer, offset, count), encodings, contentType, quotas);
- }
- public static XmlDictionaryReader CreateMtomReader (
- byte [] buffer, int offset, int count,
- Encoding [] encodings, string contentType,
- XmlDictionaryReaderQuotas quotas,
- int maxBufferSize,
- OnXmlDictionaryReaderClose onClose)
- {
- return CreateMtomReader (new MemoryStream (buffer, offset, count), encodings, contentType, quotas, maxBufferSize, onClose);
- }
- #endif
- public static XmlDictionaryReader CreateTextReader (byte [] buffer, XmlDictionaryReaderQuotas quotas)
- {
- return CreateTextReader (buffer, 0, buffer.Length, quotas);
- }
- public static XmlDictionaryReader CreateTextReader (
- byte [] buffer, int offset, int count,
- XmlDictionaryReaderQuotas quotas)
- {
- return CreateTextReader (buffer, offset, count,
- Encoding.UTF8, quotas, null);
- }
- public static XmlDictionaryReader CreateTextReader (
- byte [] buffer, int offset, int count,
- Encoding encoding,
- XmlDictionaryReaderQuotas quotas,
- OnXmlDictionaryReaderClose onClose)
- {
- return CreateTextReader (new MemoryStream (buffer, offset, count), encoding, quotas, onClose);
- }
- public static XmlDictionaryReader CreateTextReader (
- Stream stream, XmlDictionaryReaderQuotas quotas)
- {
- return CreateTextReader (stream, Encoding.UTF8, quotas, null);
- }
- public static XmlDictionaryReader CreateTextReader (
- Stream stream, Encoding encoding,
- XmlDictionaryReaderQuotas quotas,
- OnXmlDictionaryReaderClose onClose)
- {
- XmlReaderSettings s = new XmlReaderSettings ();
- XmlNameTable nt = new NameTable ();
- XmlParserContext c = new XmlParserContext (nt, new XmlNamespaceManager (nt), String.Empty, XmlSpace.None, encoding);
- XmlDictionaryReader res = new XmlSimpleDictionaryReader (XmlReader.Create (stream, s, c), null, onClose);
- res.quotas = quotas;
- return res;
- }
- #endregion
- }
- }
|