summaryrefslogtreecommitdiff
path: root/writerperfect/source/filter/DocumentHandler.cxx
blob: e1f58b2dc45b25bb8f9401d30a7b1def87aec8e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * For further information visit http://libwpd.sourceforge.net
 */
#include "DocumentHandler.hxx"
#include "FilterInternal.hxx"

#include <string.h>
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
#include <com/sun/star/xml/sax/XAttributeList.hpp>

#include <xmloff/attrlist.hxx>

// #define DEBUG_XML 1

using namespace ::rtl;
using rtl::OUString;

using com::sun::star::xml::sax::XAttributeList;

DocumentHandler::DocumentHandler(Reference < XDocumentHandler > &xHandler) :
    mxHandler(xHandler)
{
}

void DocumentHandler::startDocument()
{
    mxHandler->startDocument();
}

void DocumentHandler::endDocument()
{
    mxHandler->endDocument();
}

void DocumentHandler::startElement(const char *psName, const WPXPropertyList &xPropList)
{
#ifdef DEBUG_XML
    printf("<%s", psName);
#endif
    SvXMLAttributeList *pAttrList = new SvXMLAttributeList();
    Reference < XAttributeList > xAttrList(pAttrList);
    WPXPropertyList::Iter i(xPropList);
    for (i.rewind(); i.next(); )
    {
        // filter out libwpd elements
        if (strncmp(i.key(), "libwpd", 6) != 0)
        {
            pAttrList->AddAttribute(OUString::createFromAscii(i.key()),
                                    OUString::createFromAscii(i()->getStr().cstr()));
#ifdef DEBUG_XML
            printf(" %s=\"%s\"", i.key(), i()->getStr().cstr());
#endif
        }
    }
#ifdef DEBUG_XML
    printf(">");
#endif

    mxHandler->startElement(OUString::createFromAscii(psName), xAttrList);
}

void DocumentHandler::endElement(const char *psName)
{
#ifdef DEBUG_XML
    printf("</%s>", psName);
#endif
    mxHandler->endElement(OUString::createFromAscii(psName));
}

void DocumentHandler::characters(const WPXString &sCharacters)
{
    int lastNewline = -1;
    int length = sCharacters.len();
    for (int curr = 0; curr < length; ++curr)
    {
        if (sCharacters.cstr()[curr] == '\n')
        {
            if (curr > lastNewline + 1)
            {
                OUString sCharU16(sCharacters.cstr() + lastNewline + 1, curr - lastNewline - 1, RTL_TEXTENCODING_UTF8);
#ifdef DEBUG_XML
                WPXString sEscapedCharacters(sCharacters, true);
                printf("%s", sEscapedCharacters.cstr());
#endif
                mxHandler->characters(sCharU16);
            }
            startElement("text:line-break", WPXPropertyList());
            endElement("text:line-break");
            lastNewline = curr;
        }
    }
    if (lastNewline + 1 < length)
    {
        OUString sCharU16(sCharacters.cstr() + lastNewline + 1, length - lastNewline - 1, RTL_TEXTENCODING_UTF8);
#ifdef DEBUG_XML
        WPXString sEscapedCharacters(sCharacters, true);
        printf("%s", sEscapedCharacters.cstr());
#endif
        mxHandler->characters(sCharU16);
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */