{
#region Fields
- protected TextWriter w;
- protected bool nullEncoding = false;
- protected bool openWriter = true;
- protected bool openStartElement = false;
- protected bool openStartAttribute = false;
- protected bool documentStarted = false;
- protected bool namespaces = true;
- protected bool openAttribute = false;
- protected bool attributeWrittenForElement = false;
- protected Stack openElements = new Stack ();
- protected Formatting formatting = Formatting.None;
- protected int indentation = 2;
- protected char indentChar = ' ';
- protected string indentChars = " ";
- protected char quoteChar = '\"';
- protected int indentLevel = 0;
- protected string indentFormatting;
- protected Stream baseStream = null;
- protected string xmlLang = null;
- protected XmlSpace xmlSpace = XmlSpace.None;
- protected bool openXmlLang = false;
- protected bool openXmlSpace = false;
+ TextWriter w;
+ bool nullEncoding = false;
+ bool openWriter = true;
+ bool openStartElement = false;
+ bool openStartAttribute = false;
+ bool documentStarted = false;
+ bool namespaces = true;
+ bool openAttribute = false;
+ bool attributeWrittenForElement = false;
+ Stack openElements = new Stack ();
+ Formatting formatting = Formatting.None;
+ int indentation = 2;
+ char indentChar = ' ';
+ string indentChars = " ";
+ char quoteChar = '\"';
+ int indentLevel = 0;
+ string indentFormatting;
+ Stream baseStream = null;
+ string xmlLang = null;
+ XmlSpace xmlSpace = XmlSpace.None;
+ bool openXmlLang = false;
+ bool openXmlSpace = false;
#endregion
public XmlTextWriter (TextWriter w) : base ()
{
this.w = w;
+ nullEncoding = (w.Encoding == null);
try {
baseStream = ((StreamWriter)w).BaseStream;
set { formatting = value; }
}
- public bool IndentingOverriden
+ private bool IndentingOverriden
{
get {
if (openElements.Count == 0)
if (!openWriter) {
throw new InvalidOperationException ("The Writer is closed.");
}
-
if ((documentStarted == true) && (formatting == Formatting.Indented) && (!IndentingOverriden)) {
indentFormatting = "\r\n";
if (indentLevel > 0) {
public override void Close ()
{
- while (openElements.Count > 0) {
- WriteEndElement();
- }
+ CloseOpenAttributeAndElements ();
w.Close();
ws = WriteState.Closed;
openWriter = false;
}
+ private void CloseOpenAttributeAndElements ()
+ {
+ if (openAttribute)
+ WriteEndAttribute ();
+
+ while (openElements.Count > 0) {
+ WriteEndElement();
+ }
+ }
+
private void CloseStartElement ()
{
- if (openStartElement)
- {
+ if (openStartElement) {
w.Write(">");
ws = WriteState.Content;
openStartElement = false;
openAttribute = false;
}
- [MonoTODO]
public override void WriteEndDocument ()
{
- throw new NotImplementedException ();
+ if ((ws == WriteState.Start) || (ws == WriteState.Prolog))
+ throw new ArgumentException ("This document does not have a root element.");
+
+ CloseOpenAttributeAndElements ();
+
+ ws = WriteState.Start;
}
public override void WriteEndElement ()
+ {
+ WriteEndElementInternal (false);
+ }
+
+ private void WriteEndElementInternal (bool fullEndElement)
{
if (openElements.Count == 0)
throw new InvalidOperationException("There was no XML start tag open.");
indentLevel--;
-
CheckState ();
if (openStartElement) {
- w.Write (" />");
+ if (openAttribute)
+ WriteEndAttribute ();
+ if (fullEndElement)
+ w.Write ("></{0}>", ((XmlTextWriterOpenElement)openElements.Peek ()).Name);
+ else
+ w.Write (" />");
+
openElements.Pop ();
openStartElement = false;
- }
- else {
+ } else {
w.Write ("{0}</{1}>", indentFormatting, openElements.Pop ());
}
throw new NotImplementedException ();
}
- [MonoTODO]
public override void WriteFullEndElement ()
{
- throw new NotImplementedException ();
+ WriteEndElementInternal (true);
}
[MonoTODO]
[MonoTODO]
public override void WriteQualifiedName (string localName, string ns)
{
- throw new NotImplementedException ();
+ if (localName == null || localName == String.Empty)
+ throw new ArgumentException ();
+
+ CheckState ();
+ w.Write ("{0}:{1}", ns, localName);
}
- [MonoTODO]
public override void WriteRaw (string data)
{
- throw new NotImplementedException ();
+ WriteStringInternal (data, false);
}
[MonoTODO]
string encodingFormatting = "";
- if (!nullEncoding)
+ if (!nullEncoding)
encodingFormatting = String.Format (" encoding={0}{1}{0}", quoteChar, w.Encoding.HeaderName);
w.Write("<?xml version={0}1.0{0}{1}{2}?>", quoteChar, encodingFormatting, standaloneFormatting);
WriteStartElementInternal (prefix, localName, ns);
}
- protected override void WriteStartElementInternal (string prefix, string localName, string ns)
+ private void WriteStartElementInternal (string prefix, string localName, string ns)
{
if (prefix == null)
prefix = String.Empty;
- if (ns == null)
- ns = String.Empty;
-
if ((prefix != String.Empty) && ((ns == null) || (ns == String.Empty)))
throw new ArgumentException ("Cannot use a prefix with an empty namespace.");
CheckState ();
CloseStartElement ();
-
+
string formatXmlns = "";
string formatPrefix = "";
- if (ns != String.Empty)
+ if(ns != null)
{
- string existingPrefix = namespaceManager.LookupPrefix (ns);
+ if (ns != String.Empty)
+ {
+ string existingPrefix = namespaceManager.LookupPrefix (ns);
- if (prefix == String.Empty)
- prefix = existingPrefix;
+ if (prefix == String.Empty)
+ prefix = existingPrefix;
- if (prefix != existingPrefix)
- formatXmlns = String.Format (" xmlns:{0}={1}{2}{1}", prefix, quoteChar, ns);
- else if (existingPrefix == String.Empty)
- formatXmlns = String.Format (" xmlns={0}{1}{0}", quoteChar, ns);
- }
- else if ((prefix == String.Empty) && (namespaceManager.LookupNamespace(prefix) != String.Empty)) {
- formatXmlns = String.Format (" xmlns={0}{0}", quoteChar);
- }
+ if (prefix != existingPrefix)
+ formatXmlns = String.Format (" xmlns:{0}={1}{2}{1}", prefix, quoteChar, ns);
+ else if (existingPrefix == String.Empty)
+ formatXmlns = String.Format (" xmlns={0}{1}{0}", quoteChar, ns);
+ }
+ else if ((prefix == String.Empty) && (namespaceManager.LookupNamespace(prefix) != String.Empty)) {
+ formatXmlns = String.Format (" xmlns={0}{0}", quoteChar);
+ }
- if (prefix != String.Empty) {
- formatPrefix = prefix + ":";
+ if (prefix != String.Empty) {
+ formatPrefix = prefix + ":";
+ }
}
w.Write ("{0}<{1}{2}{3}", indentFormatting, formatPrefix, localName, formatXmlns);
+
openElements.Push (new XmlTextWriterOpenElement (formatPrefix + localName));
ws = WriteState.Element;
openStartElement = true;
namespaceManager.PushScope ();
- namespaceManager.AddNamespace (prefix, ns);
-
+ if(ns != null)
+ {
+ namespaceManager.AddNamespace (prefix, ns);
+ }
indentLevel++;
}
if (ws == WriteState.Prolog)
throw new InvalidOperationException ("Token content in state Prolog would result in an invalid XML document.");
+ WriteStringInternal (text, true);
+ }
+
+ private void WriteStringInternal (string text, bool entitize)
+ {
if (text == null)
text = String.Empty;
- if (text != String.Empty) {
+ if (text != String.Empty)
+ {
CheckState ();
- text = text.Replace ("&", "&");
- text = text.Replace ("<", "<");
- text = text.Replace (">", ">");
-
- if (openAttribute) {
- if (quoteChar == '"')
- text = text.Replace ("\"", """);
- else
- text = text.Replace ("'", "'");
+ if (entitize)
+ {
+ text = text.Replace ("&", "&");
+ text = text.Replace ("<", "<");
+ text = text.Replace (">", ">");
+
+ if (openAttribute)
+ {
+ if (quoteChar == '"')
+ text = text.Replace ("\"", """);
+ else
+ text = text.Replace ("'", "'");
+ }
}
if (!openAttribute)
+ {
+ IndentingOverriden = true;
CloseStartElement ();
-
+ }
if (!openXmlLang && !openXmlSpace)
w.Write (text);
- else {
+ else
+ {
if (openXmlLang)
xmlLang = text;
- else {
- switch (text) {
+ else
+ {
+ switch (text)
+ {
case "default":
xmlSpace = XmlSpace.Default;
break;
}
}
}
-
- IndentingOverriden = true;
}
[MonoTODO]