summaryrefslogtreecommitdiff
path: root/sax/source/tools/fshelper.cxx
blob: 3096dc6a3ee212055852d2fab4b0a5363c317e55 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include <sax/fshelper.hxx>
#include "fastserializer.hxx"
#include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
#include <comphelper/processfactory.hxx>
#include <rtl/ustrbuf.hxx>

using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;

namespace sax_fastparser {

FastSerializerHelper::FastSerializerHelper(const Reference< io::XOutputStream >& xOutputStream ) :
    mpSerializer(new FastSaxSerializer())
{
    Reference< lang::XMultiServiceFactory > xFactory = comphelper::getProcessServiceFactory();
    mxTokenHandler = Reference<xml::sax::XFastTokenHandler> ( xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.FastTokenHandler") ) ), UNO_QUERY_THROW );

    mpSerializer->setFastTokenHandler( mxTokenHandler );
    mpSerializer->setOutputStream( xOutputStream );
    mpSerializer->startDocument();
}

FastSerializerHelper::~FastSerializerHelper()
{
    mpSerializer->endDocument();

    if (mpSerializer) {
        delete mpSerializer;
        mpSerializer = NULL;
    }
}

void FastSerializerHelper::startElement(const char* elementName, ...)
{
    FastAttributeList* pAttrList = new FastAttributeList( mxTokenHandler );
    va_list args;
    va_start(args, elementName);
    while (true)
    {
        const char* pName = va_arg(args, const char*);
        if (!pName)
            break;
        const char* pValue = va_arg(args, const char*);
        if (pValue)
            pAttrList->addUnknown(pName, pValue);
    }
    va_end(args);
    const com::sun::star::uno::Reference<com::sun::star::xml::sax::XFastAttributeList> xAttrList(pAttrList);
    mpSerializer->startUnknownElement(::rtl::OUString(), ::rtl::OUString::createFromAscii(elementName), xAttrList);
}

void FastSerializerHelper::singleElement(const char* elementName, ...)
{
    FastAttributeList* pAttrList = new FastAttributeList( mxTokenHandler );
    va_list args;
    va_start(args, elementName);
    while (true)
    {
        const char* pName = va_arg(args, const char*);
        if (!pName)
            break;
        const char* pValue = va_arg(args, const char*);
        if (pValue)
            pAttrList->addUnknown(pName, pValue);
    }
    va_end(args);
    const com::sun::star::uno::Reference<com::sun::star::xml::sax::XFastAttributeList> xAttrList(pAttrList);
    mpSerializer->singleUnknownElement(::rtl::OUString(), ::rtl::OUString::createFromAscii(elementName), xAttrList);
}

void FastSerializerHelper::endElement(const char* elementName)
{
    mpSerializer->endUnknownElement(::rtl::OUString(), ::rtl::OUString::createFromAscii(elementName));
}

void FastSerializerHelper::startElementV(sal_Int32 elementTokenId, va_list args)
{
    FastAttributeList* pAttrList = new FastAttributeList( mxTokenHandler );

    while (true)
    {
        sal_Int32 nName = va_arg(args, sal_Int32);
        if (nName == FSEND)
            break;
        const char* pValue = va_arg(args, const char*);
        if (pValue)
            pAttrList->add(nName, pValue);
    }

    const com::sun::star::uno::Reference<com::sun::star::xml::sax::XFastAttributeList> xAttrList(pAttrList);
    mpSerializer->startFastElement(elementTokenId, xAttrList);
}

void FastSerializerHelper::singleElementV(sal_Int32 elementTokenId, va_list args)
{
    FastAttributeList* pAttrList = new FastAttributeList( mxTokenHandler );

    while (true)
    {
        sal_Int32 nName = va_arg(args, sal_Int32);
        if (nName == FSEND)
            break;
        const char* pValue = va_arg(args, const char*);
        if  (pValue)
            pAttrList->add(nName, pValue);
    }

    const com::sun::star::uno::Reference<com::sun::star::xml::sax::XFastAttributeList> xAttrList(pAttrList);
    mpSerializer->singleFastElement(elementTokenId, xAttrList);
}

void FastSerializerHelper::endElement(sal_Int32 elementTokenId)
{
    mpSerializer->endFastElement(elementTokenId);
}

void FastSerializerHelper::startElementV(sal_Int32 elementTokenId, XFastAttributeListRef xAttrList)
{
    mpSerializer->startFastElement(elementTokenId, xAttrList);
}


void FastSerializerHelper::singleElement(const char* elementName, XFastAttributeListRef xAttrList)
{
    mpSerializer->singleUnknownElement(::rtl::OUString(), ::rtl::OUString::createFromAscii(elementName), xAttrList);
}

void FastSerializerHelper::singleElementV(sal_Int32 elementTokenId, XFastAttributeListRef xAttrList)
{
    mpSerializer->singleFastElement(elementTokenId, xAttrList);
}

FastSerializerHelper* FastSerializerHelper::write(const char* value)
{
    return write(rtl::OUString::createFromAscii(value));
}

FastSerializerHelper* FastSerializerHelper::write(const rtl::OUString& value)
{
    mpSerializer->characters(value);
    return this;
}

FastSerializerHelper* FastSerializerHelper::write(sal_Int32 value)
{
    return write(::rtl::OUString::valueOf(value));
}

FastSerializerHelper* FastSerializerHelper::write(sal_Int64 value)
{
    return write(::rtl::OUString::valueOf(value));
}

FastSerializerHelper* FastSerializerHelper::write(float value)
{
    return write(::rtl::OUString::valueOf(value));
}

FastSerializerHelper* FastSerializerHelper::write(double value)
{
    return write(::rtl::OUString::valueOf(value));
}

FastSerializerHelper* FastSerializerHelper::writeEscaped(const char* value)
{
    return writeEscaped(::rtl::OUString::createFromAscii(value));
}

FastSerializerHelper* FastSerializerHelper::writeEscaped(const ::rtl::OUString& value)
{
    return write(FastSaxSerializer::escapeXml(value));
}

FastSerializerHelper* FastSerializerHelper::writeId(sal_Int32 tokenId)
{
    mpSerializer->writeId(tokenId);
    return this;
}

::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > FastSerializerHelper::getOutputStream()
{
    return mpSerializer->getOutputStream();
}

void FastSerializerHelper::mark( Sequence< sal_Int32 > aOrder )
{
    mpSerializer->mark( aOrder );
}

void FastSerializerHelper::mergeTopMarks( MergeMarksEnum eMergeType )
{
    mpSerializer->mergeTopMarks( eMergeType );
}

FastAttributeList * FastSerializerHelper::createAttrList()
{
    return new FastAttributeList( mxTokenHandler );
}


}

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