XmlTextWriter Can Produce Invalid XML

XmlTextWriter is .NET’s class for writing XML in a forward-only streaming manner. It is highly efficient and is the preferred way to generate XML in .NET in most circumstances. I find XmlTextWriter so useful I wrote a partial C++ implementation of it in Implenting IXmlWriter Series.

Unfortunately, XmlTextWriter isn’t quite as strict as it could be. It will let slip some invalid XML such as duplicate attributes, invalid Unicode characters in the range 0×0 to 0×20, and invalid element and attribute names. You can read about XmlTextWriter’s limitations in the article Customized XML Writer Creation.

If these limitations are an issue for you, I suggest following the instructions in “Customized XML Writer Creation” by writing a custom writer that extends the current XmlTextWriter and adds this functionality. This class can be used directly or passed to any functions which are designed to use XmlTextWriter.

How Return XML From ASPX in ASP.NET 1.1

I’m not sure if this is the “canonical” way to do it but here’s a description of how to write an ASP.NET 1.1 ASPX page which returns a XML document (e.g. when writing a home-brewed web service).

First, create a new Web Form (I will call it WebService.aspx). As we will be progamatically generating the XML in the HTTP response rather than sending the (processed) content of the ASPX file, delete everything from the ASPX file but the @Page directive, so that it looks something like:

<%@ Page language="c#" Codebehind="WebService.aspx.cs" AutoEventWireup="false"
    Inherits="WebService.WebService" %>

Next, open up the code-behind file WebService.aspx.cs. Within the Page_Load event handler, add the following code block:

private void Page_Load(object sender, System.EventArgs e)
{
    Response.ContentType = "text/xml";
    Response.ContentEncoding = Encoding.UTF8;

    using (TextWriter textWriter = new StreamWriter(Response.OutputStream,
                                                    Encoding.UTF8))
    {
        XmlTextWriter xmlWriter = new XmlTextWriter(textWriter);
        // Write XML using xmlWriter
    }
}

Notice the use of the HttpResponse.OutputStream property which allows us to write directly to the HTTP response body. Also notice that I explicitly set the Content-Type and Content-Encoding HTTP response headers, and that the encoding for both the response and the StreamWriter must match.

Once you have this block in place, you can use whatever technique you like to write XML to the xmlWriter object. For example, you can call XmlWriter methods by hand, pass xmlWriter as a parameter to XslTransform.Transform(), or use the XmlSerializer.

Implementing IXmlWriter Part 14: Supporting Writing To A Stream

This is part 14 of my Implementing IXmlWriter post series.

Today I will add support for writing the generated XML to a C++ stream to last time’s IXmlWriter.

Finally the reason why I’ve insisted on calling this series IXmlWriter (instead of StringXmlWriter) should become clear: I’ve been planning on supporting writing the generated XML to more than just a string. Specifically, today I will add the ability to write the XML to a C++ ostream object, a base class in the C++ iostream library which defines a writable stream.

To this end, I decided to write a pure-virtual interface called IXmlWriter which contains all the relevant methods for generating XML (e.g. WriteStartElement(), WriteComment(), etc.), and two concrete classes which implement this interface: StringXmlWriter (which writes to a string as before) and OstreamXmlWriter (which writes to a user-provided ostream instance). I moved the XML-generating logic from StringXmlWriter to OstreamXmlWriter and implemented StringXmlWriter in terms of OstreamXmlWriter and a stringstream instance.

The area where I probably spent the most time was deciding how to pass the ostream instance to OstreamXmlWriter and how OstreamXmlWriter would store it, if necessary. I came up with the following options:

  1. OstreamXmlWriter is passed a copy (or const reference) of the ostream instance in its constructor and stores a copy of the ostream instance as a member variable. Advantages: The typical idiom for most variables passed in the constructor. Disadvantages: For correctness, this requires a semantically correct copy constructor which is very difficult, and often impossible, to write for streams.
  2. OstreamXmlWriter is passed a reference (pointer) to the ostream instance in its constructor and stores a reference (pointer) to the ostream instance as a member variable. Advantages: Relatively simple. Disadvantages: OstreamXmlWriter becomes implicitly tied to the lifetime of the passed object, which means that a user must be sure that OstreamXmlWriter is destroyed before the ostream instance is. Code such as stringstream* ss = new stringstream(); XmlWriter w(ss); delete ss; w.WriteStartElement("blah"); may result in hard-to-find bugs.
  3. OstreamXmlWriter is passed a smart pointer object (e.g. std::auto_ptr, boost::shared_ptr) which points to the ostream instance in its constructor and stores a copy of the smart pointer object as a member variable. Advantages: Lifetime issues are handled correctly. Disadvantages: The ostream instance must be constructed on the heap and never the stack. We expose a dependency on a smart pointer implementation that users probably shouldn’t care about. (Alternative: Take a heap-constructed ostream* in the constructor, store as a smart pointer member variable. However, what if a stack-constructed ostream is passed?)</li
  4. OstreamXmlWriter takes a reference to the ostream instance as an extra parameter for every XML writing function. Advantages: Parallels operator<<, no lifetime issues. Disadvantages: Breaks IXmlWriter-as-interface.

Based on these observations, I decided to go with (2). Here’s the new test case:

std::stringstream ss;
OstreamXmlWriter xmlWriter(ss, StringXmlWriter::Formatting_Indented);

xmlWriter.WriteComment("comment");
xmlWriter.WriteStartElement("root");
  xmlWriter.WriteElementString("child", "value");
  xmlWriter.WriteComment("comment");
  xmlWriter.WriteStartElement("child");
    xmlWriter.WriteAttributeString("att", "value");
  xmlWriter.WriteEndElement();
  xmlWriter.WriteStartElement("child");
    xmlWriter.WriteStartElement("child");
      xmlWriter.WriteStartElement("child");
xmlWriter.WriteEndDocument();

// ss.str() should equal (whitespace is important):
// <!--comment-->
// <root>
//   <child>value</child>
//   <!--comment-->
//   <child att="value"/>
//   <child>
//     <child>
//       <child/>
//     </child>
//   </child>
// </root>

Because the source code is starting to get unwieldy (chorus: Too late!), I have linked to the source code files rather than insert the source code in-line with the post. Here’s the latest source code:

Unfortunately, the source code for this post has been lost.

Implementing IXmlWriter Part 13: Putting IXmlWriter Behind A Pimpl Firewall

This is part 13 of my Implementing IXmlWriter post series.

As the private members of IXmlWriter are getting too numerous and too likely to change by my judgment, today I will put last time’s IXmlWriter behind a compilation firewall (pimpl).

The idea behind the pimpl idiom is to hide as much of the class definition as possible in order to avoid requiring users of the class to recompile if the class’s private members are changed. It is accomplished by moving all private members (functions, data, etc.) into a separate class (called the implementation or pimpl class) hidden from the class definition, and replacing these members with an opaque pointer to a forward declaration of this class. It works because a C++ compiler does not need to have the full definition of a class visible in order to allocate space for a pointer to the class; every pointer is a constant, fixed size (often 4 bytes).

Guru of the Week (GotW) #24: Compilation Firewalls contains some discussion about what should belong within the pimpl class, which led me to choose to move all private type definitions, functions, and data into the pimpl class. GotW #24 also mentions some negative performance impact with respect to instantiation and the extra indirections required for accessing private members. The performance impact associated with instantiation can be mitigated by following the lessons of GotW #28: The Fast Pimpl Idiom, but I believe that instances of IXmlWriter will be created relatively infrequently and thus the extra effort required is not worth it. The impact based on the extra indirections we will just have to live with; I highly doubt a profile would identify the extra indirections as a performance bottleneck for IXmlWriter.

