protected bool openStartElement = false;
protected bool openStartAttribute = false;
protected bool documentStarted = false;
- protected bool namespaces = true;
+ private 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 = ' ';
+ private Formatting formatting = Formatting.None;
+ private int indentation = 2;
+ private char indentChar = ' ';
protected string indentChars = " ";
- protected char quoteChar = '\"';
+ private char quoteChar = '\"';
protected int indentLevel = 0;
protected string indentFormatting;
protected Stream baseStream = null;
}
}
- [MonoTODO]
public char QuoteChar {
get { return quoteChar; }
set {
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;
+ attributeWrittenForElement = false;
}
}
w.Flush ();
}
- [MonoTODO]
public override string LookupPrefix (string ns)
{
- throw new NotImplementedException ();
+ string prefix = namespaceManager.LookupPrefix (ns);
+
+ if (prefix == String.Empty)
+ prefix = null;
+
+ return prefix;
}
private void UpdateIndentChars ()
indentChars += indentChar;
}
- [MonoTODO]
public override void WriteBase64 (byte[] buffer, int index, int count)
{
- throw new NotImplementedException ();
+ w.Write (Convert.ToBase64String (buffer, index, count));
}
[MonoTODO]
public override void WriteCData (string text)
{
- if (text.IndexOf("]]>") > 0)
- {
+ if (text.IndexOf("]]>") > 0)
throw new ArgumentException ();
- }
CheckState ();
CloseStartElement ();
w.Write("<![CDATA[{0}]]>", text);
}
- [MonoTODO]
public override void WriteCharEntity (char ch)
{
- throw new NotImplementedException ();
+ Int16 intCh = (Int16)ch;
+
+ // Make sure the character is not in the surrogate pair
+ // character range, 0xd800- 0xdfff
+ if ((intCh >= -10240) && (intCh <= -8193))
+ throw new ArgumentException ("Surrogate Pair is invalid.");
+
+ w.Write("&#x{0:X};", intCh);
}
[MonoTODO]
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.");
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 ());
- namespaceManager.PopScope();
}
+
+ namespaceManager.PopScope();
}
[MonoTODO]
throw new NotImplementedException ();
}
- [MonoTODO]
public override void WriteFullEndElement ()
{
- throw new NotImplementedException ();
+ WriteEndElementInternal (true);
}
[MonoTODO]
throw new NotImplementedException ();
}
- [MonoTODO("haven't tested namespaces on attributes code yet.")]
public override void WriteStartAttribute (string prefix, string localName, string ns)
{
if ((prefix == "xml") && (localName == "lang"))
CheckState ();
+ if (ws == WriteState.Content)
+ throw new InvalidOperationException ("Token StartAttribute in state " + WriteState + " would result in an invalid XML document.");
+
if (prefix == null)
prefix = String.Empty;
ns = String.Empty;
string formatPrefix = "";
+ string formatSpace = "";
if (ns != String.Empty)
{
formatPrefix = prefix + ":";
}
- w.Write (" {0}{1}={2}", formatPrefix, localName, quoteChar);
+ if (openStartElement || attributeWrittenForElement)
+ formatSpace = " ";
+
+ w.Write ("{0}{1}{2}={3}", formatSpace, formatPrefix, localName, quoteChar);
openAttribute = true;
+ attributeWrittenForElement = true;
ws = WriteState.Attribute;
}