Using ODF Relax NG schema to generate easier XML writing classes

Jos van den Oever jos.van.den.oever at kogmbh.com
Fri Jun 10 16:06:15 BST 2011


On Friday, June 10, 2011 08:22:04 AM Jos van den Oever wrote:
> Here is an idea to improve code quality in Calligra.

Here is a version that works fine with a small example use:

#include "odf.h"
#include <QtCore/QBuffer>
#include <QtCore/QFile>

int
main() {
    QFile out;
    out.open(stdout, QIODevice::WriteOnly);
    KoXmlWriter xml(&out);
    {
        OfficeDocumentWriter doc(&xml);
        OfficeBodyWriter content(doc);
        OfficeTextWriter text(content);
        {
            TextHWriter h(text);
            h.addTextNode("Hello ODF!");
        }
        {
            TextPWriter p(text);
            p.addTextNode("This is paragraph 1.");
        }
    }
    out.close();
    return 0;
}

In the example, when an element writer has two child elements, the previous 
element has to be destroyed first. Code could be added to automatically close 
it when one of it's ancestors is used in a constructor for another element 
writer.

The constructors only accept valid parent element writers. The KoXmlWriter is 
alway available for situation where flexibility is needed. This version does 
not handle fixed values or required attributes and has no specialized setters 
for attributes that take e.g. integers. After all this is just a proposal.

You can play with this exmple in an IDE to see the autocompletion. I've added 
comments so you can see what element or attribute is written.

Cheers,
Jos

-- 
Jos van den Oever, software architect
+49 391 25 19 15 53
074 3491911
http://kogmbh.com/legal/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: odf.h.bz2
Type: application/x-bzip
Size: 41447 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/calligra-devel/attachments/20110610/3aa81843/attachment.bin>


More information about the calligra-devel mailing list