The steps required to modify IXmlWriter to use the pimpl idiom are:

  1. In the implementation file, create an internal class (a struct StringXmlWriter::StringXmlWriterImpl, in my case) and move all private members, including the implementations of the private functions, into this class. If applicable, write constructors for the pimpl class which perform the same initializations of private members that the constructors on your original class did.
  2. Within the class definition in the header file, add a forward declaration to this implementation class (e.g. struct StringXmlWriterImpl;) and a member which contains a pointer to this class (e.g. StringXmlWriterImpl* m_pimpl;). This should be the only private member left in the class. (Why not use std::auto_ptr<StringXmlWriterImpl>? See this code comments forum thread.)
  3. Now that you have a pointer variable as a member of the class, the default copy constructor and assignment operators are wrong. Either disable them (as I have done) or write correct implementations.
  4. Modify every constructor of your original class to create an instance of the pimpl class using new and the appropriate pimpl constructor.
  5. Add a destructor to your original class which deletes the instance of the pimpl class.
  6. In the implementation file, modify all references to private types and data to indirect through the pimpl instance. For example, m_xmlStr = ...; becomes m_pimpl->m_xmlStr = ...;.

Here’s the resulting header file:

// StringXmlWriter.h

class StringXmlWriter
{
public:
    enum Formatting
    {
        Formatting_Indented,
        Formatting_None
    };

    StringXmlWriter(Formatting formatting = Formatting_None,
                    int indentation = 2,
                    char indentChar = ' ');
    ~StringXmlWriter();

    std::string GetXmlString() const;
    void WriteAttributeString(const std::string& localName,
                              const std::string& text);
    void WriteAttributeString(const std::string& localName,
                              const std::string& ns,
                              const std::string& text);
    void WriteComment(const std::string& text);
    void WriteElementString(const std::string& localName,
                            const std::string& text);
    void WriteElementString(const std::string& localName,
                            const std::string& ns,
                            const std::string& text);
    void WriteEndAttribute();
    void WriteEndDocument();
    void WriteEndElement();
    void WriteStartAttribute(const std::string& localName);
    void WriteStartAttribute(const std::string& localName,
                             const std::string& ns);
    void WriteStartDocument();
    void WriteStartElement(const std::string& localName);
    void WriteStartElement(const std::string& localName,
                           const std::string& ns);
    void WriteString(const std::string& text);

private:
    struct StringXmlWriterImpl;
    StringXmlWriterImpl* m_pimpl;

private:
    // Disable copy construction and assignment
    StringXmlWriter(const StringXmlWriter&);
    StringXmlWriter& operator=(const StringXmlWriter&);
};

Here’s the resulting implementation file:

// StringXmlWriter.cpp

#include "StringXmlWriter.h"

#define ARRAYSIZE(x) ( sizeof(x) / sizeof(x[0]) )

struct CharTranslation
{
    char OriginalChar;
    const char* ReplacementString;
};

static const CharTranslation AttributeValueTranslations[] =
{
    { '"', "&quot;" },
    { '&', "&amp;" },
};

static const CharTranslation CharDataTranslations[] =
{
    { '&', "&amp;" },
    { '<', "&lt;" },
    { '>', "&gt;" },
};

struct OriginalCharEquals :
    public std::binary_function<CharTranslation, char, bool>
{
    bool operator() (const CharTranslation& translation, char ch) const
    {
        return (translation.OriginalChar == ch);
    }
};

static std::string TranslateString(const std::string& originalStr,
                                   const CharTranslation* translations,
                                   int numTranslations)
{
    // Actually one past end, needed for proper std::find_if semantics
    const CharTranslation* endTranslations = translations + numTranslations;

    std::string translatedStr;

    for (std::string::const_iterator stringIter = originalStr.begin();
         stringIter != originalStr.end();
         ++stringIter)
    {
        char ch = *stringIter;

        const CharTranslation* translation = std::find_if
            (
            translations,
            endTranslations,
            std::bind2nd(OriginalCharEquals(), ch)
            );
        if (translation != endTranslations)
        {
            translatedStr += translation->ReplacementString;
        }
        else
        {
            translatedStr += ch;
        }
    }

    return translatedStr;
}

struct StringXmlWriter::StringXmlWriterImpl
{
    StringXmlWriterImpl(Formatting formatting,
                        int indentation,
                        char indentChar) :
        m_writeState(WriteState_Start),
        m_formatting(formatting),
        m_contentWritten(false)
    {
        for (int i = 0; i < indentation; ++i)
        {
            m_indentStr += indentChar;
        }
    }

    // PRIVATE TYPES
    // =============
    enum WriteState
    {
        WriteState_Attribute, // An attribute value is being written
        WriteState_Content, // Element content is being written
        WriteState_Element, // An element start tag has been written (and is unclosed)
        WriteState_Prolog, // The prolog is being written
        WriteState_Start, // No Write() methods have been called
    };

    struct OpenElement
    {
        explicit OpenElement(const std::string& localName) :
            QName(localName)
        {
        }

        explicit OpenElement(const std::string& localName,
                             const std::string& prefix) :
            QName(prefix.empty() ? localName : prefix + “:” + localName)
        {
        }

        // The qualified name (namespace prefix-included) of the
        // opened element
        std::string QName;
        // All namespaces declared in this element (maps namespace
        // to namespace prefix)
        typedef std::map<std::string, std::string> Namespaces_t;
        Namespaces_t Namespaces;
    };

    // PRIVATE MEMBERS
    // ===============
    WriteState m_writeState;

    // Need to use a vector instead of a stack because we must be able
    // to iterate over each opened element in the stack to see if a
    // namespace has already been declared.
    typedef std::vector<OpenElement> OpenedElements_t;
    OpenedElements_t m_openedElements;

    // Needed to track whether content was written inside the XML element
    // so we know how to handle indentation.
    bool m_contentWritten;

    // The style of formatting we are using.
    StringXmlWriter::Formatting m_formatting;

    // The string to use for a single level of indentation.
    std::string m_indentStr;

    // The XML fragment that this class has generated so far.  There’s no
    // guarantee it will be valid unless WriteEndDocument() is called.
    std::string m_xmlStr;	

    // PRIVATE FUNCTIONS
    // =================
    std::string GetExistingNamespacePrefix(const std::string& ns)
    {
        for (OpenedElements_t::const_iterator openElemIter = m_openedElements.begin();
            openElemIter != m_openedElements.end();
            ++openElemIter)
        {
            OpenElement::Namespaces_t::const_iterator nsIter =
                openElemIter->Namespaces.find(ns);
            if (nsIter != openElemIter->Namespaces.end())
            {
                return nsIter->second;
            }
        }

        return “”;
    }

    std::string GetNextNamespacePrefix(const std::string& ns)
    {
        std::string nsPrefix;

        for (int i = 1; ; ++i)
        {
            std::stringstream ss;
            ss << “ns” << i;
            std::string nsPrefix = ss.str();
            if (!NamespacePrefixExists(nsPrefix))
                return nsPrefix;
        }
    }

    bool NamespacePrefixExists(const std::string& nsPrefix)
    {
        for (OpenedElements_t::const_iterator iter = m_openedElements.begin();
            iter != m_openedElements.end();
            ++iter)
        {
            for (OpenElement::Namespaces_t::const_iterator nsIter = iter->Namespaces.begin();
                nsIter != iter->Namespaces.end();
                ++nsIter)
            {
                if (nsIter->second == nsPrefix)
                    return true;
            }
        }

        return false;
    }

    void CloseOpenElement()
    {
        m_xmlStr += ‘>’;
        m_writeState = WriteState_Content;
    }

    void NewlineAndIndent()
    {
        assert(m_formatting == Formatting_Indented);

        if (!m_xmlStr.empty())
            m_xmlStr += ‘\n’;

        for (int i = 0; i != m_openedElements.size(); ++i)
        {
            m_xmlStr += m_indentStr;
        }
    }
};

StringXmlWriter::StringXmlWriter(Formatting formatting,
                                 int indentation,
                                 char indentChar) :
    m_pimpl(new StringXmlWriterImpl(formatting, indentation, indentChar))
{
}

StringXmlWriter::~StringXmlWriter()
{
    delete m_pimpl;
}

std::string StringXmlWriter::GetXmlString() const
{
    return m_pimpl->m_xmlStr;
}

void StringXmlWriter::WriteAttributeString(const std::string& localName,
                                           const std::string& text)
{
    WriteStartAttribute(localName);
    WriteString(text);
    WriteEndAttribute();
}

void StringXmlWriter::WriteAttributeString(const std::string& localName,
                                           const std::string& ns,
                                           const std::string& text)
{
    WriteStartAttribute(localName, ns);
    WriteString(text);
    WriteEndAttribute();
}

