The Labs \ Source Viewer \ SSCLI \ System.Xml \ XmlRawWriter

  1. //------------------------------------------------------------------------------
  2. // <copyright file="XmlRawWriter.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System;
  16. using System.IO;
  17. using System.Diagnostics;
  18. using System.Xml.XPath;
  19. using System.Xml.Schema;
  20. using System.Collections;
  21. namespace System.Xml
  22. {
  23.    
  24.    
  25.     /// <summary>
  26.     /// Implementations of XmlRawWriter are intended to be wrapped by the XmlWellFormedWriter. The
  27.     /// well-formed writer performs many checks in behalf of the raw writer, and keeps state that the
  28.     /// raw writer otherwise would have to keep. Therefore, the well-formed writer will call the
  29.     /// XmlRawWriter using the following rules, in order to make raw writers easier to implement:
  30.     ///
  31.     /// 1. The well-formed writer keeps a stack of element names, and always calls
  32.     /// WriteEndElement(string, string, string) instead of WriteEndElement().
  33.     /// 2. The well-formed writer tracks namespaces, and will pass himself in via the
  34.     /// WellformedWriter property. It is used in the XmlRawWriter's implementation of IXmlNamespaceResolver.
  35.     /// Thus, LookupPrefix does not have to be implemented.
  36.     /// 3. The well-formed writer tracks write states, so the raw writer doesn't need to.
  37.     /// 4. The well-formed writer will always call StartElementContent.
  38.     /// 5. The well-formed writer will always call WriteNamespaceDeclaration for namespace nodes,
  39.     /// rather than calling WriteStartAttribute().
  40.     /// 6. The well-formed writer guarantees a well-formed document, including correct call sequences,
  41.     /// correct namespaces, and correct document rule enforcement.
  42.     /// 7. All element and attribute names will be fully resolved and validated. Null will never be
  43.     /// passed for any of the name parts.
  44.     /// 8. The well-formed writer keeps track of xml:space and xml:lang.
  45.     /// 9. The well-formed writer verifies NmToken, Name, and QName values and calls WriteString().
  46.     /// </summary>
  47.     internal abstract class XmlRawWriter : XmlWriter
  48.     {
  49.         //
  50.         // Fields
  51.         //
  52.         // base64 converter
  53.         protected XmlRawWriterBase64Encoder base64Encoder;
  54.        
  55.         // namespace resolver
  56.         protected IXmlNamespaceResolver resolver;
  57.        
  58.         //
  59.         // Constants
  60.         //
  61.         internal const int SurHighStart = 55296;
  62.         // 1101 10xx
  63.         internal const int SurHighEnd = 56319;
  64.         internal const int SurLowStart = 56320;
  65.         // 1101 11xx
  66.         internal const int SurLowEnd = 57343;
  67.         internal const int SurMask = 64512;
  68.         // 1111 11xx
  69.         //
  70.         // XmlWriter implementation
  71.         //
  72.         // Raw writers do not have to track whether this is a well-formed document.
  73.         public override void WriteStartDocument()
  74.         {
  75.             throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  76.         }
  77.        
  78.         public override void WriteStartDocument(bool standalone)
  79.         {
  80.             throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  81.         }
  82.        
  83.         public override void WriteEndDocument()
  84.         {
  85.             throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  86.         }
  87.        
  88.         public override void WriteDocType(string name, string pubid, string sysid, string subset)
  89.         {
  90.         }
  91.        
  92.         // Raw writers do not have to keep a stack of element names.
  93.         public override void WriteEndElement()
  94.         {
  95.             throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  96.         }
  97.        
  98.         // Raw writers do not have to keep a stack of element names.
  99.         public override void WriteFullEndElement()
  100.         {
  101.             throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  102.         }
  103.        
  104.         // By default, convert base64 value to string and call WriteString.
  105.         public override void WriteBase64(byte[] buffer, int index, int count)
  106.         {
  107.             if (base64Encoder == null) {
  108.                 base64Encoder = new XmlRawWriterBase64Encoder(this);
  109.             }
  110.             // Encode will call WriteRaw to write out the encoded characters
  111.             base64Encoder.Encode(buffer, index, count);
  112.         }
  113.        
  114.         // Raw writers do not have to keep track of namespaces.
  115.         public override string LookupPrefix(string ns)
  116.         {
  117.             throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  118.         }
  119.        
  120.         // Raw writers do not have to keep track of write states.
  121.         public override WriteState WriteState {
  122.             get {
  123.                 throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  124.             }
  125.         }
  126.        
  127.         // Raw writers do not have to keep track of xml:space.
  128.         public override XmlSpace XmlSpace {
  129.             get {
  130.                 throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  131.             }
  132.         }
  133.        
  134.         // Raw writers do not have to keep track of xml:lang.
  135.         public override string XmlLang {
  136.             get {
  137.                 throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  138.             }
  139.         }
  140.        
  141.         // Raw writers do not have to verify NmToken values.
  142.         public override void WriteNmToken(string name)
  143.         {
  144.             throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  145.         }
  146.        
  147.         // Raw writers do not have to verify Name values.
  148.         public override void WriteName(string name)
  149.         {
  150.             throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  151.         }
  152.        
  153.         // Raw writers do not have to verify QName values.
  154.         public override void WriteQualifiedName(string localName, string ns)
  155.         {
  156.             throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  157.         }
  158.        
  159.         // Forward call to WriteString(string).
  160.         public override void WriteCData(string text)
  161.         {
  162.             WriteString(text);
  163.         }
  164.        
  165.         // Forward call to WriteString(string).
  166.         public override void WriteCharEntity(char ch)
  167.         {
  168.             WriteString(new string(new char[] {ch}));
  169.         }
  170.        
  171.         // Forward call to WriteString(string).
  172.         public override void WriteSurrogateCharEntity(char lowChar, char highChar)
  173.         {
  174.             WriteString(new string(new char[] {lowChar, highChar}));
  175.         }
  176.        
  177.         // Forward call to WriteString(string).
  178.         public override void WriteWhitespace(string ws)
  179.         {
  180.             WriteString(ws);
  181.         }
  182.        
  183.         // Forward call to WriteString(string).
  184.         public override void WriteChars(char[] buffer, int index, int count)
  185.         {
  186.             WriteString(new string(buffer, index, count));
  187.         }
  188.        
  189.         // Forward call to WriteString(string).
  190.         public override void WriteRaw(char[] buffer, int index, int count)
  191.         {
  192.             WriteString(new string(buffer, index, count));
  193.         }
  194.        
  195.         // Forward call to WriteString(string).
  196.         public override void WriteRaw(string data)
  197.         {
  198.             WriteString(data);
  199.         }
  200.        
  201.         // Override in order to handle Xml simple typed values and to pass resolver for QName values
  202.         public override void WriteValue(object value)
  203.         {
  204.             if (value == null) {
  205.                 throw new ArgumentNullException("value");
  206.             }
  207.             WriteString(XmlUntypedConverter.Untyped.ToString(value, resolver));
  208.         }
  209.        
  210.         // Override in order to handle Xml simple typed values and to pass resolver for QName values
  211.         public override void WriteValue(string value)
  212.         {
  213.             WriteString(value);
  214.         }
  215.        
  216.         // Copying to XmlRawWriter is not currently supported.
  217.         public override void WriteAttributes(XmlReader reader, bool defattr)
  218.         {
  219.             throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  220.         }
  221.        
  222.         public override void WriteNode(XmlReader reader, bool defattr)
  223.         {
  224.             throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  225.         }
  226.        
  227.         public override void WriteNode(System.Xml.XPath.XPathNavigator navigator, bool defattr)
  228.         {
  229.             throw new InvalidOperationException(Res.GetString(Res.Xml_InvalidOperation));
  230.         }
  231.        
  232.         //
  233.         // XmlRawWriter methods and properties
  234.         //
  235.         // Get and set the namespace resolver that's used by this RawWriter to resolve prefixes.
  236.         internal virtual IXmlNamespaceResolver NamespaceResolver {
  237.             get { return resolver; }
  238.             set { resolver = value; }
  239.         }
  240.        
  241.         // Write the xml declaration. This must be the first call.
  242.         internal virtual void WriteXmlDeclaration(XmlStandalone standalone)
  243.         {
  244.         }
  245.         internal virtual void WriteXmlDeclaration(string xmldecl)
  246.         {
  247.         }
  248.        
  249.         // Called after an element's attributes have been enumerated, but before any children have been
  250.         // enumerated. This method must always be called, even for empty elements.
  251.         internal abstract void StartElementContent();
  252.        
  253.         // WriteEndElement() and WriteFullEndElement() overloads, in which caller gives the full name of the
  254.         // element, so that raw writers do not need to keep a stack of element names. This method should
  255.         // always be called instead of WriteEndElement() or WriteFullEndElement() without parameters.
  256.         internal abstract void WriteEndElement(string prefix, string localName, string ns);
  257.         internal virtual void WriteFullEndElement(string prefix, string localName, string ns)
  258.         {
  259.             WriteEndElement(prefix, localName, ns);
  260.         }
  261.        
  262.         internal virtual void WriteQualifiedName(string prefix, string localName, string ns)
  263.         {
  264.             if (prefix.Length != 0) {
  265.                 WriteString(prefix);
  266.                 WriteString(":");
  267.             }
  268.             WriteString(localName);
  269.         }
  270.        
  271.         // This method must be called instead of WriteStartAttribute() for namespaces.
  272.         internal abstract void WriteNamespaceDeclaration(string prefix, string ns);
  273.        
  274.         // This is called when the remainder of a base64 value should be output.
  275.         internal virtual void WriteEndBase64()
  276.         {
  277.             // The Flush will call WriteRaw to write out the rest of the encoded characters
  278.             base64Encoder.Flush();
  279.         }
  280.        
  281.         internal virtual void Close(WriteState currentState)
  282.         {
  283.             Close();
  284.         }
  285.     }
  286. }

Developer Fusion