2002-12-21 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[mono.git] / mcs / class / System.XML / System.Xml / XmlWriter.cs
index 0d7f00113b80f1b724d4ea131452a8bbccf91130..29050c5f31512e164c8d03426557c6184dbd5aea 100644 (file)
-// -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-\r
-//\r
-// System.Xml.XmlWriter\r
-//\r
-// Author:\r
-//   Daniel Weber (daniel-weber@austin.rr.com)\r
-//\r
-// (C) 2001 Daniel Weber\r
-\r
-using System;\r
-\r
-namespace System.Xml\r
-{\r
-       /// <summary>\r
-       /// Abstract class XmlWriter\r
-       /// </summary>\r
-       public abstract class XmlWriter\r
-       {\r
-               // Private data members\r
-               //===========================================================================\r
-\r
-               // public properties\r
-               //===========================================================================\r
-               /// <summary>\r
-               /// Get the state of the writer.\r
-               /// </summary>\r
-               public abstract WriteState WriteState {get;}\r
-\r
-               /// <summary>\r
-               /// Get the current xml:lang scope, or null if not defined.\r
-               /// </summary>\r
-               public abstract string XmlLang {get;}\r
-\r
-               /// <summary>\r
-               /// get an XmlSpace representing the current xml:space scope\r
-               /// </summary>\r
-               public abstract XmlSpace XmlSpace {get;}\r
-\r
-               // Public Methods\r
-               //===========================================================================\r
-               /// <summary>\r
-               /// When overriden, closes this stream and the underlying stream.\r
-               /// </summary>\r
-               /// <exception cref="InvalidOperationException">A call is made to write more output when the stream is closed.</exception>\r
-               public abstract void Close();\r
-\r
-               /// <summary>\r
-               /// Flushes whatever is in the buffer to the underlying streams, and flushes any underlying streams.\r
-               /// </summary>\r
-               public abstract void Flush();\r
-\r
-               /// <summary>\r
-               /// Returns closest prefix in current namespace, or null if none found.\r
-               /// </summary>\r
-               /// <param name="ns">namespace URI to find a prefix for.</param>\r
-               /// <exception cref="ArgumentException">ns is null, or string.Empty</exception>\r
-               /// <returns></returns>\r
-               public abstract string LookupPrefix(string ns);\r
-\r
-               /// <summary>\r
-               /// Write out all the attributes found at the current position in the XmlReader\r
-               /// </summary>\r
-               /// <param name="reader">XmlReader to read from</param>\r
-               /// <param name="defattr">true to copy default attributes</param>\r
-               /// <exception cref="ArgumentException">Reader is a null reference</exception>\r
-               public virtual void WriteAttributes(\r
-                       XmlReader reader,\r
-                       bool defattr\r
-                       )\r
-               {\r
-                       //TODO - implement XmlWriter.WriteAttributes(XmlReader, bool)\r
-                       throw new NotImplementedException();\r
-               }\r
-\r
-\r
-       }\r
-}\r
+//
+// System.Xml.XmlTextWriter
+//
+// Author:
+//   Kral Ferch <kral_ferch@hotmail.com>
+//
+// (C) 2002 Kral Ferch
+//
+
+using System;
+
+namespace System.Xml
+{
+       public abstract class XmlWriter
+       {
+               #region Fields
+
+               protected WriteState ws = WriteState.Start;
+               protected XmlNamespaceManager namespaceManager = new XmlNamespaceManager (new NameTable ());
+
+               #endregion
+
+               #region Constructors
+
+               protected XmlWriter () { }
+
+               #endregion
+
+               #region Properties
+
+               public abstract WriteState WriteState { get; }
+               
+               public abstract string XmlLang { get; }
+
+               public abstract XmlSpace XmlSpace { get; }
+
+               #endregion
+
+               #region Methods
+
+               public abstract void Close ();
+
+               public abstract void Flush ();
+
+               public abstract string LookupPrefix (string ns);
+
+               [MonoTODO("DTDs must be implemented to use 'defattr' parameter.")]
+               public virtual void WriteAttributes (XmlReader reader, bool defattr)
+               {
+                       if(reader == null)
+                               throw new ArgumentException("null XmlReader specified.", "reader");
+
+                       switch(reader.NodeType)
+                       {
+                               case XmlNodeType.XmlDeclaration:
+                                       // this method doesn't write "<?xml " and "?>", at least MS .NET Framework as yet.
+                                       XmlDeclaration decl = new XmlDeclaration("1.0", String.Empty, String.Empty, null);
+                                       decl.Value = reader.Value;
+                                       if(decl.Version != null && decl.Version != String.Empty) WriteAttributeString("version", decl.Version);
+                                       if(decl.Encoding != null && decl.Encoding != String.Empty) WriteAttributeString("encoding", decl.Encoding);
+                                       if(decl.Standalone != null && decl.Standalone != String.Empty) WriteAttributeString("standalone", decl.Standalone);
+                                       break;
+                               case XmlNodeType.Element:
+                                       while (reader.MoveToNextAttribute ()) 
+                                       {
+                                               WriteAttributeString(reader.Prefix, reader.LocalName, reader.NamespaceURI, reader.Value);
+                                       }
+                                       break;
+                               case XmlNodeType.Attribute:
+                                       do
+                                       {
+                                               WriteAttributeString(reader.Prefix, reader.LocalName, reader.NamespaceURI, reader.Value);
+                                       }
+                                       while (reader.MoveToNextAttribute ()) ;
+                                       break;
+                               default:
+                                       throw new XmlException("NodeType is not one of Element, Attribute, nor XmlDeclaration.");
+                       }
+               }
+
+               public void WriteAttributeString (string localName, string value)
+               {
+                       WriteAttributeString ("", localName, "", value);
+               }
+
+               public void WriteAttributeString (string localName, string ns, string value)
+               {
+                       WriteAttributeString ("", localName, ns, value);
+               }
+
+               public void WriteAttributeString (string prefix, string localName, string ns, string value)
+               {
+                       if ((prefix == "xmlns") || (localName == "xmlns"))
+                         {
+                               ns = value;
+                               
+                               if (prefix == "xmlns" && namespaceManager.HasNamespace (localName))
+                                       return;
+                               
+                               /* Users need to be able to re-declare the default namespace for subnodes
+                               else if (localName == "xmlns" && namespaceManager.HasNamespace (String.Empty))
+                                       return;
+                               */
+                         }
+                       
+                       WriteStartAttribute (prefix, localName, ns);
+                       WriteString (value);
+                       WriteEndAttribute ();
+
+                       if ((prefix == "xmlns") || (localName == "xmlns")) 
+                       {
+                               if (prefix == "xmlns")
+                                       namespaceManager.AddNamespace (localName, ns);
+                               else
+                                       namespaceManager.AddNamespace ("", ns);
+                       }
+                       
+               }
+
+               public abstract void WriteBase64 (byte[] buffer, int index, int count);
+
+               public abstract void WriteBinHex (byte[] buffer, int index, int count);
+
+               public abstract void WriteCData (string text);
+
+               public abstract void WriteCharEntity (char ch);
+
+               public abstract void WriteChars (char[] buffer, int index, int count);
+
+               public abstract void WriteComment (string text);
+
+               public abstract void WriteDocType (string name, string pubid, string sysid, string subset);
+
+               public void WriteElementString (string localName, string value)
+               {
+                       WriteStartElement(localName);
+                       WriteString(value);
+                       WriteEndElement();
+               }
+
+               public void WriteElementString (string localName, string ns, string value)
+               {
+                       WriteStartElement(localName, ns);
+                       WriteString(value);
+                       WriteEndElement();
+               }
+
+               public abstract void WriteEndAttribute ();
+
+               public abstract void WriteEndDocument ();
+
+               public abstract void WriteEndElement ();
+
+               public abstract void WriteEntityRef (string name);
+
+               public abstract void WriteFullEndElement ();
+
+               public abstract void WriteName (string name);
+
+               public abstract void WriteNmToken (string name);
+
+               [MonoTODO]
+               public virtual void WriteNode (XmlReader reader, bool defattr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public abstract void WriteProcessingInstruction (string name, string text);
+
+               public abstract void WriteQualifiedName (string localName, string ns);
+
+               public abstract void WriteRaw (string data);
+
+               public abstract void WriteRaw (char[] buffer, int index, int count);
+
+               public void WriteStartAttribute (string localName, string ns)
+               {
+                       WriteStartAttribute ("", localName, ns);
+               }
+
+               public abstract void WriteStartAttribute (string prefix, string localName, string ns);
+
+               public abstract void WriteStartDocument ();
+
+               public abstract void WriteStartDocument (bool standalone);
+
+               public void WriteStartElement (string localName)
+               {
+                       WriteStartElement (String.Empty, localName, String.Empty);
+               }
+
+               public void WriteStartElement (string localName, string ns)
+               {
+                       WriteStartElement (String.Empty, localName, ns);
+               }
+
+               public abstract void WriteStartElement (string prefix, string localName, string ns);
+
+               public abstract void WriteString (string text);
+
+               public abstract void WriteSurrogateCharEntity (char lowChar, char highChar);
+
+               public abstract void WriteWhitespace (string ws);
+
+               #endregion
+       }
+}