void StringXmlWriter::WriteComment(const std::string& text)
{
    switch (m_pimpl->m_writeState)
    {
    case StringXmlWriterImpl::WriteState_Element:
        // An element is currently open.  Close the element so we can open
        // a new one.
        m_pimpl->CloseOpenElement();
        // FALL THROUGH
    case StringXmlWriterImpl::WriteState_Content:
    case StringXmlWriterImpl::WriteState_Prolog:
    case StringXmlWriterImpl::WriteState_Start:
        if (m_pimpl->m_formatting == Formatting_Indented)
        {
            m_pimpl->NewlineAndIndent();
        }

        m_pimpl->m_xmlStr += “<!–”;
        m_pimpl->m_xmlStr += text;
        m_pimpl->m_xmlStr += “–>”;
        break;
    default:
        // It doesn’t make sense to allow writing comments when writing an
        // attribute value.
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteElementString(const std::string& localName,
                                         const std::string& text)
{
    WriteStartElement(localName);
    WriteString(text);
    WriteEndElement();
}

void StringXmlWriter::WriteElementString(const std::string& localName,
                                         const std::string& ns,
                                         const std::string& text)
{
    WriteStartElement(localName, ns);
    WriteString(text);
    WriteEndElement();
}

void StringXmlWriter::WriteEndAttribute()
{
    switch (m_pimpl->m_writeState)
    {
    case StringXmlWriterImpl::WriteState_Attribute:
        m_pimpl->m_xmlStr += ‘”‘;
        m_pimpl->m_writeState = StringXmlWriterImpl::WriteState_Element;
        break;
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteEndDocument()
{
    switch (m_pimpl->m_writeState)
    {
    case StringXmlWriterImpl::WriteState_Attribute:
        WriteEndAttribute();
        // FALL THROUGH
    case StringXmlWriterImpl::WriteState_Content:
    case StringXmlWriterImpl::WriteState_Element:
        while (!m_pimpl->m_openedElements.empty())
        {
            WriteEndElement();
        }
        break;
    case StringXmlWriterImpl::WriteState_Start:
    case StringXmlWriterImpl::WriteState_Prolog:
        // DO NOTHING
        break;
    default:
        // TODO: Generate error
        break;
    }

    m_pimpl->m_writeState = StringXmlWriterImpl::WriteState_Start;
}

void StringXmlWriter::WriteEndElement()
{
    switch (m_pimpl->m_writeState)
    {
    case StringXmlWriterImpl::WriteState_Content:
        {
            std::string qname = m_pimpl->m_openedElements.back().QName;
            m_pimpl->m_openedElements.pop_back();

            if (!m_pimpl->m_contentWritten &&
                m_pimpl->m_formatting == Formatting_Indented)
            {
                m_pimpl->NewlineAndIndent();
            }

            m_pimpl->m_xmlStr += “</”;
            m_pimpl->m_xmlStr += qname;
            m_pimpl->m_xmlStr += ‘>’;
            m_pimpl->m_writeState = StringXmlWriterImpl::WriteState_Content;
            break;
        }
    case StringXmlWriterImpl::WriteState_Element:
        {
            m_pimpl->m_xmlStr += “/>”;
            m_pimpl->m_openedElements.pop_back();
            m_pimpl->m_writeState = StringXmlWriterImpl::WriteState_Content;
            break;
        }
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteStartAttribute(const std::string& localName)
{
    WriteStartAttribute(localName, “”);
}

void StringXmlWriter::WriteStartAttribute(const std::string& localName,
                                          const std::string& ns)
{
    switch (m_pimpl->m_writeState)
    {
    case StringXmlWriterImpl::WriteState_Element:
        {
        std::string nsPrefix;
        bool mustDeclareNamespace = false;

        if (!ns.empty()) {
            nsPrefix = m_pimpl->GetExistingNamespacePrefix(ns);
            if (nsPrefix.empty()) {
                nsPrefix = m_pimpl->GetNextNamespacePrefix(ns);
                m_pimpl->m_openedElements.back().Namespaces[ns] = nsPrefix;
                mustDeclareNamespace = true;
            }
        }

        if (mustDeclareNamespace) {
            m_pimpl->m_xmlStr += ” xmlns:”;
            m_pimpl->m_xmlStr += nsPrefix;
            m_pimpl->m_xmlStr += “=”";
            m_pimpl->m_xmlStr += ns;
            m_pimpl->m_xmlStr += ‘”‘;
        }

        m_pimpl->m_xmlStr += ‘ ‘;
        if (!nsPrefix.empty()) {
            m_pimpl->m_xmlStr += nsPrefix;
            m_pimpl->m_xmlStr += ‘:’;
        }
        m_pimpl->m_xmlStr += localName;
        m_pimpl->m_xmlStr += “=”";
        m_pimpl->m_writeState = StringXmlWriterImpl::WriteState_Attribute;
        break;
        }
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteStartDocument()
{
    switch (m_pimpl->m_writeState)
    {
    case StringXmlWriterImpl::WriteState_Start:
        m_pimpl->m_xmlStr += “<?xml version=”1.0”?>”;
        m_pimpl->m_writeState = StringXmlWriterImpl::WriteState_Prolog;
        break;
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteStartElement(const std::string& localName)
{
    WriteStartElement(localName, “”);
}

void StringXmlWriter::WriteStartElement(const std::string& localName,
                                        const std::string& ns)
{
    switch (m_pimpl->m_writeState)
    {
    case StringXmlWriterImpl::WriteState_Element:
        // An element is currently open.  Close the element so we can open
        // a new one.
        m_pimpl->CloseOpenElement();
        // FALL THROUGH
    case StringXmlWriterImpl::WriteState_Content:
    case StringXmlWriterImpl::WriteState_Prolog:
    case StringXmlWriterImpl::WriteState_Start:
        {
        if (m_pimpl->m_formatting == Formatting_Indented)
        {
            m_pimpl->NewlineAndIndent();
        }

        std::string nsPrefix;
        bool mustDeclareNamespace = false;

        if (!ns.empty()) {
            nsPrefix = m_pimpl->GetExistingNamespacePrefix(ns);
            if (nsPrefix.empty()) {
                nsPrefix = m_pimpl->GetNextNamespacePrefix(ns);
                mustDeclareNamespace = true;
            }
        }

        StringXmlWriterImpl::OpenElement openElement(localName, nsPrefix);
        if (mustDeclareNamespace) {
            openElement.Namespaces[ns] = nsPrefix;
        }

        m_pimpl->m_openedElements.push_back(openElement);

        m_pimpl->m_xmlStr += ‘<’;
        if (!nsPrefix.empty()) {
            m_pimpl->m_xmlStr += nsPrefix;
            m_pimpl->m_xmlStr += ‘:’;
        }
        m_pimpl->m_xmlStr += localName;
        if (mustDeclareNamespace) {
            m_pimpl->m_xmlStr += ” xmlns:”;
            m_pimpl->m_xmlStr += nsPrefix;
            m_pimpl->m_xmlStr += “=”";
            m_pimpl->m_xmlStr += ns;
            m_pimpl->m_xmlStr += ‘”‘;
        }

        m_pimpl->m_writeState = StringXmlWriterImpl::WriteState_Element;
        m_pimpl->m_contentWritten = false;
        break;
        }
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteString(const std::string& text)
{
    switch (m_pimpl->m_writeState)
    {
    case StringXmlWriterImpl::WriteState_Attribute:
        m_pimpl->m_xmlStr += TranslateString
            (
            text,
            AttributeValueTranslations,
            ARRAYSIZE(AttributeValueTranslations)
            );
        break;
    case StringXmlWriterImpl::WriteState_Element:
        // An element is currently open.  Close the element so we can start
        // writing the element content.
        m_pimpl->CloseOpenElement();
        // FALL THROUGH
    case StringXmlWriterImpl::WriteState_Content:
        m_pimpl->m_xmlStr += TranslateString
            (
            text,
            CharDataTranslations,
            ARRAYSIZE(CharDataTranslations)
            );
        m_pimpl->m_contentWritten = true;
        break;
    default:
        // TODO: Generate error
        break;
    }
}

Implementing IXmlWriter Part 12: Supporting Pretty-Printing

This is part 12 of my Implementing IXmlWriter post series.

Today I will add support for pretty-printing to last time’s IXmlWriter.

Pretty-printing is the addition of whitespace at predetermined locations to make the resulting XML easier to read than when it is all on one line. In the .NET Framework’s System.Xml.XmlTextWriter class, it is supported by the properties Formatting, which allows you to enable or disable pretty-printing; Indentation, which allows you to specify how many whitespace characters indentation should use; and IndentChar, which allows you to specify the whitespace character to use for indentation. For IXmlWriter, I instead chose to expose these features exclusively through the constructor. This frees me from the worry of a user trying to change these properties after IXmlWriter has already begun writing XML, which could produce awkward results. Default parameters are used to make the use of pretty-printing optional and straightforward.

I found the easiest way to implement this feature was to construct a complicated test case and then make the code changes accordingly. However, this method is a little disconcerting due to the lack of building a “gut feeling” of correctness. After finishing the code changes, I did a quick review and convinced myself that the logic seems right, but I doubt I will ever grow the same level of confidence that I would have had I built a mental model of the algorithm first.

In addition to pretty-printing, I also rewrote GetNextNamespacePrefix() to find the first available namespace prefix using brute force by trying ns1:, ns2:, … in order rather than using the relationship between namespace prefixes and the number of namespaces already declared. This should make it easier to add user-defined namespace prefixes in the future, as this feature would break the aforementioned relationship.

Here’s the test case:

StringXmlWriter xmlWriter(StringXmlWriter::Formatting_Indented);

xmlWriter.WriteComment("comment");
xmlWriter.WriteStartElement("root");
  xmlWriter.WriteElementString("child", "value");
  xmlWriter.WriteComment("comment");
  xmlWriter.WriteStartElement("child");
    xmlWriter.WriteAttributeString("att", "value");
  xmlWriter.WriteEndElement();
  xmlWriter.WriteStartElement("child");
    xmlWriter.WriteStartElement("child");
      xmlWriter.WriteStartElement("child");
xmlWriter.WriteEndDocument();

std::string strXML = xmlWriter.GetXmlString();

// strXML should equal (whitespace is important):
// <!--comment-->
// <root>
//   <child>value</child>
//   <!--comment-->
//   <child att="value"/>
//   <child>
//     <child>
//       <child/>
//     </child>
//   </child>
// </root>

Here’s the new header file:

// StringXmlWriter.h

class StringXmlWriter
{
public:
    enum Formatting
    {
        Formatting_Indented,
        Formatting_None
    };

    StringXmlWriter(Formatting formatting = Formatting_None,
                    int indentation = 2,
                    char indentChar = ' ');

    std::string GetXmlString() const;
    void WriteAttributeString(const std::string& localName,
                              const std::string& text);
    void WriteAttributeString(const std::string& localName,
                              const std::string& ns,
                              const std::string& text);
    void WriteComment(const std::string& text);
    void WriteElementString(const std::string& localName,
                            const std::string& text);
    void WriteElementString(const std::string& localName,
                            const std::string& ns,
                            const std::string& text);
    void WriteEndAttribute();
    void WriteEndDocument();
    void WriteEndElement();
    void WriteStartAttribute(const std::string& localName);
    void WriteStartAttribute(const std::string& localName,
                             const std::string& ns);
    void WriteStartDocument();
    void WriteStartElement(const std::string& localName);
    void WriteStartElement(const std::string& localName,
                           const std::string& ns);
    void WriteString(const std::string& text);

private:
    // PRIVATE TYPES
    // =============
    enum WriteState
    {
        WriteState_Attribute, // An attribute value is being written
        WriteState_Content, // Element content is being written
        WriteState_Element, // An element start tag has been written (and is unclosed)
        WriteState_Prolog, // The prolog is being written
        WriteState_Start, // No Write() methods have been called
    };

    struct OpenElement
    {
        explicit OpenElement(const std::string& localName) :
            QName(localName)
        {
        }

        explicit OpenElement(const std::string& localName,
                             const std::string& prefix) :
            QName(prefix.empty() ? localName : prefix + ":" + localName)
        {
        }

        // The qualified name (namespace prefix-included) of the
        // opened element
        std::string QName;
        // All namespaces declared in this element (maps namespace
        // to namespace prefix)
        typedef std::map<std::string, std::string> Namespaces_t;
        Namespaces_t Namespaces;
    };

    // PRIVATE FUNCTIONS
    // =================
    std::string GetExistingNamespacePrefix(const std::string& ns);
    std::string GetNextNamespacePrefix(const std::string& ns);
    bool NamespacePrefixExists(const std::string& nsPrefix);
    void CloseOpenElement();
    void NewlineAndIndent();

    // PRIVATE MEMBERS
    // ===============
    WriteState m_writeState;

    // Need to use a vector instead of a stack because we must be able
    // to iterate over each opened element in the stack to see if a
    // namespace has already been declared.
    typedef std::vector<OpenElement> OpenedElements_t;
    OpenedElements_t m_openedElements;

    // Needed to track whether content was written inside the XML element
    // so we know how to handle indentation.
    bool m_contentWritten;

    // The style of formatting we are using.
    Formatting m_formatting;

    // The string to use for a single level of indentation.
    std::string m_indentStr;

    // The XML fragment that this class has generated so far.  There’s no
    // guarantee it will be valid unless WriteEndDocument() is called.
    std::string m_xmlStr;	

private:
    // Disable copy construction and assignment
    StringXmlWriter(const StringXmlWriter&);
    StringXmlWriter& operator=(const StringXmlWriter&);
};

Here’s the new implementation file:

// StringXmlWriter.cpp

#include "StringXmlWriter.h"

#define ARRAYSIZE(x) ( sizeof(x) / sizeof(x[0]) )

struct CharTranslation
{
    char OriginalChar;
    const char* ReplacementString;
};

static const CharTranslation AttributeValueTranslations[] =
{
    { '"', "&quot;" },
    { '&', "&amp;" },
};

static const CharTranslation CharDataTranslations[] =
{
    { '&', "&amp;" },
    { '<', "&lt;" },
    { '>', "&gt;" },
};

struct OriginalCharEquals :
    public std::binary_function<CharTranslation, char, bool>
{
    bool operator() (const CharTranslation& translation, char ch) const
    {
        return (translation.OriginalChar == ch);
    }
};

static std::string TranslateString(const std::string& originalStr,
                                   const CharTranslation* translations,
                                   int numTranslations)
{
    // Actually one past end, needed for proper std::find_if semantics
    const CharTranslation* endTranslations = translations + numTranslations;

    std::string translatedStr;

    for (std::string::const_iterator stringIter = originalStr.begin();
         stringIter != originalStr.end();
         ++stringIter)
    {
        char ch = *stringIter;

        const CharTranslation* translation = std::find_if
            (
            translations,
            endTranslations,
            std::bind2nd(OriginalCharEquals(), ch)
            );
        if (translation != endTranslations)
        {
            translatedStr += translation->ReplacementString;
        }
        else
        {
            translatedStr += ch;
        }
    }

    return translatedStr;
}

StringXmlWriter::StringXmlWriter(Formatting formatting,
                                 int indentation,
                                 char indentChar) :
    m_writeState(WriteState_Start),
    m_formatting(formatting),
    m_contentWritten(false)
{
    for (int i = 0; i < indentation; ++i)
    {
        m_indentStr += indentChar;
    }
}

std::string StringXmlWriter::GetXmlString() const
{
    return m_xmlStr;
}

void StringXmlWriter::WriteAttributeString(const std::string& localName,
                                           const std::string& text)
{
    WriteStartAttribute(localName);
    WriteString(text);
    WriteEndAttribute();
}

void StringXmlWriter::WriteAttributeString(const std::string& localName,
                                           const std::string& ns,
                                           const std::string& text)
{
    WriteStartAttribute(localName, ns);
    WriteString(text);
    WriteEndAttribute();
}

void StringXmlWriter::WriteComment(const std::string& text)
{
    switch (m_writeState)
    {
    case WriteState_Element:
        // An element is currently open.  Close the element so we can open
        // a new one.
        CloseOpenElement();
        // FALL THROUGH
    case WriteState_Content:
    case WriteState_Prolog:
    case WriteState_Start:
        if (m_formatting == Formatting_Indented)
        {
            NewlineAndIndent();
        }

        m_xmlStr += "<!–-";
        m_xmlStr += text;
        m_xmlStr += "–->";
        break;
    default:
        // It doesn’t make sense to allow writing comments when writing an
        // attribute value.
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteElementString(const std::string& localName,
                                         const std::string& text)
{
    WriteStartElement(localName);
    WriteString(text);
    WriteEndElement();
}

void StringXmlWriter::WriteElementString(const std::string& localName,
                                         const std::string& ns,
                                         const std::string& text)
{
    WriteStartElement(localName, ns);
    WriteString(text);
    WriteEndElement();
}

void StringXmlWriter::WriteEndAttribute()
{
    switch (m_writeState)
    {
    case WriteState_Attribute:
        m_xmlStr += '"';
        m_writeState = WriteState_Element;
        break;
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteEndDocument()
{
    switch (m_writeState)
    {
    case WriteState_Attribute:
        WriteEndAttribute();
        // FALL THROUGH
    case WriteState_Content:
    case WriteState_Element:
        while (!m_openedElements.empty())
        {
            WriteEndElement();
        }
        break;
    case WriteState_Start:
    case WriteState_Prolog:
        // DO NOTHING
        break;
    default:
        // TODO: Generate error
        break;
    }

    m_writeState = WriteState_Start;
}

void StringXmlWriter::WriteEndElement()
{
    switch (m_writeState)
    {
    case WriteState_Content:
        {
            std::string qname = m_openedElements.back().QName;
            m_openedElements.pop_back();

            if (!m_contentWritten &&
                m_formatting == Formatting_Indented)
            {
                NewlineAndIndent();
            }

            m_xmlStr += "</";
            m_xmlStr += qname;
            m_xmlStr += '>';
            m_writeState = WriteState_Content;
            break;
        }
    case WriteState_Element:
        {
            m_xmlStr += "/>";
            m_openedElements.pop_back();
            m_writeState = WriteState_Content;
            break;
        }
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteStartAttribute(const std::string& localName)
{
    WriteStartAttribute(localName, "");
}

void StringXmlWriter::WriteStartAttribute(const std::string& localName,
                                          const std::string& ns)
{
    switch (m_writeState)
    {
    case WriteState_Element:
        {
        std::string nsPrefix;
        bool mustDeclareNamespace = false;

        if (!ns.empty()) {
            nsPrefix = GetExistingNamespacePrefix(ns);
            if (nsPrefix.empty()) {
                nsPrefix = GetNextNamespacePrefix(ns);
                m_openedElements.back().Namespaces[ns] = nsPrefix;
                mustDeclareNamespace = true;
            }
        }

        if (mustDeclareNamespace) {
            m_xmlStr += " xmlns:";
            m_xmlStr += nsPrefix;
            m_xmlStr += "=\"";
            m_xmlStr += ns;
            m_xmlStr += '"';
        }

        m_xmlStr += ' ';
        if (!nsPrefix.empty()) {
            m_xmlStr += nsPrefix;
            m_xmlStr += ':';
        }
        m_xmlStr += localName;
        m_xmlStr += "=\"";
        m_writeState = WriteState_Attribute;
        break;
        }
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteStartDocument()
{
    switch (m_writeState)
    {
    case WriteState_Start:
        m_xmlStr += "<?xml version=\"1.0\"?>";
        m_writeState = WriteState_Prolog;
        break;
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteStartElement(const std::string& localName)
{
    WriteStartElement(localName, "");
}

void StringXmlWriter::WriteStartElement(const std::string& localName,
                                        const std::string& ns)
{
    switch (m_writeState)
    {
    case WriteState_Element:
        // An element is currently open.  Close the element so we can open
        // a new one.
        CloseOpenElement();
        // FALL THROUGH
    case WriteState_Content:
    case WriteState_Prolog:
    case WriteState_Start:
        {
        if (m_formatting == Formatting_Indented)
        {
            NewlineAndIndent();
        }

        std::string nsPrefix;
        bool mustDeclareNamespace = false;

        if (!ns.empty()) {
            nsPrefix = GetExistingNamespacePrefix(ns);
            if (nsPrefix.empty()) {
                nsPrefix = GetNextNamespacePrefix(ns);
                mustDeclareNamespace = true;
            }
        }

        OpenElement openElement(localName, nsPrefix);
        if (mustDeclareNamespace) {
            openElement.Namespaces[ns] = nsPrefix;
        }

        m_openedElements.push_back(openElement);

        m_xmlStr += '<';
        if (!nsPrefix.empty()) {
            m_xmlStr += nsPrefix;
            m_xmlStr += ':';
        }
        m_xmlStr += localName;
        if (mustDeclareNamespace) {
            m_xmlStr += " xmlns:";
            m_xmlStr += nsPrefix;
            m_xmlStr += "=\"";
            m_xmlStr += ns;
            m_xmlStr += '"';
        }

        m_writeState = WriteState_Element;
        m_contentWritten = false;
        break;
        }
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteString(const std::string& text)
{
    switch (m_writeState)
    {
    case WriteState_Attribute:
        m_xmlStr += TranslateString
            (
            text,
            AttributeValueTranslations,
            ARRAYSIZE(AttributeValueTranslations)
            );
        break;
    case WriteState_Element:
        // An element is currently open.  Close the element so we can start
        // writing the element content.
        CloseOpenElement();
        // FALL THROUGH
    case WriteState_Content:
        m_xmlStr += TranslateString
            (
            text,
            CharDataTranslations,
            ARRAYSIZE(CharDataTranslations)
            );
        m_contentWritten = true;
        break;
    default:
        // TODO: Generate error
        break;
    }
}

std::string StringXmlWriter::GetExistingNamespacePrefix(const std::string& ns)
{
    for (OpenedElements_t::const_iterator openElemIter = m_openedElements.begin();
         openElemIter != m_openedElements.end();
         ++openElemIter)
    {
        OpenElement::Namespaces_t::const_iterator nsIter =
            openElemIter->Namespaces.find(ns);
        if (nsIter != openElemIter->Namespaces.end())
        {
            return nsIter->second;
        }
    }

    return "";
}

std::string StringXmlWriter::GetNextNamespacePrefix(const std::string& ns)
{
    std::string nsPrefix;

    for (int i = 1; ; ++i)
    {
        std::stringstream ss;
        ss << "ns" << i;
        std::string nsPrefix = ss.str();
        if (!NamespacePrefixExists(nsPrefix))
            return nsPrefix;
    }
}

bool StringXmlWriter::NamespacePrefixExists(const std::string& nsPrefix)
{
    for (OpenedElements_t::const_iterator iter = m_openedElements.begin();
         iter != m_openedElements.end();
         ++iter)
    {
        for (OpenElement::Namespaces_t::const_iterator nsIter = iter->Namespaces.begin();
             nsIter != iter->Namespaces.end();
             ++nsIter)
        {
            if (nsIter->second == nsPrefix)
                return true;
        }
    }

    return false;
}

void StringXmlWriter::CloseOpenElement()
{
    m_xmlStr += '>';
    m_writeState = WriteState_Content;
}

void StringXmlWriter::NewlineAndIndent()
{
    assert(m_formatting == Formatting_Indented);

    if (!m_xmlStr.empty())
        m_xmlStr += '\n';

    for (int i = 0; i != m_openedElements.size(); ++i)
    {
        m_xmlStr += m_indentStr;
    }
}

Implementing IXmlWriter Part 11: Supporting Namespaces

This is part 11 of my Implementing IXmlWriter post series.

Today I will add support for namespaces to last time’s IXmlWriter.

Namespaces are defined by the W3C recommendation Namespaces in XML. Using namespaces requires two parts: a namespace declaration, which associates a prefix with a namespace name (a user-defined, ideally globally-unique string which defines the namespace, often in the form of a URL); and the assignment of XML elements and attributes to this namespace by using the aforementioned prefix.

Here’s an example of a XML document that uses namespaces:

<?xml version="1.0"?>
<bk:book xmlns:bk='urn:loc.gov:books'>
</bk:book>

The xmlns:bk='urn:loc.gov:books' is the namespace declaration, and it assigns the prefix bk: to the namespace name urn:loc.gov:books. The book element is declared as a member of the urn:loc.gov:books namespace (and not the default, empty namespace) by the usage of this prefix.

There are a few subtleties to the use of namespaces. A common one is that while you can declare a default namespace into which unprefixed elements are automatically assigned (through the use of xmlns="..."), unprefixed attributes are not automatically assigned into this namespace. In other words, the following XML fragments are not equivalent because the title attributes are in different namespaces:

<-- The title attribute is in the
    urn:loc.gov:books namespace -->
<bk:book bk:title='Cheaper by the Dozen'
         xmlns:bk='urn:loc.gov:books'>
</bk:book>
<-- The title attribute is in the
    empty namespace -->
<book title='Cheaper by the Dozen'
      xmlns='urn:loc.gov:books'>
</book>

An important point to note (and one which we will take advantage of shortly) is that the value of the prefix is meaningless — it is simply a shorthand way of denoting the membership of a XML element or attribute in a namespace. In other words, if I replaced bk: with foobar: everywhere in the above code, the resulting document would be equivalent to the original. Therefore, for now, I choose to not allow users of IXmlWriter to control the namespace prefixes — I will assign them automatically as ns1:, ns2:, …

In order to keep track of what namespaces have already been declared, I will store them (in addition to the namespace-qualified name QName) in m_openedElements. Because I need the ability to search all declared namespaces for all opened elements, I will change m_openedElements from a std::stack to a std::vector. Furthermore, because namespaces can only be declared in an essentially stack-like manner, I can assign namespace prefixes by simply counting up the total number of namespace prefixes already declared and adding one. I will not support default namespaces at this time.

Here are the five test cases I developed for this functionality:

// Test simple namespaces
StringXmlWriter xmlWriter;

xmlWriter.WriteStartElement("root", "namespace1");
xmlWriter.WriteEndElement();

std::string strXML = xmlWriter.GetXmlString();
// strXML should be <ns1:root xmlns:ns1="namespace1"/>
// Test attribute namespacing
StringXmlWriter xmlWriter;

xmlWriter.WriteStartElement("root", "namespace1");
  xmlWriter.WriteAttributeString("att", "namespace1", "value");
xmlWriter.WriteEndElement();

std::string strXML = xmlWriter.GetXmlString();
// strXML should be <ns1:root xmlns:ns1="namespace1" ns1:att="value"/>
// Test child namespace declarations
StringXmlWriter xmlWriter;

xmlWriter.WriteStartElement("root", "namespace1");
  xmlWriter.WriteElementString("child", "namespace2", "value");
xmlWriter.WriteEndElement();

std::string strXML = xmlWriter.GetXmlString();
// strXML should be (on one line):
// <ns1:root xmlns:ns1="namespace1">
//   <ns2:child xmlns:ns2="namespace2">value</ns2:child>
// </ns1:root>
// Complicated namespace test
StringXmlWriter xmlWriter;

xmlWriter.WriteStartElement("root", "namespace1");
  xmlWriter.WriteStartElement("child", "namespace2");
    xmlWriter.WriteAttributeString("att1", "namespace1", "value1");
    xmlWriter.WriteAttributeString("att2", "namespace2", "value2");
    xmlWriter.WriteAttributeString("att3", "namespace3", "value3");
    xmlWriter.WriteAttributeString("att4", "value4");
    xmlWriter.WriteStartElement("child2", "namespace3");
      xmlWriter.WriteString("value");
    xmlWriter.WriteEndElement();
  xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();

std::string strXML = xmlWriter.GetXmlString();
// strXML should be (on one line):
// <ns1:root xmlns:ns1="namespace1">
//   <ns2:child xmlns:ns2="namespace2" ns1:att1="value1" ns2:att2="value2"
//              xmlns:ns3="namespace3" ns3:att3="value3" att4="value4">
//     <ns3:child2>value</ns3:child2>
//   </ns2:child>
// </ns1:root>
// Test "sibling" namespace declarations
StringXmlWriter xmlWriter;

xmlWriter.WriteStartElement("root");
  xmlWriter.WriteStartElement("child1", "namespace1");
  xmlWriter.WriteEndElement();
  xmlWriter.WriteStartElement("child2", "namespace1");
  xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();

std::string strXML = xmlWriter.GetXmlString();
// strXML should be (on one line):
// <root>
//   <ns1:child1 xmlns:ns1="namespace1"/>
//   <ns1:child2 xmlns:ns1="namespace1"/>
// </root>

Here’s the new header file:

// StringXmlWriter.h

class StringXmlWriter
{
private:
    enum WriteState
    {
        WriteState_Attribute, // An attribute value is being written
        WriteState_Content, // Element content is being written
        WriteState_Element, // An element start tag has been written (and is unclosed)
        WriteState_Prolog, // The prolog is being written
        WriteState_Start, // No Write() methods have been called
    };

    struct OpenElement
    {
        explicit OpenElement(const std::string& localName) :
            QName(localName)
        {
        }

        explicit OpenElement(const std::string& localName,
                             const std::string& prefix) :
            QName(prefix.empty() ? localName : prefix + “:” + localName)
        {
        }

        // The qualified name (namespace prefix-included) of the
        // opened element
        std::string QName;
        // All namespaces declared in this element (maps namespace
        // to namespace prefix)
        typedef std::map<std::string, std::string> Namespaces_t;
        Namespaces_t Namespaces;
    };

    WriteState m_writeState;

    // Need to use a vector instead of a stack because we must be able
    // to iterate over each opened element in the stack to see if a
    // namespace has already been declared.
    typedef std::vector<OpenElement> OpenedElements_t;
    OpenedElements_t m_openedElements;
    std::string m_xmlStr;

public:
    StringXmlWriter();

    std::string GetXmlString() const;
    void WriteAttributeString(const std::string& localName,
                              const std::string& text);
    void WriteAttributeString(const std::string& localName,
                              const std::string& ns,
                              const std::string& text);
    void WriteComment(const std::string& text);
    void WriteElementString(const std::string& localName,
                            const std::string& text);
    void WriteElementString(const std::string& localName,
                            const std::string& ns,
                            const std::string& text);
    void WriteEndAttribute();
    void WriteEndDocument();
    void WriteEndElement();
    void WriteStartAttribute(const std::string& localName);
    void WriteStartAttribute(const std::string& localName,
                             const std::string& ns);
    void WriteStartDocument();
    void WriteStartElement(const std::string& localName);
    void WriteStartElement(const std::string& localName,
                           const std::string& ns);
    void WriteString(const std::string& text);

private:
    // Disable copy construction and assignment
    StringXmlWriter(const StringXmlWriter&);
    StringXmlWriter& operator=(const StringXmlWriter&);

    std::string GetExistingNamespacePrefix(const std::string& ns);
    std::string GetNextNamespacePrefix(const std::string& ns);
};

Here’s the new implementation file:

// StringXmlWriter.cpp

#include "StringXmlWriter.h"

#define ARRAYSIZE(x) ( sizeof(x) / sizeof(x[0]) )

struct CharTranslation
{
    char OriginalChar;
    const char* ReplacementString;
};

static const CharTranslation AttributeValueTranslations[] =
{
    { '"', "&quot;" },
    { '&', "&amp;" },
};

static const CharTranslation CharDataTranslations[] =
{
    { '&', "&amp;" },
    { '<', "&lt;" },
    { '>', "&gt;" },
};

struct OriginalCharEquals :
    public std::binary_function<CharTranslation, char, bool>
{
    bool operator() (const CharTranslation& translation, char ch) const
    {
        return (translation.OriginalChar == ch);
    }
};

static std::string TranslateString(const std::string& originalStr,
                                   const CharTranslation* translations,
                                   int numTranslations)
{
    // Actually one past end, needed for proper std::find_if semantics
    const CharTranslation* endTranslations = translations + numTranslations;

    std::string translatedStr;

    for (std::string::const_iterator stringIter = originalStr.begin();
         stringIter != originalStr.end();
         ++stringIter)
    {
        char ch = *stringIter;

        const CharTranslation* translation = std::find_if
            (
            translations,
            endTranslations,
            std::bind2nd(OriginalCharEquals(), ch)
            );
        if (translation != endTranslations)
        {
            translatedStr += translation->ReplacementString;
        }
        else
        {
            translatedStr += ch;
        }
    }

    return translatedStr;
}

StringXmlWriter::StringXmlWriter() : m_writeState(WriteState_Start)
{
}

std::string StringXmlWriter::GetXmlString() const
{
    return m_xmlStr;
}

void StringXmlWriter::WriteAttributeString(const std::string& localName,
                                           const std::string& text)
{
    WriteStartAttribute(localName);
    WriteString(text);
    WriteEndAttribute();
}

void StringXmlWriter::WriteAttributeString(const std::string& localName,
                                           const std::string& ns,
                                           const std::string& text)
{
    WriteStartAttribute(localName, ns);
    WriteString(text);
    WriteEndAttribute();
}

void StringXmlWriter::WriteComment(const std::string& text)
{
    switch (m_writeState)
    {
    case WriteState_Element:
        // An element is currently open.  Close the element so we can open
        // a new one.
        m_xmlStr += ‘>’;
        m_writeState = WriteState_Content;
        // FALL THROUGH
    case WriteState_Content:
    case WriteState_Prolog:
    case WriteState_Start:
        m_xmlStr += “<!–”;
        m_xmlStr += text;
        m_xmlStr += “–>”;
        break;
    default:
        // It doesn’t make sense to allow writing comments when writing an
        // attribute value.
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteElementString(const std::string& localName,
                                         const std::string& text)
{
    WriteStartElement(localName);
    WriteString(text);
    WriteEndElement();
}

void StringXmlWriter::WriteElementString(const std::string& localName,
                                         const std::string& ns,
                                         const std::string& text)
{
    WriteStartElement(localName, ns);
    WriteString(text);
    WriteEndElement();
}

void StringXmlWriter::WriteEndAttribute()
{
    switch (m_writeState)
    {
    case WriteState_Attribute:
        m_xmlStr += ‘”‘;
        m_writeState = WriteState_Element;
        break;
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteEndDocument()
{
    switch (m_writeState)
    {
    case WriteState_Attribute:
        WriteEndAttribute();
        // FALL THROUGH
    case WriteState_Content:
    case WriteState_Element:
        while (!m_openedElements.empty())
        {
            WriteEndElement();
        }
        break;
    case WriteState_Start:
    case WriteState_Prolog:
        // DO NOTHING
        break;
    default:
        // TODO: Generate error
        break;
    }

    m_writeState = WriteState_Start;
}

void StringXmlWriter::WriteEndElement()
{
    switch (m_writeState)
    {
    case WriteState_Content:
        {
            m_xmlStr += “</”;
            m_xmlStr += m_openedElements.back().QName;
            m_xmlStr += ‘>’;
            m_openedElements.pop_back();
            m_writeState = WriteState_Content;
            break;
        }
    case WriteState_Element:
        {
            m_xmlStr += “/>”;
            m_openedElements.pop_back();
            m_writeState = WriteState_Content;
            break;
        }
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteStartAttribute(const std::string& localName)
{
    WriteStartAttribute(localName, “”);
}

void StringXmlWriter::WriteStartAttribute(const std::string& localName,
                                          const std::string& ns)
{
    switch (m_writeState)
    {
    case WriteState_Element:
        {
        std::string nsPrefix;
        bool mustDeclareNamespace = false;

        if (!ns.empty()) {
            nsPrefix = GetExistingNamespacePrefix(ns);
            if (nsPrefix.empty()) {
                nsPrefix = GetNextNamespacePrefix(ns);
                m_openedElements.back().Namespaces[ns] = nsPrefix;
                mustDeclareNamespace = true;
            }
        }

        if (mustDeclareNamespace) {
            m_xmlStr += ” xmlns:”;
            m_xmlStr += nsPrefix;
            m_xmlStr += “=”";
            m_xmlStr += ns;
            m_xmlStr += ‘”‘;
        }

        m_xmlStr += ‘ ‘;
        if (!nsPrefix.empty()) {
            m_xmlStr += nsPrefix;
            m_xmlStr += ‘:’;
        }
        m_xmlStr += localName;
        m_xmlStr += “=”";
        m_writeState = WriteState_Attribute;
        break;
        }
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteStartDocument()
{
    switch (m_writeState)
    {
    case WriteState_Start:
        m_xmlStr += “<?xml version=”1.0”?>”;
        m_writeState = WriteState_Prolog;
        break;
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteStartElement(const std::string& localName)
{
    WriteStartElement(localName, “”);
}

void StringXmlWriter::WriteStartElement(const std::string& localName,
                                        const std::string& ns)
{
    switch (m_writeState)
    {
    case WriteState_Element:
        // An element is currently open.  Close the element so we can open
        // a new one.
        m_xmlStr += ‘>’;
        // FALL THROUGH
    case WriteState_Content:
    case WriteState_Prolog:
    case WriteState_Start:
        {
        std::string nsPrefix;
        bool mustDeclareNamespace = false;

        if (!ns.empty()) {
            nsPrefix = GetExistingNamespacePrefix(ns);
            if (nsPrefix.empty()) {
                nsPrefix = GetNextNamespacePrefix(ns);
                mustDeclareNamespace = true;
            }
        }

        OpenElement openElement(localName, nsPrefix);
        if (mustDeclareNamespace) {
            openElement.Namespaces[ns] = nsPrefix;
        }

        m_openedElements.push_back(openElement);

        m_xmlStr += ‘<’;
        if (!nsPrefix.empty()) {
            m_xmlStr += nsPrefix;
            m_xmlStr += ‘:’;
        }
        m_xmlStr += localName;
        if (mustDeclareNamespace) {
            m_xmlStr += ” xmlns:”;
            m_xmlStr += nsPrefix;
            m_xmlStr += “=”";
            m_xmlStr += ns;
            m_xmlStr += ‘”‘;
        }
        m_writeState = WriteState_Element;
        break;
        }
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteString(const std::string& text)
{
    switch (m_writeState)
    {
    case WriteState_Attribute:
        m_xmlStr += TranslateString
            (
            text,
            AttributeValueTranslations,
            ARRAYSIZE(AttributeValueTranslations)
            );
        break;
    case WriteState_Element:
        // An element is currently open.  Close the element so we can start
        // writing the element content.
        m_xmlStr += ‘>’;
        m_writeState = WriteState_Content;
        // FALL THROUGH
    case WriteState_Content:
        m_xmlStr += TranslateString
            (
            text,
            CharDataTranslations,
            ARRAYSIZE(CharDataTranslations)
            );
        break;
    default:
        // TODO: Generate error
        break;
    }
}

std::string StringXmlWriter::GetExistingNamespacePrefix(const std::string& ns)
{
    for (OpenedElements_t::const_iterator openElemIter = m_openedElements.begin();
         openElemIter != m_openedElements.end();
         ++openElemIter)
    {
        OpenElement::Namespaces_t::const_iterator nsIter =
            openElemIter->Namespaces.find(ns);
        if (nsIter != openElemIter->Namespaces.end())
        {
            return nsIter->second;
        }
    }

    return “”;
}

std::string StringXmlWriter::GetNextNamespacePrefix(const std::string& ns)
{
    // Namespace prefixes are named ns1, ns2, …  They directly correlate to
    // the total number of namespaces already declared.

    size_t totalNumNamespaces = 0;
    for (OpenedElements_t::const_iterator iter = m_openedElements.begin();
         iter != m_openedElements.end();
         ++iter)
    {
        totalNumNamespaces += iter->Namespaces.size();
    }

    std::stringstream ss;
    ss << “ns” << (totalNumNamespaces + 1);
    return ss.str();
}

Implementing IXmlWriter Part 10: Supporting WriteComment()

This is part 10 of my Implementing IXmlWriter post series.

Today I will add support for the function WriteComment() to last time’s IXmlWriter.

Quoting from Section 2.5: Comments of the XML 1.0 spec:

Comments MAY appear anywhere in a document outside other markup; in addition, they MAY appear within the document type declaration at places allowed by the grammar.

Considering this, we should allow writing comments in virtually every WriteState that the IXmlWriter can be in. In fact, some quick thought confirms that we should allow it for every WriteState but WriteState_Attribute, as a comment cannot be legally represented between the quotation marks which delimit an attribute value. With this in mind, here’s the test case I wrote:

StringXmlWriter xmlWriter;

xmlWriter.WriteComment("comment1");
xmlWriter.WriteStartDocument();
  xmlWriter.WriteComment("comment2");
  xmlWriter.WriteStartElement("root");
    xmlWriter.WriteComment("comment3");
    xmlWriter.WriteString("value");
xmlWriter.WriteEndDocument();

std::string strXML = xmlWriter.GetXmlString();
// strXML should be (on one line):
// <!--comment1-->
// <?xml version="1.0"?>
// <!--comment2-->
// <root><!--comment3-->value</root>

Before I provide the implementation of writing comments (which is, in fact, trivial), I wanted to mention that in the course of writing and testing this feature I noticed a bug from last time’s IXmlWriter. Consider the above test case with the WriteComment() lines removed:

StringXmlWriter xmlWriter;

xmlWriter.WriteStartDocument();
  xmlWriter.WriteStartElement("root");
    xmlWriter.WriteString("value");
xmlWriter.WriteEndDocument();

std::string strXML = xmlWriter.GetXmlString();
// strXML should be <?xml version="1.0"?><root>value</root>

If you run this test case against the previous version of IXmlWriter, it outputs the clearly invalid string <?xml version="1.0"?><root>value. This is because WriteEndElement() didn’t properly anticipate the WriteState of WriteState_Content. Therefore, in addition to adding WriteComment() support, I also fixed this bug.

Here’s the new header file:

// StringXmlWriter.h

class StringXmlWriter
{
private:
    enum WriteState
    {
        WriteState_Attribute, // An attribute value is being written
        WriteState_Content, // Element content is being written
        WriteState_Element, // An element start tag has been written (and is unclosed)
        WriteState_Prolog, // The prolog is being written
        WriteState_Start, // No Write() methods have been called
    };

    WriteState m_writeState;
    std::stack<std::string> m_openedElements;
    std::string m_xmlStr;

public:
    StringXmlWriter();

    std::string GetXmlString() const;
    void WriteAttributeString(const std::string& localName,
                              const std::string& text);
    void WriteComment(const std::string& text);
    void WriteElementString(const std::string& localName,
                            const std::string& text);
    void WriteEndAttribute();
    void WriteEndDocument();
    void WriteEndElement();
    void WriteStartAttribute(const std::string& localName);
    void WriteStartDocument();
    void WriteStartElement(const std::string& localName);
    void WriteString(const std::string& text);

private:
    // Disable copy construction and assignment
    StringXmlWriter(const StringXmlWriter&);
    StringXmlWriter& operator=(const StringXmlWriter&);
};

Here’s the new implementation file:

// StringXmlWriter.cpp

#include "StringXmlWriter.h"

#define ARRAYSIZE(x) ( sizeof(x) / sizeof(x[0]) )

struct CharTranslation
{
    char OriginalChar;
    const char* ReplacementString;
};

static const CharTranslation AttributeValueTranslations[] =
{
    { '"', "&quot;" },
    { '&', "&amp;" },
};

static const CharTranslation CharDataTranslations[] =
{
    { '&', "&amp;" },
    { '<', "&lt;" },
    { '>', "&gt;" },
};

struct OriginalCharEquals :
    public std::binary_function<CharTranslation, char, bool>
{
    bool operator() (const CharTranslation& translation, char ch) const
    {
        return (translation.OriginalChar == ch);
    }
};

static std::string TranslateString(const std::string& originalStr,
                                   const CharTranslation* translations,
                                   int numTranslations)
{
    // Actually one past end, needed for proper std::find_if semantics
    const CharTranslation* endTranslations = translations + numTranslations;

    std::string translatedStr;

    for (std::string::const_iterator stringIter = originalStr.begin();
         stringIter != originalStr.end();
         ++stringIter)
    {
        char ch = *stringIter;

        const CharTranslation* translation = std::find_if
            (
            translations,
            endTranslations,
            std::bind2nd(OriginalCharEquals(), ch)
            );
        if (translation != endTranslations)
        {
            translatedStr += translation->ReplacementString;
        }
        else
        {
            translatedStr += ch;
        }
    }

    return translatedStr;
}

StringXmlWriter::StringXmlWriter() : m_writeState(WriteState_Start)
{
}

std::string StringXmlWriter::GetXmlString() const
{
    return m_xmlStr;
}

void StringXmlWriter::WriteAttributeString(const std::string& localName,
                                           const std::string& text)
{
    WriteStartAttribute(localName);
    WriteString(text);
    WriteEndAttribute();
}

void StringXmlWriter::WriteComment(const std::string& text)
{
    switch (m_writeState)
    {
    case WriteState_Element:
        // An element is currently open.  Close the element so we can open
        // a new one.
        m_xmlStr += ‘>’;
        m_writeState = WriteState_Content;
        // FALL THROUGH
    case WriteState_Content:
    case WriteState_Prolog:
    case WriteState_Start:
        m_xmlStr += “<!–”;
        m_xmlStr += text;
        m_xmlStr += “–>”;
        break;
    default:
        // It doesn’t make sense to allow writing comments when writing an
        // attribute value.
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteElementString(const std::string& localName,
                                         const std::string& text)
{
    WriteStartElement(localName);
    WriteString(text);
    WriteEndElement();
}

void StringXmlWriter::WriteEndAttribute()
{
    switch (m_writeState)
    {
    case WriteState_Attribute:
        m_xmlStr += ‘”‘;
        m_writeState = WriteState_Element;
        break;
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteEndDocument()
{
    switch (m_writeState)
    {
    case WriteState_Attribute:
        WriteEndAttribute();
        // FALL THROUGH
    case WriteState_Content:
    case WriteState_Element:
        while (!m_openedElements.empty())
        {
            WriteEndElement();
        }
        break;
    case WriteState_Start:
    case WriteState_Prolog:
        // DO NOTHING
        break;
    default:
        // TODO: Generate error
        break;
    }

    m_writeState = WriteState_Start;
}

void StringXmlWriter::WriteEndElement()
{
    switch (m_writeState)
    {
    case WriteState_Content:
        {
            m_xmlStr += “</”;
            m_xmlStr += m_openedElements.top();
            m_xmlStr += ‘>’;
            m_openedElements.pop();
            m_writeState = WriteState_Content;
            break;
        }
    case WriteState_Element:
        {
            m_xmlStr += “/>”;
            m_openedElements.pop();
            m_writeState = WriteState_Content;
            break;
        }
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteStartAttribute(const std::string& localName)
{
    switch (m_writeState)
    {
    case WriteState_Element:
        m_xmlStr += ‘ ‘;
        m_xmlStr += localName;
        m_xmlStr += “=”";
        m_writeState = WriteState_Attribute;
        break;
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteStartDocument()
{
    switch (m_writeState)
    {
    case WriteState_Start:
        m_xmlStr += “<?xml version=”1.0”?>”;
        m_writeState = WriteState_Prolog;
        break;
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteStartElement(const std::string& localName)
{
    switch (m_writeState)
    {
    case WriteState_Element:
        // An element is currently open.  Close the element so we can open
        // a new one.
        m_xmlStr += ‘>’;
        // FALL THROUGH
    case WriteState_Content:
    case WriteState_Prolog:
    case WriteState_Start:
        m_openedElements.push(localName);
        m_xmlStr += ‘<’;
        m_xmlStr += localName;
        m_writeState = WriteState_Element;
        break;
    default:
        // TODO: Generate error
        break;
    }
}

void StringXmlWriter::WriteString(const std::string& text)
{
    switch (m_writeState)
    {
    case WriteState_Attribute:
        m_xmlStr += TranslateString
            (
            text,
            AttributeValueTranslations,
            ARRAYSIZE(AttributeValueTranslations)
            );
        break;
    case WriteState_Element:
        // An element is currently open.  Close the element so we can start
        // writing the element content.
        m_xmlStr += ‘>’;
        m_writeState = WriteState_Content;
        // FALL THROUGH
    case WriteState_Content:
        m_xmlStr += TranslateString
            (
            text,
            CharDataTranslations,
            ARRAYSIZE(CharDataTranslations)
            );
        break;
    default:
        // TODO: Generate error
        break;
    }
}